Uh oh!
There was an error while loading. Please reload this page.
bpo-29960 _random.Random corrupted on exception in setstate(). - #1019
Conversation
mention-bot
commented
Apr 7, 2017
@bladebryan, thanks for your PR! By analyzing the history of the files in this pull request, we identified @rhettinger, @serhiy-storchaka and @benjaminp to be potential reviewers. |
serhiy-storchaka
left a comment
There was a problem hiding this comment.
Seems this is your first contribution. Add your name in Misc/ACKS.
| state = (int(x) for x in state_values) | ||
| self.assertRaises(TypeError, self.gen.setstate, (2, state, 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) |
There was a problem hiding this comment.
I don't think that the test is correct. Why not generating bits before calling setstate()?
There was a problem hiding this comment.
I'm testing that the failed calls to setstate() did not change the state. I save the initial state, then the failed calls happen. Now I want to know if I'm still in the initial state. I don't check for state equality because a generator's state might not support equality test. I test by generating bits, restoring the initial state, and generating what should be the same bits.
I'm not testing the succeeding call to setstate(). There are other test cases for that, so I assume it works.
There was a problem hiding this comment.
I confirm that the test is correct.
| Core and Builtins | ||
| ----------------- | ||
| - bpo-29960: Preserve generator state when _random.Random.setstate() |
There was a problem hiding this comment.
This entry should be added in the "Library" section rather than "Core and Builtins".
serhiy-storchaka
commented
Apr 22, 2017
Thank you for your contribution @bladebryan! Do you mind to backport your chages? |
…ythonGH-1019). (cherry picked from commit 9616a82)
…ythonGH-1019). (cherry picked from commit 9616a82)
…ythonGH-1019). (cherry picked from commit 9616a82)
Changes the _random.Random.setstate() function so that if it raises an exception the state of the generator is unchanged.