[Python-checkins] bpo-44018: random.seed() no longer mutates its inputs (GH-25856) (GH-25872)

rhettinger webhook-mailer at python.org
Mon May 3 22:45:37 EDT 2021


https://github.com/python/cpython/commit/2995bff4269d274c0a3abfd45dc33b28f0c3e25f
commit: 2995bff4269d274c0a3abfd45dc33b28f0c3e25f
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: rhettinger <rhettinger at users.noreply.github.com>
date: 2021-05-03T19:45:30-07:00
summary:

bpo-44018: random.seed() no longer mutates its inputs (GH-25856) (GH-25872)

files:
A Misc/NEWS.d/next/Library/2021-05-03-10-07-43.bpo-44018.VDyW8f.rst
M Lib/random.py
M Lib/test/test_random.py

diff --git a/Lib/random.py b/Lib/random.py
index 3a835aef0bc1d4..1310a2d9d0e071 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -154,8 +154,7 @@ def seed(self, a=None, version=2):
         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 66908868a6e9ef..5354eddab6958e 100644
--- a/Lib/test/test_random.py
+++ b/Lib/test/test_random.py
@@ -57,6 +57,11 @@ def __hash__(self):
         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 00000000000000..87c7d83a7f35c5
--- /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.



More information about the Python-checkins mailing list