Skip to content

Commit 6fe8a1d

Browse files
committed
Address long-standing Python 2.6 try/except bug
Fixes #405
1 parent b0b67e9 commit 6fe8a1d

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

test/bytecode_2.4/07_try_except.pyc

1.36 KB
Binary file not shown.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Bug portion of Issue #405 https://github.com/rocky/python-uncompyle6/issues/405
2+
# Bug was detecting if/else as the last item in a "try: .. except" block.
3+
class Saveframe(object):
4+
"""A saveframe. Use the classmethod from_scratch to create one."""
5+
6+
frame_list = {}
7+
8+
def frame_dict(self):
9+
return
10+
11+
# Next line is 1477
12+
def __setitem__(self, key, item):
13+
# Next line is 1481
14+
if isinstance(item, Saveframe):
15+
try:
16+
self.frame_list[key] = item
17+
except TypeError:
18+
if key in (self.frame_dict()):
19+
dict((frame.name, frame) for frame in self.frame_list)
20+
for pos, frame in enumerate(self.frame_list):
21+
if frame.name == key:
22+
self.frame_list[pos] = item
23+
else:
24+
raise KeyError(
25+
"Saveframe with name '%s' does not exist and "
26+
"therefore cannot be written to. Use the add_saveframe method to add new saveframes."
27+
% key
28+
)
29+
# Next line is 1498
30+
raise ValueError("You can only assign an entry to a saveframe splice.")
31+
32+
33+
x = Saveframe()
34+
x.__setitem__("foo", 5)

uncompyle6/parsers/parse26.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ def p_stmt26(self, args):
130130
# Semantic actions want else_suitel to be at index 3
131131
ifelsestmtl ::= testexpr c_stmts_opt cf_jb_cf_pop else_suitel
132132
ifelsestmtc ::= testexpr c_stmts_opt ja_cf_pop else_suitec
133+
ifelsestmt ::= testexpr stmts_opt ja_cf_pop else_suite
134+
135+
# The last except of a "try: ... except" can do this...
136+
except_suite ::= stmts_opt COME_FROM JUMP_ABSOLUTE POP_TOP
133137
134138
# Semantic actions want suite_stmts_opt to be at index 3
135139
with ::= expr setupwith SETUP_FINALLY suite_stmts_opt

0 commit comments

Comments
 (0)