You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-02 16:44:46 +08:00
Remove a false negative test for try/except in 25
This commit is contained in:
@@ -470,14 +470,16 @@ class Python26Parser(Python2Parser):
|
|||||||
return tokens[last].offset != ja_attr
|
return tokens[last].offset != ja_attr
|
||||||
elif lhs == "try_except":
|
elif lhs == "try_except":
|
||||||
# We need to distinguish "try_except" from "tryelsestmt"; we do that
|
# 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:
|
# If we have:
|
||||||
# insn
|
# <insn>
|
||||||
# POP_TOP
|
# POP_TOP
|
||||||
# END_FINALLY
|
# END_FINALLY
|
||||||
# COME_FROM
|
# COME_FROM
|
||||||
# then insn has to be either a JUMP_FORWARD or a RETURN_VALUE
|
# then <insn> has to be either a a jump of some sort (JUMP_FORWARD, BREAK_LOOP, JUMP_BACK, or RETURN_VALUE).
|
||||||
# and if it is JUMP_FORWARD, then it has to be a JUMP_FORWARD to right after
|
# Furthermore, if it is JUMP_FORWARD, then it has to be a JUMP_FORWARD to right after
|
||||||
# COME_FROM
|
# COME_FROM
|
||||||
if last == len(tokens):
|
if last == len(tokens):
|
||||||
last -= 1
|
last -= 1
|
||||||
@@ -491,7 +493,7 @@ class Python26Parser(Python2Parser):
|
|||||||
# A jump of 2 is a jump around POP_TOP, END_FINALLY which
|
# A jump of 2 is a jump around POP_TOP, END_FINALLY which
|
||||||
# would indicate try/else rather than try
|
# would indicate try/else rather than try
|
||||||
return tokens[last - 3].kind not in frozenset(
|
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)
|
) or (tokens[last - 3] == "JUMP_FORWARD" and tokens[last - 3].attr != 2)
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
@@ -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]
|
come_from_except = ast[-1]
|
||||||
if rule == (
|
if rule == (
|
||||||
"try_except",
|
"try_except",
|
||||||
(
|
(
|
||||||
"SETUP_EXCEPT",
|
"SETUP_EXCEPT",
|
||||||
"suite_stmts_opt",
|
"suite_stmts_opt",
|
||||||
"POP_BLOCK",
|
"POP_BLOCK",
|
||||||
"except_handler",
|
"except_handler",
|
||||||
"opt_come_from_except",
|
"opt_come_from_except",
|
||||||
),
|
),
|
||||||
):
|
):
|
||||||
if come_from_except[0] == "COME_FROM":
|
if come_from_except[0] == "COME_FROM":
|
||||||
# There should be at least two COME_FROMs, one from an
|
# 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
|
pass
|
||||||
|
|
||||||
elif rule == (
|
elif rule == (
|
||||||
"try_except",
|
"try_except",
|
||||||
(
|
(
|
||||||
"SETUP_EXCEPT",
|
"SETUP_EXCEPT",
|
||||||
"suite_stmts_opt",
|
"suite_stmts_opt",
|
||||||
"POP_BLOCK",
|
"POP_BLOCK",
|
||||||
"except_handler",
|
"except_handler",
|
||||||
"COME_FROM",
|
"COME_FROM",
|
||||||
),
|
),
|
||||||
):
|
):
|
||||||
return come_from_except.attr < tokens[first].offset
|
return come_from_except.attr < tokens[first].offset
|
||||||
|
|
||||||
elif rule == (
|
elif rule == (
|
||||||
'try_except',
|
"try_except",
|
||||||
(
|
(
|
||||||
'SETUP_EXCEPT',
|
"SETUP_EXCEPT",
|
||||||
'suite_stmts_opt',
|
"suite_stmts_opt",
|
||||||
'POP_BLOCK',
|
"POP_BLOCK",
|
||||||
'except_handler',
|
"except_handler",
|
||||||
'\\e_opt_come_from_except'
|
"\\e_opt_come_from_except",
|
||||||
),
|
),
|
||||||
):
|
):
|
||||||
# Find END_FINALLY.
|
# Find END_FINALLY.
|
||||||
for i in range(last, first, -1):
|
for i in range(last, first, -1):
|
||||||
if tokens[i] == "END_FINALLY":
|
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.kind.startswith("JUMP"):
|
||||||
if jump_before_finally == "JUMP_FORWARD":
|
if jump_before_finally == "JUMP_FORWARD":
|
||||||
# If there is a JUMP_FORWARD before
|
# 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
|
# beyond tokens[last].off2int() then
|
||||||
# this is a try/else rather than an
|
# this is a try/else rather than an
|
||||||
# try (no else).
|
# 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":
|
elif jump_before_finally == "JUMP_BACK":
|
||||||
# If there is a JUMP_BACK before the
|
# If there is a JUMP_BACK before the
|
||||||
# END_FINALLY then this is a looping
|
# 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
|
# jump or this is a try/else rather
|
||||||
# than an try (no else).
|
# than an try (no else).
|
||||||
except_handler = ast[3]
|
except_handler = ast[3]
|
||||||
if (except_handler == "except_handler" and
|
if (
|
||||||
except_handler[0] == "JUMP_FORWARD"):
|
except_handler == "except_handler"
|
||||||
|
and except_handler[0] == "JUMP_FORWARD"
|
||||||
|
):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
pass
|
pass
|
||||||
|
Reference in New Issue
Block a user