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

Delta Between Two Patch Sets: Python/compile.c

Issue 11682: PEP 380 reference implementation for 3.3
Left Patch Set: Created 7 years, 7 months ago
Right Patch Set: Created 7 years, 6 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 287 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 b = u->u_blocks; 460 b = u->u_blocks;
451 while (b != NULL) { 461 while (b != NULL) {
452 if (b->b_instr) 462 if (b->b_instr)
453 PyObject_Free((void *)b->b_instr); 463 PyObject_Free((void *)b->b_instr);
454 next = b->b_list; 464 next = b->b_list;
455 PyObject_Free((void *)b); 465 PyObject_Free((void *)b);
456 b = next; 466 b = next;
457 } 467 }
458 Py_CLEAR(u->u_ste); 468 Py_CLEAR(u->u_ste);
459 Py_CLEAR(u->u_name); 469 Py_CLEAR(u->u_name);
470 Py_CLEAR(u->u_qualname);
460 Py_CLEAR(u->u_consts); 471 Py_CLEAR(u->u_consts);
461 Py_CLEAR(u->u_names); 472 Py_CLEAR(u->u_names);
462 Py_CLEAR(u->u_varnames); 473 Py_CLEAR(u->u_varnames);
463 Py_CLEAR(u->u_freevars); 474 Py_CLEAR(u->u_freevars);
464 Py_CLEAR(u->u_cellvars); 475 Py_CLEAR(u->u_cellvars);
465 Py_CLEAR(u->u_private); 476 Py_CLEAR(u->u_private);
466 PyObject_Free(u); 477 PyObject_Free(u);
467 } 478 }
468 479
469 static int 480 static int
470 compiler_enter_scope(struct compiler *c, identifier name, void *key, 481 compiler_enter_scope(struct compiler *c, identifier name,
471 int lineno) 482 int scope_type, void *key, int lineno)
472 { 483 {
473 struct compiler_unit *u; 484 struct compiler_unit *u;
474 485
475 u = (struct compiler_unit *)PyObject_Malloc(sizeof( 486 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
476 struct compiler_unit)); 487 struct compiler_unit));
477 if (!u) { 488 if (!u) {
478 PyErr_NoMemory(); 489 PyErr_NoMemory();
479 return 0; 490 return 0;
480 } 491 }
481 memset(u, 0, sizeof(struct compiler_unit)); 492 memset(u, 0, sizeof(struct compiler_unit));
493 u->u_scope_type = scope_type;
482 u->u_argcount = 0; 494 u->u_argcount = 0;
483 u->u_kwonlyargcount = 0; 495 u->u_kwonlyargcount = 0;
484 u->u_ste = PySymtable_Lookup(c->c_st, key); 496 u->u_ste = PySymtable_Lookup(c->c_st, key);
485 if (!u->u_ste) { 497 if (!u->u_ste) {
486 compiler_unit_free(u); 498 compiler_unit_free(u);
487 return 0; 499 return 0;
488 } 500 }
489 Py_INCREF(name); 501 Py_INCREF(name);
490 u->u_name = name; 502 u->u_name = name;
491 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
559 /* 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 */
560 if (PySequence_DelItem(c->c_stack, n) < 0) 572 if (PySequence_DelItem(c->c_stack, n) < 0)
561 Py_FatalError("compiler_exit_scope()"); 573 Py_FatalError("compiler_exit_scope()");
562 compiler_unit_check(c->u); 574 compiler_unit_check(c->u);
563 } 575 }
564 else 576 else
565 c->u = NULL; 577 c->u = NULL;
566 578
567 } 579 }
568 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
569 /* Allocate a new block and return a pointer to it. 634 /* Allocate a new block and return a pointer to it.
570 Returns NULL on error. 635 Returns NULL on error.
571 */ 636 */
572 637
573 static basicblock * 638 static basicblock *
574 compiler_new_block(struct compiler *c) 639 compiler_new_block(struct compiler *c)
575 { 640 {
576 basicblock *b; 641 basicblock *b;
577 struct compiler_unit *u; 642 struct compiler_unit *u;
578 643
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
856 return -oparg; 921 return -oparg;
857 #define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256)) 922 #define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
858 case CALL_FUNCTION: 923 case CALL_FUNCTION:
859 return -NARGS(oparg); 924 return -NARGS(oparg);
860 case CALL_FUNCTION_VAR: 925 case CALL_FUNCTION_VAR:
861 case CALL_FUNCTION_KW: 926 case CALL_FUNCTION_KW:
862 return -NARGS(oparg)-1; 927 return -NARGS(oparg)-1;
863 case CALL_FUNCTION_VAR_KW: 928 case CALL_FUNCTION_VAR_KW:
864 return -NARGS(oparg)-2; 929 return -NARGS(oparg)-2;
865 case MAKE_FUNCTION: 930 case MAKE_FUNCTION:
866 return -NARGS(oparg) - ((oparg >> 16) & 0xffff); 931 return -1 -NARGS(oparg) - ((oparg >> 16) & 0xffff);
867 case MAKE_CLOSURE: 932 case MAKE_CLOSURE:
868 return -1 - NARGS(oparg) - ((oparg >> 16) & 0xffff); 933 return -2 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
869 #undef NARGS 934 #undef NARGS
870 case BUILD_SLICE: 935 case BUILD_SLICE:
871 if (oparg == 3) 936 if (oparg == 3)
872 return -2; 937 return -2;
873 else 938 else
874 return -1; 939 return -1;
875 940
876 case LOAD_CLOSURE: 941 case LOAD_CLOSURE:
877 return 1; 942 return 1;
878 case LOAD_DEREF: 943 case LOAD_DEREF:
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after
1188 { 1253 {
1189 PyCodeObject *co; 1254 PyCodeObject *co;
1190 int addNone = 1; 1255 int addNone = 1;
1191 static PyObject *module; 1256 static PyObject *module;
1192 if (!module) { 1257 if (!module) {
1193 module = PyUnicode_InternFromString("<module>"); 1258 module = PyUnicode_InternFromString("<module>");
1194 if (!module) 1259 if (!module)
1195 return NULL; 1260 return NULL;
1196 } 1261 }
1197 /* Use 0 for firstlineno initially, will fixup in assemble(). */ 1262 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1198 if (!compiler_enter_scope(c, module, mod, 0)) 1263 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0))
1199 return NULL; 1264 return NULL;
1200 switch (mod->kind) { 1265 switch (mod->kind) {
1201 case Module_kind: 1266 case Module_kind:
1202 if (!compiler_body(c, mod->v.Module.body)) { 1267 if (!compiler_body(c, mod->v.Module.body)) {
1203 compiler_exit_scope(c); 1268 compiler_exit_scope(c);
1204 return 0; 1269 return 0;
1205 } 1270 }
1206 break; 1271 break;
1207 case Interactive_kind: 1272 case Interactive_kind:
1208 c->c_interactive = 1; 1273 c->c_interactive = 1;
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
1264 if (k == NULL) 1329 if (k == NULL)
1265 return -1; 1330 return -1;
1266 v = PyDict_GetItem(dict, k); 1331 v = PyDict_GetItem(dict, k);
1267 Py_DECREF(k); 1332 Py_DECREF(k);
1268 if (v == NULL) 1333 if (v == NULL)
1269 return -1; 1334 return -1;
1270 return PyLong_AS_LONG(v); 1335 return PyLong_AS_LONG(v);
1271 } 1336 }
1272 1337
1273 static int 1338 static int
1274 compiler_make_closure(struct compiler *c, PyCodeObject *co, int args) 1339 compiler_make_closure(struct compiler *c, PyCodeObject *co, int args, PyObject * qualname)
1275 { 1340 {
1276 int i, free = PyCode_GetNumFree(co); 1341 int i, free = PyCode_GetNumFree(co);
1342 if (qualname == NULL)
1343 qualname = co->co_name;
1344
1277 if (free == 0) { 1345 if (free == 0) {
1278 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);
1279 ADDOP_I(c, MAKE_FUNCTION, args); 1348 ADDOP_I(c, MAKE_FUNCTION, args);
1280 return 1; 1349 return 1;
1281 } 1350 }
1282 for (i = 0; i < free; ++i) { 1351 for (i = 0; i < free; ++i) {
1283 /* Bypass com_addop_varname because it will generate 1352 /* Bypass com_addop_varname because it will generate
1284 LOAD_DEREF but LOAD_CLOSURE is needed. 1353 LOAD_DEREF but LOAD_CLOSURE is needed.
1285 */ 1354 */
1286 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); 1355 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1287 int arg, reftype; 1356 int arg, reftype;
1288 1357
(...skipping 16 matching lines...) Expand all
1305 PyBytes_AS_STRING(c->u->u_name), 1374 PyBytes_AS_STRING(c->u->u_name),
1306 reftype, arg, 1375 reftype, arg,
1307 _PyUnicode_AsString(co->co_name), 1376 _PyUnicode_AsString(co->co_name),
1308 PyObject_REPR(co->co_freevars)); 1377 PyObject_REPR(co->co_freevars));
1309 Py_FatalError("compiler_make_closure()"); 1378 Py_FatalError("compiler_make_closure()");
1310 } 1379 }
1311 ADDOP_I(c, LOAD_CLOSURE, arg); 1380 ADDOP_I(c, LOAD_CLOSURE, arg);
1312 } 1381 }
1313 ADDOP_I(c, BUILD_TUPLE, free); 1382 ADDOP_I(c, BUILD_TUPLE, free);
1314 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);
1315 ADDOP_I(c, MAKE_CLOSURE, args); 1385 ADDOP_I(c, MAKE_CLOSURE, args);
1316 return 1; 1386 return 1;
1317 } 1387 }
1318 1388
1319 static int 1389 static int
1320 compiler_decorators(struct compiler *c, asdl_seq* decos) 1390 compiler_decorators(struct compiler *c, asdl_seq* decos)
1321 { 1391 {
1322 int i; 1392 int i;
1323 1393
1324 if (!decos) 1394 if (!decos)
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
1446 1516
1447 error: 1517 error:
1448 Py_DECREF(names); 1518 Py_DECREF(names);
1449 return -1; 1519 return -1;
1450 } 1520 }
1451 1521
1452 static int 1522 static int
1453 compiler_function(struct compiler *c, stmt_ty s) 1523 compiler_function(struct compiler *c, stmt_ty s)
1454 { 1524 {
1455 PyCodeObject *co; 1525 PyCodeObject *co;
1456 PyObject *first_const = Py_None; 1526 PyObject *qualname, *first_const = Py_None;
1457 arguments_ty args = s->v.FunctionDef.args; 1527 arguments_ty args = s->v.FunctionDef.args;
1458 expr_ty returns = s->v.FunctionDef.returns; 1528 expr_ty returns = s->v.FunctionDef.returns;
1459 asdl_seq* decos = s->v.FunctionDef.decorator_list; 1529 asdl_seq* decos = s->v.FunctionDef.decorator_list;
1460 stmt_ty st; 1530 stmt_ty st;
1461 int i, n, docstring, kw_default_count = 0, arglength; 1531 int i, n, docstring, kw_default_count = 0, arglength;
1462 int num_annotations; 1532 int num_annotations;
1463 1533
1464 assert(s->kind == FunctionDef_kind); 1534 assert(s->kind == FunctionDef_kind);
1465 1535
1466 if (!compiler_decorators(c, decos)) 1536 if (!compiler_decorators(c, decos))
1467 return 0; 1537 return 0;
1468 if (args->kwonlyargs) { 1538 if (args->kwonlyargs) {
1469 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, 1539 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1470 args->kw_defaults); 1540 args->kw_defaults);
1471 if (res < 0) 1541 if (res < 0)
1472 return 0; 1542 return 0;
1473 kw_default_count = res; 1543 kw_default_count = res;
1474 } 1544 }
1475 if (args->defaults) 1545 if (args->defaults)
1476 VISIT_SEQ(c, expr, args->defaults); 1546 VISIT_SEQ(c, expr, args->defaults);
1477 num_annotations = compiler_visit_annotations(c, args, returns); 1547 num_annotations = compiler_visit_annotations(c, args, returns);
1478 if (num_annotations < 0) 1548 if (num_annotations < 0)
1479 return 0; 1549 return 0;
1480 assert((num_annotations & 0xFFFF) == num_annotations); 1550 assert((num_annotations & 0xFFFF) == num_annotations);
1481 1551
1482 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,
1483 s->lineno)) 1554 s->lineno))
1484 return 0; 1555 return 0;
1485 1556
1486 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0); 1557 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
1487 docstring = compiler_isdocstring(st); 1558 docstring = compiler_isdocstring(st);
1488 if (docstring && c->c_optimize < 2) 1559 if (docstring && c->c_optimize < 2)
1489 first_const = st->v.Expr.value->v.Str.s; 1560 first_const = st->v.Expr.value->v.Str.s;
1490 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) {
1491 compiler_exit_scope(c); 1562 compiler_exit_scope(c);
1492 return 0; 1563 return 0;
1493 } 1564 }
1494 1565
1495 c->u->u_argcount = asdl_seq_LEN(args->args); 1566 c->u->u_argcount = asdl_seq_LEN(args->args);
1496 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); 1567 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1497 n = asdl_seq_LEN(s->v.FunctionDef.body); 1568 n = asdl_seq_LEN(s->v.FunctionDef.body);
1498 /* 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 */
1499 for (i = docstring; i < n; i++) { 1570 for (i = docstring; i < n; i++) {
1500 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i); 1571 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1501 VISIT_IN_SCOPE(c, stmt, st); 1572 VISIT_IN_SCOPE(c, stmt, st);
1502 } 1573 }
1503 co = assemble(c, 1); 1574 co = assemble(c, 1);
1575 qualname = compiler_scope_qualname(c);
1504 compiler_exit_scope(c); 1576 compiler_exit_scope(c);
1505 if (co == NULL) 1577 if (qualname == NULL || co == NULL) {
1506 return 0; 1578 Py_XDECREF(qualname);
1579 Py_XDECREF(co);
1580 return 0;
1581 }
1507 1582
1508 arglength = asdl_seq_LEN(args->defaults); 1583 arglength = asdl_seq_LEN(args->defaults);
1509 arglength |= kw_default_count << 8; 1584 arglength |= kw_default_count << 8;
1510 arglength |= num_annotations << 16; 1585 arglength |= num_annotations << 16;
1511 compiler_make_closure(c, co, arglength); 1586 compiler_make_closure(c, co, arglength, qualname);
1587 Py_DECREF(qualname);
1512 Py_DECREF(co); 1588 Py_DECREF(co);
1513 1589
1514 /* decorators */ 1590 /* decorators */
1515 for (i = 0; i < asdl_seq_LEN(decos); i++) { 1591 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1516 ADDOP_I(c, CALL_FUNCTION, 1); 1592 ADDOP_I(c, CALL_FUNCTION, 1);
1517 } 1593 }
1518 1594
1519 return compiler_nameop(c, s->v.FunctionDef.name, Store); 1595 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1520 } 1596 }
1521 1597
(...skipping 14 matching lines...) Expand all
1536 <func> is a function/closure created from the class body; 1612 <func> is a function/closure created from the class body;
1537 it has a single argument (__locals__) where the dict 1613 it has a single argument (__locals__) where the dict
1538 (or MutableSequence) representing the locals is passed 1614 (or MutableSequence) representing the locals is passed
1539 <name> is the class name 1615 <name> is the class name
1540 <bases> is the positional arguments and *varargs argument 1616 <bases> is the positional arguments and *varargs argument
1541 <keywords> is the keyword arguments and **kwds argument 1617 <keywords> is the keyword arguments and **kwds argument
1542 This borrows from compiler_call. 1618 This borrows from compiler_call.
1543 */ 1619 */
1544 1620
1545 /* 1. compile the class body into a code object */ 1621 /* 1. compile the class body into a code object */
1546 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))
1547 return 0; 1624 return 0;
1548 /* this block represents what we do in the new scope */ 1625 /* this block represents what we do in the new scope */
1549 { 1626 {
1550 /* use the class name for name mangling */ 1627 /* use the class name for name mangling */
1551 Py_INCREF(s->v.ClassDef.name); 1628 Py_INCREF(s->v.ClassDef.name);
1552 Py_XDECREF(c->u->u_private); 1629 Py_XDECREF(c->u->u_private);
1553 c->u->u_private = s->v.ClassDef.name; 1630 c->u->u_private = s->v.ClassDef.name;
1554 /* force it to have one mandatory argument */ 1631 /* force it to have one mandatory argument */
1555 c->u->u_argcount = 1; 1632 c->u->u_argcount = 1;
1556 /* load the first argument (__locals__) ... */ 1633 /* load the first argument (__locals__) ... */
1557 ADDOP_I(c, LOAD_FAST, 0); 1634 ADDOP_I(c, LOAD_FAST, 0);
1558 /* ... and store it into f_locals */ 1635 /* ... and store it into f_locals */
1559 ADDOP_IN_SCOPE(c, STORE_LOCALS); 1636 ADDOP_IN_SCOPE(c, STORE_LOCALS);
1560 /* load (global) __name__ ... */ 1637 /* load (global) __name__ ... */
1561 str = PyUnicode_InternFromString("__name__"); 1638 str = PyUnicode_InternFromString("__name__");
1562 if (!str || !compiler_nameop(c, str, Load)) { 1639 if (!str || !compiler_nameop(c, str, Load)) {
1563 Py_XDECREF(str); 1640 Py_XDECREF(str);
1564 compiler_exit_scope(c); 1641 compiler_exit_scope(c);
1565 return 0; 1642 return 0;
1566 } 1643 }
1567 Py_DECREF(str); 1644 Py_DECREF(str);
1568 /* ... and store it as __module__ */ 1645 /* ... and store it as __module__ */
1569 str = PyUnicode_InternFromString("__module__"); 1646 str = PyUnicode_InternFromString("__module__");
1570 if (!str || !compiler_nameop(c, str, Store)) { 1647 if (!str || !compiler_nameop(c, str, Store)) {
1571 Py_XDECREF(str); 1648 Py_XDECREF(str);
1572 compiler_exit_scope(c); 1649 compiler_exit_scope(c);
1573 return 0; 1650 return 0;
1574 } 1651 }
1575 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);
1576 /* compile the body proper */ 1668 /* compile the body proper */
1577 if (!compiler_body(c, s->v.ClassDef.body)) { 1669 if (!compiler_body(c, s->v.ClassDef.body)) {
1578 compiler_exit_scope(c); 1670 compiler_exit_scope(c);
1579 return 0; 1671 return 0;
1580 } 1672 }
1581 /* return the (empty) __class__ cell */ 1673 /* return the (empty) __class__ cell */
1582 str = PyUnicode_InternFromString("@__class__"); 1674 str = PyUnicode_InternFromString("@__class__");
1583 if (str == NULL) { 1675 if (str == NULL) {
1584 compiler_exit_scope(c); 1676 compiler_exit_scope(c);
1585 return 0; 1677 return 0;
(...skipping 16 matching lines...) Expand all
1602 } 1694 }
1603 /* leave the new scope */ 1695 /* leave the new scope */
1604 compiler_exit_scope(c); 1696 compiler_exit_scope(c);
1605 if (co == NULL) 1697 if (co == NULL)
1606 return 0; 1698 return 0;
1607 1699
1608 /* 2. load the 'build_class' function */ 1700 /* 2. load the 'build_class' function */
1609 ADDOP(c, LOAD_BUILD_CLASS); 1701 ADDOP(c, LOAD_BUILD_CLASS);
1610 1702
1611 /* 3. load a function (or closure) made from the code object */ 1703 /* 3. load a function (or closure) made from the code object */
1612 compiler_make_closure(c, co, 0); 1704 compiler_make_closure(c, co, 0, NULL);
1613 Py_DECREF(co); 1705 Py_DECREF(co);
1614 1706
1615 /* 4. load class name */ 1707 /* 4. load class name */
1616 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts); 1708 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1617 1709
1618 /* 5. generate the rest of the code for the call */ 1710 /* 5. generate the rest of the code for the call */
1619 if (!compiler_call_helper(c, 2, 1711 if (!compiler_call_helper(c, 2,
1620 s->v.ClassDef.bases, 1712 s->v.ClassDef.bases,
1621 s->v.ClassDef.keywords, 1713 s->v.ClassDef.keywords,
1622 s->v.ClassDef.starargs, 1714 s->v.ClassDef.starargs,
(...skipping 30 matching lines...) Expand all
1653 compiler_use_next_block(c, next); 1745 compiler_use_next_block(c, next);
1654 VISIT(c, expr, e->v.IfExp.orelse); 1746 VISIT(c, expr, e->v.IfExp.orelse);
1655 compiler_use_next_block(c, end); 1747 compiler_use_next_block(c, end);
1656 return 1; 1748 return 1;
1657 } 1749 }
1658 1750
1659 static int 1751 static int
1660 compiler_lambda(struct compiler *c, expr_ty e) 1752 compiler_lambda(struct compiler *c, expr_ty e)
1661 { 1753 {
1662 PyCodeObject *co; 1754 PyCodeObject *co;
1755 PyObject *qualname;
1663 static identifier name; 1756 static identifier name;
1664 int kw_default_count = 0, arglength; 1757 int kw_default_count = 0, arglength;
1665 arguments_ty args = e->v.Lambda.args; 1758 arguments_ty args = e->v.Lambda.args;
1666 assert(e->kind == Lambda_kind); 1759 assert(e->kind == Lambda_kind);
1667 1760
1668 if (!name) { 1761 if (!name) {
1669 name = PyUnicode_InternFromString("<lambda>"); 1762 name = PyUnicode_InternFromString("<lambda>");
1670 if (!name) 1763 if (!name)
1671 return 0; 1764 return 0;
1672 } 1765 }
1673 1766
1674 if (args->kwonlyargs) { 1767 if (args->kwonlyargs) {
1675 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, 1768 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1676 args->kw_defaults); 1769 args->kw_defaults);
1677 if (res < 0) return 0; 1770 if (res < 0) return 0;
1678 kw_default_count = res; 1771 kw_default_count = res;
1679 } 1772 }
1680 if (args->defaults) 1773 if (args->defaults)
1681 VISIT_SEQ(c, expr, args->defaults); 1774 VISIT_SEQ(c, expr, args->defaults);
1682 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))
1683 return 0; 1777 return 0;
1684 1778
1685 /* 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
1686 docstring. */ 1780 docstring. */
1687 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)
1688 return 0; 1782 return 0;
1689 1783
1690 c->u->u_argcount = asdl_seq_LEN(args->args); 1784 c->u->u_argcount = asdl_seq_LEN(args->args);
1691 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); 1785 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1692 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); 1786 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1693 if (c->u->u_ste->ste_generator) { 1787 if (c->u->u_ste->ste_generator) {
1694 ADDOP_IN_SCOPE(c, POP_TOP); 1788 ADDOP_IN_SCOPE(c, POP_TOP);
1695 } 1789 }
1696 else { 1790 else {
1697 ADDOP_IN_SCOPE(c, RETURN_VALUE); 1791 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1698 } 1792 }
1699 co = assemble(c, 1); 1793 co = assemble(c, 1);
1794 qualname = compiler_scope_qualname(c);
1700 compiler_exit_scope(c); 1795 compiler_exit_scope(c);
1701 if (co == NULL) 1796 if (qualname == NULL || co == NULL)
1702 return 0; 1797 return 0;
1703 1798
1704 arglength = asdl_seq_LEN(args->defaults); 1799 arglength = asdl_seq_LEN(args->defaults);
1705 arglength |= kw_default_count << 8; 1800 arglength |= kw_default_count << 8;
1706 compiler_make_closure(c, co, arglength); 1801 compiler_make_closure(c, co, arglength, qualname);
1802 Py_DECREF(qualname);
1707 Py_DECREF(co); 1803 Py_DECREF(co);
1708 1804
1709 return 1; 1805 return 1;
1710 } 1806 }
1711 1807
1712 static int 1808 static int
1713 compiler_if(struct compiler *c, stmt_ty s) 1809 compiler_if(struct compiler *c, stmt_ty s)
1714 { 1810 {
1715 basicblock *end, *next; 1811 basicblock *end, *next;
1716 int constant; 1812 int constant;
(...skipping 1193 matching lines...) Expand 10 before | Expand all | Expand 10 after
2910 3006
2911 return 1; 3007 return 1;
2912 } 3008 }
2913 3009
2914 static int 3010 static int
2915 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,
2916 asdl_seq *generators, expr_ty elt, expr_ty val) 3012 asdl_seq *generators, expr_ty elt, expr_ty val)
2917 { 3013 {
2918 PyCodeObject *co = NULL; 3014 PyCodeObject *co = NULL;
2919 expr_ty outermost_iter; 3015 expr_ty outermost_iter;
3016 PyObject *qualname = NULL;
2920 3017
2921 outermost_iter = ((comprehension_ty) 3018 outermost_iter = ((comprehension_ty)
2922 asdl_seq_GET(generators, 0))->iter; 3019 asdl_seq_GET(generators, 0))->iter;
2923 3020
2924 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))
2925 goto error; 3023 goto error;
2926 3024
2927 if (type != COMP_GENEXP) { 3025 if (type != COMP_GENEXP) {
2928 int op; 3026 int op;
2929 switch (type) { 3027 switch (type) {
2930 case COMP_LISTCOMP: 3028 case COMP_LISTCOMP:
2931 op = BUILD_LIST; 3029 op = BUILD_LIST;
2932 break; 3030 break;
2933 case COMP_SETCOMP: 3031 case COMP_SETCOMP:
2934 op = BUILD_SET; 3032 op = BUILD_SET;
(...skipping 12 matching lines...) Expand all
2947 3045
2948 if (!compiler_comprehension_generator(c, generators, 0, elt, 3046 if (!compiler_comprehension_generator(c, generators, 0, elt,
2949 val, type)) 3047 val, type))
2950 goto error_in_scope; 3048 goto error_in_scope;
2951 3049
2952 if (type != COMP_GENEXP) { 3050 if (type != COMP_GENEXP) {
2953 ADDOP(c, RETURN_VALUE); 3051 ADDOP(c, RETURN_VALUE);
2954 } 3052 }
2955 3053
2956 co = assemble(c, 1); 3054 co = assemble(c, 1);
3055 qualname = compiler_scope_qualname(c);
2957 compiler_exit_scope(c); 3056 compiler_exit_scope(c);
2958 if (co == NULL) 3057 if (qualname == NULL || co == NULL)
2959 goto error; 3058 goto error;
2960 3059
2961 if (!compiler_make_closure(c, co, 0)) 3060 if (!compiler_make_closure(c, co, 0, qualname))
2962 goto error; 3061 goto error;
3062 Py_DECREF(qualname);
2963 Py_DECREF(co); 3063 Py_DECREF(co);
2964 3064
2965 VISIT(c, expr, outermost_iter); 3065 VISIT(c, expr, outermost_iter);
2966 ADDOP(c, GET_ITER); 3066 ADDOP(c, GET_ITER);
2967 ADDOP_I(c, CALL_FUNCTION, 1); 3067 ADDOP_I(c, CALL_FUNCTION, 1);
2968 return 1; 3068 return 1;
2969 error_in_scope: 3069 error_in_scope:
2970 compiler_exit_scope(c); 3070 compiler_exit_scope(c);
2971 error: 3071 error:
3072 Py_XDECREF(qualname);
2972 Py_XDECREF(co); 3073 Py_XDECREF(co);
2973 return 0; 3074 return 0;
2974 } 3075 }
2975 3076
2976 static int 3077 static int
2977 compiler_genexp(struct compiler *c, expr_ty e) 3078 compiler_genexp(struct compiler *c, expr_ty e)
2978 { 3079 {
2979 static identifier name; 3080 static identifier name;
2980 if (!name) { 3081 if (!name) {
2981 name = PyUnicode_FromString("<genexpr>"); 3082 name = PyUnicode_FromString("<genexpr>");
(...skipping 1134 matching lines...) Expand 10 before | Expand all | Expand 10 after
4116 4217
4117 #undef PyAST_Compile 4218 #undef PyAST_Compile
4118 PyAPI_FUNC(PyCodeObject *) 4219 PyAPI_FUNC(PyCodeObject *)
4119 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, 4220 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4120 PyArena *arena) 4221 PyArena *arena)
4121 { 4222 {
4122 return PyAST_CompileEx(mod, filename, flags, -1, arena); 4223 return PyAST_CompileEx(mod, filename, flags, -1, arena);
4123 } 4224 }
4124 4225
4125 4226
LEFTRIGHT

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