Skip to content

Commit 8f849ea

Browse files
rhettingerlisroach
authored andcommitted
bpo-32554: Deprecate hashing arbitrary types in random.seed() (pythonGH-15382)
1 parent 2efea85 commit 8f849ea

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

Doc/library/random.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ Bookkeeping functions
8686
.. versionchanged:: 3.2
8787
Moved to the version 2 scheme which uses all of the bits in a string seed.
8888

89+
.. deprecated:: 3.9
90+
In the future, the *seed* must be one of the following types:
91+
*NoneType*, :class:`int`, :class:`float`, :class:`str`,
92+
:class:`bytes`, or :class:`bytearray`.
93+
8994
.. function:: getstate()
9095

9196
Return an object capturing the current internal state of the generator. This
@@ -316,6 +321,11 @@ Alternative Generator
316321
Class that implements the default pseudo-random number generator used by the
317322
:mod:`random` module.
318323

324+
.. deprecated:: 3.9
325+
In the future, the *seed* must be one of the following types:
326+
:class:`NoneType`, :class:`int`, :class:`float`, :class:`str`,
327+
:class:`bytes`, or :class:`bytearray`.
328+
319329
.. class:: SystemRandom([seed])
320330

321331
Class that uses the :func:`os.urandom` function for generating random numbers

Doc/whatsnew/3.9.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ Deprecated
169169
of Python. For the majority of use cases users can leverage the Abstract Syntax
170170
Tree (AST) generation and compilation stage, using the :mod:`ast` module.
171171

172+
* The :mod:`random` module currently accepts any hashable type as a
173+
possible seed value. Unfortunately, some of those types are not
174+
guaranteed to have a deterministic hash value. After Python 3.9,
175+
the module will restrict its seeds to *None*, :class:`int`,
176+
:class:`float`, :class:`str`, :class:`bytes`, and :class:`bytearray`.
177+
172178

173179
Removed
174180
=======

Lib/random.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ def __init_subclass__(cls, /, **kwargs):
121121
break
122122

123123
def seed(self, a=None, version=2):
124-
"""Initialize internal state from hashable object.
124+
"""Initialize internal state from a seed.
125+
126+
The only supported seed types are None, int, float,
127+
str, bytes, and bytearray.
125128
126129
None or no argument seeds from current time or from an operating
127130
system specific randomness source if available.
@@ -143,12 +146,20 @@ def seed(self, a=None, version=2):
143146
x ^= len(a)
144147
a = -2 if x == -1 else x
145148

146-
if version == 2 and isinstance(a, (str, bytes, bytearray)):
149+
elif version == 2 and isinstance(a, (str, bytes, bytearray)):
147150
if isinstance(a, str):
148151
a = a.encode()
149152
a += _sha512(a).digest()
150153
a = int.from_bytes(a, 'big')
151154

155+
elif not isinstance(a, (type(None), int, float, str, bytes, bytearray)):
156+
_warn('Seeding based on hashing is deprecated\n'
157+
'since Python 3.9 and will be removed in a subsequent '
158+
'version. The only \n'
159+
'supported seed types are: None, '
160+
'int, float, str, bytes, and bytearray.',
161+
DeprecationWarning, 2)
162+
152163
super().seed(a)
153164
self.gauss_next = None
154165

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Deprecate having random.seed() call hash on arbitrary types.

0 commit comments

Comments
 (0)