diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2019-08-22 16:19:36 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-22 16:19:36 (GMT) |
commit | d0cdeaab76fef8a6e5a04665df226b6659111e4e (patch) | |
tree | 71f9d3b0791543ef421090997152c713ef4f8684 /Lib/random.py | |
parent | 4109263a7edce11194e301138cf66fa2d07f7ce4 (diff) | |
download | cpython-d0cdeaab76fef8a6e5a04665df226b6659111e4e.zip cpython-d0cdeaab76fef8a6e5a04665df226b6659111e4e.tar.gz cpython-d0cdeaab76fef8a6e5a04665df226b6659111e4e.tar.bz2 |
bpo-32554: Deprecate hashing arbitrary types in random.seed() (GH-15382)
Diffstat (limited to 'Lib/random.py')
-rw-r--r-- | Lib/random.py | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/Lib/random.py b/Lib/random.py index 365a019..be4401c 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -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 |