Author twouters
Recipients lukasz.langa, pablogsal, twouters
Date 2019-09-11.14:27:50
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1568212070.98.0.977590744203.issue38115@roundup.psfhosted.org>
In-reply-to
Content
The peephole optimizer in Python 2.7 and later (and probably a *lot* earlier) has a bug where if the optimizer entirely optimizes away the last line(s) of a function, the lnotab references invalid bytecode offsets:

>>> def f(cond1, cond2):
...     while 1:
...         return 3
...     while 1:
...         return 5
...     return 6
... 
>>> list(dis.findlinestarts(f.__code__))
[(0, 3), (4, 5), (8, 6)]
>>> len(f.__code__.co_code)
8
>>> f.__code__.co_code[8]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: index out of range

The problem is that the lnotab-readjustment in Python/peephole.c doesn't account for trailing NOPs in a bytecode string. I haven't been able to reproduce this before Python 3.8, probably because the optimizer wasn't capable of optimizing things aggressively enough to end a bytecode string with NOPs.

I have a fix for this bug already.
History
Date User Action Args
2019-09-11 14:27:51twouterssetrecipients: + twouters, lukasz.langa, pablogsal
2019-09-11 14:27:50twouterssetmessageid: <1568212070.98.0.977590744203.issue38115@roundup.psfhosted.org>
2019-09-11 14:27:50twouterslinkissue38115 messages
2019-09-11 14:27:50twouterscreate