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

Delta Between Two Patch Sets: Python/compile.c

Issue 23722: During metaclass.__init__, super() of the constructed class does not work
Left Patch Set: Created 3 years, 5 months ago
Right Patch Set: Created 3 years 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
« Python/bltinmodule.c ('K') | « Python/bltinmodule.c ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 static int compiler_addop_i(struct compiler *, int, Py_ssize_t); 172 static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
173 static int compiler_addop_j(struct compiler *, int, basicblock *, int); 173 static int compiler_addop_j(struct compiler *, int, basicblock *, int);
174 static int compiler_error(struct compiler *, const char *); 174 static int compiler_error(struct compiler *, const char *);
175 static int compiler_nameop(struct compiler *, identifier, expr_context_ty); 175 static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
176 176
177 static PyCodeObject *compiler_mod(struct compiler *, mod_ty); 177 static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
178 static int compiler_visit_stmt(struct compiler *, stmt_ty); 178 static int compiler_visit_stmt(struct compiler *, stmt_ty);
179 static int compiler_visit_keyword(struct compiler *, keyword_ty); 179 static int compiler_visit_keyword(struct compiler *, keyword_ty);
180 static int compiler_visit_expr(struct compiler *, expr_ty); 180 static int compiler_visit_expr(struct compiler *, expr_ty);
181 static int compiler_augassign(struct compiler *, stmt_ty); 181 static int compiler_augassign(struct compiler *, stmt_ty);
182 static int compiler_annassign(struct compiler *, stmt_ty);
182 static int compiler_visit_slice(struct compiler *, slice_ty, 183 static int compiler_visit_slice(struct compiler *, slice_ty,
183 expr_context_ty); 184 expr_context_ty);
184 185
185 static int compiler_push_fblock(struct compiler *, enum fblocktype, 186 static int compiler_push_fblock(struct compiler *, enum fblocktype,
186 basicblock *); 187 basicblock *);
187 static void compiler_pop_fblock(struct compiler *, enum fblocktype, 188 static void compiler_pop_fblock(struct compiler *, enum fblocktype,
188 basicblock *); 189 basicblock *);
189 /* Returns true if there is a loop on the fblock stack. */ 190 /* Returns true if there is a loop on the fblock stack. */
190 static int compiler_in_loop(struct compiler *); 191 static int compiler_in_loop(struct compiler *);
191 192
192 static int inplace_binop(struct compiler *, operator_ty); 193 static int inplace_binop(struct compiler *, operator_ty);
193 static int expr_constant(struct compiler *, expr_ty); 194 static int expr_constant(struct compiler *, expr_ty);
194 195
195 static int compiler_with(struct compiler *, stmt_ty, int); 196 static int compiler_with(struct compiler *, stmt_ty, int);
196 static int compiler_async_with(struct compiler *, stmt_ty, int); 197 static int compiler_async_with(struct compiler *, stmt_ty, int);
197 static int compiler_async_for(struct compiler *, stmt_ty); 198 static int compiler_async_for(struct compiler *, stmt_ty);
198 static int compiler_call_helper(struct compiler *c, int n, 199 static int compiler_call_helper(struct compiler *c, int n,
199 asdl_seq *args, 200 asdl_seq *args,
200 asdl_seq *keywords); 201 asdl_seq *keywords);
201 static int compiler_try_except(struct compiler *, stmt_ty); 202 static int compiler_try_except(struct compiler *, stmt_ty);
202 static int compiler_set_qualname(struct compiler *); 203 static int compiler_set_qualname(struct compiler *);
204
205 static int compiler_sync_comprehension_generator(
206 struct compiler *c,
207 asdl_seq *generators, int gen_index,
208 expr_ty elt, expr_ty val, int type);
209
210 static int compiler_async_comprehension_generator(
211 struct compiler *c,
212 asdl_seq *generators, int gen_index,
213 expr_ty elt, expr_ty val, int type);
203 214
204 static PyCodeObject *assemble(struct compiler *, int addNone); 215 static PyCodeObject *assemble(struct compiler *, int addNone);
205 static PyObject *__doc__; 216 static PyObject *__doc__;
206 217
207 #define CAPSULE_NAME "compile.c compiler unit" 218 #define CAPSULE_NAME "compile.c compiler unit"
208 219
209 PyObject * 220 PyObject *
210 _Py_Mangle(PyObject *privateobj, PyObject *ident) 221 _Py_Mangle(PyObject *privateobj, PyObject *ident)
211 { 222 {
212 /* Name mangling: __private becomes _classname__private. 223 /* Name mangling: __private becomes _classname__private.
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
469 } 480 }
470 Py_DECREF(sorted_keys); 481 Py_DECREF(sorted_keys);
471 return dest; 482 return dest;
472 } 483 }
473 484
474 static void 485 static void
475 compiler_unit_check(struct compiler_unit *u) 486 compiler_unit_check(struct compiler_unit *u)
476 { 487 {
477 basicblock *block; 488 basicblock *block;
478 for (block = u->u_blocks; block != NULL; block = block->b_list) { 489 for (block = u->u_blocks; block != NULL; block = block->b_list) {
479 assert((Py_uintptr_t)block != 0xcbcbcbcbU); 490 assert((uintptr_t)block != 0xcbcbcbcbU);
480 assert((Py_uintptr_t)block != 0xfbfbfbfbU); 491 assert((uintptr_t)block != 0xfbfbfbfbU);
481 assert((Py_uintptr_t)block != 0xdbdbdbdbU); 492 assert((uintptr_t)block != 0xdbdbdbdbU);
482 if (block->b_instr != NULL) { 493 if (block->b_instr != NULL) {
483 assert(block->b_ialloc > 0); 494 assert(block->b_ialloc > 0);
484 assert(block->b_iused > 0); 495 assert(block->b_iused > 0);
485 assert(block->b_ialloc >= block->b_iused); 496 assert(block->b_ialloc >= block->b_iused);
486 } 497 }
487 else { 498 else {
488 assert (block->b_iused == 0); 499 assert (block->b_iused == 0);
489 assert (block->b_ialloc == 0); 500 assert (block->b_ialloc == 0);
490 } 501 }
491 } 502 }
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
797 b->b_ialloc = DEFAULT_BLOCK_SIZE; 808 b->b_ialloc = DEFAULT_BLOCK_SIZE;
798 memset((char *)b->b_instr, 0, 809 memset((char *)b->b_instr, 0,
799 sizeof(struct instr) * DEFAULT_BLOCK_SIZE); 810 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
800 } 811 }
801 else if (b->b_iused == b->b_ialloc) { 812 else if (b->b_iused == b->b_ialloc) {
802 struct instr *tmp; 813 struct instr *tmp;
803 size_t oldsize, newsize; 814 size_t oldsize, newsize;
804 oldsize = b->b_ialloc * sizeof(struct instr); 815 oldsize = b->b_ialloc * sizeof(struct instr);
805 newsize = oldsize << 1; 816 newsize = oldsize << 1;
806 817
807 if (oldsize > (PY_SIZE_MAX >> 1)) { 818 if (oldsize > (SIZE_MAX >> 1)) {
808 PyErr_NoMemory(); 819 PyErr_NoMemory();
809 return -1; 820 return -1;
810 } 821 }
811 822
812 if (newsize == 0) { 823 if (newsize == 0) {
813 PyErr_NoMemory(); 824 PyErr_NoMemory();
814 return -1; 825 return -1;
815 } 826 }
816 b->b_ialloc <<= 1; 827 b->b_ialloc <<= 1;
817 tmp = (struct instr *)PyObject_Realloc( 828 tmp = (struct instr *)PyObject_Realloc(
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
926 case SETUP_WITH: 937 case SETUP_WITH:
927 return 7; 938 return 7;
928 case WITH_CLEANUP_START: 939 case WITH_CLEANUP_START:
929 return 1; 940 return 1;
930 case WITH_CLEANUP_FINISH: 941 case WITH_CLEANUP_FINISH:
931 return -1; /* XXX Sometimes more */ 942 return -1; /* XXX Sometimes more */
932 case RETURN_VALUE: 943 case RETURN_VALUE:
933 return -1; 944 return -1;
934 case IMPORT_STAR: 945 case IMPORT_STAR:
935 return -1; 946 return -1;
947 case SETUP_ANNOTATIONS:
948 return 0;
936 case YIELD_VALUE: 949 case YIELD_VALUE:
937 return 0; 950 return 0;
938 case YIELD_FROM: 951 case YIELD_FROM:
939 return -1; 952 return -1;
940 case POP_BLOCK: 953 case POP_BLOCK:
941 return 0; 954 return 0;
942 case POP_EXCEPT: 955 case POP_EXCEPT:
943 return 0; /* -3 except if bad bytecode */ 956 return 0; /* -3 except if bad bytecode */
944 case END_FINALLY: 957 case END_FINALLY:
945 return -1; /* or -2 or -3 if exception occurred */ 958 return -1; /* or -2 or -3 if exception occurred */
(...skipping 17 matching lines...) Expand all
963 return -1; 976 return -1;
964 case DELETE_GLOBAL: 977 case DELETE_GLOBAL:
965 return 0; 978 return 0;
966 case LOAD_CONST: 979 case LOAD_CONST:
967 return 1; 980 return 1;
968 case LOAD_NAME: 981 case LOAD_NAME:
969 return 1; 982 return 1;
970 case BUILD_TUPLE: 983 case BUILD_TUPLE:
971 case BUILD_LIST: 984 case BUILD_LIST:
972 case BUILD_SET: 985 case BUILD_SET:
986 case BUILD_STRING:
973 return 1-oparg; 987 return 1-oparg;
974 case BUILD_LIST_UNPACK: 988 case BUILD_LIST_UNPACK:
975 case BUILD_TUPLE_UNPACK: 989 case BUILD_TUPLE_UNPACK:
990 case BUILD_TUPLE_UNPACK_WITH_CALL:
976 case BUILD_SET_UNPACK: 991 case BUILD_SET_UNPACK:
977 case BUILD_MAP_UNPACK: 992 case BUILD_MAP_UNPACK:
993 case BUILD_MAP_UNPACK_WITH_CALL:
978 return 1 - oparg; 994 return 1 - oparg;
979 case BUILD_MAP_UNPACK_WITH_CALL:
980 return 1 - (oparg & 0xFF);
981 case BUILD_MAP: 995 case BUILD_MAP:
982 return 1 - 2*oparg; 996 return 1 - 2*oparg;
983 case BUILD_CONST_KEY_MAP: 997 case BUILD_CONST_KEY_MAP:
984 return -oparg; 998 return -oparg;
985 case LOAD_ATTR: 999 case LOAD_ATTR:
986 return 0; 1000 return 0;
987 case COMPARE_OP: 1001 case COMPARE_OP:
988 return -1; 1002 return -1;
989 case IMPORT_NAME: 1003 case IMPORT_NAME:
990 return -1; 1004 return -1;
(...skipping 21 matching lines...) Expand all
1012 case SETUP_FINALLY: 1026 case SETUP_FINALLY:
1013 return 6; /* can push 3 values for the new exception 1027 return 6; /* can push 3 values for the new exception
1014 + 3 others for the previous exception state */ 1028 + 3 others for the previous exception state */
1015 1029
1016 case LOAD_FAST: 1030 case LOAD_FAST:
1017 return 1; 1031 return 1;
1018 case STORE_FAST: 1032 case STORE_FAST:
1019 return -1; 1033 return -1;
1020 case DELETE_FAST: 1034 case DELETE_FAST:
1021 return 0; 1035 return 0;
1036 case STORE_ANNOTATION:
1037 return -1;
1022 1038
1023 case RAISE_VARARGS: 1039 case RAISE_VARARGS:
1024 return -oparg; 1040 return -oparg;
1025 #define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
1026 case CALL_FUNCTION: 1041 case CALL_FUNCTION:
1027 return -NARGS(oparg); 1042 return -oparg;
1028 case CALL_FUNCTION_VAR:
1029 case CALL_FUNCTION_KW: 1043 case CALL_FUNCTION_KW:
1030 return -NARGS(oparg)-1; 1044 return -oparg-1;
1031 case CALL_FUNCTION_VAR_KW: 1045 case CALL_FUNCTION_EX:
1032 return -NARGS(oparg)-2; 1046 return - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0);
1033 #undef NARGS
1034 case MAKE_FUNCTION: 1047 case MAKE_FUNCTION:
1035 return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) - 1048 return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1036 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0); 1049 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
1037 case BUILD_SLICE: 1050 case BUILD_SLICE:
1038 if (oparg == 3) 1051 if (oparg == 3)
1039 return -2; 1052 return -2;
1040 else 1053 else
1041 return -1; 1054 return -1;
1042 1055
1043 case LOAD_CLOSURE: 1056 case LOAD_CLOSURE:
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after
1350 case Ellipsis_kind: 1363 case Ellipsis_kind:
1351 return Py_Ellipsis; 1364 return Py_Ellipsis;
1352 case NameConstant_kind: 1365 case NameConstant_kind:
1353 return e->v.NameConstant.value; 1366 return e->v.NameConstant.value;
1354 default: 1367 default:
1355 assert(!is_const(e)); 1368 assert(!is_const(e));
1356 return NULL; 1369 return NULL;
1357 } 1370 }
1358 } 1371 }
1359 1372
1360 /* Compile a sequence of statements, checking for a docstring. */ 1373 /* Search if variable annotations are present statically in a block. */
1374
1375 static int
1376 find_ann(asdl_seq *stmts)
1377 {
1378 int i, j, res = 0;
1379 stmt_ty st;
1380
1381 for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1382 st = (stmt_ty)asdl_seq_GET(stmts, i);
1383 switch (st->kind) {
1384 case AnnAssign_kind:
1385 return 1;
1386 case For_kind:
1387 res = find_ann(st->v.For.body) ||
1388 find_ann(st->v.For.orelse);
1389 break;
1390 case AsyncFor_kind:
1391 res = find_ann(st->v.AsyncFor.body) ||
1392 find_ann(st->v.AsyncFor.orelse);
1393 break;
1394 case While_kind:
1395 res = find_ann(st->v.While.body) ||
1396 find_ann(st->v.While.orelse);
1397 break;
1398 case If_kind:
1399 res = find_ann(st->v.If.body) ||
1400 find_ann(st->v.If.orelse);
1401 break;
1402 case With_kind:
1403 res = find_ann(st->v.With.body);
1404 break;
1405 case AsyncWith_kind:
1406 res = find_ann(st->v.AsyncWith.body);
1407 break;
1408 case Try_kind:
1409 for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1410 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1411 st->v.Try.handlers, j);
1412 if (find_ann(handler->v.ExceptHandler.body)) {
1413 return 1;
1414 }
1415 }
1416 res = find_ann(st->v.Try.body) ||
1417 find_ann(st->v.Try.finalbody) ||
1418 find_ann(st->v.Try.orelse);
1419 break;
1420 default:
1421 res = 0;
1422 }
1423 if (res) {
1424 break;
1425 }
1426 }
1427 return res;
1428 }
1429
1430 /* Compile a sequence of statements, checking for a docstring
1431 and for annotations. */
1361 1432
1362 static int 1433 static int
1363 compiler_body(struct compiler *c, asdl_seq *stmts) 1434 compiler_body(struct compiler *c, asdl_seq *stmts)
1364 { 1435 {
1365 int i = 0; 1436 int i = 0;
1366 stmt_ty st; 1437 stmt_ty st;
1367 1438
1439 /* Set current line number to the line number of first statement.
1440 This way line number for SETUP_ANNOTATIONS will always
1441 coincide with the line number of first "real" statement in module.
1442 If body is empy, then lineno will be set later in assemble. */
1443 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE &&
1444 !c->u->u_lineno && asdl_seq_LEN(stmts)) {
1445 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1446 c->u->u_lineno = st->lineno;
1447 }
1448 /* Every annotated class and module should have __annotations__. */
1449 if (find_ann(stmts)) {
1450 ADDOP(c, SETUP_ANNOTATIONS);
1451 }
1368 if (!asdl_seq_LEN(stmts)) 1452 if (!asdl_seq_LEN(stmts))
1369 return 1; 1453 return 1;
1370 st = (stmt_ty)asdl_seq_GET(stmts, 0); 1454 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1371 if (compiler_isdocstring(st) && c->c_optimize < 2) { 1455 if (compiler_isdocstring(st) && c->c_optimize < 2) {
1372 /* don't generate docstrings if -OO */ 1456 /* don't generate docstrings if -OO */
1373 i = 1; 1457 i = 1;
1374 VISIT(c, expr, st->v.Expr.value); 1458 VISIT(c, expr, st->v.Expr.value);
1375 if (!compiler_nameop(c, __doc__, Store)) 1459 if (!compiler_nameop(c, __doc__, Store))
1376 return 0; 1460 return 0;
1377 } 1461 }
(...skipping 17 matching lines...) Expand all
1395 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0)) 1479 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0))
1396 return NULL; 1480 return NULL;
1397 switch (mod->kind) { 1481 switch (mod->kind) {
1398 case Module_kind: 1482 case Module_kind:
1399 if (!compiler_body(c, mod->v.Module.body)) { 1483 if (!compiler_body(c, mod->v.Module.body)) {
1400 compiler_exit_scope(c); 1484 compiler_exit_scope(c);
1401 return 0; 1485 return 0;
1402 } 1486 }
1403 break; 1487 break;
1404 case Interactive_kind: 1488 case Interactive_kind:
1489 if (find_ann(mod->v.Interactive.body)) {
1490 ADDOP(c, SETUP_ANNOTATIONS);
1491 }
1405 c->c_interactive = 1; 1492 c->c_interactive = 1;
1406 VISIT_SEQ_IN_SCOPE(c, stmt, 1493 VISIT_SEQ_IN_SCOPE(c, stmt,
1407 mod->v.Interactive.body); 1494 mod->v.Interactive.body);
1408 break; 1495 break;
1409 case Expression_kind: 1496 case Expression_kind:
1410 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); 1497 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1411 addNone = 0; 1498 addNone = 0;
1412 break; 1499 break;
1413 case Suite_kind: 1500 case Suite_kind:
1414 PyErr_SetString(PyExc_SystemError, 1501 PyErr_SetString(PyExc_SystemError,
(...skipping 13 matching lines...) Expand all
1428 /* The test for LOCAL must come before the test for FREE in order to 1515 /* The test for LOCAL must come before the test for FREE in order to
1429 handle classes where name is both local and free. The local var is 1516 handle classes where name is both local and free. The local var is
1430 a method and the free var is a free var referenced within a method. 1517 a method and the free var is a free var referenced within a method.
1431 */ 1518 */
1432 1519
1433 static int 1520 static int
1434 get_ref_type(struct compiler *c, PyObject *name) 1521 get_ref_type(struct compiler *c, PyObject *name)
1435 { 1522 {
1436 int scope; 1523 int scope;
1437 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && 1524 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
1438 !PyUnicode_CompareWithASCIIString(name, "__class__")) 1525 _PyUnicode_EqualToASCIIString(name, "__class__"))
1439 return CELL; 1526 return CELL;
1440 scope = PyST_GetScope(c->u->u_ste, name); 1527 scope = PyST_GetScope(c->u->u_ste, name);
1441 if (scope == 0) { 1528 if (scope == 0) {
1442 char buf[350]; 1529 char buf[350];
1443 PyOS_snprintf(buf, sizeof(buf), 1530 PyOS_snprintf(buf, sizeof(buf),
1444 "unknown scope for %.100s in %.100s(%s)\n" 1531 "unknown scope for %.100s in %.100s(%s)\n"
1445 "symbols: %s\nlocals: %s\nglobals: %s", 1532 "symbols: %s\nlocals: %s\nglobals: %s",
1446 PyUnicode_AsUTF8(name), 1533 PyUnicode_AsUTF8(name),
1447 PyUnicode_AsUTF8(c->u->u_name), 1534 PyUnicode_AsUTF8(c->u->u_name),
1448 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)), 1535 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)),
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after
1799 co = assemble(c, 1); 1886 co = assemble(c, 1);
1800 qualname = c->u->u_qualname; 1887 qualname = c->u->u_qualname;
1801 Py_INCREF(qualname); 1888 Py_INCREF(qualname);
1802 compiler_exit_scope(c); 1889 compiler_exit_scope(c);
1803 if (co == NULL) { 1890 if (co == NULL) {
1804 Py_XDECREF(qualname); 1891 Py_XDECREF(qualname);
1805 Py_XDECREF(co); 1892 Py_XDECREF(co);
1806 return 0; 1893 return 0;
1807 } 1894 }
1808 1895
1809 if (is_async)
1810 co->co_flags |= CO_COROUTINE;
1811 compiler_make_closure(c, co, funcflags, qualname); 1896 compiler_make_closure(c, co, funcflags, qualname);
1812 Py_DECREF(qualname); 1897 Py_DECREF(qualname);
1813 Py_DECREF(co); 1898 Py_DECREF(co);
1814 1899
1815 /* decorators */ 1900 /* decorators */
1816 for (i = 0; i < asdl_seq_LEN(decos); i++) { 1901 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1817 ADDOP_I(c, CALL_FUNCTION, 1); 1902 ADDOP_I(c, CALL_FUNCTION, 1);
1818 } 1903 }
1819 1904
1820 return compiler_nameop(c, name, Store); 1905 return compiler_nameop(c, name, Store);
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
1875 Py_XDECREF(str); 1960 Py_XDECREF(str);
1876 compiler_exit_scope(c); 1961 compiler_exit_scope(c);
1877 return 0; 1962 return 0;
1878 } 1963 }
1879 Py_DECREF(str); 1964 Py_DECREF(str);
1880 /* compile the body proper */ 1965 /* compile the body proper */
1881 if (!compiler_body(c, s->v.ClassDef.body)) { 1966 if (!compiler_body(c, s->v.ClassDef.body)) {
1882 compiler_exit_scope(c); 1967 compiler_exit_scope(c);
1883 return 0; 1968 return 0;
1884 } 1969 }
1970 /* Return __classcell__ if it is referenced, otherwise return None */
1885 if (c->u->u_ste->ste_needs_class_closure) { 1971 if (c->u->u_ste->ste_needs_class_closure) {
1886 /* store __classcell__ into class namespace */ 1972 /* Store __classcell__ into class namespace & return it */
1887 str = PyUnicode_InternFromString("__class__"); 1973 str = PyUnicode_InternFromString("__class__");
1888 if (str == NULL) { 1974 if (str == NULL) {
1889 compiler_exit_scope(c); 1975 compiler_exit_scope(c);
1890 return 0; 1976 return 0;
1891 } 1977 }
1892 i = compiler_lookup_arg(c->u->u_cellvars, str); 1978 i = compiler_lookup_arg(c->u->u_cellvars, str);
1893 Py_DECREF(str); 1979 Py_DECREF(str);
1894 if (i < 0) { 1980 if (i < 0) {
1895 compiler_exit_scope(c); 1981 compiler_exit_scope(c);
1896 return 0; 1982 return 0;
1897 } 1983 }
1898 assert(i == 0); 1984 assert(i == 0);
1899 1985
1900 ADDOP_I(c, LOAD_CLOSURE, i); 1986 ADDOP_I(c, LOAD_CLOSURE, i);
1987 ADDOP(c, DUP_TOP);
1901 str = PyUnicode_InternFromString("__classcell__"); 1988 str = PyUnicode_InternFromString("__classcell__");
tecki 2016/07/17 18:49:21 At the end of the class body, the compiler used to
1902 if (!str || !compiler_nameop(c, str, Store)) { 1989 if (!str || !compiler_nameop(c, str, Store)) {
1903 Py_XDECREF(str); 1990 Py_XDECREF(str);
1904 compiler_exit_scope(c); 1991 compiler_exit_scope(c);
1905 return 0; 1992 return 0;
1906 } 1993 }
1907 Py_DECREF(str); 1994 Py_DECREF(str);
1908 } 1995 }
1909 else { 1996 else {
1910 /* This happens when nobody references the cell. */ 1997 /* No methods referenced __class__, so just return None */
1911 assert(PyDict_Size(c->u->u_cellvars) == 0); 1998 assert(PyDict_Size(c->u->u_cellvars) == 0);
1912 } 1999 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2000 }
2001 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1913 /* create the code object */ 2002 /* create the code object */
1914 co = assemble(c, 1); 2003 co = assemble(c, 1);
1915 } 2004 }
1916 /* leave the new scope */ 2005 /* leave the new scope */
1917 compiler_exit_scope(c); 2006 compiler_exit_scope(c);
1918 if (co == NULL) 2007 if (co == NULL)
1919 return 0; 2008 return 0;
1920 2009
1921 /* 2. load the 'build_class' function */ 2010 /* 2. load the 'build_class' function */
1922 ADDOP(c, LOAD_BUILD_CLASS); 2011 ADDOP(c, LOAD_BUILD_CLASS);
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
2085 compiler_pop_fblock(c, LOOP, start); 2174 compiler_pop_fblock(c, LOOP, start);
2086 VISIT_SEQ(c, stmt, s->v.For.orelse); 2175 VISIT_SEQ(c, stmt, s->v.For.orelse);
2087 compiler_use_next_block(c, end); 2176 compiler_use_next_block(c, end);
2088 return 1; 2177 return 1;
2089 } 2178 }
2090 2179
2091 2180
2092 static int 2181 static int
2093 compiler_async_for(struct compiler *c, stmt_ty s) 2182 compiler_async_for(struct compiler *c, stmt_ty s)
2094 { 2183 {
2095 static PyObject *stopiter_error = NULL; 2184 _Py_IDENTIFIER(StopAsyncIteration);
2185
2096 basicblock *try, *except, *end, *after_try, *try_cleanup, 2186 basicblock *try, *except, *end, *after_try, *try_cleanup,
2097 *after_loop, *after_loop_else; 2187 *after_loop, *after_loop_else;
2098 2188
2099 if (stopiter_error == NULL) { 2189 PyObject *stop_aiter_error = _PyUnicode_FromId(&PyId_StopAsyncIteration);
2100 stopiter_error = PyUnicode_InternFromString("StopAsyncIteration"); 2190 if (stop_aiter_error == NULL) {
2101 if (stopiter_error == NULL) 2191 return 0;
2102 return 0;
2103 } 2192 }
2104 2193
2105 try = compiler_new_block(c); 2194 try = compiler_new_block(c);
2106 except = compiler_new_block(c); 2195 except = compiler_new_block(c);
2107 end = compiler_new_block(c); 2196 end = compiler_new_block(c);
2108 after_try = compiler_new_block(c); 2197 after_try = compiler_new_block(c);
2109 try_cleanup = compiler_new_block(c); 2198 try_cleanup = compiler_new_block(c);
2110 after_loop = compiler_new_block(c); 2199 after_loop = compiler_new_block(c);
2111 after_loop_else = compiler_new_block(c); 2200 after_loop_else = compiler_new_block(c);
2112 2201
(...skipping 21 matching lines...) Expand all
2134 ADDOP_O(c, LOAD_CONST, Py_None, consts); 2223 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2135 ADDOP(c, YIELD_FROM); 2224 ADDOP(c, YIELD_FROM);
2136 VISIT(c, expr, s->v.AsyncFor.target); 2225 VISIT(c, expr, s->v.AsyncFor.target);
2137 ADDOP(c, POP_BLOCK); 2226 ADDOP(c, POP_BLOCK);
2138 compiler_pop_fblock(c, EXCEPT, try); 2227 compiler_pop_fblock(c, EXCEPT, try);
2139 ADDOP_JREL(c, JUMP_FORWARD, after_try); 2228 ADDOP_JREL(c, JUMP_FORWARD, after_try);
2140 2229
2141 2230
2142 compiler_use_next_block(c, except); 2231 compiler_use_next_block(c, except);
2143 ADDOP(c, DUP_TOP); 2232 ADDOP(c, DUP_TOP);
2144 ADDOP_O(c, LOAD_GLOBAL, stopiter_error, names); 2233 ADDOP_O(c, LOAD_GLOBAL, stop_aiter_error, names);
2145 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH); 2234 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2146 ADDOP_JABS(c, POP_JUMP_IF_FALSE, try_cleanup); 2235 ADDOP_JABS(c, POP_JUMP_IF_FALSE, try_cleanup);
2147 2236
2148 ADDOP(c, POP_TOP); 2237 ADDOP(c, POP_TOP);
2149 ADDOP(c, POP_TOP); 2238 ADDOP(c, POP_TOP);
2150 ADDOP(c, POP_TOP); 2239 ADDOP(c, POP_TOP);
2151 ADDOP(c, POP_EXCEPT); /* for SETUP_EXCEPT */ 2240 ADDOP(c, POP_EXCEPT); /* for SETUP_EXCEPT */
2152 ADDOP(c, POP_BLOCK); /* for SETUP_LOOP */ 2241 ADDOP(c, POP_BLOCK); /* for SETUP_LOOP */
2153 ADDOP_JABS(c, JUMP_ABSOLUTE, after_loop_else); 2242 ADDOP_JABS(c, JUMP_ABSOLUTE, after_loop_else);
2154 2243
(...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after
2596 } 2685 }
2597 2686
2598 /* build up the names */ 2687 /* build up the names */
2599 for (i = 0; i < n; i++) { 2688 for (i = 0; i < n; i++) {
2600 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); 2689 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2601 Py_INCREF(alias->name); 2690 Py_INCREF(alias->name);
2602 PyTuple_SET_ITEM(names, i, alias->name); 2691 PyTuple_SET_ITEM(names, i, alias->name);
2603 } 2692 }
2604 2693
2605 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && 2694 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
2606 !PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module, "__future__")) { 2695 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
2607 Py_DECREF(level); 2696 Py_DECREF(level);
2608 Py_DECREF(names); 2697 Py_DECREF(names);
2609 return compiler_error(c, "from __future__ imports must occur " 2698 return compiler_error(c, "from __future__ imports must occur "
2610 "at the beginning of the file"); 2699 "at the beginning of the file");
2611 } 2700 }
2612 2701
2613 ADDOP_O(c, LOAD_CONST, level, consts); 2702 ADDOP_O(c, LOAD_CONST, level, consts);
2614 Py_DECREF(level); 2703 Py_DECREF(level);
2615 ADDOP_O(c, LOAD_CONST, names, consts); 2704 ADDOP_O(c, LOAD_CONST, names, consts);
2616 Py_DECREF(names); 2705 Py_DECREF(names);
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
2719 2808
2720 switch (s->kind) { 2809 switch (s->kind) {
2721 case FunctionDef_kind: 2810 case FunctionDef_kind:
2722 return compiler_function(c, s, 0); 2811 return compiler_function(c, s, 0);
2723 case ClassDef_kind: 2812 case ClassDef_kind:
2724 return compiler_class(c, s); 2813 return compiler_class(c, s);
2725 case Return_kind: 2814 case Return_kind:
2726 if (c->u->u_ste->ste_type != FunctionBlock) 2815 if (c->u->u_ste->ste_type != FunctionBlock)
2727 return compiler_error(c, "'return' outside function"); 2816 return compiler_error(c, "'return' outside function");
2728 if (s->v.Return.value) { 2817 if (s->v.Return.value) {
2818 if (c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2819 return compiler_error(
2820 c, "'return' with value in async generator");
2729 VISIT(c, expr, s->v.Return.value); 2821 VISIT(c, expr, s->v.Return.value);
2730 } 2822 }
2731 else 2823 else
2732 ADDOP_O(c, LOAD_CONST, Py_None, consts); 2824 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2733 ADDOP(c, RETURN_VALUE); 2825 ADDOP(c, RETURN_VALUE);
2734 break; 2826 break;
2735 case Delete_kind: 2827 case Delete_kind:
2736 VISIT_SEQ(c, expr, s->v.Delete.targets) 2828 VISIT_SEQ(c, expr, s->v.Delete.targets)
2737 break; 2829 break;
2738 case Assign_kind: 2830 case Assign_kind:
2739 n = asdl_seq_LEN(s->v.Assign.targets); 2831 n = asdl_seq_LEN(s->v.Assign.targets);
2740 VISIT(c, expr, s->v.Assign.value); 2832 VISIT(c, expr, s->v.Assign.value);
2741 for (i = 0; i < n; i++) { 2833 for (i = 0; i < n; i++) {
2742 if (i < n - 1) 2834 if (i < n - 1)
2743 ADDOP(c, DUP_TOP); 2835 ADDOP(c, DUP_TOP);
2744 VISIT(c, expr, 2836 VISIT(c, expr,
2745 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); 2837 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2746 } 2838 }
2747 break; 2839 break;
2748 case AugAssign_kind: 2840 case AugAssign_kind:
2749 return compiler_augassign(c, s); 2841 return compiler_augassign(c, s);
2842 case AnnAssign_kind:
2843 return compiler_annassign(c, s);
2750 case For_kind: 2844 case For_kind:
2751 return compiler_for(c, s); 2845 return compiler_for(c, s);
2752 case While_kind: 2846 case While_kind:
2753 return compiler_while(c, s); 2847 return compiler_while(c, s);
2754 case If_kind: 2848 case If_kind:
2755 return compiler_if(c, s); 2849 return compiler_if(c, s);
2756 case Raise_kind: 2850 case Raise_kind:
2757 n = 0; 2851 n = 0;
2758 if (s->v.Raise.exc) { 2852 if (s->v.Raise.exc) {
2759 VISIT(c, expr, s->v.Raise.exc); 2853 VISIT(c, expr, s->v.Raise.exc);
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
2930 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; 3024 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2931 3025
2932 PyObject *dict = c->u->u_names; 3026 PyObject *dict = c->u->u_names;
2933 PyObject *mangled; 3027 PyObject *mangled;
2934 /* XXX AugStore isn't used anywhere! */ 3028 /* XXX AugStore isn't used anywhere! */
2935 3029
2936 mangled = _Py_Mangle(c->u->u_private, name); 3030 mangled = _Py_Mangle(c->u->u_private, name);
2937 if (!mangled) 3031 if (!mangled)
2938 return 0; 3032 return 0;
2939 3033
2940 assert(PyUnicode_CompareWithASCIIString(name, "None") && 3034 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
2941 PyUnicode_CompareWithASCIIString(name, "True") && 3035 !_PyUnicode_EqualToASCIIString(name, "True") &&
2942 PyUnicode_CompareWithASCIIString(name, "False")); 3036 !_PyUnicode_EqualToASCIIString(name, "False"));
2943 3037
2944 op = 0; 3038 op = 0;
2945 optype = OP_NAME; 3039 optype = OP_NAME;
2946 scope = PyST_GetScope(c->u->u_ste, mangled); 3040 scope = PyST_GetScope(c->u->u_ste, mangled);
2947 switch (scope) { 3041 switch (scope) {
2948 case FREE: 3042 case FREE:
2949 dict = c->u->u_freevars; 3043 dict = c->u->u_freevars;
2950 optype = OP_DEREF; 3044 optype = OP_DEREF;
2951 break; 3045 break;
2952 case CELL: 3046 case CELL:
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after
3313 { 3407 {
3314 VISIT(c, expr, e->v.Call.func); 3408 VISIT(c, expr, e->v.Call.func);
3315 return compiler_call_helper(c, 0, 3409 return compiler_call_helper(c, 0,
3316 e->v.Call.args, 3410 e->v.Call.args,
3317 e->v.Call.keywords); 3411 e->v.Call.keywords);
3318 } 3412 }
3319 3413
3320 static int 3414 static int
3321 compiler_joined_str(struct compiler *c, expr_ty e) 3415 compiler_joined_str(struct compiler *c, expr_ty e)
3322 { 3416 {
3323 /* Concatenate parts of a string using ''.join(parts). There are
3324 probably better ways of doing this.
3325
3326 This is used for constructs like "'x=' f'{42}'", which have to
3327 be evaluated at compile time. */
3328
3329 static PyObject *empty_string;
3330 static PyObject *join_string;
3331
3332 if (!empty_string) {
3333 empty_string = PyUnicode_FromString("");
3334 if (!empty_string)
3335 return 0;
3336 }
3337 if (!join_string) {
3338 join_string = PyUnicode_FromString("join");
3339 if (!join_string)
3340 return 0;
3341 }
3342
3343 ADDOP_O(c, LOAD_CONST, empty_string, consts);
3344 ADDOP_NAME(c, LOAD_ATTR, join_string, names);
3345 VISIT_SEQ(c, expr, e->v.JoinedStr.values); 3417 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
3346 ADDOP_I(c, BUILD_LIST, asdl_seq_LEN(e->v.JoinedStr.values)); 3418 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
3347 ADDOP_I(c, CALL_FUNCTION, 1);
3348 return 1; 3419 return 1;
3349 } 3420 }
3350 3421
3351 /* Used to implement f-strings. Format a single value. */ 3422 /* Used to implement f-strings. Format a single value. */
3352 static int 3423 static int
3353 compiler_formatted_value(struct compiler *c, expr_ty e) 3424 compiler_formatted_value(struct compiler *c, expr_ty e)
3354 { 3425 {
3355 /* Our oparg encodes 2 pieces of information: the conversion 3426 /* Our oparg encodes 2 pieces of information: the conversion
3356 character, and whether or not a format_spec was provided. 3427 character, and whether or not a format_spec was provided.
3357 3428
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
3428 return 1; 3499 return 1;
3429 } 3500 }
3430 3501
3431 /* shared code between compiler_call and compiler_class */ 3502 /* shared code between compiler_call and compiler_class */
3432 static int 3503 static int
3433 compiler_call_helper(struct compiler *c, 3504 compiler_call_helper(struct compiler *c,
3434 int n, /* Args already pushed */ 3505 int n, /* Args already pushed */
3435 asdl_seq *args, 3506 asdl_seq *args,
3436 asdl_seq *keywords) 3507 asdl_seq *keywords)
3437 { 3508 {
3438 int code = 0; 3509 Py_ssize_t i, nseen, nelts, nkwelts;
3439 Py_ssize_t nelts, i, nseen; 3510 int mustdictunpack = 0;
3440 int nkw;
3441 3511
3442 /* the number of tuples and dictionaries on the stack */ 3512 /* the number of tuples and dictionaries on the stack */
3443 Py_ssize_t nsubargs = 0, nsubkwargs = 0; 3513 Py_ssize_t nsubargs = 0, nsubkwargs = 0;
3444 3514
3445 nkw = 0;
3446 nseen = 0; /* the number of positional arguments on the stack */
3447 nelts = asdl_seq_LEN(args); 3515 nelts = asdl_seq_LEN(args);
3516 nkwelts = asdl_seq_LEN(keywords);
3517
3518 for (i = 0; i < nkwelts; i++) {
3519 keyword_ty kw = asdl_seq_GET(keywords, i);
3520 if (kw->arg == NULL) {
3521 mustdictunpack = 1;
3522 break;
3523 }
3524 }
3525
3526 nseen = n; /* the number of positional arguments on the stack */
3448 for (i = 0; i < nelts; i++) { 3527 for (i = 0; i < nelts; i++) {
3449 expr_ty elt = asdl_seq_GET(args, i); 3528 expr_ty elt = asdl_seq_GET(args, i);
3450 if (elt->kind == Starred_kind) { 3529 if (elt->kind == Starred_kind) {
3451 /* A star-arg. If we've seen positional arguments, 3530 /* A star-arg. If we've seen positional arguments,
3452 pack the positional arguments into a 3531 pack the positional arguments into a tuple. */
3453 tuple. */
3454 if (nseen) { 3532 if (nseen) {
3455 ADDOP_I(c, BUILD_TUPLE, nseen); 3533 ADDOP_I(c, BUILD_TUPLE, nseen);
3456 nseen = 0; 3534 nseen = 0;
3457 nsubargs++; 3535 nsubargs++;
3458 } 3536 }
3459 VISIT(c, expr, elt->v.Starred.value); 3537 VISIT(c, expr, elt->v.Starred.value);
3460 nsubargs++; 3538 nsubargs++;
3461 } 3539 }
3462 else if (nsubargs) { 3540 else {
3463 /* We've seen star-args already, so we
3464 count towards items-to-pack-into-tuple. */
3465 VISIT(c, expr, elt); 3541 VISIT(c, expr, elt);
3466 nseen++; 3542 nseen++;
3467 } 3543 }
3468 else { 3544 }
3469 /* Positional arguments before star-arguments 3545
3470 are left on the stack. */ 3546 /* Same dance again for keyword arguments */
3471 VISIT(c, expr, elt); 3547 if (nsubargs || mustdictunpack) {
3472 n++; 3548 if (nseen) {
3473 } 3549 /* Pack up any trailing positional arguments. */
3474 } 3550 ADDOP_I(c, BUILD_TUPLE, nseen);
3475 if (nseen) { 3551 nsubargs++;
3476 /* Pack up any trailing positional arguments. */ 3552 }
3477 ADDOP_I(c, BUILD_TUPLE, nseen);
3478 nsubargs++;
3479 }
3480 if (nsubargs) {
3481 code |= 1;
3482 if (nsubargs > 1) { 3553 if (nsubargs > 1) {
3483 /* If we ended up with more than one stararg, we need 3554 /* If we ended up with more than one stararg, we need
3484 to concatenate them into a single sequence. */ 3555 to concatenate them into a single sequence. */
3485 ADDOP_I(c, BUILD_LIST_UNPACK, nsubargs); 3556 ADDOP_I(c, BUILD_TUPLE_UNPACK_WITH_CALL, nsubargs);
3486 } 3557 }
3487 } 3558 else if (nsubargs == 0) {
3488 3559 ADDOP_I(c, BUILD_TUPLE, 0);
3489 /* Same dance again for keyword arguments */ 3560 }
3490 nseen = 0; /* the number of keyword arguments on the stack following */ 3561 nseen = 0; /* the number of keyword arguments on the stack following */
3491 nelts = asdl_seq_LEN(keywords); 3562 for (i = 0; i < nkwelts; i++) {
3492 for (i = 0; i < nelts; i++) { 3563 keyword_ty kw = asdl_seq_GET(keywords, i);
3493 keyword_ty kw = asdl_seq_GET(keywords, i); 3564 if (kw->arg == NULL) {
3494 if (kw->arg == NULL) { 3565 /* A keyword argument unpacking. */
3495 /* A keyword argument unpacking. */ 3566 if (nseen) {
3496 if (nseen) {
3497 if (nsubkwargs) {
3498 if (!compiler_subkwargs(c, keywords, i - nseen, i)) 3567 if (!compiler_subkwargs(c, keywords, i - nseen, i))
3499 return 0; 3568 return 0;
3500 nsubkwargs++; 3569 nsubkwargs++;
3570 nseen = 0;
3501 } 3571 }
3502 else { 3572 VISIT(c, expr, kw->value);
3503 Py_ssize_t j; 3573 nsubkwargs++;
3504 for (j = 0; j < nseen; j++) {
3505 VISIT(c, keyword, asdl_seq_GET(keywords, j));
3506 }
3507 nkw = nseen;
3508 }
3509 nseen = 0;
3510 } 3574 }
3511 VISIT(c, expr, kw->value); 3575 else {
3512 nsubkwargs++; 3576 nseen++;
3513 } 3577 }
3514 else { 3578 }
3515 nseen++; 3579 if (nseen) {
3516 }
3517 }
3518 if (nseen) {
3519 if (nsubkwargs) {
3520 /* Pack up any trailing keyword arguments. */ 3580 /* Pack up any trailing keyword arguments. */
3521 if (!compiler_subkwargs(c, keywords, nelts - nseen, nelts)) 3581 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts))
3522 return 0; 3582 return 0;
3523 nsubkwargs++; 3583 nsubkwargs++;
3524 } 3584 }
3525 else {
3526 VISIT_SEQ(c, keyword, keywords);
3527 nkw = nseen;
3528 }
3529 }
3530 if (nsubkwargs) {
3531 code |= 2;
3532 if (nsubkwargs > 1) { 3585 if (nsubkwargs > 1) {
3533 /* Pack it all up */ 3586 /* Pack it all up */
3534 int function_pos = n + (code & 1) + 2 * nkw + 1; 3587 ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs);
3535 ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs | (function_pos << 8)); 3588 }
3536 } 3589 ADDOP_I(c, CALL_FUNCTION_EX, nsubkwargs > 0);
3537 } 3590 return 1;
3538 assert(n < 1<<8); 3591 }
3539 assert(nkw < 1<<24); 3592 else if (nkwelts) {
3540 n |= nkw << 8; 3593 PyObject *names;
3541 3594 VISIT_SEQ(c, keyword, keywords);
3542 switch (code) { 3595 names = PyTuple_New(nkwelts);
3543 case 0: 3596 if (names == NULL) {
3544 ADDOP_I(c, CALL_FUNCTION, n); 3597 return 0;
3545 break; 3598 }
3546 case 1: 3599 for (i = 0; i < nkwelts; i++) {
3547 ADDOP_I(c, CALL_FUNCTION_VAR, n); 3600 keyword_ty kw = asdl_seq_GET(keywords, i);
3548 break; 3601 Py_INCREF(kw->arg);
3549 case 2: 3602 PyTuple_SET_ITEM(names, i, kw->arg);
3550 ADDOP_I(c, CALL_FUNCTION_KW, n); 3603 }
3551 break; 3604 ADDOP_N(c, LOAD_CONST, names, consts);
3552 case 3: 3605 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
3553 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n); 3606 return 1;
3554 break; 3607 }
3555 } 3608 else {
3556 return 1; 3609 ADDOP_I(c, CALL_FUNCTION, n + nelts);
3610 return 1;
3611 }
3557 } 3612 }
3558 3613
3559 3614
3560 /* List and set comprehensions and generator expressions work by creating a 3615 /* List and set comprehensions and generator expressions work by creating a
3561 nested function to perform the actual iteration. This means that the 3616 nested function to perform the actual iteration. This means that the
3562 iteration variables don't leak into the current scope. 3617 iteration variables don't leak into the current scope.
3563 The defined function is called immediately following its definition, with the 3618 The defined function is called immediately following its definition, with the
3564 result of that call being the result of the expression. 3619 result of that call being the result of the expression.
3565 The LC/SC version returns the populated container, while the GE version is 3620 The LC/SC version returns the populated container, while the GE version is
3566 flagged in symtable.c as a generator, so it returns the generator object 3621 flagged in symtable.c as a generator, so it returns the generator object
3567 when the function is called. 3622 when the function is called.
3568 This code *knows* that the loop cannot contain break, continue, or return, 3623 This code *knows* that the loop cannot contain break, continue, or return,
3569 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops. 3624 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
3570 3625
3571 Possible cleanups: 3626 Possible cleanups:
3572 - iterate over the generator sequence instead of using recursion 3627 - iterate over the generator sequence instead of using recursion
3573 */ 3628 */
3574 3629
3630
3575 static int 3631 static int
3576 compiler_comprehension_generator(struct compiler *c, 3632 compiler_comprehension_generator(struct compiler *c,
3577 asdl_seq *generators, int gen_index, 3633 asdl_seq *generators, int gen_index,
3578 expr_ty elt, expr_ty val, int type) 3634 expr_ty elt, expr_ty val, int type)
3635 {
3636 comprehension_ty gen;
3637 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
3638 if (gen->is_async) {
3639 return compiler_async_comprehension_generator(
3640 c, generators, gen_index, elt, val, type);
3641 } else {
3642 return compiler_sync_comprehension_generator(
3643 c, generators, gen_index, elt, val, type);
3644 }
3645 }
3646
3647 static int
3648 compiler_sync_comprehension_generator(struct compiler *c,
3649 asdl_seq *generators, int gen_index,
3650 expr_ty elt, expr_ty val, int type)
3579 { 3651 {
3580 /* generate code for the iterator, then each of the ifs, 3652 /* generate code for the iterator, then each of the ifs,
3581 and then write to the element */ 3653 and then write to the element */
3582 3654
3583 comprehension_ty gen; 3655 comprehension_ty gen;
3584 basicblock *start, *anchor, *skip, *if_cleanup; 3656 basicblock *start, *anchor, *skip, *if_cleanup;
3585 Py_ssize_t i, n; 3657 Py_ssize_t i, n;
3586 3658
3587 start = compiler_new_block(c); 3659 start = compiler_new_block(c);
3588 skip = compiler_new_block(c); 3660 skip = compiler_new_block(c);
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
3656 compiler_use_next_block(c, skip); 3728 compiler_use_next_block(c, skip);
3657 } 3729 }
3658 compiler_use_next_block(c, if_cleanup); 3730 compiler_use_next_block(c, if_cleanup);
3659 ADDOP_JABS(c, JUMP_ABSOLUTE, start); 3731 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3660 compiler_use_next_block(c, anchor); 3732 compiler_use_next_block(c, anchor);
3661 3733
3662 return 1; 3734 return 1;
3663 } 3735 }
3664 3736
3665 static int 3737 static int
3738 compiler_async_comprehension_generator(struct compiler *c,
3739 asdl_seq *generators, int gen_index,
3740 expr_ty elt, expr_ty val, int type)
3741 {
3742 _Py_IDENTIFIER(StopAsyncIteration);
3743
3744 comprehension_ty gen;
3745 basicblock *anchor, *skip, *if_cleanup, *try,
3746 *after_try, *except, *try_cleanup;
3747 Py_ssize_t i, n;
3748
3749 PyObject *stop_aiter_error = _PyUnicode_FromId(&PyId_StopAsyncIteration);
3750 if (stop_aiter_error == NULL) {
3751 return 0;
3752 }
3753
3754 try = compiler_new_block(c);
3755 after_try = compiler_new_block(c);
3756 try_cleanup = compiler_new_block(c);
3757 except = compiler_new_block(c);
3758 skip = compiler_new_block(c);
3759 if_cleanup = compiler_new_block(c);
3760 anchor = compiler_new_block(c);
3761
3762 if (skip == NULL || if_cleanup == NULL || anchor == NULL ||
3763 try == NULL || after_try == NULL ||
3764 except == NULL || after_try == NULL) {
3765 return 0;
3766 }
3767
3768 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
3769
3770 if (gen_index == 0) {
3771 /* Receive outermost iter as an implicit argument */
3772 c->u->u_argcount = 1;
3773 ADDOP_I(c, LOAD_FAST, 0);
3774 }
3775 else {
3776 /* Sub-iter - calculate on the fly */
3777 VISIT(c, expr, gen->iter);
3778 ADDOP(c, GET_AITER);
3779 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3780 ADDOP(c, YIELD_FROM);
3781 }
3782
3783 compiler_use_next_block(c, try);
3784
3785
3786 ADDOP_JREL(c, SETUP_EXCEPT, except);
3787 if (!compiler_push_fblock(c, EXCEPT, try))
3788 return 0;
3789
3790 ADDOP(c, GET_ANEXT);
3791 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3792 ADDOP(c, YIELD_FROM);
3793 VISIT(c, expr, gen->target);
3794 ADDOP(c, POP_BLOCK);
3795 compiler_pop_fblock(c, EXCEPT, try);
3796 ADDOP_JREL(c, JUMP_FORWARD, after_try);
3797
3798
3799 compiler_use_next_block(c, except);
3800 ADDOP(c, DUP_TOP);
3801 ADDOP_O(c, LOAD_GLOBAL, stop_aiter_error, names);
3802 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
3803 ADDOP_JABS(c, POP_JUMP_IF_FALSE, try_cleanup);
3804
3805 ADDOP(c, POP_TOP);
3806 ADDOP(c, POP_TOP);
3807 ADDOP(c, POP_TOP);
3808 ADDOP(c, POP_EXCEPT); /* for SETUP_EXCEPT */
3809 ADDOP_JABS(c, JUMP_ABSOLUTE, anchor);
3810
3811
3812 compiler_use_next_block(c, try_cleanup);
3813 ADDOP(c, END_FINALLY);
3814
3815 compiler_use_next_block(c, after_try);
3816
3817 n = asdl_seq_LEN(gen->ifs);
3818 for (i = 0; i < n; i++) {
3819 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
3820 VISIT(c, expr, e);
3821 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
3822 NEXT_BLOCK(c);
3823 }
3824
3825 if (++gen_index < asdl_seq_LEN(generators))
3826 if (!compiler_comprehension_generator(c,
3827 generators, gen_index,
3828 elt, val, type))
3829 return 0;
3830
3831 /* only append after the last for generator */
3832 if (gen_index >= asdl_seq_LEN(generators)) {
3833 /* comprehension specific code */
3834 switch (type) {
3835 case COMP_GENEXP:
3836 VISIT(c, expr, elt);
3837 ADDOP(c, YIELD_VALUE);
3838 ADDOP(c, POP_TOP);
3839 break;
3840 case COMP_LISTCOMP:
3841 VISIT(c, expr, elt);
3842 ADDOP_I(c, LIST_APPEND, gen_index + 1);
3843 break;
3844 case COMP_SETCOMP:
3845 VISIT(c, expr, elt);
3846 ADDOP_I(c, SET_ADD, gen_index + 1);
3847 break;
3848 case COMP_DICTCOMP:
3849 /* With 'd[k] = v', v is evaluated before k, so we do
3850 the same. */
3851 VISIT(c, expr, val);
3852 VISIT(c, expr, elt);
3853 ADDOP_I(c, MAP_ADD, gen_index + 1);
3854 break;
3855 default:
3856 return 0;
3857 }
3858
3859 compiler_use_next_block(c, skip);
3860 }
3861 compiler_use_next_block(c, if_cleanup);
3862 ADDOP_JABS(c, JUMP_ABSOLUTE, try);
3863 compiler_use_next_block(c, anchor);
3864 ADDOP(c, POP_TOP);
3865
3866 return 1;
3867 }
3868
3869 static int
3666 compiler_comprehension(struct compiler *c, expr_ty e, int type, 3870 compiler_comprehension(struct compiler *c, expr_ty e, int type,
3667 identifier name, asdl_seq *generators, expr_ty elt, 3871 identifier name, asdl_seq *generators, expr_ty elt,
3668 expr_ty val) 3872 expr_ty val)
3669 { 3873 {
3670 PyCodeObject *co = NULL; 3874 PyCodeObject *co = NULL;
3671 expr_ty outermost_iter; 3875 comprehension_ty outermost;
3672 PyObject *qualname = NULL; 3876 PyObject *qualname = NULL;
3673 3877 int is_async_function = c->u->u_ste->ste_coroutine;
3674 outermost_iter = ((comprehension_ty) 3878 int is_async_generator = 0;
3675 asdl_seq_GET(generators, 0))->iter; 3879
3880 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
3676 3881
3677 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, 3882 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
3678 (void *)e, e->lineno)) 3883 (void *)e, e->lineno))
3884 {
3679 goto error; 3885 goto error;
3886 }
3887
3888 is_async_generator = c->u->u_ste->ste_coroutine;
3889
3890 if (is_async_generator && !is_async_function) {
3891 if (e->lineno > c->u->u_lineno) {
3892 c->u->u_lineno = e->lineno;
3893 c->u->u_lineno_set = 0;
3894 }
3895 compiler_error(c, "asynchronous comprehension outside of "
3896 "an asynchronous function");
3897 goto error_in_scope;
3898 }
3680 3899
3681 if (type != COMP_GENEXP) { 3900 if (type != COMP_GENEXP) {
3682 int op; 3901 int op;
3683 switch (type) { 3902 switch (type) {
3684 case COMP_LISTCOMP: 3903 case COMP_LISTCOMP:
3685 op = BUILD_LIST; 3904 op = BUILD_LIST;
3686 break; 3905 break;
3687 case COMP_SETCOMP: 3906 case COMP_SETCOMP:
3688 op = BUILD_SET; 3907 op = BUILD_SET;
3689 break; 3908 break;
(...skipping 22 matching lines...) Expand all
3712 Py_INCREF(qualname); 3931 Py_INCREF(qualname);
3713 compiler_exit_scope(c); 3932 compiler_exit_scope(c);
3714 if (co == NULL) 3933 if (co == NULL)
3715 goto error; 3934 goto error;
3716 3935
3717 if (!compiler_make_closure(c, co, 0, qualname)) 3936 if (!compiler_make_closure(c, co, 0, qualname))
3718 goto error; 3937 goto error;
3719 Py_DECREF(qualname); 3938 Py_DECREF(qualname);
3720 Py_DECREF(co); 3939 Py_DECREF(co);
3721 3940
3722 VISIT(c, expr, outermost_iter); 3941 VISIT(c, expr, outermost->iter);
3723 ADDOP(c, GET_ITER); 3942
3943 if (outermost->is_async) {
3944 ADDOP(c, GET_AITER);
3945 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3946 ADDOP(c, YIELD_FROM);
3947 } else {
3948 ADDOP(c, GET_ITER);
3949 }
3950
3724 ADDOP_I(c, CALL_FUNCTION, 1); 3951 ADDOP_I(c, CALL_FUNCTION, 1);
3952
3953 if (is_async_generator && type != COMP_GENEXP) {
3954 ADDOP(c, GET_AWAITABLE);
3955 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3956 ADDOP(c, YIELD_FROM);
3957 }
3958
3725 return 1; 3959 return 1;
3726 error_in_scope: 3960 error_in_scope:
3727 compiler_exit_scope(c); 3961 compiler_exit_scope(c);
3728 error: 3962 error:
3729 Py_XDECREF(qualname); 3963 Py_XDECREF(qualname);
3730 Py_XDECREF(co); 3964 Py_XDECREF(co);
3731 return 0; 3965 return 0;
3732 } 3966 }
3733 3967
3734 static int 3968 static int
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
3789 assert(e->kind == DictComp_kind); 4023 assert(e->kind == DictComp_kind);
3790 return compiler_comprehension(c, e, COMP_DICTCOMP, name, 4024 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
3791 e->v.DictComp.generators, 4025 e->v.DictComp.generators,
3792 e->v.DictComp.key, e->v.DictComp.value); 4026 e->v.DictComp.key, e->v.DictComp.value);
3793 } 4027 }
3794 4028
3795 4029
3796 static int 4030 static int
3797 compiler_visit_keyword(struct compiler *c, keyword_ty k) 4031 compiler_visit_keyword(struct compiler *c, keyword_ty k)
3798 { 4032 {
3799 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3800 VISIT(c, expr, k->value); 4033 VISIT(c, expr, k->value);
3801 return 1; 4034 return 1;
3802 } 4035 }
3803 4036
3804 /* Test whether expression is constant. For constants, report 4037 /* Test whether expression is constant. For constants, report
3805 whether they are true or false. 4038 whether they are true or false.
3806 4039
3807 Return values: 1 for true, 0 for false, -1 for non-constant. 4040 Return values: 1 for true, 0 for false, -1 for non-constant.
3808 */ 4041 */
3809 4042
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
4054 return compiler_genexp(c, e); 4287 return compiler_genexp(c, e);
4055 case ListComp_kind: 4288 case ListComp_kind:
4056 return compiler_listcomp(c, e); 4289 return compiler_listcomp(c, e);
4057 case SetComp_kind: 4290 case SetComp_kind:
4058 return compiler_setcomp(c, e); 4291 return compiler_setcomp(c, e);
4059 case DictComp_kind: 4292 case DictComp_kind:
4060 return compiler_dictcomp(c, e); 4293 return compiler_dictcomp(c, e);
4061 case Yield_kind: 4294 case Yield_kind:
4062 if (c->u->u_ste->ste_type != FunctionBlock) 4295 if (c->u->u_ste->ste_type != FunctionBlock)
4063 return compiler_error(c, "'yield' outside function"); 4296 return compiler_error(c, "'yield' outside function");
4064 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
4065 return compiler_error(c, "'yield' inside async function");
4066 if (e->v.Yield.value) { 4297 if (e->v.Yield.value) {
4067 VISIT(c, expr, e->v.Yield.value); 4298 VISIT(c, expr, e->v.Yield.value);
4068 } 4299 }
4069 else { 4300 else {
4070 ADDOP_O(c, LOAD_CONST, Py_None, consts); 4301 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4071 } 4302 }
4072 ADDOP(c, YIELD_VALUE); 4303 ADDOP(c, YIELD_VALUE);
4073 break; 4304 break;
4074 case YieldFrom_kind: 4305 case YieldFrom_kind:
4075 if (c->u->u_ste->ste_type != FunctionBlock) 4306 if (c->u->u_ste->ste_type != FunctionBlock)
4076 return compiler_error(c, "'yield' outside function"); 4307 return compiler_error(c, "'yield' outside function");
4077 4308
4078 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) 4309 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
4079 return compiler_error(c, "'yield from' inside async function"); 4310 return compiler_error(c, "'yield from' inside async function");
4080 4311
4081 VISIT(c, expr, e->v.YieldFrom.value); 4312 VISIT(c, expr, e->v.YieldFrom.value);
4082 ADDOP(c, GET_YIELD_FROM_ITER); 4313 ADDOP(c, GET_YIELD_FROM_ITER);
4083 ADDOP_O(c, LOAD_CONST, Py_None, consts); 4314 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4084 ADDOP(c, YIELD_FROM); 4315 ADDOP(c, YIELD_FROM);
4085 break; 4316 break;
4086 case Await_kind: 4317 case Await_kind:
4087 if (c->u->u_ste->ste_type != FunctionBlock) 4318 if (c->u->u_ste->ste_type != FunctionBlock)
4088 return compiler_error(c, "'await' outside function"); 4319 return compiler_error(c, "'await' outside function");
4089 4320
4090 if (c->u->u_scope_type == COMPILER_SCOPE_COMPREHENSION) 4321 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
4091 return compiler_error( 4322 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION)
4092 c, "'await' expressions in comprehensions are not supported");
4093
4094 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION)
4095 return compiler_error(c, "'await' outside async function"); 4323 return compiler_error(c, "'await' outside async function");
4096 4324
4097 VISIT(c, expr, e->v.Await.value); 4325 VISIT(c, expr, e->v.Await.value);
4098 ADDOP(c, GET_AWAITABLE); 4326 ADDOP(c, GET_AWAITABLE);
4099 ADDOP_O(c, LOAD_CONST, Py_None, consts); 4327 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4100 ADDOP(c, YIELD_FROM); 4328 ADDOP(c, YIELD_FROM);
4101 break; 4329 break;
4102 case Compare_kind: 4330 case Compare_kind:
4103 return compiler_compare(c, e); 4331 return compiler_compare(c, e);
4104 case Call_kind: 4332 case Call_kind:
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
4243 default: 4471 default:
4244 PyErr_Format(PyExc_SystemError, 4472 PyErr_Format(PyExc_SystemError,
4245 "invalid node type (%d) for augmented assignment", 4473 "invalid node type (%d) for augmented assignment",
4246 e->kind); 4474 e->kind);
4247 return 0; 4475 return 0;
4248 } 4476 }
4249 return 1; 4477 return 1;
4250 } 4478 }
4251 4479
4252 static int 4480 static int
4481 check_ann_expr(struct compiler *c, expr_ty e)
4482 {
4483 VISIT(c, expr, e);
4484 ADDOP(c, POP_TOP);
4485 return 1;
4486 }
4487
4488 static int
4489 check_annotation(struct compiler *c, stmt_ty s)
4490 {
4491 /* Annotations are only evaluated in a module or class. */
4492 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
4493 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
4494 return check_ann_expr(c, s->v.AnnAssign.annotation);
4495 }
4496 return 1;
4497 }
4498
4499 static int
4500 check_ann_slice(struct compiler *c, slice_ty sl)
4501 {
4502 switch(sl->kind) {
4503 case Index_kind:
4504 return check_ann_expr(c, sl->v.Index.value);
4505 case Slice_kind:
4506 if (sl->v.Slice.lower && !check_ann_expr(c, sl->v.Slice.lower)) {
4507 return 0;
4508 }
4509 if (sl->v.Slice.upper && !check_ann_expr(c, sl->v.Slice.upper)) {
4510 return 0;
4511 }
4512 if (sl->v.Slice.step && !check_ann_expr(c, sl->v.Slice.step)) {
4513 return 0;
4514 }
4515 break;
4516 default:
4517 PyErr_SetString(PyExc_SystemError,
4518 "unexpected slice kind");
4519 return 0;
4520 }
4521 return 1;
4522 }
4523
4524 static int
4525 check_ann_subscr(struct compiler *c, slice_ty sl)
4526 {
4527 /* We check that everything in a subscript is defined at runtime. */
4528 Py_ssize_t i, n;
4529
4530 switch (sl->kind) {
4531 case Index_kind:
4532 case Slice_kind:
4533 if (!check_ann_slice(c, sl)) {
4534 return 0;
4535 }
4536 break;
4537 case ExtSlice_kind:
4538 n = asdl_seq_LEN(sl->v.ExtSlice.dims);
4539 for (i = 0; i < n; i++) {
4540 slice_ty subsl = (slice_ty)asdl_seq_GET(sl->v.ExtSlice.dims, i);
4541 switch (subsl->kind) {
4542 case Index_kind:
4543 case Slice_kind:
4544 if (!check_ann_slice(c, subsl)) {
4545 return 0;
4546 }
4547 break;
4548 case ExtSlice_kind:
4549 default:
4550 PyErr_SetString(PyExc_SystemError,
4551 "extended slice invalid in nested slice");
4552 return 0;
4553 }
4554 }
4555 break;
4556 default:
4557 PyErr_Format(PyExc_SystemError,
4558 "invalid subscript kind %d", sl->kind);
4559 return 0;
4560 }
4561 return 1;
4562 }
4563
4564 static int
4565 compiler_annassign(struct compiler *c, stmt_ty s)
4566 {
4567 expr_ty targ = s->v.AnnAssign.target;
4568 PyObject* mangled;
4569
4570 assert(s->kind == AnnAssign_kind);
4571
4572 /* We perform the actual assignment first. */
4573 if (s->v.AnnAssign.value) {
4574 VISIT(c, expr, s->v.AnnAssign.value);
4575 VISIT(c, expr, targ);
4576 }
4577 switch (targ->kind) {
4578 case Name_kind:
4579 /* If we have a simple name in a module or class, store annotation. */
4580 if (s->v.AnnAssign.simple &&
4581 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
4582 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
4583 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
4584 if (!mangled) {
4585 return 0;
4586 }
4587 VISIT(c, expr, s->v.AnnAssign.annotation);
4588 /* ADDOP_N decrefs its argument */
4589 ADDOP_N(c, STORE_ANNOTATION, mangled, names);
4590 }
4591 break;
4592 case Attribute_kind:
4593 if (!s->v.AnnAssign.value &&
4594 !check_ann_expr(c, targ->v.Attribute.value)) {
4595 return 0;
4596 }
4597 break;
4598 case Subscript_kind:
4599 if (!s->v.AnnAssign.value &&
4600 (!check_ann_expr(c, targ->v.Subscript.value) ||
4601 !check_ann_subscr(c, targ->v.Subscript.slice))) {
4602 return 0;
4603 }
4604 break;
4605 default:
4606 PyErr_Format(PyExc_SystemError,
4607 "invalid node type (%d) for annotated assignment",
4608 targ->kind);
4609 return 0;
4610 }
4611 /* Annotation is evaluated last. */
4612 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
4613 return 0;
4614 }
4615 return 1;
4616 }
4617
4618 static int
4253 compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b) 4619 compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
4254 { 4620 {
4255 struct fblockinfo *f; 4621 struct fblockinfo *f;
4256 if (c->u->u_nfblocks >= CO_MAXBLOCKS) { 4622 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
4257 PyErr_SetString(PyExc_SyntaxError, 4623 PyErr_SetString(PyExc_SyntaxError,
4258 "too many statically nested blocks"); 4624 "too many statically nested blocks");
4259 return 0; 4625 return 0;
4260 } 4626 }
4261 f = &c->u->u_fblock[c->u->u_nfblocks++]; 4627 f = &c->u->u_fblock[c->u->u_nfblocks++];
4262 f->fb_type = t; 4628 f->fb_type = t;
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
4540 assemble_init(struct assembler *a, int nblocks, int firstlineno) 4906 assemble_init(struct assembler *a, int nblocks, int firstlineno)
4541 { 4907 {
4542 memset(a, 0, sizeof(struct assembler)); 4908 memset(a, 0, sizeof(struct assembler));
4543 a->a_lineno = firstlineno; 4909 a->a_lineno = firstlineno;
4544 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); 4910 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
4545 if (!a->a_bytecode) 4911 if (!a->a_bytecode)
4546 return 0; 4912 return 0;
4547 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); 4913 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
4548 if (!a->a_lnotab) 4914 if (!a->a_lnotab)
4549 return 0; 4915 return 0;
4550 if ((size_t)nblocks > PY_SIZE_MAX / sizeof(basicblock *)) { 4916 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
4551 PyErr_NoMemory(); 4917 PyErr_NoMemory();
4552 return 0; 4918 return 0;
4553 } 4919 }
4554 a->a_postorder = (basicblock **)PyObject_Malloc( 4920 a->a_postorder = (basicblock **)PyObject_Malloc(
4555 sizeof(basicblock *) * nblocks); 4921 sizeof(basicblock *) * nblocks);
4556 if (!a->a_postorder) { 4922 if (!a->a_postorder) {
4557 PyErr_NoMemory(); 4923 PyErr_NoMemory();
4558 return 0; 4924 return 0;
4559 } 4925 }
4560 return 1; 4926 return 1;
(...skipping 23 matching lines...) Expand all
4584 the instruction's bytecode offset and line number. See 4950 the instruction's bytecode offset and line number. See
4585 Objects/lnotab_notes.txt for the description of the line number table. */ 4951 Objects/lnotab_notes.txt for the description of the line number table. */
4586 4952
4587 static int 4953 static int
4588 assemble_lnotab(struct assembler *a, struct instr *i) 4954 assemble_lnotab(struct assembler *a, struct instr *i)
4589 { 4955 {
4590 int d_bytecode, d_lineno; 4956 int d_bytecode, d_lineno;
4591 Py_ssize_t len; 4957 Py_ssize_t len;
4592 unsigned char *lnotab; 4958 unsigned char *lnotab;
4593 4959
4594 d_bytecode = a->a_offset - a->a_lineno_off; 4960 d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT);
4595 d_lineno = i->i_lineno - a->a_lineno; 4961 d_lineno = i->i_lineno - a->a_lineno;
4596 4962
4597 assert(d_bytecode >= 0); 4963 assert(d_bytecode >= 0);
4598 4964
4599 if(d_bytecode == 0 && d_lineno == 0) 4965 if(d_bytecode == 0 && d_lineno == 0)
4600 return 1; 4966 return 1;
4601 4967
4602 if (d_bytecode > 255) { 4968 if (d_bytecode > 255) {
4603 int j, nbytes, ncodes = d_bytecode / 255; 4969 int j, nbytes, ncodes = d_bytecode / 255;
4604 nbytes = a->a_lnotab_off + 2 * ncodes; 4970 nbytes = a->a_lnotab_off + 2 * ncodes;
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
4691 /* assemble_emit() 5057 /* assemble_emit()
4692 Extend the bytecode with a new instruction. 5058 Extend the bytecode with a new instruction.
4693 Update lnotab if necessary. 5059 Update lnotab if necessary.
4694 */ 5060 */
4695 5061
4696 static int 5062 static int
4697 assemble_emit(struct assembler *a, struct instr *i) 5063 assemble_emit(struct assembler *a, struct instr *i)
4698 { 5064 {
4699 int size, arg = 0; 5065 int size, arg = 0;
4700 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); 5066 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
4701 char *code; 5067 _Py_CODEUNIT *code;
4702 5068
4703 arg = i->i_oparg; 5069 arg = i->i_oparg;
4704 size = instrsize(arg); 5070 size = instrsize(arg);
4705 if (i->i_lineno && !assemble_lnotab(a, i)) 5071 if (i->i_lineno && !assemble_lnotab(a, i))
4706 return 0; 5072 return 0;
4707 if (a->a_offset + size >= len) { 5073 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
4708 if (len > PY_SSIZE_T_MAX / 2) 5074 if (len > PY_SSIZE_T_MAX / 2)
4709 return 0; 5075 return 0;
4710 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) 5076 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
4711 return 0; 5077 return 0;
4712 } 5078 }
4713 code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; 5079 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
4714 a->a_offset += size; 5080 a->a_offset += size;
4715 write_op_arg((unsigned char*)code, i->i_opcode, arg, size); 5081 write_op_arg(code, i->i_opcode, arg, size);
4716 return 1; 5082 return 1;
4717 } 5083 }
4718 5084
4719 static void 5085 static void
4720 assemble_jump_offsets(struct assembler *a, struct compiler *c) 5086 assemble_jump_offsets(struct assembler *a, struct compiler *c)
4721 { 5087 {
4722 basicblock *b; 5088 basicblock *b;
4723 int bsize, totsize, extended_arg_recompile; 5089 int bsize, totsize, extended_arg_recompile;
4724 int i; 5090 int i;
4725 5091
(...skipping 16 matching lines...) Expand all
4742 /* Relative jumps are computed relative to 5108 /* Relative jumps are computed relative to
4743 the instruction pointer after fetching 5109 the instruction pointer after fetching
4744 the jump instruction. 5110 the jump instruction.
4745 */ 5111 */
4746 bsize += isize; 5112 bsize += isize;
4747 if (instr->i_jabs || instr->i_jrel) { 5113 if (instr->i_jabs || instr->i_jrel) {
4748 instr->i_oparg = instr->i_target->b_offset; 5114 instr->i_oparg = instr->i_target->b_offset;
4749 if (instr->i_jrel) { 5115 if (instr->i_jrel) {
4750 instr->i_oparg -= bsize; 5116 instr->i_oparg -= bsize;
4751 } 5117 }
5118 instr->i_oparg *= sizeof(_Py_CODEUNIT);
4752 if (instrsize(instr->i_oparg) != isize) { 5119 if (instrsize(instr->i_oparg) != isize) {
4753 extended_arg_recompile = 1; 5120 extended_arg_recompile = 1;
4754 } 5121 }
4755 } 5122 }
4756 } 5123 }
4757 } 5124 }
4758 5125
4759 /* XXX: This is an awful hack that could hurt performance, but 5126 /* XXX: This is an awful hack that could hurt performance, but
4760 on the bright side it should work until we come up 5127 on the bright side it should work until we come up
4761 with a better solution. 5128 with a better solution.
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
4799 static int 5166 static int
4800 compute_code_flags(struct compiler *c) 5167 compute_code_flags(struct compiler *c)
4801 { 5168 {
4802 PySTEntryObject *ste = c->u->u_ste; 5169 PySTEntryObject *ste = c->u->u_ste;
4803 int flags = 0; 5170 int flags = 0;
4804 Py_ssize_t n; 5171 Py_ssize_t n;
4805 if (ste->ste_type == FunctionBlock) { 5172 if (ste->ste_type == FunctionBlock) {
4806 flags |= CO_NEWLOCALS | CO_OPTIMIZED; 5173 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
4807 if (ste->ste_nested) 5174 if (ste->ste_nested)
4808 flags |= CO_NESTED; 5175 flags |= CO_NESTED;
4809 if (ste->ste_generator) 5176 if (ste->ste_generator && !ste->ste_coroutine)
4810 flags |= CO_GENERATOR; 5177 flags |= CO_GENERATOR;
5178 if (!ste->ste_generator && ste->ste_coroutine)
5179 flags |= CO_COROUTINE;
5180 if (ste->ste_generator && ste->ste_coroutine)
5181 flags |= CO_ASYNC_GENERATOR;
4811 if (ste->ste_varargs) 5182 if (ste->ste_varargs)
4812 flags |= CO_VARARGS; 5183 flags |= CO_VARARGS;
4813 if (ste->ste_varkeywords) 5184 if (ste->ste_varkeywords)
4814 flags |= CO_VARKEYWORDS; 5185 flags |= CO_VARKEYWORDS;
4815 } 5186 }
4816 5187
4817 /* (Only) inherit compilerflags in PyCF_MASK */ 5188 /* (Only) inherit compilerflags in PyCF_MASK */
4818 flags |= (c->c_flags->cf_flags & PyCF_MASK); 5189 flags |= (c->c_flags->cf_flags & PyCF_MASK);
4819 5190
4820 n = PyDict_Size(c->u->u_freevars); 5191 n = PyDict_Size(c->u->u_freevars);
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
4961 5332
4962 nblocks = 0; 5333 nblocks = 0;
4963 entryblock = NULL; 5334 entryblock = NULL;
4964 for (b = c->u->u_blocks; b != NULL; b = b->b_list) { 5335 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4965 nblocks++; 5336 nblocks++;
4966 entryblock = b; 5337 entryblock = b;
4967 } 5338 }
4968 5339
4969 /* Set firstlineno if it wasn't explicitly set. */ 5340 /* Set firstlineno if it wasn't explicitly set. */
4970 if (!c->u->u_firstlineno) { 5341 if (!c->u->u_firstlineno) {
4971 if (entryblock && entryblock->b_instr) 5342 if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno)
4972 c->u->u_firstlineno = entryblock->b_instr->i_lineno; 5343 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4973 else 5344 else
4974 c->u->u_firstlineno = 1; 5345 c->u->u_firstlineno = 1;
4975 } 5346 }
4976 if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) 5347 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4977 goto error; 5348 goto error;
4978 dfs(c, entryblock, &a); 5349 dfs(c, entryblock, &a);
4979 5350
4980 /* Can't modify the bytecode after computing jump offsets. */ 5351 /* Can't modify the bytecode after computing jump offsets. */
4981 assemble_jump_offsets(&a, c); 5352 assemble_jump_offsets(&a, c);
4982 5353
4983 /* Emit code in reverse postorder from dfs. */ 5354 /* Emit code in reverse postorder from dfs. */
4984 for (i = a.a_nblocks - 1; i >= 0; i--) { 5355 for (i = a.a_nblocks - 1; i >= 0; i--) {
4985 b = a.a_postorder[i]; 5356 b = a.a_postorder[i];
4986 for (j = 0; j < b->b_iused; j++) 5357 for (j = 0; j < b->b_iused; j++)
4987 if (!assemble_emit(&a, &b->b_instr[j])) 5358 if (!assemble_emit(&a, &b->b_instr[j]))
4988 goto error; 5359 goto error;
4989 } 5360 }
4990 5361
4991 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) 5362 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4992 goto error; 5363 goto error;
4993 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset) < 0) 5364 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0)
4994 goto error; 5365 goto error;
4995 5366
4996 co = makecode(c, &a); 5367 co = makecode(c, &a);
4997 error: 5368 error:
4998 assemble_free(&a); 5369 assemble_free(&a);
4999 return co; 5370 return co;
5000 } 5371 }
5001 5372
5002 #undef PyAST_Compile 5373 #undef PyAST_Compile
5003 PyAPI_FUNC(PyCodeObject *) 5374 PyAPI_FUNC(PyCodeObject *)
5004 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, 5375 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
5005 PyArena *arena) 5376 PyArena *arena)
5006 { 5377 {
5007 return PyAST_CompileEx(mod, filename, flags, -1, arena); 5378 return PyAST_CompileEx(mod, filename, flags, -1, arena);
5008 } 5379 }
LEFTRIGHT

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