bpo-42008: Fix internal _random.Random() seeding for the one argument case (GH-22668)
This commit is contained in:
parent
711381dfb0
commit
b8fde8b541
|
@ -414,6 +414,15 @@ class TestBasicOps:
|
||||||
r = _random.Random()
|
r = _random.Random()
|
||||||
self.assertRaises(TypeError, pickle.dumps, r, proto)
|
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):
|
def test_bug_1727780(self):
|
||||||
# verify that version-2-pickles can be loaded
|
# verify that version-2-pickles can be loaded
|
||||||
# fine, whether they are created on 32-bit or 64-bit
|
# fine, whether they are created on 32-bit or 64-bit
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix _random.Random() seeding.
|
|
@ -519,6 +519,7 @@ random_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
{
|
{
|
||||||
RandomObject *self;
|
RandomObject *self;
|
||||||
PyObject *tmp;
|
PyObject *tmp;
|
||||||
|
PyObject *arg = NULL;
|
||||||
_randomstate *state = _randomstate_type(type);
|
_randomstate *state = _randomstate_type(type);
|
||||||
|
|
||||||
if (type == (PyTypeObject*)state->Random_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);
|
self = (RandomObject *)PyType_GenericAlloc(type, 0);
|
||||||
if (self == NULL)
|
if (self == NULL)
|
||||||
return 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) {
|
if (tmp == NULL) {
|
||||||
Py_DECREF(self);
|
Py_DECREF(self);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
Py_DECREF(tmp);
|
Py_DECREF(tmp);
|
||||||
|
|
||||||
return (PyObject *)self;
|
return (PyObject *)self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue