]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44018: random.seed() no longer mutates its inputs (GH-25856)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Mon, 3 May 2021 23:11:35 +0000 (16:11 -0700)
committerGitHub <noreply@github.com>
Mon, 3 May 2021 23:11:35 +0000 (16:11 -0700)
Lib/random.py
Lib/test/test_random.py
Misc/NEWS.d/next/Library/2021-05-03-10-07-43.bpo-44018.VDyW8f.rst [new file with mode: 0644]

index 3a835aef0bc1d4388056e94a39d3a286cbdc67e6..1310a2d9d0e07104ee8b67a0efc2e004bfe62277 100644 (file)
@@ -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'
index 66908868a6e9efeb447b9584120826b3ee07205b..5354eddab6958e8e39fa0c66a9f40cfced34561c 100644 (file)
@@ -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 (file)
index 0000000..87c7d83
--- /dev/null
@@ -0,0 +1 @@
+random.seed() no longer mutates bytearray inputs.