Separate rules for set, tuple, dict from list...

Sync fragments.py with pysource
This commit is contained in:
rocky
2017-12-05 00:12:11 -05:00
parent 00b95dd72e
commit 2d628acf60
5 changed files with 22 additions and 6 deletions

View File

@@ -279,8 +279,13 @@ class Python2Parser(PythonParser):
self.add_unique_rule("expr1024 ::=%s" % (' expr32' * 32), self.add_unique_rule("expr1024 ::=%s" % (' expr32' * 32),
opname_base, v, customize) opname_base, v, customize)
self.seen1024 = True self.seen1024 = True
rule = ('list ::= ' + 'expr1024 '*thousands + collection = opname_base[opname_base.find('_')+1:].lower()
rule = (('%s ::= ' % collection) + 'expr1024 '*thousands +
'expr32 '*thirty32s + 'expr '*(v % 32) + opname) 'expr32 '*thirty32s + 'expr '*(v % 32) + opname)
self.add_unique_rules([
"expr ::= %s" % collection,
rule], customize)
continue
elif opname_base == 'BUILD_MAP': elif opname_base == 'BUILD_MAP':
if opname == 'BUILD_MAP_n': if opname == 'BUILD_MAP_n':
# PyPy sometimes has no count. Sigh. # PyPy sometimes has no count. Sigh.

View File

@@ -678,10 +678,13 @@ class Python3Parser(PythonParser):
rule = ('load_closure ::= %s%s' % (('LOAD_CLOSURE ' * v), opname)) rule = ('load_closure ::= %s%s' % (('LOAD_CLOSURE ' * v), opname))
self.add_unique_rule(rule, opname, token.attr, customize) self.add_unique_rule(rule, opname, token.attr, customize)
if not is_LOAD_CLOSURE or v == 0: if not is_LOAD_CLOSURE or v == 0:
rule = ('list ::= ' + 'expr1024 ' * int(v//1024) + collection = opname_base[opname_base.find('_')+1:].lower()
rule = (('%s ::= ' % collection) + 'expr1024 ' * int(v//1024) +
'expr32 ' * int((v//32) % 32) + 'expr32 ' * int((v//32) % 32) +
'expr ' * (v % 32) + opname) 'expr ' * (v % 32) + opname)
self.add_unique_rule(rule, opname, token.attr, customize) self.add_unique_rules([
'expr ::= %s' % collection,
rule], customize)
continue continue
elif opname_base == 'BUILD_SLICE': elif opname_base == 'BUILD_SLICE':
if token.attr == 2: if token.attr == 2:

View File

@@ -364,7 +364,12 @@ class FragmentsWalker(pysource.SourceWalker, object):
def n_LOAD_CONST(self, node): def n_LOAD_CONST(self, node):
start = len(self.f.getvalue()) start = len(self.f.getvalue())
data = node.pattr; datatype = type(data) data = node.pattr; datatype = type(data)
if isinstance(datatype, int) and data == minint: if isinstance(data, float) and str(data) in frozenset(['nan', '-nan', 'inf', '-inf']):
# float values 'nan' and 'inf' are not directly representable in Python at least
# before 3.5 and even there it is via a library constant.
# So we will canonicalize their representation as float('nan') and float('inf')
self.write("float('%s')" % data)
elif isinstance(datatype, int) and data == minint:
# convert to hex, since decimal representation # convert to hex, since decimal representation
# would result in 'LOAD_CONST; UNARY_NEGATIVE' # would result in 'LOAD_CONST; UNARY_NEGATIVE'
# change:hG/2002-02-07: this was done for all negative integers # change:hG/2002-02-07: this was done for all negative integers
@@ -1457,6 +1462,9 @@ class FragmentsWalker(pysource.SourceWalker, object):
self.indent_less(INDENT_PER_LEVEL) self.indent_less(INDENT_PER_LEVEL)
self.prec = p self.prec = p
self.prune() self.prune()
return
n_set = n_tuple = n_build_set = n_list
def template_engine(self, entry, startnode): def template_engine(self, entry, startnode):
"""The format template interpetation engine. See the comment at the """The format template interpetation engine. See the comment at the

View File

@@ -500,7 +500,7 @@ def make_function3(self, node, is_lambda, nested=1, codeNode=None):
if (expr_node[0] == 'LOAD_CONST' and if (expr_node[0] == 'LOAD_CONST' and
isinstance(expr_node[0].attr, tuple)): isinstance(expr_node[0].attr, tuple)):
defparams = list(expr_node[0].attr) defparams = list(expr_node[0].attr)
elif expr_node[0] == 'list': elif expr_node[0] in frozenset(('list', 'tuple', 'dict', 'set')):
defparams = [self.traverse(n, indent='') for n in expr_node[0][:-1]] defparams = [self.traverse(n, indent='') for n in expr_node[0][:-1]]
else: else:
defparams = [] defparams = []

View File

@@ -1883,7 +1883,7 @@ class SourceWalker(GenericASTTraversal, object):
self.prune() self.prune()
return return
n_build_set = n_list n_set = n_tuple = n_build_set = n_list
def n_unpack(self, node): def n_unpack(self, node):
if node[0].kind.startswith('UNPACK_EX'): if node[0].kind.startswith('UNPACK_EX'):