Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
d0cdeaab
Commit
d0cdeaab
authored
Aug 22, 2019
by
Raymond Hettinger
Committed by
GitHub
Aug 22, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-32554: Deprecate hashing arbitrary types in random.seed() (GH-15382)
parent
4109263a
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
30 additions
and
2 deletions
+30
-2
Doc/library/random.rst
Doc/library/random.rst
+10
-0
Doc/whatsnew/3.9.rst
Doc/whatsnew/3.9.rst
+6
-0
Lib/random.py
Lib/random.py
+13
-2
Misc/NEWS.d/next/Library/2019-08-22-01-49-05.bpo-32554.4xiXyM.rst
...S.d/next/Library/2019-08-22-01-49-05.bpo-32554.4xiXyM.rst
+1
-0
No files found.
Doc/library/random.rst
View file @
d0cdeaab
...
...
@@ -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
...
...
Doc/whatsnew/3.9.rst
View file @
d0cdeaab
...
...
@@ -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
=======
...
...
Lib/random.py
View file @
d0cdeaab
...
...
@@ -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
)):
el
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'
)
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
...
...
Misc/NEWS.d/next/Library/2019-08-22-01-49-05.bpo-32554.4xiXyM.rst
0 → 100644
View file @
d0cdeaab
Deprecate having random.seed() call hash on arbitrary types.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment