diff options
author | Alexandre Vassalotti <alexandre@peadrop.com> | 2010-01-09 23:35:54 (GMT) |
---|---|---|
committer | Alexandre Vassalotti <alexandre@peadrop.com> | 2010-01-09 23:35:54 (GMT) |
commit | ee936a21308679654b2d458166ff094ed735fef7 (patch) | |
tree | 612cd109e8ede4080f58f30ece3212d8e0f996d2 /Lib/compiler | |
parent | e36561352895170f28f77f0b4ec4292e35d1cc01 (diff) | |
download | cpython-ee936a21308679654b2d458166ff094ed735fef7.zip cpython-ee936a21308679654b2d458166ff094ed735fef7.tar.gz cpython-ee936a21308679654b2d458166ff094ed735fef7.tar.bz2 |
Issue #2335: Backport set literals syntax from Python 3.x.
Diffstat (limited to 'Lib/compiler')
-rw-r--r-- | Lib/compiler/ast.py | 16 | ||||
-rw-r--r-- | Lib/compiler/pyassem.py | 2 | ||||
-rw-r--r-- | Lib/compiler/pycodegen.py | 6 | ||||
-rw-r--r-- | Lib/compiler/transformer.py | 24 |
4 files changed, 40 insertions, 8 deletions
diff --git a/Lib/compiler/ast.py b/Lib/compiler/ast.py index f923077..f47434c 100644 --- a/Lib/compiler/ast.py +++ b/Lib/compiler/ast.py @@ -1107,6 +1107,22 @@ class RightShift(Node): def __repr__(self): return "RightShift((%s, %s))" % (repr(self.left), repr(self.right)) +class Set(Node): + def __init__(self, nodes, lineno=None): + self.nodes = nodes + self.lineno = lineno + + def getChildren(self): + return tuple(flatten(self.nodes)) + + def getChildNodes(self): + nodelist = [] + nodelist.extend(flatten_nodes(self.nodes)) + return tuple(nodelist) + + def __repr__(self): + return "Set(%s)" % (repr(self.nodes),) + class Slice(Node): def __init__(self, expr, flags, lower, upper, lineno=None): self.expr = expr diff --git a/Lib/compiler/pyassem.py b/Lib/compiler/pyassem.py index be0255d..88510de 100644 --- a/Lib/compiler/pyassem.py +++ b/Lib/compiler/pyassem.py @@ -734,6 +734,8 @@ class StackDepthTracker: return -count+1 def BUILD_LIST(self, count): return -count+1 + def BUILD_SET(self, count): + return -count+1 def CALL_FUNCTION(self, argc): hi, lo = divmod(argc, 256) return -(lo + hi * 2) diff --git a/Lib/compiler/pycodegen.py b/Lib/compiler/pycodegen.py index 960c4fc..bef9c70 100644 --- a/Lib/compiler/pycodegen.py +++ b/Lib/compiler/pycodegen.py @@ -1215,6 +1215,12 @@ class CodeGenerator: self.visit(elt) self.emit('BUILD_LIST', len(node.nodes)) + def visitSet(self, node): + self.set_lineno(node) + for elt in node.nodes: + self.visit(elt) + self.emit('BUILD_SET', len(node.nodes)) + def visitSliceobj(self, node): for child in node.nodes: self.visit(child) diff --git a/Lib/compiler/transformer.py b/Lib/compiler/transformer.py index 2a156d3..816f13b 100644 --- a/Lib/compiler/transformer.py +++ b/Lib/compiler/transformer.py @@ -749,7 +749,7 @@ class Transformer: def atom_lbrace(self, nodelist): if nodelist[1][0] == token.RBRACE: return Dict((), lineno=nodelist[0][2]) - return self.com_dictmaker(nodelist[1]) + return self.com_dictorsetmaker(nodelist[1]) def atom_backquote(self, nodelist): return Backquote(self.com_node(nodelist[1])) @@ -1197,13 +1197,21 @@ class Transformer: assert node[0] == symbol.gen_iter return node[1] - def com_dictmaker(self, nodelist): - # dictmaker: test ':' test (',' test ':' value)* [','] - items = [] - for i in range(1, len(nodelist), 4): - items.append((self.com_node(nodelist[i]), - self.com_node(nodelist[i+2]))) - return Dict(items, lineno=items[0][0].lineno) + def com_dictorsetmaker(self, nodelist): + # dictorsetmaker: ( (test ':' test (',' test ':' test)* [',']) | + # (test (',' test)* [',']) ) + assert nodelist[0] == symbol.dictorsetmaker + if len(nodelist) == 2 or nodelist[2][0] == token.COMMA: + items = [] + for i in range(1, len(nodelist), 2): + items.append(self.com_node(nodelist[i])) + return Set(items, lineno=items[0].lineno) + else: + items = [] + for i in range(1, len(nodelist), 4): + items.append((self.com_node(nodelist[i]), + self.com_node(nodelist[i+2]))) + return Dict(items, lineno=items[0][0].lineno) def com_apply_trailer(self, primaryNode, nodelist): t = nodelist[1][0] |