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

This commit is contained in:
Raymond Hettinger 2021-05-03 16:11:35 -07:00 committed by GitHub
parent 2fc857a572
commit b05352e4c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 2 deletions

View 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'

View 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

View File

@ -0,0 +1 @@
random.seed() no longer mutates bytearray inputs.