diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2021-05-03 23:11:35 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-03 23:11:35 (GMT) |
commit | b05352e4c2f25b292fb7de0ab927e74415bc2dd8 (patch) | |
tree | bf3a7f893a153057fb236b935fdc634c3e373f1d | |
parent | 2fc857a5721a5b42bcb696c9cae1bbcc82a91b17 (diff) | |
download | cpython-b05352e4c2f25b292fb7de0ab927e74415bc2dd8.zip cpython-b05352e4c2f25b292fb7de0ab927e74415bc2dd8.tar.gz cpython-b05352e4c2f25b292fb7de0ab927e74415bc2dd8.tar.bz2 |
bpo-44018: random.seed() no longer mutates its inputs (GH-25856)
-rw-r--r-- | Lib/random.py | 3 | ||||
-rw-r--r-- | Lib/test/test_random.py | 5 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2021-05-03-10-07-43.bpo-44018.VDyW8f.rst | 1 |
3 files changed, 7 insertions, 2 deletions
diff --git a/Lib/random.py b/Lib/random.py index 3a835ae..1310a2d 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -154,8 +154,7 @@ class Random(_random.Random): elif version == 2 and isinstance(a, (str, bytes, bytearray)): if isinstance(a, str): a = a.encode() - a += _sha512(a).digest() - a = int.from_bytes(a, 'big') + a = int.from_bytes(a + _sha512(a).digest(), 'big') elif not isinstance(a, (type(None), int, float, str, bytes, bytearray)): _warn('Seeding based on hashing is deprecated\n' diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py index 6690886..5354edd 100644 --- a/Lib/test/test_random.py +++ b/Lib/test/test_random.py @@ -57,6 +57,11 @@ class TestBasicOps: self.assertRaises(TypeError, self.gen.seed, 1, 2, 3, 4) self.assertRaises(TypeError, type(self.gen), []) + def test_seed_no_mutate_bug_44018(self): + a = bytearray(b'1234') + self.gen.seed(a) + self.assertEqual(a, bytearray(b'1234')) + @unittest.mock.patch('random._urandom') # os.urandom def test_seed_when_randomness_source_not_found(self, urandom_mock): # Random.seed() uses time.time() when an operating system specific diff --git a/Misc/NEWS.d/next/Library/2021-05-03-10-07-43.bpo-44018.VDyW8f.rst b/Misc/NEWS.d/next/Library/2021-05-03-10-07-43.bpo-44018.VDyW8f.rst new file mode 100644 index 0000000..87c7d83 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-05-03-10-07-43.bpo-44018.VDyW8f.rst @@ -0,0 +1 @@ +random.seed() no longer mutates bytearray inputs. |