Author Peter Ebden
Recipients Peter Ebden
Date 2018-09-18.13:53:47
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1537278827.47.0.956365154283.issue34722@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2018-09-18 13:53:47Peter Ebdensetrecipients: + Peter Ebden
2018-09-18 13:53:47Peter Ebdensetmessageid: <1537278827.47.0.956365154283.issue34722@psf.upfronthosting.co.za>
2018-09-18 13:53:47Peter Ebdenlinkissue34722 messages
2018-09-18 13:53:47Peter Ebdencreate