You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Non-deterministic bytecode generation for frozenset #78903
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.
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.
>>> sorted({'', None})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'NoneType' and 'str'
peterebden commented on Sep 19, 2018
peterebdenmannequin
MannequinAuthor
More actions
Thanks for the pointer, I'll have a bit more of a dig into it (although Serhiy makes a good point too...).
Activity
peterebden commentedon Sep 18, 2018
We've found that the following code produces non-deterministic bytecode,
even post PEP-552:
It's not too hard to test it:
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.
rhettinger commentedon Sep 18, 2018
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.
serhiy-storchaka commentedon Sep 18, 2018
Not all types are orderable.
peterebden commentedon Sep 19, 2018
Thanks for the pointer, I'll have a bit more of a dig into it (although Serhiy makes a good point too...).
benjaminp commentedon Sep 20, 2018
Possibly we should just sort the individual marsahalled entries of the frozenset.
rhettinger commentedon Jun 1, 2019
Bumping up the priority a bit on this one. It would be nice to get it in for 3.8.
yan12125 commentedon Jul 13, 2019
I encounter another case that leads to non-deterministic bytecode. For example, with commit e6b46aa + PR 9472, I got:
In this case there are no sets. I guess the cause is different. Should I open a new issue?
yan12125 commentedon Sep 5, 2021
bpo-37596 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.
[-]Non-deterministic bytecode generation[/-][+]Non-deterministic bytecode generation for frozenset[/+]pythongh-106287: do not write objects after encountering an unmarshal…