Skip to content

Commit 2995bff

Browse files
bpo-44018: random.seed() no longer mutates its inputs (GH-25856) (GH-25872)
1 parent d194e1e commit 2995bff

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

Lib/random.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ def seed(self, a=None, version=2):
154154
elif version == 2 and isinstance(a, (str, bytes, bytearray)):
155155
if isinstance(a, str):
156156
a = a.encode()
157-
a += _sha512(a).digest()
158-
a = int.from_bytes(a, 'big')
157+
a = int.from_bytes(a + _sha512(a).digest(), 'big')
159158

160159
elif not isinstance(a, (type(None), int, float, str, bytes, bytearray)):
161160
_warn('Seeding based on hashing is deprecated\n'

Lib/test/test_random.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ def __hash__(self):
5757
self.assertRaises(TypeError, self.gen.seed, 1, 2, 3, 4)
5858
self.assertRaises(TypeError, type(self.gen), [])
5959

60+
def test_seed_no_mutate_bug_44018(self):
61+
a = bytearray(b'1234')
62+
self.gen.seed(a)
63+
self.assertEqual(a, bytearray(b'1234'))
64+
6065
@unittest.mock.patch('random._urandom') # os.urandom
6166
def test_seed_when_randomness_source_not_found(self, urandom_mock):
6267
# Random.seed() uses time.time() when an operating system specific
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
random.seed() no longer mutates bytearray inputs.

0 commit comments

Comments
 (0)