Remove a false negative test for try/except in 25

This commit is contained in:
rocky 2024-07-12 12:39:27 -04:00
parent 9d150e0707
commit a42bef12d2
2 changed files with 42 additions and 35 deletions

View File

@ -470,14 +470,16 @@ class Python26Parser(Python2Parser):
return tokens[last].offset != ja_attr
elif lhs == "try_except":
# We need to distinguish "try_except" from "tryelsestmt"; we do that
# by checking the jump before the END_FINALLY
# by looking for a jump before the END_FINALLY to the "else" clause of
# "try else".
#
# If we have:
# insn
# <insn>
# POP_TOP
# END_FINALLY
# COME_FROM
# then insn has to be either a JUMP_FORWARD or a RETURN_VALUE
# and if it is JUMP_FORWARD, then it has to be a JUMP_FORWARD to right after
# then <insn> has to be either a a jump of some sort (JUMP_FORWARD, BREAK_LOOP, JUMP_BACK, or RETURN_VALUE).
# Furthermore, if it is JUMP_FORWARD, then it has to be a JUMP_FORWARD to right after
# COME_FROM
if last == len(tokens):
last -= 1
@ -491,7 +493,7 @@ class Python26Parser(Python2Parser):
# A jump of 2 is a jump around POP_TOP, END_FINALLY which
# would indicate try/else rather than try
return tokens[last - 3].kind not in frozenset(
("JUMP_FORWARD", "RETURN_VALUE")
("JUMP_FORWARD", "JUMP_BACK", "BREAK_LOOP", "RETURN_VALUE")
) or (tokens[last - 3] == "JUMP_FORWARD" and tokens[last - 3].attr != 2)
return False

View File

@ -1,16 +1,17 @@
# Copyright (c) 2020, 2022 Rocky Bernstein
# Copyright (c) 2020, 2022, 2024 Rocky Bernstein
def tryexcept(self, lhs, n, rule, ast, tokens, first, last):
def tryexcept(self, lhs, n: int, rule, ast, tokens, first: int, last: int):
come_from_except = ast[-1]
if rule == (
"try_except",
(
"SETUP_EXCEPT",
"suite_stmts_opt",
"POP_BLOCK",
"except_handler",
"opt_come_from_except",
),
"try_except",
(
"SETUP_EXCEPT",
"suite_stmts_opt",
"POP_BLOCK",
"except_handler",
"opt_come_from_except",
),
):
if come_from_except[0] == "COME_FROM":
# There should be at least two COME_FROMs, one from an
@ -20,31 +21,31 @@ def tryexcept(self, lhs, n, rule, ast, tokens, first, last):
pass
elif rule == (
"try_except",
(
"SETUP_EXCEPT",
"suite_stmts_opt",
"POP_BLOCK",
"except_handler",
"COME_FROM",
),
"try_except",
(
"SETUP_EXCEPT",
"suite_stmts_opt",
"POP_BLOCK",
"except_handler",
"COME_FROM",
),
):
return come_from_except.attr < tokens[first].offset
elif rule == (
'try_except',
(
'SETUP_EXCEPT',
'suite_stmts_opt',
'POP_BLOCK',
'except_handler',
'\\e_opt_come_from_except'
),
"try_except",
(
"SETUP_EXCEPT",
"suite_stmts_opt",
"POP_BLOCK",
"except_handler",
"\\e_opt_come_from_except",
),
):
# Find END_FINALLY.
for i in range(last, first, -1):
if tokens[i] == "END_FINALLY":
jump_before_finally = tokens[i-1]
jump_before_finally = tokens[i - 1]
if jump_before_finally.kind.startswith("JUMP"):
if jump_before_finally == "JUMP_FORWARD":
# If there is a JUMP_FORWARD before
@ -52,7 +53,9 @@ def tryexcept(self, lhs, n, rule, ast, tokens, first, last):
# beyond tokens[last].off2int() then
# this is a try/else rather than an
# try (no else).
return tokens[i-1].attr > tokens[last].off2int(prefer_last=True)
return tokens[i - 1].attr > tokens[last].off2int(
prefer_last=True
)
elif jump_before_finally == "JUMP_BACK":
# If there is a JUMP_BACK before the
# END_FINALLY then this is a looping
@ -61,8 +64,10 @@ def tryexcept(self, lhs, n, rule, ast, tokens, first, last):
# jump or this is a try/else rather
# than an try (no else).
except_handler = ast[3]
if (except_handler == "except_handler" and
except_handler[0] == "JUMP_FORWARD"):
if (
except_handler == "except_handler"
and except_handler[0] == "JUMP_FORWARD"
):
return True
return False
pass