bpo-31482: Missing bytes support for random.seed() version 1#3614
Conversation
| """ | ||
|
|
||
| if version == 1 and isinstance(a, (str, bytes)): | ||
| a = a.decode('latin-1') if isinstance(a, bytes) else a |
There was a problem hiding this comment.
Maybe instead convert a string to a list of integers and remove ord() in the following code?
if isinstance(a, str):
a = list(map(ord, a))
This would make the following code clearer and maybe slightly faster.
There was a problem hiding this comment.
I prefer to leave the current code alone and just add a conversion for the bytes which is so uncommon that no one ever noticed the lack of bytes support.
| # Verify that version 1 seeds are unaffected by hash randomization | ||
| # when the seeds are expressed as bytes rather than strings | ||
|
|
||
| self.gen.seed(b'nofar', version=1) # hash('nofar') == 5990528763808513177 |
There was a problem hiding this comment.
hash('nofar') == 5990528763808513177
This is not true because of hash randomization.
There was a problem hiding this comment.
The comment referred to the Python2.7 hash() function (non-randomized). That is what is being emulated by version 1.
There was a problem hiding this comment.
Could you add a comment for this?
|
In general LGTM. But tests for string and bytes seeds test only ASCII strings. Would be nice to add tests for non-ASCII strings. For example if use encoding other than "latin-1" in your patch, this bug wouldn't be catched by existing tests. |
|
Thanks @rhettinger for the PR 🌮🎉.. I'm working now to backport this PR to: 3.6. |
|
GH-3629 is a backport of this pull request to the 3.6 branch. |
…ythonGH-3614) bpo-31482: Missing bytes support for random.seed() version 1 pythonGH-3614 (cherry picked from commit 132a7d7)
|
Commit message is duplicated. |
|
Thanks @rhettinger for the PR 🌮🎉.. I'm working now to backport this PR to: 3.6. |
…ythonGH-3614) bpo-31482: Missing bytes support for random.seed() version 1 pythonGH-3614 (cherry picked from commit 132a7d7)
|
GH-3659 is a backport of this pull request to the 3.6 branch. |
https://bugs.python.org/issue31482