classification
Title: random.seed mutates input bytearray
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: arjaz, mark.dickinson, miguendes, miss-islington, pablogsal, rhettinger, shreyanavigyan
Priority: normal Keywords: patch

Created on 2021-05-03 09:13 by arjaz, last changed 2021-07-18 15:30 by pablogsal. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 25856 merged rhettinger, 2021-05-03 17:12
PR 25864 merged miss-islington, 2021-05-03 23:11
PR 25865 closed miss-islington, 2021-05-03 23:11
PR 25867 closed miss-islington, 2021-05-04 00:02
PR 25872 merged miss-islington, 2021-05-04 02:02
Messages (8)
msg392782 - (view) Author: Eugene Rossokha (arjaz) Date: 2021-05-03 09:13
https://github.com/python/cpython/blob/master/Lib/random.py#L157
If bytearray is passed as a seed, the function will change it.
It either has to be documented, or the implementation should change.
msg392784 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-05-03 09:35
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.
msg392786 - (view) Author: Eugene Rossokha (arjaz) Date: 2021-05-03 10:00
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
msg392789 - (view) Author: Miguel Brito (miguendes) * Date: 2021-05-03 10:20
The problem is that random seed will do

```
            if isinstance(a, str):
                a = a.encode()
            a += _sha512(a).digest()
            a = int.from_bytes(a, 'big')
```

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.
msg392801 - (view) Author: Shreyan Avigyan (shreyanavigyan) * Date: 2021-05-03 13:00
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.)
msg392821 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-05-03 16:41
Okay, I now understand your report and will prepare a fix.
msg392844 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-05-03 23:36
New changeset e733e9951d0116e9af66b66772e708412d7f5280 by Miss Islington (bot) in branch '3.9':
bpo-44018: random.seed() no longer mutates its inputs (GH-25856) (GH-25864)
https://github.com/python/cpython/commit/e733e9951d0116e9af66b66772e708412d7f5280
msg392849 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-05-04 02:45
New changeset 2995bff4269d274c0a3abfd45dc33b28f0c3e25f by Miss Islington (bot) in branch '3.10':
bpo-44018: random.seed() no longer mutates its inputs (GH-25856) (GH-25872)
https://github.com/python/cpython/commit/2995bff4269d274c0a3abfd45dc33b28f0c3e25f
History
Date User Action Args
2021-07-18 15:30:52pablogsalsetpull_requests: - pull_request25770
2021-07-18 15:26:31pablogsalsetnosy: + pablogsal

pull_requests: + pull_request25770
2021-05-04 04:02:13rhettingersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-05-04 02:45:36rhettingersetmessages: + msg392849
2021-05-04 02:02:55miss-islingtonsetpull_requests: + pull_request24551
2021-05-04 00:02:25miss-islingtonsetpull_requests: + pull_request24548
2021-05-03 23:36:17rhettingersetmessages: + msg392844
2021-05-03 23:11:56miss-islingtonsetpull_requests: + pull_request24546
2021-05-03 23:11:51miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request24545
2021-05-03 17:12:56rhettingersetkeywords: + patch
stage: patch review
pull_requests: + pull_request24540
2021-05-03 16:41:41rhettingersetmessages: + msg392821
title: Bug in random.seed -> random.seed mutates input bytearray
2021-05-03 13:00:13shreyanavigyansetnosy: + shreyanavigyan
messages: + msg392801
2021-05-03 10:20:29miguendessetnosy: + miguendes
messages: + msg392789
2021-05-03 10:00:53arjazsetmessages: + msg392786
2021-05-03 09:35:48rhettingersetassignee: rhettinger
messages: + msg392784
2021-05-03 09:24:39xtreaksetnosy: + rhettinger, mark.dickinson
2021-05-03 09:13:38arjazcreate