Skip to content

bpo-32554: Deprecate hashing arbitrary types in random.seed() #15382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 22, 2019
Merged
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
10 changes: 10 additions & 0 deletions Doc/library/random.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ Bookkeeping functions
.. versionchanged:: 3.2
Moved to the version 2 scheme which uses all of the bits in a string seed.

.. deprecated:: 3.9
In the future, the *seed* must be one of the following types:
*NoneType*, :class:`int`, :class:`float`, :class:`str`,
:class:`bytes`, or :class:`bytearray`.

.. function:: getstate()

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

.. deprecated:: 3.9
In the future, the *seed* must be one of the following types:
:class:`NoneType`, :class:`int`, :class:`float`, :class:`str`,
:class:`bytes`, or :class:`bytearray`.

.. class:: SystemRandom([seed])

Class that uses the :func:`os.urandom` function for generating random numbers
Expand Down
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ Deprecated
of Python. For the majority of use cases users can leverage the Abstract Syntax
Tree (AST) generation and compilation stage, using the :mod:`ast` module.

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


Removed
=======
Expand Down
15 changes: 13 additions & 2 deletions Lib/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ def __init_subclass__(cls, /, **kwargs):
break

def seed(self, a=None, version=2):
"""Initialize internal state from hashable object.
"""Initialize internal state from a seed.

The only supported seed types are None, int, float,
str, bytes, and bytearray.

None or no argument seeds from current time or from an operating
system specific randomness source if available.
Expand All @@ -143,12 +146,20 @@ def seed(self, a=None, version=2):
x ^= len(a)
a = -2 if x == -1 else x

if version == 2 and isinstance(a, (str, bytes, bytearray)):
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')

elif not isinstance(a, (type(None), int, float, str, bytes, bytearray)):
_warn('Seeding based on hashing is deprecated\n'
'since Python 3.9 and will be removed in a subsequent '
'version. The only \n'
'supported seed types are: None, '
'int, float, str, bytes, and bytearray.',
DeprecationWarning, 2)

super().seed(a)
self.gauss_next = None

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecate having random.seed() call hash on arbitrary types.