Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Lib/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ def seed(self, a=None, version=2):
if 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')

super().seed(a)
self.gauss_next = None
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
random.seed() no longer mutates bytearray inputs.