]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-42008: Fix internal _random.Random() seeding for the one argument case (GH-22668)
authorAMIR <31338382+amiremohamadi@users.noreply.github.com>
Mon, 21 Dec 2020 23:45:50 +0000 (03:15 +0330)
committerGitHub <noreply@github.com>
Mon, 21 Dec 2020 23:45:50 +0000 (15:45 -0800)
Lib/test/test_random.py
Misc/NEWS.d/next/Core and Builtins/2020-10-12-14-51-59.bpo-42008.ijWw2I.rst [new file with mode: 0644]
Modules/_randommodule.c

index 327bfa3bbda1bd2460cf89e3b1bf301bc7888ca5..e7f911d12875e154a9e53be41718029b4e68c4fc 100644 (file)
@@ -414,6 +414,15 @@ class TestBasicOps:
             r = _random.Random()
             self.assertRaises(TypeError, pickle.dumps, r, proto)
 
+    @test.support.cpython_only
+    def test_bug_42008(self):
+        # _random.Random should call seed with first element of arg tuple
+        import _random
+        r1 = _random.Random()
+        r1.seed(8675309)
+        r2 = _random.Random(8675309)
+        self.assertEqual(r1.random(), r2.random())
+
     def test_bug_1727780(self):
         # verify that version-2-pickles can be loaded
         # fine, whether they are created on 32-bit or 64-bit
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-10-12-14-51-59.bpo-42008.ijWw2I.rst b/Misc/NEWS.d/next/Core and Builtins/2020-10-12-14-51-59.bpo-42008.ijWw2I.rst
new file mode 100644 (file)
index 0000000..1b50a0e
--- /dev/null
@@ -0,0 +1 @@
+Fix _random.Random() seeding.
index ad4fd474428d4b8933beebbd4d8677b6944a3440..99be69c06556e6cf926da1957cc15e045f706537 100644 (file)
@@ -519,6 +519,7 @@ random_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
     RandomObject *self;
     PyObject *tmp;
+    PyObject *arg = NULL;
     _randomstate *state = _randomstate_type(type);
 
     if (type == (PyTypeObject*)state->Random_Type &&
@@ -529,12 +530,22 @@ random_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
     self = (RandomObject *)PyType_GenericAlloc(type, 0);
     if (self == NULL)
         return NULL;
-    tmp = random_seed(self, args);
+
+    if (PyTuple_GET_SIZE(args) > 1) {
+        PyErr_SetString(PyExc_TypeError, "Random() requires 0 or 1 argument");
+        return NULL;
+    }
+
+    if (PyTuple_GET_SIZE(args) == 1)
+        arg = PyTuple_GET_ITEM(args, 0);
+    
+    tmp = random_seed(self, arg);
     if (tmp == NULL) {
         Py_DECREF(self);
         return NULL;
     }
     Py_DECREF(tmp);
+
     return (PyObject *)self;
 }