[Python-checkins] bpo-42008: Fix internal _random.Random() seeding for the one argument case (GH-22668)

rhettinger webhook-mailer at python.org
Mon Dec 21 18:46:00 EST 2020


https://github.com/python/cpython/commit/b8fde8b5418b75d2935d0ff93b20d45d5350f206
commit: b8fde8b5418b75d2935d0ff93b20d45d5350f206
branch: master
author: AMIR <31338382+amiremohamadi at users.noreply.github.com>
committer: rhettinger <rhettinger at users.noreply.github.com>
date: 2020-12-21T15:45:50-08:00
summary:

bpo-42008: Fix internal _random.Random() seeding for the one argument case (GH-22668)

files:
A Misc/NEWS.d/next/Core and Builtins/2020-10-12-14-51-59.bpo-42008.ijWw2I.rst
M Lib/test/test_random.py
M Modules/_randommodule.c

diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py
index 327bfa3bbda1b..e7f911d12875e 100644
--- a/Lib/test/test_random.py
+++ b/Lib/test/test_random.py
@@ -414,6 +414,15 @@ def test_bug_41052(self):
             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
index 0000000000000..1b50a0ef3b02d
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2020-10-12-14-51-59.bpo-42008.ijWw2I.rst	
@@ -0,0 +1 @@
+Fix _random.Random() seeding.
diff --git a/Modules/_randommodule.c b/Modules/_randommodule.c
index ad4fd474428d4..99be69c06556e 100644
--- a/Modules/_randommodule.c
+++ b/Modules/_randommodule.c
@@ -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;
 }
 



More information about the Python-checkins mailing list