Python 3.0 while1 if bug...

Is a workaround. We really need more tagging in of SETUP_LOOP and COME_FROM.
This commit is contained in:
rocky
2016-11-13 01:23:57 -05:00
parent 50c2e1bda9
commit 80a4ad4f1b
7 changed files with 33 additions and 7 deletions

Binary file not shown.

View File

@@ -0,0 +1,9 @@
# From python 3.4 sre.pyc
while 1:
if __file__:
while 1:
if __file__:
break
raise RuntimeError
else:
raise RuntimeError

View File

@@ -337,6 +337,9 @@ class Python3Parser(PythonParser):
whileelsestmt ::= SETUP_LOOP testexpr l_stmts_opt JUMP_BACK POP_BLOCK
else_suite COME_FROM_LOOP
# FIXME: This gets confused with if/else in a loop. But while/else in Python
# is probably pretty rare.
while1elsestmt ::= SETUP_LOOP l_stmts JUMP_BACK
else_suite COME_FROM_LOOP

View File

@@ -23,7 +23,16 @@ class Python30Parser(Python3Parser):
jmp_true ::= JUMP_IF_TRUE POP_TOP
jmp_false ::= JUMP_IF_FALSE POP_TOP
while1elsestmt ::= SETUP_LOOP l_stmts POP_TOP else_suite COME_FROM_LOOP JUMP_BACK
# Used to keep index order the same in semantic actions
jb_pop_top ::= JUMP_BACK POP_TOP
# FIXME: Add COME_FROM designators
# This gets confused with while1elsestmt. But this is probably more common
while1stmt ::= SETUP_LOOP l_stmts COME_FROM_LOOP
else_suitel ::= l_stmts COME_FROM_LOOP JUMP_BACK
ifelsestmtl ::= testexpr c_stmts_opt jb_pop_top else_suitel
withasstmt ::= expr setupwithas designator suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM_FINALLY

View File

@@ -149,7 +149,7 @@ class Scanner3(Scanner):
"""
show_asm = self.show_asm if not show_asm else show_asm
# show_asm = 'before'
# show_asm = 'after'
if show_asm in ('both', 'before'):
bytecode = Bytecode(co, self.opc)
for instr in bytecode.get_instructions(co):
@@ -314,7 +314,9 @@ class Scanner3(Scanner):
if target <= inst.offset:
next_opname = self.opname[self.code[inst.offset+3]]
if (inst.offset in self.stmts and
next_opname not in ('END_FINALLY', 'POP_BLOCK')
next_opname not in ('END_FINALLY', 'POP_BLOCK',
# Python 3.0 only uses POP_TOP
'POP_TOP')
and inst.offset not in self.not_continue):
opname = 'CONTINUE'
else:

View File

@@ -1772,7 +1772,7 @@ def deparse_code(version, co, out=StringIO(), showasm=False, showast=False,
if deparsed.ast_errors:
deparsed.write("# NOTE: have decompilation errors.\n")
deparsed.write("# Use -t option to show full of errors.")
deparsed.write("# Use -t option to show full context.")
for err in deparsed.ast_errors:
deparsed.write(err)
deparsed.ERROR = True

View File

@@ -311,7 +311,7 @@ TABLE_DIRECT = {
'whileTruestmt': ( '%|while True:\n%+%c%-\n\n', 1 ),
'whilestmt': ( '%|while %c:\n%+%c%-\n\n', 1, 2 ),
'while1stmt': ( '%|while 1:\n%+%c%-\n\n', 1 ),
'while1elsestmt': ( '%|while 1:\n%+%c%-%|else:\n%+%c%-\n\n', 1, 3 ),
'while1elsestmt': ( '%|while 1:\n%+%c%-%|else:\n%+%c%-\n\n', 1, -2 ),
'whileelsestmt': ( '%|while %c:\n%+%c%-%|else:\n%+%c%-\n\n', 1, 2, -2 ),
'whileelselaststmt': ( '%|while %c:\n%+%c%-%|else:\n%+%c%-', 1, 2, -2 ),
'forstmt': ( '%|for %c in %c:\n%+%c%-\n\n', 3, 1, 4 ),
@@ -1010,7 +1010,10 @@ class SourceWalker(GenericASTTraversal, object):
def n_ifelsestmt(self, node, preprocess=False):
else_suite = node[3]
try:
n = else_suite[0]
except:
from trepan.api import debug; debug()
if len(n) == 1 == len(n[0]) and n[0] == '_stmts':
n = n[0][0][0]
@@ -2322,7 +2325,7 @@ def deparse_code(version, co, out=sys.stdout, showasm=None, showast=False,
if deparsed.ast_errors:
deparsed.write("# NOTE: have decompilation errors.\n")
deparsed.write("# Use -t option to full context of errors.")
deparsed.write("# Use -t option to show full context.")
for err in deparsed.ast_errors:
deparsed.write(err)
raise SourceWalkerError("Deparsing hit an internal grammar-rule bug")