Skip to content

Commit a42bef1

Browse files
committed
Remove a false negative test for try/except in 25
1 parent 9d150e0 commit a42bef1

File tree

2 files changed

+42
-35
lines changed

2 files changed

+42
-35
lines changed

uncompyle6/parsers/parse26.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -470,14 +470,16 @@ def reduce_is_invalid(self, rule, ast, tokens, first, last):
470470
return tokens[last].offset != ja_attr
471471
elif lhs == "try_except":
472472
# We need to distinguish "try_except" from "tryelsestmt"; we do that
473-
# by checking the jump before the END_FINALLY
473+
# by looking for a jump before the END_FINALLY to the "else" clause of
474+
# "try else".
475+
#
474476
# If we have:
475-
# insn
477+
# <insn>
476478
# POP_TOP
477479
# END_FINALLY
478480
# COME_FROM
479-
# then insn has to be either a JUMP_FORWARD or a RETURN_VALUE
480-
# and if it is JUMP_FORWARD, then it has to be a JUMP_FORWARD to right after
481+
# then <insn> has to be either a a jump of some sort (JUMP_FORWARD, BREAK_LOOP, JUMP_BACK, or RETURN_VALUE).
482+
# Furthermore, if it is JUMP_FORWARD, then it has to be a JUMP_FORWARD to right after
481483
# COME_FROM
482484
if last == len(tokens):
483485
last -= 1
@@ -491,7 +493,7 @@ def reduce_is_invalid(self, rule, ast, tokens, first, last):
491493
# A jump of 2 is a jump around POP_TOP, END_FINALLY which
492494
# would indicate try/else rather than try
493495
return tokens[last - 3].kind not in frozenset(
494-
("JUMP_FORWARD", "RETURN_VALUE")
496+
("JUMP_FORWARD", "JUMP_BACK", "BREAK_LOOP", "RETURN_VALUE")
495497
) or (tokens[last - 3] == "JUMP_FORWARD" and tokens[last - 3].attr != 2)
496498

497499
return False

uncompyle6/parsers/reducecheck/tryexcept.py

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
# Copyright (c) 2020, 2022 Rocky Bernstein
1+
# Copyright (c) 2020, 2022, 2024 Rocky Bernstein
22

3-
def tryexcept(self, lhs, n, rule, ast, tokens, first, last):
3+
4+
def tryexcept(self, lhs, n: int, rule, ast, tokens, first: int, last: int):
45
come_from_except = ast[-1]
56
if rule == (
6-
"try_except",
7-
(
8-
"SETUP_EXCEPT",
9-
"suite_stmts_opt",
10-
"POP_BLOCK",
11-
"except_handler",
12-
"opt_come_from_except",
13-
),
7+
"try_except",
8+
(
9+
"SETUP_EXCEPT",
10+
"suite_stmts_opt",
11+
"POP_BLOCK",
12+
"except_handler",
13+
"opt_come_from_except",
14+
),
1415
):
1516
if come_from_except[0] == "COME_FROM":
1617
# There should be at least two COME_FROMs, one from an
@@ -20,39 +21,41 @@ def tryexcept(self, lhs, n, rule, ast, tokens, first, last):
2021
pass
2122

2223
elif rule == (
23-
"try_except",
24-
(
25-
"SETUP_EXCEPT",
26-
"suite_stmts_opt",
27-
"POP_BLOCK",
28-
"except_handler",
29-
"COME_FROM",
30-
),
24+
"try_except",
25+
(
26+
"SETUP_EXCEPT",
27+
"suite_stmts_opt",
28+
"POP_BLOCK",
29+
"except_handler",
30+
"COME_FROM",
31+
),
3132
):
3233
return come_from_except.attr < tokens[first].offset
3334

3435
elif rule == (
35-
'try_except',
36-
(
37-
'SETUP_EXCEPT',
38-
'suite_stmts_opt',
39-
'POP_BLOCK',
40-
'except_handler',
41-
'\\e_opt_come_from_except'
42-
),
36+
"try_except",
37+
(
38+
"SETUP_EXCEPT",
39+
"suite_stmts_opt",
40+
"POP_BLOCK",
41+
"except_handler",
42+
"\\e_opt_come_from_except",
43+
),
4344
):
4445
# Find END_FINALLY.
4546
for i in range(last, first, -1):
4647
if tokens[i] == "END_FINALLY":
47-
jump_before_finally = tokens[i-1]
48+
jump_before_finally = tokens[i - 1]
4849
if jump_before_finally.kind.startswith("JUMP"):
4950
if jump_before_finally == "JUMP_FORWARD":
5051
# If there is a JUMP_FORWARD before
5152
# the END_FINALLY to some jumps place
5253
# beyond tokens[last].off2int() then
5354
# this is a try/else rather than an
5455
# try (no else).
55-
return tokens[i-1].attr > tokens[last].off2int(prefer_last=True)
56+
return tokens[i - 1].attr > tokens[last].off2int(
57+
prefer_last=True
58+
)
5659
elif jump_before_finally == "JUMP_BACK":
5760
# If there is a JUMP_BACK before the
5861
# END_FINALLY then this is a looping
@@ -61,8 +64,10 @@ def tryexcept(self, lhs, n, rule, ast, tokens, first, last):
6164
# jump or this is a try/else rather
6265
# than an try (no else).
6366
except_handler = ast[3]
64-
if (except_handler == "except_handler" and
65-
except_handler[0] == "JUMP_FORWARD"):
67+
if (
68+
except_handler == "except_handler"
69+
and except_handler[0] == "JUMP_FORWARD"
70+
):
6671
return True
6772
return False
6873
pass

0 commit comments

Comments
 (0)