New submission from Bryan G. Olson: Demo:
Run the Python library's test_random.py under the Python debugger and check the generator at the start of test_shuffle(): C:\bin\Python36>python -m pdb Lib\test\test_random.py > c:\bin\python36\lib\test\test_random.py(1)<module>() -> import unittest (Pdb) break 61 Breakpoint 1 at c:\bin\python36\lib\test\test_random.py:61 (Pdb) continue .............................> c:\bin\python36\lib\test\test_random.py(61)test_shuffle() -> shuffle = self.gen.shuffle (Pdb) list 56 # randomness source is not available. 57 urandom_mock.side_effect = NotImplementedError 58 self.test_seedargs() 59 60 def test_shuffle(self): 61 B-> shuffle = self.gen.shuffle 62 lst = [] 63 shuffle(lst) 64 self.assertEqual(lst, []) 65 lst = [37] 66 shuffle(lst) (Pdb) p self.gen.getrandbits(31) 2137781566 (Pdb) p self.gen.getrandbits(31) 2137781566 (Pdb) p self.gen.getrandbits(31) 2137781566 (Pdb) p self.gen.getrandbits(31) 2137781566 (Pdb) p self.gen.getrandbits(31) 2137781566 That's not random. Diagnosis: The order in which test functions run is the lexicographic order of their names. Thus unittest ran test_setstate_middle_arg() before running test_shuffle(). test_setstate_middle_arg() did some failed calls to _random.Random.setstate(), which raised exceptions as planned, but also trashed the state of the generator. test_random.py continues to use the same instance of _random.Random after setstate() raises exceptions. The documentation for Random.setstate() does not specify what happens to the state of the generator if setstate() raises an exception. Fortunately the generator recommended for secure applications, SystemRandom, does not implement setstate(). Solution: The fix I prefer is a small change to random_setstate() in _randommodule.c, so that it does not change the state of the generator until the operation is sure to succeed. ---------- components: Library (Lib) messages: 290977 nosy: bryangeneolson priority: normal severity: normal status: open title: _random.Random state corrupted on exception type: behavior versions: Python 3.7 _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue29960> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com