Python 3 except clause parsing bug

This commit is contained in:
rocky
2016-06-19 10:19:45 -04:00
parent ff014a8393
commit 5d86a4e536
6 changed files with 32 additions and 27 deletions

Binary file not shown.

View File

@@ -0,0 +1,9 @@
# From python 3.4 contextlib
# JUMP_BACK in while causes a problem
while exit_callbacks:
try:
if cb:
exc_details = 5
except:
exc_details = new_exc_details
pass

View File

@@ -40,8 +40,13 @@ class PythonParser(GenericASTBuilder):
setattr(self, i, None)
def error(self, tokens, index):
start = index - 2 if index - 2 > 0 else 0
finish = index +2 if index + 2 < len(tokens) else len(tokens)
# Find the last line boundary
for start in range(index, -1, -1):
if tokens[start].linestart: break
pass
for finish in range(index+1, len(tokens)):
if tokens[finish].linestart: break
pass
err_token = tokens[index]
print("Token context:")
for i in range(start, finish):
@@ -118,6 +123,19 @@ class PythonParser(GenericASTBuilder):
genexpr_func ::= LOAD_FAST FOR_ITER designator comp_iter JUMP_BACK
'''
def p_jump(self, args):
"""
_jump ::= JUMP_ABSOLUTE
_jump ::= JUMP_FORWARD
_jump ::= JUMP_BACK
# Note: Python < 2.7 doesn't have POP_JUMP_IF ...
jmp_false ::= POP_JUMP_IF_FALSE
jmp_false ::= JUMP_IF_FALSE
jmp_true ::= POP_JUMP_IF_TRUE
jmp_true ::= JUMP_IF_TRUE
"""
def p_dictcomp(self, args):
'''
expr ::= dictcomp

View File

@@ -189,15 +189,6 @@ class Python2Parser(PythonParser):
classdefdeco1 ::= expr classdefdeco2 CALL_FUNCTION_1
classdefdeco2 ::= LOAD_CONST expr mkfunc CALL_FUNCTION_0 BUILD_CLASS
_jump ::= JUMP_ABSOLUTE
_jump ::= JUMP_FORWARD
_jump ::= JUMP_BACK
jmp_false ::= POP_JUMP_IF_FALSE
jmp_false ::= JUMP_IF_FALSE
jmp_true ::= POP_JUMP_IF_TRUE
jmp_true ::= JUMP_IF_TRUE
assert ::= assert_expr jmp_true LOAD_ASSERT RAISE_VARARGS_1
assert2 ::= assert_expr jmp_true LOAD_ASSERT expr CALL_FUNCTION_1 RAISE_VARARGS_1
assert2 ::= assert_expr jmp_true LOAD_ASSERT expr RAISE_VARARGS_2
@@ -270,8 +261,7 @@ class Python2Parser(PythonParser):
except_cond2 ::= DUP_TOP expr COMPARE_OP
jmp_false POP_TOP designator POP_TOP
except ::= POP_TOP POP_TOP POP_TOP c_stmts_opt JUMP_FORWARD
except ::= POP_TOP POP_TOP POP_TOP c_stmts_opt jmp_abs
except ::= POP_TOP POP_TOP POP_TOP c_stmts_opt _jump
except ::= POP_TOP POP_TOP POP_TOP return_stmts
jmp_abs ::= JUMP_ABSOLUTE

View File

@@ -195,15 +195,6 @@ class Python3Parser(PythonParser):
classdefdeco1 ::= expr classdefdeco2 CALL_FUNCTION_1
classdefdeco2 ::= LOAD_CONST expr mkfunc CALL_FUNCTION_0 BUILD_CLASS
_jump ::= JUMP_ABSOLUTE
_jump ::= JUMP_FORWARD
_jump ::= JUMP_BACK
jmp_false ::= POP_JUMP_IF_FALSE
jmp_false ::= JUMP_IF_FALSE
jmp_true ::= POP_JUMP_IF_TRUE
jmp_true ::= JUMP_IF_TRUE
assert ::= assert_expr jmp_true LOAD_ASSERT RAISE_VARARGS_1
assert2 ::= assert_expr jmp_true LOAD_ASSERT expr CALL_FUNCTION_1 RAISE_VARARGS_1
assert2 ::= assert_expr jmp_true LOAD_ASSERT expr RAISE_VARARGS_2
@@ -293,15 +284,14 @@ class Python3Parser(PythonParser):
except_cond2 ::= DUP_TOP expr COMPARE_OP
jmp_false POP_TOP designator POP_TOP
except ::= POP_TOP POP_TOP POP_TOP c_stmts_opt POP_EXCEPT JUMP_FORWARD
except ::= POP_TOP POP_TOP POP_TOP c_stmts_opt POP_EXCEPT _jump
except ::= POP_TOP POP_TOP POP_TOP return_stmts
except_pop_except ::= POP_TOP POP_TOP POP_TOP POP_EXCEPT c_stmts_opt JUMP_FORWARD
except_pop_except ::= POP_TOP POP_TOP POP_TOP POP_EXCEPT c_stmts_opt jmp_abs
jmp_abs ::= JUMP_ABSOLUTE
jmp_abs ::= JUMP_BACK
withstmt ::= expr SETUP_WITH POP_TOP suite_stmts_opt
POP_BLOCK LOAD_CONST COME_FROM
WITH_CLEANUP END_FINALLY
@@ -314,7 +304,6 @@ class Python3Parser(PythonParser):
def p_misc(self, args):
"""
_jump ::= NOP
try_middle ::= JUMP_FORWARD COME_FROM except_stmts END_FINALLY NOP COME_FROM
"""

View File

@@ -331,7 +331,6 @@ TABLE_DIRECT = {
'tf_trystmt': ( '%c%-%c%+', 1, 3 ),
'tf_tryelsestmt': ( '%c%-%c%|else:\n%+%c', 1, 3, 4 ),
'except': ('%|except:\n%+%c%-', 3 ),
'except_pop_except': ('%|except:\n%+%c%-', 4 ),
'except_cond1': ( '%|except %c:\n', 1 ),
'except_cond2': ( '%|except %c as %c:\n', 1, 5 ),
'except_suite': ( '%+%c%-%C', 0, (1, maxint, '') ),