bpo-32554: Deprecate hashing arbitrary types in random.seed() (GH-15382)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Thu, 22 Aug 2019 16:19:36 +0000 (09:19 -0700)
committerGitHub <noreply@github.com>
Thu, 22 Aug 2019 16:19:36 +0000 (09:19 -0700)
Doc/library/random.rst
Doc/whatsnew/3.9.rst
Lib/random.py
Misc/NEWS.d/next/Library/2019-08-22-01-49-05.bpo-32554.4xiXyM.rst [new file with mode: 0644]

index 90b86248e6e1c3654886836998f2675fc03f571d..1bd1856937be5b3e6a36facac32a5d5fa512ad43 100644 (file)
@@ -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
@@ -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
index 6615a2e28effa1a6e705a465a50a1a47e5430ded..05a17a5c73905a1400406a6659ade3fc8772ca65 100644 (file)
@@ -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
 =======
index 365a01957203f275d72b439702cc445841366529..be4401c554d7e5205d122984916c44e6b6a86290 100644 (file)
@@ -121,7 +121,10 @@ class Random(_random.Random):
                 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.
@@ -143,12 +146,20 @@ class Random(_random.Random):
             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
 
diff --git a/Misc/NEWS.d/next/Library/2019-08-22-01-49-05.bpo-32554.4xiXyM.rst b/Misc/NEWS.d/next/Library/2019-08-22-01-49-05.bpo-32554.4xiXyM.rst
new file mode 100644 (file)
index 0000000..752cf48
--- /dev/null
@@ -0,0 +1 @@
+Deprecate having random.seed() call hash on arbitrary types.