X Tutup
Skip to content

Commit a760188

Browse files
committed
Improve BUILD_xxx_UNPACK slightly
1 parent ad345ef commit a760188

File tree

4 files changed

+46
-19
lines changed

4 files changed

+46
-19
lines changed
295 Bytes
Binary file not shown.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Bug in Python 3.5 is getting the two star'd arguments right.
2+
def sum(a,b,c,d):
3+
return a + b + c + d
4+
5+
args=(1,2)
6+
sum(*args, *args)
7+
8+
# FIXME: this is handled incorrectly
9+
# (*c,) = (3,4)

uncompyle6/parsers/parse3.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,15 @@ def add_custom_rules(self, tokens, customize):
485485
486486
# build_class (see load_build_class)
487487
488-
build_list ::= {expr}^n BUILD_LIST_n
489-
build_list ::= {expr}^n BUILD_TUPLE_n
488+
# Even the below say _list, in the semantic rules we
489+
# disambiguate tuples, and sets from lists
490+
491+
build_list ::= {expr}^n BUILD_LIST_n
492+
build_list ::= {expr}^n BUILD_TUPLE_n
493+
build_list ::= {expr}^n BUILD_SET_n
494+
build_list ::= {expr}^n BUILD_LIST_UNPACK_n
495+
build_list ::= {expr}^n BUILD_SET_UNPACK_n
496+
build_list ::= {expr}^n BUILD_TUPLE_UNPACK_n
490497
491498
load_closure ::= {LOAD_CLOSURE}^n BUILD_TUPLE_n
492499
# call_function (see custom_classfunc_rule)

uncompyle6/semantics/pysource.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,23 +1493,29 @@ def n_build_list(self, node):
14931493
self.prec = 100
14941494
lastnode = node.pop()
14951495
lastnodetype = lastnode.type
1496+
1497+
# If this build list is inside a CALL_FUNCTION_VAR,
1498+
# then the first * has already been printed.
1499+
# Until I have a better way to check for CALL_FUNCTION_VAR,
1500+
# will assume that if the text ends in *.
1501+
last_was_star = self.f.getvalue().endswith('*')
1502+
14961503
have_star = False
1497-
if lastnodetype.startswith('BUILD_LIST'):
1498-
# 3.5+ has BUILD_LIST_UNPACK
1499-
if lastnodetype == 'BUILD_LIST_UNPACK':
1500-
# FIXME: need to handle range of BUILD_LIST_UNPACK
1501-
have_star = True
1502-
endchar = ''
1503-
else:
1504-
self.write('['); endchar = ']'
1505-
elif lastnodetype.startswith('BUILD_TUPLE'):
1506-
self.write('('); endchar = ')'
1507-
elif lastnodetype.startswith('BUILD_SET'):
1508-
self.write('{'); endchar = '}'
1509-
elif lastnodetype.startswith('ROT_TWO'):
1510-
self.write('('); endchar = ')'
1504+
if lastnodetype.endswith('UNPACK'):
1505+
# FIXME: need to handle range of BUILD_LIST_UNPACK
1506+
have_star = True
1507+
endchar = ''
15111508
else:
1512-
raise 'Internal Error: n_build_list expects list or tuple'
1509+
if lastnodetype.startswith('BUILD_LIST'):
1510+
self.write('['); endchar = ']'
1511+
elif lastnodetype.startswith('BUILD_TUPLE'):
1512+
self.write('('); endchar = ')'
1513+
elif lastnodetype.startswith('BUILD_SET'):
1514+
self.write('{'); endchar = '}'
1515+
elif lastnodetype.startswith('ROT_TWO'):
1516+
self.write('('); endchar = ')'
1517+
else:
1518+
raise 'Internal Error: n_build_list expects list or tuple'
15131519

15141520
flat_elems = []
15151521
for elem in node:
@@ -1536,8 +1542,13 @@ def n_build_list(self, node):
15361542
sep += '\n' + self.indent + INDENT_PER_LEVEL[:-1]
15371543
else:
15381544
if sep != '': sep += ' '
1539-
if have_star:
1540-
sep += '*'
1545+
if not last_was_star:
1546+
if have_star:
1547+
sep += '*'
1548+
pass
1549+
pass
1550+
else:
1551+
last_was_star = False
15411552
self.write(sep, value)
15421553
sep = ','
15431554
if lastnode.attr == 1 and lastnodetype.startswith('BUILD_TUPLE'):

0 commit comments

Comments
 (0)
X Tutup