Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(6793)

Delta Between Two Patch Sets: Python/compile.c

Issue 11682: PEP 380 reference implementation for 3.3
Left Patch Set: Created 7 years, 10 months ago
Right Patch Set: Created 7 years, 4 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 /* 1 /*
2 * This file compiles an abstract syntax tree (AST) into Python bytecode. 2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
3 * 3 *
4 * The primary entry point is PyAST_Compile(), which returns a 4 * The primary entry point is PyAST_Compile(), which returns a
5 * PyCodeObject. The compiler makes several passes to build the code 5 * PyCodeObject. The compiler makes several passes to build the code
6 * object: 6 * object:
7 * 1. Checks for future statements. See future.c 7 * 1. Checks for future statements. See future.c
8 * 2. Builds a symbol table. See symtable.c. 8 * 2. Builds a symbol table. See symtable.c.
9 * 3. Generate code for basic blocks. See compiler_mod() in this file. 9 * 3. Generate code for basic blocks. See compiler_mod() in this file.
10 * 4. Assemble the basic blocks into final code. See assemble() in 10 * 4. Assemble the basic blocks into final code. See assemble() in
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 compiler IR. 83 compiler IR.
84 */ 84 */
85 85
86 enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END }; 86 enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
87 87
88 struct fblockinfo { 88 struct fblockinfo {
89 enum fblocktype fb_type; 89 enum fblocktype fb_type;
90 basicblock *fb_block; 90 basicblock *fb_block;
91 }; 91 };
92 92
93 enum {
94 COMPILER_SCOPE_MODULE,
95 COMPILER_SCOPE_CLASS,
96 COMPILER_SCOPE_FUNCTION,
97 COMPILER_SCOPE_COMPREHENSION,
98 };
99
93 /* The following items change on entry and exit of code blocks. 100 /* The following items change on entry and exit of code blocks.
94 They must be saved and restored when returning to a block. 101 They must be saved and restored when returning to a block.
95 */ 102 */
96 struct compiler_unit { 103 struct compiler_unit {
97 PySTEntryObject *u_ste; 104 PySTEntryObject *u_ste;
98 105
99 PyObject *u_name; 106 PyObject *u_name;
107 PyObject *u_qualname; /* dot-separated qualified name (lazy) */
108 int u_scope_type;
109
100 /* The following fields are dicts that map objects to 110 /* The following fields are dicts that map objects to
101 the index of them in co_XXX. The index is used as 111 the index of them in co_XXX. The index is used as
102 the argument for opcodes that refer to those collections. 112 the argument for opcodes that refer to those collections.
103 */ 113 */
104 PyObject *u_consts; /* all constants */ 114 PyObject *u_consts; /* all constants */
105 PyObject *u_names; /* all names */ 115 PyObject *u_names; /* all names */
106 PyObject *u_varnames; /* local variables */ 116 PyObject *u_varnames; /* local variables */
107 PyObject *u_cellvars; /* cell variables */ 117 PyObject *u_cellvars; /* cell variables */
108 PyObject *u_freevars; /* free variables */ 118 PyObject *u_freevars; /* free variables */
109 119
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 152
143 int c_optimize; /* optimization level */ 153 int c_optimize; /* optimization level */
144 int c_interactive; /* true if in interactive mode */ 154 int c_interactive; /* true if in interactive mode */
145 int c_nestlevel; 155 int c_nestlevel;
146 156
147 struct compiler_unit *u; /* compiler state for current block */ 157 struct compiler_unit *u; /* compiler state for current block */
148 PyObject *c_stack; /* Python list holding compiler_unit ptrs */ 158 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
149 PyArena *c_arena; /* pointer to memory allocation arena */ 159 PyArena *c_arena; /* pointer to memory allocation arena */
150 }; 160 };
151 161
152 static int compiler_enter_scope(struct compiler *, identifier, void *, int); 162 static int compiler_enter_scope(struct compiler *, identifier, int, void *, int) ;
153 static void compiler_free(struct compiler *); 163 static void compiler_free(struct compiler *);
154 static basicblock *compiler_new_block(struct compiler *); 164 static basicblock *compiler_new_block(struct compiler *);
155 static int compiler_next_instr(struct compiler *, basicblock *); 165 static int compiler_next_instr(struct compiler *, basicblock *);
156 static int compiler_addop(struct compiler *, int); 166 static int compiler_addop(struct compiler *, int);
157 static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *); 167 static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
158 static int compiler_addop_i(struct compiler *, int, int); 168 static int compiler_addop_i(struct compiler *, int, int);
159 static int compiler_addop_j(struct compiler *, int, basicblock *, int); 169 static int compiler_addop_j(struct compiler *, int, basicblock *, int);
160 static basicblock *compiler_use_new_block(struct compiler *); 170 static basicblock *compiler_use_new_block(struct compiler *);
161 static int compiler_error(struct compiler *, const char *); 171 static int compiler_error(struct compiler *, const char *);
162 static int compiler_nameop(struct compiler *, identifier, expr_context_ty); 172 static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
(...skipping 27 matching lines...) Expand all
190 static PyCodeObject *assemble(struct compiler *, int addNone); 200 static PyCodeObject *assemble(struct compiler *, int addNone);
191 static PyObject *__doc__; 201 static PyObject *__doc__;
192 202
193 #define COMPILER_CAPSULE_NAME_COMPILER_UNIT "compile.c compiler unit" 203 #define COMPILER_CAPSULE_NAME_COMPILER_UNIT "compile.c compiler unit"
194 204
195 PyObject * 205 PyObject *
196 _Py_Mangle(PyObject *privateobj, PyObject *ident) 206 _Py_Mangle(PyObject *privateobj, PyObject *ident)
197 { 207 {
198 /* Name mangling: __private becomes _classname__private. 208 /* Name mangling: __private becomes _classname__private.
199 This is independent from how the name is used. */ 209 This is independent from how the name is used. */
200 const Py_UNICODE *p, *name = PyUnicode_AS_UNICODE(ident); 210 PyObject *result;
201 Py_UNICODE *buffer; 211 size_t nlen, plen, ipriv;
202 size_t nlen, plen; 212 Py_UCS4 maxchar;
203 if (privateobj == NULL || !PyUnicode_Check(privateobj) || 213 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
204 name == NULL || name[0] != '_' || name[1] != '_') { 214 PyUnicode_READ_CHAR(ident, 0) != '_' ||
215 PyUnicode_READ_CHAR(ident, 1) != '_') {
205 Py_INCREF(ident); 216 Py_INCREF(ident);
206 return ident; 217 return ident;
207 } 218 }
208 p = PyUnicode_AS_UNICODE(privateobj); 219 nlen = PyUnicode_GET_LENGTH(ident);
209 nlen = Py_UNICODE_strlen(name); 220 plen = PyUnicode_GET_LENGTH(privateobj);
210 /* Don't mangle __id__ or names with dots. 221 /* Don't mangle __id__ or names with dots.
211 222
212 The only time a name with a dot can occur is when 223 The only time a name with a dot can occur is when
213 we are compiling an import statement that has a 224 we are compiling an import statement that has a
214 package name. 225 package name.
215 226
216 TODO(jhylton): Decide whether we want to support 227 TODO(jhylton): Decide whether we want to support
217 mangling of the module name, e.g. __M.X. 228 mangling of the module name, e.g. __M.X.
218 */ 229 */
219 if ((name[nlen-1] == '_' && name[nlen-2] == '_') 230 if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
220 || Py_UNICODE_strchr(name, '.')) { 231 PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
232 PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
221 Py_INCREF(ident); 233 Py_INCREF(ident);
222 return ident; /* Don't mangle __whatever__ */ 234 return ident; /* Don't mangle __whatever__ */
223 } 235 }
224 /* Strip leading underscores from class name */ 236 /* Strip leading underscores from class name */
225 while (*p == '_') 237 ipriv = 0;
226 p++; 238 while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
227 if (*p == 0) { 239 ipriv++;
240 if (ipriv == plen) {
228 Py_INCREF(ident); 241 Py_INCREF(ident);
229 return ident; /* Don't mangle if class is just underscores */ 242 return ident; /* Don't mangle if class is just underscores */
230 } 243 }
231 plen = Py_UNICODE_strlen(p); 244 plen -= ipriv;
232 245
233 assert(1 <= PY_SSIZE_T_MAX - nlen); 246 assert(1 <= PY_SSIZE_T_MAX - nlen);
234 assert(1 + nlen <= PY_SSIZE_T_MAX - plen); 247 assert(1 + nlen <= PY_SSIZE_T_MAX - plen);
235 248
236 ident = PyUnicode_FromStringAndSize(NULL, 1 + nlen + plen); 249 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
237 if (!ident) 250 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
238 return 0; 251 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
239 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */ 252
240 buffer = PyUnicode_AS_UNICODE(ident); 253 result = PyUnicode_New(1 + nlen + plen, maxchar);
241 buffer[0] = '_'; 254 if (!result)
242 Py_UNICODE_strncpy(buffer+1, p, plen); 255 return 0;
243 Py_UNICODE_strcpy(buffer+1+plen, name); 256 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
244 return ident; 257 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
258 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
259 Py_DECREF(result);
260 return NULL;
261 }
262 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
263 Py_DECREF(result);
264 return NULL;
265 }
266 return result;
245 } 267 }
246 268
247 static int 269 static int
248 compiler_init(struct compiler *c) 270 compiler_init(struct compiler *c)
249 { 271 {
250 memset(c, 0, sizeof(struct compiler)); 272 memset(c, 0, sizeof(struct compiler));
251 273
252 c->c_stack = PyList_New(0); 274 c->c_stack = PyList_New(0);
253 if (!c->c_stack) 275 if (!c->c_stack)
254 return 0; 276 return 0;
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 b = u->u_blocks; 460 b = u->u_blocks;
439 while (b != NULL) { 461 while (b != NULL) {
440 if (b->b_instr) 462 if (b->b_instr)
441 PyObject_Free((void *)b->b_instr); 463 PyObject_Free((void *)b->b_instr);
442 next = b->b_list; 464 next = b->b_list;
443 PyObject_Free((void *)b); 465 PyObject_Free((void *)b);
444 b = next; 466 b = next;
445 } 467 }
446 Py_CLEAR(u->u_ste); 468 Py_CLEAR(u->u_ste);
447 Py_CLEAR(u->u_name); 469 Py_CLEAR(u->u_name);
470 Py_CLEAR(u->u_qualname);
448 Py_CLEAR(u->u_consts); 471 Py_CLEAR(u->u_consts);
449 Py_CLEAR(u->u_names); 472 Py_CLEAR(u->u_names);
450 Py_CLEAR(u->u_varnames); 473 Py_CLEAR(u->u_varnames);
451 Py_CLEAR(u->u_freevars); 474 Py_CLEAR(u->u_freevars);
452 Py_CLEAR(u->u_cellvars); 475 Py_CLEAR(u->u_cellvars);
453 Py_CLEAR(u->u_private); 476 Py_CLEAR(u->u_private);
454 PyObject_Free(u); 477 PyObject_Free(u);
455 } 478 }
456 479
457 static int 480 static int
458 compiler_enter_scope(struct compiler *c, identifier name, void *key, 481 compiler_enter_scope(struct compiler *c, identifier name,
459 int lineno) 482 int scope_type, void *key, int lineno)
460 { 483 {
461 struct compiler_unit *u; 484 struct compiler_unit *u;
462 485
463 u = (struct compiler_unit *)PyObject_Malloc(sizeof( 486 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
464 struct compiler_unit)); 487 struct compiler_unit));
465 if (!u) { 488 if (!u) {
466 PyErr_NoMemory(); 489 PyErr_NoMemory();
467 return 0; 490 return 0;
468 } 491 }
469 memset(u, 0, sizeof(struct compiler_unit)); 492 memset(u, 0, sizeof(struct compiler_unit));
493 u->u_scope_type = scope_type;
470 u->u_argcount = 0; 494 u->u_argcount = 0;
471 u->u_kwonlyargcount = 0; 495 u->u_kwonlyargcount = 0;
472 u->u_ste = PySymtable_Lookup(c->c_st, key); 496 u->u_ste = PySymtable_Lookup(c->c_st, key);
473 if (!u->u_ste) { 497 if (!u->u_ste) {
474 compiler_unit_free(u); 498 compiler_unit_free(u);
475 return 0; 499 return 0;
476 } 500 }
477 Py_INCREF(name); 501 Py_INCREF(name);
478 u->u_name = name; 502 u->u_name = name;
479 u->u_varnames = list2dict(u->u_ste->ste_varnames); 503 u->u_varnames = list2dict(u->u_ste->ste_varnames);
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
547 /* we are deleting from a list so this really shouldn't fail */ 571 /* we are deleting from a list so this really shouldn't fail */
548 if (PySequence_DelItem(c->c_stack, n) < 0) 572 if (PySequence_DelItem(c->c_stack, n) < 0)
549 Py_FatalError("compiler_exit_scope()"); 573 Py_FatalError("compiler_exit_scope()");
550 compiler_unit_check(c->u); 574 compiler_unit_check(c->u);
551 } 575 }
552 else 576 else
553 c->u = NULL; 577 c->u = NULL;
554 578
555 } 579 }
556 580
581 static PyObject *
582 compiler_scope_qualname(struct compiler *c)
583 {
584 Py_ssize_t stack_size, i;
585 _Py_static_string(dot, ".");
586 _Py_static_string(locals, "<locals>");
587 struct compiler_unit *u;
588 PyObject *capsule, *name, *seq, *dot_str, *locals_str;
589
590 u = c->u;
591 if (u->u_qualname != NULL) {
592 Py_INCREF(u->u_qualname);
593 return u->u_qualname;
594 }
595
596 seq = PyList_New(0);
597 if (seq == NULL)
598 return NULL;
599
600 stack_size = PyList_GET_SIZE(c->c_stack);
601 for (i = 0; i < stack_size; i++) {
602 capsule = PyList_GET_ITEM(c->c_stack, i);
603 u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CAPSU LE_NAME_COMPILER_UNIT);
604 assert(u);
605 if (u->u_scope_type == COMPILER_SCOPE_MODULE)
606 continue;
607 if (PyList_Append(seq, u->u_name))
608 goto _error;
609 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION) {
610 locals_str = _PyUnicode_FromId(&locals);
611 if (locals_str == NULL)
612 goto _error;
613 if (PyList_Append(seq, locals_str))
614 goto _error;
615 }
616 }
617 u = c->u;
618 if (PyList_Append(seq, u->u_name))
619 goto _error;
620 dot_str = _PyUnicode_FromId(&dot);
621 if (dot_str == NULL)
622 goto _error;
623 name = PyUnicode_Join(dot_str, seq);
624 Py_DECREF(seq);
625 u->u_qualname = name;
626 Py_XINCREF(name);
627 return name;
628
629 _error:
630 Py_XDECREF(seq);
631 return NULL;
632 }
633
557 /* Allocate a new block and return a pointer to it. 634 /* Allocate a new block and return a pointer to it.
558 Returns NULL on error. 635 Returns NULL on error.
559 */ 636 */
560 637
561 static basicblock * 638 static basicblock *
562 compiler_new_block(struct compiler *c) 639 compiler_new_block(struct compiler *c)
563 { 640 {
564 basicblock *b; 641 basicblock *b;
565 struct compiler_unit *u; 642 struct compiler_unit *u;
566 643
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
844 return -oparg; 921 return -oparg;
845 #define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256)) 922 #define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
846 case CALL_FUNCTION: 923 case CALL_FUNCTION:
847 return -NARGS(oparg); 924 return -NARGS(oparg);
848 case CALL_FUNCTION_VAR: 925 case CALL_FUNCTION_VAR:
849 case CALL_FUNCTION_KW: 926 case CALL_FUNCTION_KW:
850 return -NARGS(oparg)-1; 927 return -NARGS(oparg)-1;
851 case CALL_FUNCTION_VAR_KW: 928 case CALL_FUNCTION_VAR_KW:
852 return -NARGS(oparg)-2; 929 return -NARGS(oparg)-2;
853 case MAKE_FUNCTION: 930 case MAKE_FUNCTION:
854 return -NARGS(oparg) - ((oparg >> 16) & 0xffff); 931 return -1 -NARGS(oparg) - ((oparg >> 16) & 0xffff);
855 case MAKE_CLOSURE: 932 case MAKE_CLOSURE:
856 return -1 - NARGS(oparg) - ((oparg >> 16) & 0xffff); 933 return -2 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
857 #undef NARGS 934 #undef NARGS
858 case BUILD_SLICE: 935 case BUILD_SLICE:
859 if (oparg == 3) 936 if (oparg == 3)
860 return -2; 937 return -2;
861 else 938 else
862 return -1; 939 return -1;
863 940
864 case LOAD_CLOSURE: 941 case LOAD_CLOSURE:
865 return 1; 942 return 1;
866 case LOAD_DEREF: 943 case LOAD_DEREF:
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after
1176 { 1253 {
1177 PyCodeObject *co; 1254 PyCodeObject *co;
1178 int addNone = 1; 1255 int addNone = 1;
1179 static PyObject *module; 1256 static PyObject *module;
1180 if (!module) { 1257 if (!module) {
1181 module = PyUnicode_InternFromString("<module>"); 1258 module = PyUnicode_InternFromString("<module>");
1182 if (!module) 1259 if (!module)
1183 return NULL; 1260 return NULL;
1184 } 1261 }
1185 /* Use 0 for firstlineno initially, will fixup in assemble(). */ 1262 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1186 if (!compiler_enter_scope(c, module, mod, 0)) 1263 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0))
1187 return NULL; 1264 return NULL;
1188 switch (mod->kind) { 1265 switch (mod->kind) {
1189 case Module_kind: 1266 case Module_kind:
1190 if (!compiler_body(c, mod->v.Module.body)) { 1267 if (!compiler_body(c, mod->v.Module.body)) {
1191 compiler_exit_scope(c); 1268 compiler_exit_scope(c);
1192 return 0; 1269 return 0;
1193 } 1270 }
1194 break; 1271 break;
1195 case Interactive_kind: 1272 case Interactive_kind:
1196 c->c_interactive = 1; 1273 c->c_interactive = 1;
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
1252 if (k == NULL) 1329 if (k == NULL)
1253 return -1; 1330 return -1;
1254 v = PyDict_GetItem(dict, k); 1331 v = PyDict_GetItem(dict, k);
1255 Py_DECREF(k); 1332 Py_DECREF(k);
1256 if (v == NULL) 1333 if (v == NULL)
1257 return -1; 1334 return -1;
1258 return PyLong_AS_LONG(v); 1335 return PyLong_AS_LONG(v);
1259 } 1336 }
1260 1337
1261 static int 1338 static int
1262 compiler_make_closure(struct compiler *c, PyCodeObject *co, int args) 1339 compiler_make_closure(struct compiler *c, PyCodeObject *co, int args, PyObject * qualname)
1263 { 1340 {
1264 int i, free = PyCode_GetNumFree(co); 1341 int i, free = PyCode_GetNumFree(co);
1342 if (qualname == NULL)
1343 qualname = co->co_name;
1344
1265 if (free == 0) { 1345 if (free == 0) {
1266 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts); 1346 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1347 ADDOP_O(c, LOAD_CONST, qualname, consts);
1267 ADDOP_I(c, MAKE_FUNCTION, args); 1348 ADDOP_I(c, MAKE_FUNCTION, args);
1268 return 1; 1349 return 1;
1269 } 1350 }
1270 for (i = 0; i < free; ++i) { 1351 for (i = 0; i < free; ++i) {
1271 /* Bypass com_addop_varname because it will generate 1352 /* Bypass com_addop_varname because it will generate
1272 LOAD_DEREF but LOAD_CLOSURE is needed. 1353 LOAD_DEREF but LOAD_CLOSURE is needed.
1273 */ 1354 */
1274 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); 1355 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1275 int arg, reftype; 1356 int arg, reftype;
1276 1357
(...skipping 16 matching lines...) Expand all
1293 PyBytes_AS_STRING(c->u->u_name), 1374 PyBytes_AS_STRING(c->u->u_name),
1294 reftype, arg, 1375 reftype, arg,
1295 _PyUnicode_AsString(co->co_name), 1376 _PyUnicode_AsString(co->co_name),
1296 PyObject_REPR(co->co_freevars)); 1377 PyObject_REPR(co->co_freevars));
1297 Py_FatalError("compiler_make_closure()"); 1378 Py_FatalError("compiler_make_closure()");
1298 } 1379 }
1299 ADDOP_I(c, LOAD_CLOSURE, arg); 1380 ADDOP_I(c, LOAD_CLOSURE, arg);
1300 } 1381 }
1301 ADDOP_I(c, BUILD_TUPLE, free); 1382 ADDOP_I(c, BUILD_TUPLE, free);
1302 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts); 1383 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1384 ADDOP_O(c, LOAD_CONST, qualname, consts);
1303 ADDOP_I(c, MAKE_CLOSURE, args); 1385 ADDOP_I(c, MAKE_CLOSURE, args);
1304 return 1; 1386 return 1;
1305 } 1387 }
1306 1388
1307 static int 1389 static int
1308 compiler_decorators(struct compiler *c, asdl_seq* decos) 1390 compiler_decorators(struct compiler *c, asdl_seq* decos)
1309 { 1391 {
1310 int i; 1392 int i;
1311 1393
1312 if (!decos) 1394 if (!decos)
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
1434 1516
1435 error: 1517 error:
1436 Py_DECREF(names); 1518 Py_DECREF(names);
1437 return -1; 1519 return -1;
1438 } 1520 }
1439 1521
1440 static int 1522 static int
1441 compiler_function(struct compiler *c, stmt_ty s) 1523 compiler_function(struct compiler *c, stmt_ty s)
1442 { 1524 {
1443 PyCodeObject *co; 1525 PyCodeObject *co;
1444 PyObject *first_const = Py_None; 1526 PyObject *qualname, *first_const = Py_None;
1445 arguments_ty args = s->v.FunctionDef.args; 1527 arguments_ty args = s->v.FunctionDef.args;
1446 expr_ty returns = s->v.FunctionDef.returns; 1528 expr_ty returns = s->v.FunctionDef.returns;
1447 asdl_seq* decos = s->v.FunctionDef.decorator_list; 1529 asdl_seq* decos = s->v.FunctionDef.decorator_list;
1448 stmt_ty st; 1530 stmt_ty st;
1449 int i, n, docstring, kw_default_count = 0, arglength; 1531 int i, n, docstring, kw_default_count = 0, arglength;
1450 int num_annotations; 1532 int num_annotations;
1451 1533
1452 assert(s->kind == FunctionDef_kind); 1534 assert(s->kind == FunctionDef_kind);
1453 1535
1454 if (!compiler_decorators(c, decos)) 1536 if (!compiler_decorators(c, decos))
1455 return 0; 1537 return 0;
1456 if (args->kwonlyargs) { 1538 if (args->kwonlyargs) {
1457 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, 1539 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1458 args->kw_defaults); 1540 args->kw_defaults);
1459 if (res < 0) 1541 if (res < 0)
1460 return 0; 1542 return 0;
1461 kw_default_count = res; 1543 kw_default_count = res;
1462 } 1544 }
1463 if (args->defaults) 1545 if (args->defaults)
1464 VISIT_SEQ(c, expr, args->defaults); 1546 VISIT_SEQ(c, expr, args->defaults);
1465 num_annotations = compiler_visit_annotations(c, args, returns); 1547 num_annotations = compiler_visit_annotations(c, args, returns);
1466 if (num_annotations < 0) 1548 if (num_annotations < 0)
1467 return 0; 1549 return 0;
1468 assert((num_annotations & 0xFFFF) == num_annotations); 1550 assert((num_annotations & 0xFFFF) == num_annotations);
1469 1551
1470 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s, 1552 if (!compiler_enter_scope(c, s->v.FunctionDef.name,
1553 COMPILER_SCOPE_FUNCTION, (void *)s,
1471 s->lineno)) 1554 s->lineno))
1472 return 0; 1555 return 0;
1473 1556
1474 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0); 1557 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
1475 docstring = compiler_isdocstring(st); 1558 docstring = compiler_isdocstring(st);
1476 if (docstring && c->c_optimize < 2) 1559 if (docstring && c->c_optimize < 2)
1477 first_const = st->v.Expr.value->v.Str.s; 1560 first_const = st->v.Expr.value->v.Str.s;
1478 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) { 1561 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1479 compiler_exit_scope(c); 1562 compiler_exit_scope(c);
1480 return 0; 1563 return 0;
1481 } 1564 }
1482 1565
1483 c->u->u_argcount = asdl_seq_LEN(args->args); 1566 c->u->u_argcount = asdl_seq_LEN(args->args);
1484 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); 1567 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1485 n = asdl_seq_LEN(s->v.FunctionDef.body); 1568 n = asdl_seq_LEN(s->v.FunctionDef.body);
1486 /* if there was a docstring, we need to skip the first statement */ 1569 /* if there was a docstring, we need to skip the first statement */
1487 for (i = docstring; i < n; i++) { 1570 for (i = docstring; i < n; i++) {
1488 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i); 1571 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1489 VISIT_IN_SCOPE(c, stmt, st); 1572 VISIT_IN_SCOPE(c, stmt, st);
1490 } 1573 }
1491 co = assemble(c, 1); 1574 co = assemble(c, 1);
1575 qualname = compiler_scope_qualname(c);
1492 compiler_exit_scope(c); 1576 compiler_exit_scope(c);
1493 if (co == NULL) 1577 if (qualname == NULL || co == NULL) {
1494 return 0; 1578 Py_XDECREF(qualname);
1579 Py_XDECREF(co);
1580 return 0;
1581 }
1495 1582
1496 arglength = asdl_seq_LEN(args->defaults); 1583 arglength = asdl_seq_LEN(args->defaults);
1497 arglength |= kw_default_count << 8; 1584 arglength |= kw_default_count << 8;
1498 arglength |= num_annotations << 16; 1585 arglength |= num_annotations << 16;
1499 compiler_make_closure(c, co, arglength); 1586 compiler_make_closure(c, co, arglength, qualname);
1587 Py_DECREF(qualname);
1500 Py_DECREF(co); 1588 Py_DECREF(co);
1501 1589
1502 /* decorators */ 1590 /* decorators */
1503 for (i = 0; i < asdl_seq_LEN(decos); i++) { 1591 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1504 ADDOP_I(c, CALL_FUNCTION, 1); 1592 ADDOP_I(c, CALL_FUNCTION, 1);
1505 } 1593 }
1506 1594
1507 return compiler_nameop(c, s->v.FunctionDef.name, Store); 1595 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1508 } 1596 }
1509 1597
(...skipping 14 matching lines...) Expand all
1524 <func> is a function/closure created from the class body; 1612 <func> is a function/closure created from the class body;
1525 it has a single argument (__locals__) where the dict 1613 it has a single argument (__locals__) where the dict
1526 (or MutableSequence) representing the locals is passed 1614 (or MutableSequence) representing the locals is passed
1527 <name> is the class name 1615 <name> is the class name
1528 <bases> is the positional arguments and *varargs argument 1616 <bases> is the positional arguments and *varargs argument
1529 <keywords> is the keyword arguments and **kwds argument 1617 <keywords> is the keyword arguments and **kwds argument
1530 This borrows from compiler_call. 1618 This borrows from compiler_call.
1531 */ 1619 */
1532 1620
1533 /* 1. compile the class body into a code object */ 1621 /* 1. compile the class body into a code object */
1534 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, s->lineno)) 1622 if (!compiler_enter_scope(c, s->v.ClassDef.name,
1623 COMPILER_SCOPE_CLASS, (void *)s, s->lineno))
1535 return 0; 1624 return 0;
1536 /* this block represents what we do in the new scope */ 1625 /* this block represents what we do in the new scope */
1537 { 1626 {
1538 /* use the class name for name mangling */ 1627 /* use the class name for name mangling */
1539 Py_INCREF(s->v.ClassDef.name); 1628 Py_INCREF(s->v.ClassDef.name);
1540 Py_XDECREF(c->u->u_private); 1629 Py_XDECREF(c->u->u_private);
1541 c->u->u_private = s->v.ClassDef.name; 1630 c->u->u_private = s->v.ClassDef.name;
1542 /* force it to have one mandatory argument */ 1631 /* force it to have one mandatory argument */
1543 c->u->u_argcount = 1; 1632 c->u->u_argcount = 1;
1544 /* load the first argument (__locals__) ... */ 1633 /* load the first argument (__locals__) ... */
1545 ADDOP_I(c, LOAD_FAST, 0); 1634 ADDOP_I(c, LOAD_FAST, 0);
1546 /* ... and store it into f_locals */ 1635 /* ... and store it into f_locals */
1547 ADDOP_IN_SCOPE(c, STORE_LOCALS); 1636 ADDOP_IN_SCOPE(c, STORE_LOCALS);
1548 /* load (global) __name__ ... */ 1637 /* load (global) __name__ ... */
1549 str = PyUnicode_InternFromString("__name__"); 1638 str = PyUnicode_InternFromString("__name__");
1550 if (!str || !compiler_nameop(c, str, Load)) { 1639 if (!str || !compiler_nameop(c, str, Load)) {
1551 Py_XDECREF(str); 1640 Py_XDECREF(str);
1552 compiler_exit_scope(c); 1641 compiler_exit_scope(c);
1553 return 0; 1642 return 0;
1554 } 1643 }
1555 Py_DECREF(str); 1644 Py_DECREF(str);
1556 /* ... and store it as __module__ */ 1645 /* ... and store it as __module__ */
1557 str = PyUnicode_InternFromString("__module__"); 1646 str = PyUnicode_InternFromString("__module__");
1558 if (!str || !compiler_nameop(c, str, Store)) { 1647 if (!str || !compiler_nameop(c, str, Store)) {
1559 Py_XDECREF(str); 1648 Py_XDECREF(str);
1560 compiler_exit_scope(c); 1649 compiler_exit_scope(c);
1561 return 0; 1650 return 0;
1562 } 1651 }
1563 Py_DECREF(str); 1652 Py_DECREF(str);
1653 /* store the __qualname__ */
1654 str = compiler_scope_qualname(c);
1655 if (!str) {
1656 compiler_exit_scope(c);
1657 return 0;
1658 }
1659 ADDOP_O(c, LOAD_CONST, str, consts);
1660 Py_DECREF(str);
1661 str = PyUnicode_InternFromString("__qualname__");
1662 if (!str || !compiler_nameop(c, str, Store)) {
1663 Py_XDECREF(str);
1664 compiler_exit_scope(c);
1665 return 0;
1666 }
1667 Py_DECREF(str);
1564 /* compile the body proper */ 1668 /* compile the body proper */
1565 if (!compiler_body(c, s->v.ClassDef.body)) { 1669 if (!compiler_body(c, s->v.ClassDef.body)) {
1566 compiler_exit_scope(c); 1670 compiler_exit_scope(c);
1567 return 0; 1671 return 0;
1568 } 1672 }
1569 /* return the (empty) __class__ cell */ 1673 /* return the (empty) __class__ cell */
1570 str = PyUnicode_InternFromString("@__class__"); 1674 str = PyUnicode_InternFromString("@__class__");
1571 if (str == NULL) { 1675 if (str == NULL) {
1572 compiler_exit_scope(c); 1676 compiler_exit_scope(c);
1573 return 0; 1677 return 0;
(...skipping 16 matching lines...) Expand all
1590 } 1694 }
1591 /* leave the new scope */ 1695 /* leave the new scope */
1592 compiler_exit_scope(c); 1696 compiler_exit_scope(c);
1593 if (co == NULL) 1697 if (co == NULL)
1594 return 0; 1698 return 0;
1595 1699
1596 /* 2. load the 'build_class' function */ 1700 /* 2. load the 'build_class' function */
1597 ADDOP(c, LOAD_BUILD_CLASS); 1701 ADDOP(c, LOAD_BUILD_CLASS);
1598 1702
1599 /* 3. load a function (or closure) made from the code object */ 1703 /* 3. load a function (or closure) made from the code object */
1600 compiler_make_closure(c, co, 0); 1704 compiler_make_closure(c, co, 0, NULL);
1601 Py_DECREF(co); 1705 Py_DECREF(co);
1602 1706
1603 /* 4. load class name */ 1707 /* 4. load class name */
1604 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts); 1708 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1605 1709
1606 /* 5. generate the rest of the code for the call */ 1710 /* 5. generate the rest of the code for the call */
1607 if (!compiler_call_helper(c, 2, 1711 if (!compiler_call_helper(c, 2,
1608 s->v.ClassDef.bases, 1712 s->v.ClassDef.bases,
1609 s->v.ClassDef.keywords, 1713 s->v.ClassDef.keywords,
1610 s->v.ClassDef.starargs, 1714 s->v.ClassDef.starargs,
(...skipping 30 matching lines...) Expand all
1641 compiler_use_next_block(c, next); 1745 compiler_use_next_block(c, next);
1642 VISIT(c, expr, e->v.IfExp.orelse); 1746 VISIT(c, expr, e->v.IfExp.orelse);
1643 compiler_use_next_block(c, end); 1747 compiler_use_next_block(c, end);
1644 return 1; 1748 return 1;
1645 } 1749 }
1646 1750
1647 static int 1751 static int
1648 compiler_lambda(struct compiler *c, expr_ty e) 1752 compiler_lambda(struct compiler *c, expr_ty e)
1649 { 1753 {
1650 PyCodeObject *co; 1754 PyCodeObject *co;
1755 PyObject *qualname;
1651 static identifier name; 1756 static identifier name;
1652 int kw_default_count = 0, arglength; 1757 int kw_default_count = 0, arglength;
1653 arguments_ty args = e->v.Lambda.args; 1758 arguments_ty args = e->v.Lambda.args;
1654 assert(e->kind == Lambda_kind); 1759 assert(e->kind == Lambda_kind);
1655 1760
1656 if (!name) { 1761 if (!name) {
1657 name = PyUnicode_InternFromString("<lambda>"); 1762 name = PyUnicode_InternFromString("<lambda>");
1658 if (!name) 1763 if (!name)
1659 return 0; 1764 return 0;
1660 } 1765 }
1661 1766
1662 if (args->kwonlyargs) { 1767 if (args->kwonlyargs) {
1663 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, 1768 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1664 args->kw_defaults); 1769 args->kw_defaults);
1665 if (res < 0) return 0; 1770 if (res < 0) return 0;
1666 kw_default_count = res; 1771 kw_default_count = res;
1667 } 1772 }
1668 if (args->defaults) 1773 if (args->defaults)
1669 VISIT_SEQ(c, expr, args->defaults); 1774 VISIT_SEQ(c, expr, args->defaults);
1670 if (!compiler_enter_scope(c, name, (void *)e, e->lineno)) 1775 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_FUNCTION,
1776 (void *)e, e->lineno))
1671 return 0; 1777 return 0;
1672 1778
1673 /* Make None the first constant, so the lambda can't have a 1779 /* Make None the first constant, so the lambda can't have a
1674 docstring. */ 1780 docstring. */
1675 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0) 1781 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)
1676 return 0; 1782 return 0;
1677 1783
1678 c->u->u_argcount = asdl_seq_LEN(args->args); 1784 c->u->u_argcount = asdl_seq_LEN(args->args);
1679 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); 1785 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1680 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); 1786 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1681 if (c->u->u_ste->ste_generator) { 1787 if (c->u->u_ste->ste_generator) {
1682 ADDOP_IN_SCOPE(c, POP_TOP); 1788 ADDOP_IN_SCOPE(c, POP_TOP);
1683 } 1789 }
1684 else { 1790 else {
1685 ADDOP_IN_SCOPE(c, RETURN_VALUE); 1791 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1686 } 1792 }
1687 co = assemble(c, 1); 1793 co = assemble(c, 1);
1794 qualname = compiler_scope_qualname(c);
1688 compiler_exit_scope(c); 1795 compiler_exit_scope(c);
1689 if (co == NULL) 1796 if (qualname == NULL || co == NULL)
1690 return 0; 1797 return 0;
1691 1798
1692 arglength = asdl_seq_LEN(args->defaults); 1799 arglength = asdl_seq_LEN(args->defaults);
1693 arglength |= kw_default_count << 8; 1800 arglength |= kw_default_count << 8;
1694 compiler_make_closure(c, co, arglength); 1801 compiler_make_closure(c, co, arglength, qualname);
1802 Py_DECREF(qualname);
1695 Py_DECREF(co); 1803 Py_DECREF(co);
1696 1804
1697 return 1; 1805 return 1;
1698 } 1806 }
1699 1807
1700 static int 1808 static int
1701 compiler_if(struct compiler *c, stmt_ty s) 1809 compiler_if(struct compiler *c, stmt_ty s)
1702 { 1810 {
1703 basicblock *end, *next; 1811 basicblock *end, *next;
1704 int constant; 1812 int constant;
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after
2079 2187
2080 static int 2188 static int
2081 compiler_import_as(struct compiler *c, identifier name, identifier asname) 2189 compiler_import_as(struct compiler *c, identifier name, identifier asname)
2082 { 2190 {
2083 /* The IMPORT_NAME opcode was already generated. This function 2191 /* The IMPORT_NAME opcode was already generated. This function
2084 merely needs to bind the result to a name. 2192 merely needs to bind the result to a name.
2085 2193
2086 If there is a dot in name, we need to split it and emit a 2194 If there is a dot in name, we need to split it and emit a
2087 LOAD_ATTR for each name. 2195 LOAD_ATTR for each name.
2088 */ 2196 */
2089 const Py_UNICODE *src = PyUnicode_AS_UNICODE(name); 2197 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
2090 const Py_UNICODE *dot = Py_UNICODE_strchr(src, '.'); 2198 PyUnicode_GET_LENGTH(name), 1);
2091 if (dot) { 2199 if (dot == -2)
2200 return -1;
2201 if (dot != -1) {
2092 /* Consume the base module name to get the first attribute */ 2202 /* Consume the base module name to get the first attribute */
2093 src = dot + 1; 2203 Py_ssize_t pos = dot + 1;
2094 while (dot) { 2204 while (dot != -1) {
2095 /* NB src is only defined when dot != NULL */
2096 PyObject *attr; 2205 PyObject *attr;
2097 dot = Py_UNICODE_strchr(src, '.'); 2206 dot = PyUnicode_FindChar(name, '.', pos,
2098 attr = PyUnicode_FromUnicode(src, 2207 PyUnicode_GET_LENGTH(name), 1);
2099 dot ? dot - src : Py_UNICODE_strlen(src)); 2208 if (dot == -2)
2209 return -1;
2210 attr = PyUnicode_Substring(name, pos,
2211 (dot != -1) ? dot :
2212 PyUnicode_GET_LENGTH(name));
2100 if (!attr) 2213 if (!attr)
2101 return -1; 2214 return -1;
2102 ADDOP_O(c, LOAD_ATTR, attr, names); 2215 ADDOP_O(c, LOAD_ATTR, attr, names);
2103 Py_DECREF(attr); 2216 Py_DECREF(attr);
2104 src = dot + 1; 2217 pos = dot + 1;
2105 } 2218 }
2106 } 2219 }
2107 return compiler_nameop(c, asname, Store); 2220 return compiler_nameop(c, asname, Store);
2108 } 2221 }
2109 2222
2110 static int 2223 static int
2111 compiler_import(struct compiler *c, stmt_ty s) 2224 compiler_import(struct compiler *c, stmt_ty s)
2112 { 2225 {
2113 /* The Import node stores a module name like a.b.c as a single 2226 /* The Import node stores a module name like a.b.c as a single
2114 string. This is convenient for all cases except 2227 string. This is convenient for all cases except
(...skipping 18 matching lines...) Expand all
2133 ADDOP_O(c, LOAD_CONST, Py_None, consts); 2246 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2134 ADDOP_NAME(c, IMPORT_NAME, alias->name, names); 2247 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2135 2248
2136 if (alias->asname) { 2249 if (alias->asname) {
2137 r = compiler_import_as(c, alias->name, alias->asname); 2250 r = compiler_import_as(c, alias->name, alias->asname);
2138 if (!r) 2251 if (!r)
2139 return r; 2252 return r;
2140 } 2253 }
2141 else { 2254 else {
2142 identifier tmp = alias->name; 2255 identifier tmp = alias->name;
2143 const Py_UNICODE *base = PyUnicode_AS_UNICODE(alias->name); 2256 Py_ssize_t dot = PyUnicode_FindChar(
2144 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.'); 2257 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
2145 if (dot) 2258 if (dot != -1)
2146 tmp = PyUnicode_FromUnicode(base, 2259 tmp = PyUnicode_Substring(alias->name, 0, dot);
2147 dot - base);
2148 r = compiler_nameop(c, tmp, Store); 2260 r = compiler_nameop(c, tmp, Store);
2149 if (dot) { 2261 if (dot != -1) {
2150 Py_DECREF(tmp); 2262 Py_DECREF(tmp);
2151 } 2263 }
2152 if (!r) 2264 if (!r)
2153 return r; 2265 return r;
2154 } 2266 }
2155 } 2267 }
2156 return 1; 2268 return 1;
2157 } 2269 }
2158 2270
2159 static int 2271 static int
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
2202 if (s->v.ImportFrom.module) { 2314 if (s->v.ImportFrom.module) {
2203 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); 2315 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2204 } 2316 }
2205 else { 2317 else {
2206 ADDOP_NAME(c, IMPORT_NAME, empty_string, names); 2318 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
2207 } 2319 }
2208 for (i = 0; i < n; i++) { 2320 for (i = 0; i < n; i++) {
2209 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); 2321 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2210 identifier store_name; 2322 identifier store_name;
2211 2323
2212 if (i == 0 && *PyUnicode_AS_UNICODE(alias->name) == '*') { 2324 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
2213 assert(n == 1); 2325 assert(n == 1);
2214 ADDOP(c, IMPORT_STAR); 2326 ADDOP(c, IMPORT_STAR);
2215 return 1; 2327 return 1;
2216 } 2328 }
2217 2329
2218 ADDOP_NAME(c, IMPORT_FROM, alias->name, names); 2330 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2219 store_name = alias->name; 2331 store_name = alias->name;
2220 if (alias->asname) 2332 if (alias->asname)
2221 store_name = alias->asname; 2333 store_name = alias->asname;
2222 2334
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after
2516 break; 2628 break;
2517 case GLOBAL_EXPLICIT: 2629 case GLOBAL_EXPLICIT:
2518 optype = OP_GLOBAL; 2630 optype = OP_GLOBAL;
2519 break; 2631 break;
2520 default: 2632 default:
2521 /* scope can be 0 */ 2633 /* scope can be 0 */
2522 break; 2634 break;
2523 } 2635 }
2524 2636
2525 /* XXX Leave assert here, but handle __doc__ and the like better */ 2637 /* XXX Leave assert here, but handle __doc__ and the like better */
2526 assert(scope || PyUnicode_AS_UNICODE(name)[0] == '_'); 2638 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
2527 2639
2528 switch (optype) { 2640 switch (optype) {
2529 case OP_DEREF: 2641 case OP_DEREF:
2530 switch (ctx) { 2642 switch (ctx) {
2531 case Load: op = LOAD_DEREF; break; 2643 case Load: op = LOAD_DEREF; break;
2532 case Store: op = STORE_DEREF; break; 2644 case Store: op = STORE_DEREF; break;
2533 case AugLoad: 2645 case AugLoad:
2534 case AugStore: 2646 case AugStore:
2535 break; 2647 break;
2536 case Del: op = DELETE_DEREF; break; 2648 case Del: op = DELETE_DEREF; break;
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after
2894 3006
2895 return 1; 3007 return 1;
2896 } 3008 }
2897 3009
2898 static int 3010 static int
2899 compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name, 3011 compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
2900 asdl_seq *generators, expr_ty elt, expr_ty val) 3012 asdl_seq *generators, expr_ty elt, expr_ty val)
2901 { 3013 {
2902 PyCodeObject *co = NULL; 3014 PyCodeObject *co = NULL;
2903 expr_ty outermost_iter; 3015 expr_ty outermost_iter;
3016 PyObject *qualname = NULL;
2904 3017
2905 outermost_iter = ((comprehension_ty) 3018 outermost_iter = ((comprehension_ty)
2906 asdl_seq_GET(generators, 0))->iter; 3019 asdl_seq_GET(generators, 0))->iter;
2907 3020
2908 if (!compiler_enter_scope(c, name, (void *)e, e->lineno)) 3021 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
3022 (void *)e, e->lineno))
2909 goto error; 3023 goto error;
2910 3024
2911 if (type != COMP_GENEXP) { 3025 if (type != COMP_GENEXP) {
2912 int op; 3026 int op;
2913 switch (type) { 3027 switch (type) {
2914 case COMP_LISTCOMP: 3028 case COMP_LISTCOMP:
2915 op = BUILD_LIST; 3029 op = BUILD_LIST;
2916 break; 3030 break;
2917 case COMP_SETCOMP: 3031 case COMP_SETCOMP:
2918 op = BUILD_SET; 3032 op = BUILD_SET;
(...skipping 12 matching lines...) Expand all
2931 3045
2932 if (!compiler_comprehension_generator(c, generators, 0, elt, 3046 if (!compiler_comprehension_generator(c, generators, 0, elt,
2933 val, type)) 3047 val, type))
2934 goto error_in_scope; 3048 goto error_in_scope;
2935 3049
2936 if (type != COMP_GENEXP) { 3050 if (type != COMP_GENEXP) {
2937 ADDOP(c, RETURN_VALUE); 3051 ADDOP(c, RETURN_VALUE);
2938 } 3052 }
2939 3053
2940 co = assemble(c, 1); 3054 co = assemble(c, 1);
3055 qualname = compiler_scope_qualname(c);
2941 compiler_exit_scope(c); 3056 compiler_exit_scope(c);
2942 if (co == NULL) 3057 if (qualname == NULL || co == NULL)
2943 goto error; 3058 goto error;
2944 3059
2945 if (!compiler_make_closure(c, co, 0)) 3060 if (!compiler_make_closure(c, co, 0, qualname))
2946 goto error; 3061 goto error;
3062 Py_DECREF(qualname);
2947 Py_DECREF(co); 3063 Py_DECREF(co);
2948 3064
2949 VISIT(c, expr, outermost_iter); 3065 VISIT(c, expr, outermost_iter);
2950 ADDOP(c, GET_ITER); 3066 ADDOP(c, GET_ITER);
2951 ADDOP_I(c, CALL_FUNCTION, 1); 3067 ADDOP_I(c, CALL_FUNCTION, 1);
2952 return 1; 3068 return 1;
2953 error_in_scope: 3069 error_in_scope:
2954 compiler_exit_scope(c); 3070 compiler_exit_scope(c);
2955 error: 3071 error:
3072 Py_XDECREF(qualname);
2956 Py_XDECREF(co); 3073 Py_XDECREF(co);
2957 return 0; 3074 return 0;
2958 } 3075 }
2959 3076
2960 static int 3077 static int
2961 compiler_genexp(struct compiler *c, expr_ty e) 3078 compiler_genexp(struct compiler *c, expr_ty e)
2962 { 3079 {
2963 static identifier name; 3080 static identifier name;
2964 if (!name) { 3081 if (!name) {
2965 name = PyUnicode_FromString("<genexpr>"); 3082 name = PyUnicode_FromString("<genexpr>");
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
3039 char *id; 3156 char *id;
3040 switch (e->kind) { 3157 switch (e->kind) {
3041 case Ellipsis_kind: 3158 case Ellipsis_kind:
3042 return 1; 3159 return 1;
3043 case Num_kind: 3160 case Num_kind:
3044 return PyObject_IsTrue(e->v.Num.n); 3161 return PyObject_IsTrue(e->v.Num.n);
3045 case Str_kind: 3162 case Str_kind:
3046 return PyObject_IsTrue(e->v.Str.s); 3163 return PyObject_IsTrue(e->v.Str.s);
3047 case Name_kind: 3164 case Name_kind:
3048 /* optimize away names that can't be reassigned */ 3165 /* optimize away names that can't be reassigned */
3049 id = PyBytes_AS_STRING( 3166 id = PyUnicode_AsUTF8(e->v.Name.id);
3050 _PyUnicode_AsDefaultEncodedString(e->v.Name.id));
3051 if (strcmp(id, "True") == 0) return 1; 3167 if (strcmp(id, "True") == 0) return 1;
3052 if (strcmp(id, "False") == 0) return 0; 3168 if (strcmp(id, "False") == 0) return 0;
3053 if (strcmp(id, "None") == 0) return 0; 3169 if (strcmp(id, "None") == 0) return 0;
3054 if (strcmp(id, "__debug__") == 0) 3170 if (strcmp(id, "__debug__") == 0)
3055 return ! c->c_optimize; 3171 return ! c->c_optimize;
3056 /* fall through */ 3172 /* fall through */
3057 default: 3173 default:
3058 return -1; 3174 return -1;
3059 } 3175 }
3060 } 3176 }
(...skipping 1040 matching lines...) Expand 10 before | Expand all | Expand 10 after
4101 4217
4102 #undef PyAST_Compile 4218 #undef PyAST_Compile
4103 PyAPI_FUNC(PyCodeObject *) 4219 PyAPI_FUNC(PyCodeObject *)
4104 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, 4220 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4105 PyArena *arena) 4221 PyArena *arena)
4106 { 4222 {
4107 return PyAST_CompileEx(mod, filename, flags, -1, arena); 4223 return PyAST_CompileEx(mod, filename, flags, -1, arena);
4108 } 4224 }
4109 4225
4110 4226
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+