This issue tracker has been migrated to GitHub ,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2018年09月18日 13:53 by Peter Ebden, last changed 2022年04月11日 14:59 by admin.
| Pull Requests | |||
|---|---|---|---|
| URL | Status | Linked | Edit |
| PR 9472 | open | Peter Ebden, 2018年09月21日 14:35 | |
| Messages (8) | |||
|---|---|---|---|
| msg325644 - (view) | Author: Peter Ebden (Peter Ebden) * | Date: 2018年09月18日 13:53 | |
We've found that the following code produces non-deterministic bytecode,
even post PEP-552:
def test(x):
if x in {'ONE', 'TWO', 'THREE'}:
pass
It's not too hard to test it:
$ python3.7 -m compileall --invalidation-mode=unchecked-hash test.py
Compiling 'test.py'...
$ sha1sum __pycache__/test.cpython-37.pyc
61e5682ca95e8707b4ef2a79f64566664dafd800 __pycache__/test.cpython-37.pyc
$ rm __pycache__/test.cpython-37.pyc
$ python3.7 -m compileall --invalidation-mode=unchecked-hash test.py
Compiling 'test.py'...
$ sha1sum __pycache__/test.cpython-37.pyc
222a06621b491879e5317b34e9dd715bacd89b7d __pycache__/test.cpython-37.pyc
It looks like the peephole optimiser is converting the LOAD_CONST instructions
for the set into a single LOAD_CONST for a frozenset which then serialises in
nondeterministic order. One can hence work around it by setting PYTHONHASHSEED
to a known value.
I'm happy to help out with this if needed, although I don't have a lot of
familiarity with the relevant code.
|
|||
| msg325657 - (view) | Author: Raymond Hettinger (rhettinger) * (Python committer) | Date: 2018年09月18日 15:19 | |
Have a look at line 512 in Python/marshal.c which calls PyObject_GetIter(). We would need to add PySequence_List() and PyList_Sort(). This will slow down marshaling but would make the bytecode deterministic. |
|||
| msg325658 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) | Date: 2018年09月18日 15:29 | |
Not all types are orderable.
>>> sorted({'', None})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'NoneType' and 'str'
|
|||
| msg325736 - (view) | Author: Peter Ebden (Peter Ebden) * | Date: 2018年09月19日 09:11 | |
Thanks for the pointer, I'll have a bit more of a dig into it (although Serhiy makes a good point too...). |
|||
| msg325837 - (view) | Author: Benjamin Peterson (benjamin.peterson) * (Python committer) | Date: 2018年09月20日 04:53 | |
Possibly we should just sort the individual marsahalled entries of the frozenset. |
|||
| msg344238 - (view) | Author: Raymond Hettinger (rhettinger) * (Python committer) | Date: 2019年06月01日 21:15 | |
Bumping up the priority a bit on this one. It would be nice to get it in for 3.8. |
|||
| msg347820 - (view) | Author: (yan12125) * | Date: 2019年07月13日 13:52 | |
I encounter another case that leads to non-deterministic bytecode. For example, with commit e6b46aafad3427463d6264a68824df4797e682f1 + PR 9472, I got: $ cat foobar.py _m = None $ PYTHONHASHSEED=0 ./python -m compileall --invalidation-mode=unchecked-hash foobar.py Compiling 'foobar.py'... $ sha256sum __pycache__/foobar.cpython-39.pyc 7f84b08d5536390d6ce4ccb2d65e259449c56549ee9cc67560f61771824f20ea __pycache__/foobar.cpython-39.pyc $ rm __pycache__/foobar.cpython-39.pyc $ PYTHONHASHSEED=1 ./python -m compileall --invalidation-mode=unchecked-hash foobar.py Compiling 'foobar.py'... $ sha256sum __pycache__/foobar.cpython-39.pyc 46dadbb92ad6e1e5b5f8abe9f107086cd231b2b80c15fe84f86e2081a6b8c428 __pycache__/foobar.cpython-39.pyc In this case there are no sets. I guess the cause is different. Should I open a new issue? |
|||
| msg401066 - (view) | Author: (yan12125) * | Date: 2021年09月05日 02:16 | |
issue37596 merged a fix to enable deterministic frozensets. I think this issue can be closed? Regarding my last comment msg347820 - it seems similar to one of https://bugs.python.org/issue34033 or https://bugs.python.org/issue34093. I followed those tickets instead. |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:59:06 | admin | set | github: 78903 |
| 2021年09月05日 02:16:59 | yan12125 | set | messages: + msg401066 |
| 2020年04月12日 16:46:23 | jefferyto | set | nosy:
+ jefferyto |
| 2019年07月13日 13:52:34 | yan12125 | set | messages: + msg347820 |
| 2019年07月09日 17:54:40 | xtreak | set | nosy:
+ pablogsal |
| 2019年07月09日 15:12:12 | serhiy.storchaka | set | versions: + Python 3.9, - Python 3.8 |
| 2019年07月09日 12:37:25 | yan12125 | set | nosy:
+ yan12125 |
| 2019年06月01日 21:15:26 | rhettinger | set | priority: normal -> high messages: + msg344238 versions: + Python 3.8, - Python 3.7 |
| 2018年09月21日 14:35:48 | Peter Ebden | set | keywords:
+ patch stage: patch review pull_requests: + pull_request8885 |
| 2018年09月20日 04:53:17 | benjamin.peterson | set | messages: + msg325837 |
| 2018年09月19日 09:11:37 | Peter Ebden | set | messages: + msg325736 |
| 2018年09月18日 15:38:59 | eric.snow | set | nosy:
+ benjamin.peterson |
| 2018年09月18日 15:29:44 | serhiy.storchaka | set | nosy:
+ serhiy.storchaka messages: + msg325658 |
| 2018年09月18日 15:19:31 | rhettinger | set | nosy:
+ rhettinger messages: + msg325657 |
| 2018年09月18日 13:53:47 | Peter Ebden | create | |