diff options
author | Guido van Rossum <guido@python.org> | 2006-08-28 15:27:34 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2006-08-28 15:27:34 (GMT) |
commit | 86e58e239e39845e706c4afa392423f0fedcdf39 (patch) | |
tree | 1d0f4d942e644ee5c903636d87176b98a7203371 /Lib | |
parent | ecfd0b2f3bfd622c3ba148e53d3feebb8c1ae721 (diff) | |
download | cpython-86e58e239e39845e706c4afa392423f0fedcdf39.zip cpython-86e58e239e39845e706c4afa392423f0fedcdf39.tar.gz cpython-86e58e239e39845e706c4afa392423f0fedcdf39.tar.bz2 |
SF patch 1547796 by Georg Brandl -- set literals.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/compiler/ast.py | 20 | ||||
-rw-r--r-- | Lib/compiler/pyassem.py | 2 | ||||
-rw-r--r-- | Lib/compiler/pycodegen.py | 6 | ||||
-rw-r--r-- | Lib/compiler/transformer.py | 21 | ||||
-rw-r--r-- | Lib/opcode.py | 13 | ||||
-rw-r--r-- | Lib/test/test_grammar.py | 9 | ||||
-rw-r--r-- | Lib/test/test_set.py | 11 |
7 files changed, 61 insertions, 21 deletions
diff --git a/Lib/compiler/ast.py b/Lib/compiler/ast.py index 94a6262..6953325 100644 --- a/Lib/compiler/ast.py +++ b/Lib/compiler/ast.py @@ -542,7 +542,6 @@ class Function(Node): self.kwargs = 1 - def getChildren(self): children = [] children.append(self.decorators) @@ -572,6 +571,7 @@ class GenExpr(Node): self.argnames = ['.0'] self.varargs = self.kwargs = None + def getChildren(self): return self.code, @@ -589,7 +589,6 @@ class GenExprFor(Node): self.lineno = lineno self.is_outmost = False - def getChildren(self): children = [] children.append(self.assign) @@ -766,7 +765,6 @@ class Lambda(Node): self.kwargs = 1 - def getChildren(self): children = [] children.append(self.argnames) @@ -1091,6 +1089,22 @@ class RightShift(Node): def __repr__(self): return "RightShift((%s, %s))" % (repr(self.left), repr(self.right)) +class Set(Node): + def __init__(self, items, lineno=None): + self.items = items + self.lineno = lineno + + def getChildren(self): + return tuple(flatten(self.items)) + + def getChildNodes(self): + nodelist = [] + nodelist.extend(flatten_nodes(self.items)) + return tuple(nodelist) + + def __repr__(self): + return "Set(%s)" % (repr(self.items),) + 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 95f5c3e..542d704 100644 --- a/Lib/compiler/pyassem.py +++ b/Lib/compiler/pyassem.py @@ -793,6 +793,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 cc197e3..af99045 100644 --- a/Lib/compiler/pycodegen.py +++ b/Lib/compiler/pycodegen.py @@ -1241,6 +1241,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.items: + self.visit(elt) + self.emit('BUILD_SET', len(node.items)) + 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 b564300..42640e8 100644 --- a/Lib/compiler/transformer.py +++ b/Lib/compiler/transformer.py @@ -738,7 +738,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_dictsetmaker(nodelist[1]) def atom_backquote(self, nodelist): return Backquote(self.com_node(nodelist[1])) @@ -1182,13 +1182,20 @@ class Transformer: assert node[0] == symbol.gen_iter return node[1] - def com_dictmaker(self, nodelist): - # dictmaker: test ':' test (',' test ':' value)* [','] + def com_dictsetmaker(self, nodelist): + # dictsetmaker: (test ':' test (',' test ':' value)* [',']) | (test (',' test)* [',']) 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) + if nodelist[2] != ':': + # it's a set + for i in range(1, len(nodelist), 2): + items.append(self.com_node(nodelist[i])) + return Set(items, lineno=items[0].lineno) + else: + # it's a dict + 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] diff --git a/Lib/opcode.py b/Lib/opcode.py index c2d7a59..cf8d909 100644 --- a/Lib/opcode.py +++ b/Lib/opcode.py @@ -137,12 +137,13 @@ hasconst.append(100) name_op('LOAD_NAME', 101) # Index in name list def_op('BUILD_TUPLE', 102) # Number of tuple items def_op('BUILD_LIST', 103) # Number of list items -def_op('BUILD_MAP', 104) # Always zero for now -name_op('LOAD_ATTR', 105) # Index in name list -def_op('COMPARE_OP', 106) # Comparison operator -hascompare.append(106) -name_op('IMPORT_NAME', 107) # Index in name list -name_op('IMPORT_FROM', 108) # Index in name list +def_op('BUILD_SET', 104) # Number of set items +def_op('BUILD_MAP', 105) # Always zero for now +name_op('LOAD_ATTR', 106) # Index in name list +def_op('COMPARE_OP', 107) # Comparison operator +hascompare.append(107) +name_op('IMPORT_NAME', 108) # Index in name list +name_op('IMPORT_FROM', 109) # Index in name list jrel_op('JUMP_FORWARD', 110) # Number of bytes to skip jrel_op('JUMP_IF_FALSE', 111) # "" diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py index 331d527..93dc9ec 100644 --- a/Lib/test/test_grammar.py +++ b/Lib/test/test_grammar.py @@ -685,8 +685,8 @@ print L print 'atoms' -### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | NAME | NUMBER | STRING -### dictmaker: test ':' test (',' test ':' test)* [','] +### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING +### dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [',']) x = (1) x = (1 or 2 or 3) @@ -706,6 +706,11 @@ x = {'one': 1, 'two': 2} x = {'one': 1, 'two': 2,} x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6} +x = {'one'} +x = {'one', 1,} +x = {'one', 'two', 'three'} +x = {2, 3, 4,} + x = x x = 'x' x = 123 diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py index 23926be..556e390 100644 --- a/Lib/test/test_set.py +++ b/Lib/test/test_set.py @@ -261,6 +261,11 @@ class TestSet(TestJointOps): t = self.thetype(s) self.assertNotEqual(id(s), id(t)) + def test_set_literal(self): + s = set([1,2,3]) + t = {1,2,3} + self.assertEqual(s, t) + def test_hash(self): self.assertRaises(TypeError, hash, self.s) @@ -626,7 +631,7 @@ class TestBasicOpsEmpty(TestBasicOps): self.set = set(self.values) self.dup = set(self.values) self.length = 0 - self.repr = "set([])" + self.repr = "{}" #------------------------------------------------------------------------------ @@ -637,7 +642,7 @@ class TestBasicOpsSingleton(TestBasicOps): self.set = set(self.values) self.dup = set(self.values) self.length = 1 - self.repr = "set([3])" + self.repr = "{3}" def test_in(self): self.failUnless(3 in self.set) @@ -654,7 +659,7 @@ class TestBasicOpsTuple(TestBasicOps): self.set = set(self.values) self.dup = set(self.values) self.length = 1 - self.repr = "set([(0, 'zero')])" + self.repr = "{(0, 'zero')}" def test_in(self): self.failUnless((0, "zero") in self.set) |