Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions 5 Lib/test/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ def test_setstate_first_arg(self):
self.assertRaises(ValueError, self.gen.setstate, (1, None, None))

def test_setstate_middle_arg(self):
start_state = self.gen.getstate()
# Wrong type, s/b tuple
self.assertRaises(TypeError, self.gen.setstate, (2, None, None))
# Wrong length, s/b 625
Expand All @@ -324,6 +325,10 @@ def test_setstate_middle_arg(self):
self.gen.setstate((2, (1,)*624+(625,), None))
with self.assertRaises((ValueError, OverflowError)):
self.gen.setstate((2, (1,)*624+(-1,), None))
# Failed calls to setstate() should not have changed the state.
bits100 = self.gen.getrandbits(100)
self.gen.setstate(start_state)
self.assertEqual(self.gen.getrandbits(100), bits100)

def test_referenceImplementation(self):
# Compare the python implementation with results from the original
Expand Down
1 change: 1 addition & 0 deletions 1 Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,7 @@ Milan Oberkirch
Pascal Oberndoerfer
Jeffrey Ollie
Adam Olsen
Bryan Olson
Grant Olson
Koray Oner
Piet van Oostrum
Expand Down
3 changes: 3 additions & 0 deletions 3 Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ Extension Modules
Library
-------

- bpo-29960: Preserve generator state when _random.Random.setstate()
raises an exception. Patch by Bryan Olson.

- bpo-30310: tkFont now supports unicode options (e.g. font family).

- bpo-30414: multiprocessing.Queue._feed background running
Expand Down
5 changes: 4 additions & 1 deletion 5 Modules/_randommodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ random_setstate(RandomObject *self, PyObject *state)
int i;
unsigned long element;
long index;
unsigned long new_state[N];

if (!PyTuple_Check(state)) {
PyErr_SetString(PyExc_TypeError,
Expand All @@ -357,7 +358,7 @@ random_setstate(RandomObject *self, PyObject *state)
element = PyLong_AsUnsignedLong(PyTuple_GET_ITEM(state, i));
if (element == (unsigned long)-1 && PyErr_Occurred())
return NULL;
self->state[i] = element & 0xffffffffUL; /* Make sure we get sane state */
new_state[i] = element & 0xffffffffUL; /* Make sure we get sane state */
}

index = PyLong_AsLong(PyTuple_GET_ITEM(state, i));
Expand All @@ -368,6 +369,8 @@ random_setstate(RandomObject *self, PyObject *state)
return NULL;
}
self->index = (int)index;
for (i = 0; i < N; i++)
self->state[i] = new_state[i];

Py_INCREF(Py_None);
return Py_None;
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.