Better try/else detection

This commit is contained in:
rocky
2019-12-09 14:04:57 -05:00
parent fdac1e3c46
commit 7d9c4ce8ca
2 changed files with 31 additions and 1 deletions

View File

@@ -105,8 +105,9 @@ case $PYVERSION in
[test_generators.py]=1
[test_grp.py]=1 # Long test - might work Control flow?
[test_opcodes.py]=1
[test_pyclbr.py]=1 # Bug in checkModule()/ismethod() try confused for try-else
[test_pwd.py]=1 # Long test - might work? Control flow?
[test_re.py]=1 # Probably Control flow?
[test_re.py]=1 # try confused with try-else again
[test_queue.py]=1 # Control flow?
[test_trace.py]=1 # Line numbers are expected to be different
[test_urllib2net.py]=1 # Fails on its own. May need interactive input

View File

@@ -639,6 +639,8 @@ class Python2Parser(PythonParser):
self.check_reduce["raise_stmt1"] = "tokens"
self.check_reduce["assert_expr_and"] = "AST"
self.check_reduce["tryelsestmt"] = "AST"
self.check_reduce["tryelsestmtl"] = "AST"
self.check_reduce["aug_assign2"] = "AST"
self.check_reduce["or"] = "AST"
# self.check_reduce['_stmts'] = 'AST'
@@ -674,6 +676,33 @@ class Python2Parser(PythonParser):
elif lhs in ("delete_subscript", "del_expr"):
op = ast[0][0]
return op.kind in ("and", "or")
elif lhs in ("tryelsestmt", "tryelsetmtl"):
# Check the end of the except handler that there isn't a jump from
# inside the except handler to the end. If that happens
# then this is a "try" with no "else".
except_handler = ast[3]
if except_handler == "except_handler":
come_from = except_handler[-1]
# We only care about the *first* come_from because that is the
# the innermost one. So if the "tryelse" is invalid (should be a "try")
# ti will be invalid here.
if come_from == "COME_FROM":
first_come_from = except_handler[-1]
else:
assert come_from == "come_froms"
first_come_from = come_from[0]
leading_jump = except_handler[0]
# We really don't care that this is a jump per-se. But
# we could also check that this jumps to the end of the except if
# desired.
if isinstance(leading_jump, SyntaxTree):
except_handler_first_offset = leading_jump.first_child().off2int()
else:
except_handler_first_offset = leading_jump.off2int()
return first_come_from.attr > except_handler_first_offset
return False