Skip to content

Commit 8e04132

Browse files
committed
Fix some Python set comprehension bugs
1 parent c10b9ba commit 8e04132

File tree

6 files changed

+21
-9
lines changed

6 files changed

+21
-9
lines changed

test/add-test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
cfile = "bytecode_%s/%s" % (version, short) + 'c'
1010
print("byte-compiling %s to %s" % (path, cfile))
1111
py_compile.compile(path, cfile)
12-
if sys.version >= (2, 6, 0):
12+
if isinstance(version, str) or version >= (2, 6, 0):
1313
os.system("../bin/uncompyle6 -a -t %s" % cfile)
221 Bytes
Binary file not shown.
453 Bytes
Binary file not shown.
153 Bytes
Binary file not shown.

test/simple_source/comprehension/05_set_comprehension.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ def __new__(classdict):
99
members = {k: classdict[k] for k in classdict._member_names}
1010
return members
1111

12-
# Bug from Python 3.4 asyncio/tasks.py
13-
def as_completed(fs, *, loop=None):
14-
todo = {async(f, loop=loop) for f in set(fs)}
12+
# Bug from 3.5.1 enum.py in 2.7, and 3.x
13+
{a for b in bases for a in b.__dict__}

uncompyle6/semantics/pysource.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,30 +1155,43 @@ def comprehension_walk3(self, node, iter_index, code_index=-5):
11551155

11561156
# find innermost node
11571157
if_node = None
1158+
comp_for = None
1159+
comp_designator = None
1160+
if n == 'comp_iter':
1161+
comp_for = n
1162+
comp_designator = ast[3]
1163+
11581164
while n in ('list_iter', 'comp_iter'):
11591165
n = n[0] # recurse one step
1160-
if n == 'list_for':
1166+
if n in ('list_for', 'comp_for'):
11611167
if n[2] == 'designator':
11621168
designator = n[2]
11631169
n = n[3]
1164-
elif n in ['list_if', 'list_if_not', 'comp_if']:
1170+
elif n in ('list_if', 'list_if_not', 'comp_if'):
11651171
if_node = n[0]
11661172
if n[1] == 'designator':
11671173
designator = n[1]
11681174
n = n[2]
11691175
pass
11701176
pass
11711177

1178+
# Python 2.7+ starts including set_comp_body
11721179
# Python 3.5+ starts including setcomp_func
1173-
assert n.type in ('lc_body', 'comp_body', 'setcomp_func'), ast
1180+
assert n.type in ('lc_body', 'comp_body', 'setcomp_func', 'set_comp_body'), ast
11741181
assert designator, "Couldn't find designator in list/set comprehension"
11751182

11761183
self.preorder(n[0])
11771184
self.write(' for ')
1178-
self.preorder(designator)
1185+
if comp_designator:
1186+
self.preorder(comp_designator)
1187+
else:
1188+
self.preorder(designator)
1189+
11791190
self.write(' in ')
11801191
self.preorder(node[-3])
1181-
if if_node:
1192+
if comp_designator:
1193+
self.preorder(comp_for)
1194+
elif if_node:
11821195
self.write(' if ')
11831196
self.preorder(if_node)
11841197
self.prec = p

0 commit comments

Comments
 (0)