-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
random.seed mutates input bytearray #88184
Comments
https://github.com/python/cpython/blob/master/Lib/random.py#L157 |
I don't see a bug here. As documented, "a str, bytes, or bytearray object gets converted to an int and all of its bits are used." The various input types are converted to an integer and it doesn't really matter how it is done as long as it is consistent. FWIW, the docs link to the source code so it can be examined if needed. |
I find the following behaviour very confusing: >>> import random
>>> a = bytearray("1234", "utf-8")
>>> b = bytearray("1234", "utf-8")
>>> a == b
True
>>> random.seed(a)
>>> a == b
False
>>> a
bytearray(b'1234\xd4\x04U\x9f`.\xabo\xd6\x02\xacv\x80\xda\xcb\xfa\xad\xd1603^\x95\x1f\tz\xf3\x90\x0e\x9d\xe1v\xb6\xdb(Q/.\x00\x0b\x9d\x04\xfb\xa5\x13>\x8b\x1cn\x8d\xf5\x9d\xb3\xa8\xab\x9d`\xbeK\x97\xcc\x9e\x81\xdb')
>>> b
bytearray(b'1234') The function doesn't document it will change the input argument |
The problem is that random seed will do
and that will modify the bytearray in place. >>> a = bytearray("1234", "utf-8")
>>> a += b"digest"
>>> a
bytearray(b'1234digest') IMHO, seed shouldn't modify the input. Since str, and bytes are immutable that will only happen when passing a bytearray which is not consistent. |
One solution to these would be making a copy of the parameter using copy.deepcopy and then performing operations on that copy. (The downside of this solution is that it will change the behavior.) |
Okay, I now understand your report and will prepare a fix. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: