Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 86e58e2

Browse files
committed
SF patch 1547796 by Georg Brandl -- set literals.
1 parent ecfd0b2 commit 86e58e2

22 files changed

Lines changed: 229 additions & 72 deletions

File tree

Doc/lib/libdis.tex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,10 @@ \subsection{Python Byte Code Instructions}
501501
Works as \code{BUILD_TUPLE}, but creates a list.
502502
\end{opcodedesc}
503503

504+
\begin{opcodedesc}{BUILD_SET}{count}
505+
Works as \code{BUILD_TUPLE}, but creates a set.
506+
\end{opcodedesc}
507+
504508
\begin{opcodedesc}{BUILD_MAP}{zero}
505509
Pushes a new empty dictionary object onto the stack. The argument is
506510
ignored and set to zero by the compiler.

Grammar/Grammar

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ factor: ('+'|'-'|'~') factor | power
101101
power: atom trailer* ['**' factor]
102102
atom: ('(' [yield_expr|testlist_gexp] ')' |
103103
'[' [listmaker] ']' |
104-
'{' [dictmaker] '}' |
104+
'{' [dictsetmaker] '}' |
105105
NAME | NUMBER | STRING+)
106106
listmaker: test ( list_for | (',' test)* [','] )
107107
testlist_gexp: test ( gen_for | (',' test)* [','] )
@@ -112,7 +112,7 @@ subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
112112
sliceop: ':' [test]
113113
exprlist: expr (',' expr)* [',']
114114
testlist: test (',' test)* [',']
115-
dictmaker: test ':' test (',' test ':' test)* [',']
115+
dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [','])
116116

117117
classdef: 'class' NAME ['(' [testlist] ')'] ':' suite
118118

Include/Python-ast.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ struct _stmt {
184184
};
185185

186186
enum _expr_kind {BoolOp_kind=1, BinOp_kind=2, UnaryOp_kind=3, Lambda_kind=4,
187-
IfExp_kind=5, Dict_kind=6, ListComp_kind=7,
188-
GeneratorExp_kind=8, Yield_kind=9, Compare_kind=10,
189-
Call_kind=11, Num_kind=12, Str_kind=13, Attribute_kind=14,
190-
Subscript_kind=15, Name_kind=16, List_kind=17, Tuple_kind=18};
187+
IfExp_kind=5, Dict_kind=6, Set_kind=7, ListComp_kind=8,
188+
GeneratorExp_kind=9, Yield_kind=10, Compare_kind=11,
189+
Call_kind=12, Num_kind=13, Str_kind=14, Attribute_kind=15,
190+
Subscript_kind=16, Name_kind=17, List_kind=18, Tuple_kind=19};
191191
struct _expr {
192192
enum _expr_kind kind;
193193
union {
@@ -223,6 +223,10 @@ struct _expr {
223223
asdl_seq *values;
224224
} Dict;
225225

226+
struct {
227+
asdl_seq *elts;
228+
} Set;
229+
226230
struct {
227231
expr_ty elt;
228232
asdl_seq *generators;
@@ -399,6 +403,7 @@ expr_ty IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int
399403
col_offset, PyArena *arena);
400404
expr_ty Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset,
401405
PyArena *arena);
406+
expr_ty Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena);
402407
expr_ty ListComp(expr_ty elt, asdl_seq * generators, int lineno, int
403408
col_offset, PyArena *arena);
404409
expr_ty GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int

Include/graminit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
#define sliceop 324
7070
#define exprlist 325
7171
#define testlist 326
72-
#define dictmaker 327
72+
#define dictsetmaker 327
7373
#define classdef 328
7474
#define arglist 329
7575
#define argument 330

Include/opcode.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,12 @@ extern "C" {
9797
#define LOAD_NAME 101 /* Index in name list */
9898
#define BUILD_TUPLE 102 /* Number of tuple items */
9999
#define BUILD_LIST 103 /* Number of list items */
100-
#define BUILD_MAP 104 /* Always zero for now */
101-
#define LOAD_ATTR 105 /* Index in name list */
102-
#define COMPARE_OP 106 /* Comparison operator */
103-
#define IMPORT_NAME 107 /* Index in name list */
104-
#define IMPORT_FROM 108 /* Index in name list */
100+
#define BUILD_SET 104 /* Number of set items */
101+
#define BUILD_MAP 105 /* Always zero for now */
102+
#define LOAD_ATTR 106 /* Index in name list */
103+
#define COMPARE_OP 107 /* Comparison operator */
104+
#define IMPORT_NAME 108 /* Index in name list */
105+
#define IMPORT_FROM 109 /* Index in name list */
105106

106107
#define JUMP_FORWARD 110 /* Number of bytes to skip */
107108
#define JUMP_IF_FALSE 111 /* "" */

Lib/compiler/ast.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,6 @@ def __init__(self, decorators, name, argnames, defaults, flags, doc, code, linen
542542
self.kwargs = 1
543543

544544

545-
546545
def getChildren(self):
547546
children = []
548547
children.append(self.decorators)
@@ -572,6 +571,7 @@ def __init__(self, code, lineno=None):
572571
self.argnames = ['.0']
573572
self.varargs = self.kwargs = None
574573

574+
575575
def getChildren(self):
576576
return self.code,
577577

@@ -589,7 +589,6 @@ def __init__(self, assign, iter, ifs, lineno=None):
589589
self.lineno = lineno
590590
self.is_outmost = False
591591

592-
593592
def getChildren(self):
594593
children = []
595594
children.append(self.assign)
@@ -766,7 +765,6 @@ def __init__(self, argnames, defaults, flags, code, lineno=None):
766765
self.kwargs = 1
767766

768767

769-
770768
def getChildren(self):
771769
children = []
772770
children.append(self.argnames)
@@ -1091,6 +1089,22 @@ def getChildNodes(self):
10911089
def __repr__(self):
10921090
return "RightShift((%s, %s))" % (repr(self.left), repr(self.right))
10931091

1092+
class Set(Node):
1093+
def __init__(self, items, lineno=None):
1094+
self.items = items
1095+
self.lineno = lineno
1096+
1097+
def getChildren(self):
1098+
return tuple(flatten(self.items))
1099+
1100+
def getChildNodes(self):
1101+
nodelist = []
1102+
nodelist.extend(flatten_nodes(self.items))
1103+
return tuple(nodelist)
1104+
1105+
def __repr__(self):
1106+
return "Set(%s)" % (repr(self.items),)
1107+
10941108
class Slice(Node):
10951109
def __init__(self, expr, flags, lower, upper, lineno=None):
10961110
self.expr = expr

Lib/compiler/pyassem.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,8 @@ def BUILD_TUPLE(self, count):
793793
return -count+1
794794
def BUILD_LIST(self, count):
795795
return -count+1
796+
def BUILD_SET(self, count):
797+
return -count+1
796798
def CALL_FUNCTION(self, argc):
797799
hi, lo = divmod(argc, 256)
798800
return -(lo + hi * 2)

Lib/compiler/pycodegen.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,12 @@ def visitList(self, node):
12411241
self.visit(elt)
12421242
self.emit('BUILD_LIST', len(node.nodes))
12431243

1244+
def visitSet(self, node):
1245+
self.set_lineno(node)
1246+
for elt in node.items:
1247+
self.visit(elt)
1248+
self.emit('BUILD_SET', len(node.items))
1249+
12441250
def visitSliceobj(self, node):
12451251
for child in node.nodes:
12461252
self.visit(child)

Lib/compiler/transformer.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ def atom_lsqb(self, nodelist):
738738
def atom_lbrace(self, nodelist):
739739
if nodelist[1][0] == token.RBRACE:
740740
return Dict((), lineno=nodelist[0][2])
741-
return self.com_dictmaker(nodelist[1])
741+
return self.com_dictsetmaker(nodelist[1])
742742

743743
def atom_backquote(self, nodelist):
744744
return Backquote(self.com_node(nodelist[1]))
@@ -1182,13 +1182,20 @@ def com_gen_iter(self, node):
11821182
assert node[0] == symbol.gen_iter
11831183
return node[1]
11841184

1185-
def com_dictmaker(self, nodelist):
1186-
# dictmaker: test ':' test (',' test ':' value)* [',']
1185+
def com_dictsetmaker(self, nodelist):
1186+
# dictsetmaker: (test ':' test (',' test ':' value)* [',']) | (test (',' test)* [','])
11871187
items = []
1188-
for i in range(1, len(nodelist), 4):
1189-
items.append((self.com_node(nodelist[i]),
1190-
self.com_node(nodelist[i+2])))
1191-
return Dict(items, lineno=items[0][0].lineno)
1188+
if nodelist[2] != ':':
1189+
# it's a set
1190+
for i in range(1, len(nodelist), 2):
1191+
items.append(self.com_node(nodelist[i]))
1192+
return Set(items, lineno=items[0].lineno)
1193+
else:
1194+
# it's a dict
1195+
for i in range(1, len(nodelist), 4):
1196+
items.append((self.com_node(nodelist[i]),
1197+
self.com_node(nodelist[i+2])))
1198+
return Dict(items, lineno=items[0][0].lineno)
11921199

11931200
def com_apply_trailer(self, primaryNode, nodelist):
11941201
t = nodelist[1][0]

Lib/opcode.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,13 @@ def jabs_op(name, op):
137137
name_op('LOAD_NAME', 101) # Index in name list
138138
def_op('BUILD_TUPLE', 102) # Number of tuple items
139139
def_op('BUILD_LIST', 103) # Number of list items
140-
def_op('BUILD_MAP', 104) # Always zero for now
141-
name_op('LOAD_ATTR', 105) # Index in name list
142-
def_op('COMPARE_OP', 106) # Comparison operator
143-
hascompare.append(106)
144-
name_op('IMPORT_NAME', 107) # Index in name list
145-
name_op('IMPORT_FROM', 108) # Index in name list
140+
def_op('BUILD_SET', 104) # Number of set items
141+
def_op('BUILD_MAP', 105) # Always zero for now
142+
name_op('LOAD_ATTR', 106) # Index in name list
143+
def_op('COMPARE_OP', 107) # Comparison operator
144+
hascompare.append(107)
145+
name_op('IMPORT_NAME', 108) # Index in name list
146+
name_op('IMPORT_FROM', 109) # Index in name list
146147

147148
jrel_op('JUMP_FORWARD', 110) # Number of bytes to skip
148149
jrel_op('JUMP_IF_FALSE', 111) # ""

0 commit comments

Comments
 (0)