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

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, 4 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__");
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);
1913 ADDOP_O(c, LOAD_CONST, Py_None, consts); 2000 }
1914 ADDOP_IN_SCOPE(c, RETURN_VALUE); 2001 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1915 /* create the code object */ 2002 /* create the code object */
1916 co = assemble(c, 1); 2003 co = assemble(c, 1);
1917 } 2004 }
1918 /* leave the new scope */ 2005 /* leave the new scope */
1919 compiler_exit_scope(c); 2006 compiler_exit_scope(c);
1920 if (co == NULL) 2007 if (co == NULL)
1921 return 0; 2008 return 0;
1922 2009
1923 /* 2. load the 'build_class' function */ 2010 /* 2. load the 'build_class' function */
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
2087 compiler_pop_fblock(c, LOOP, start); 2174 compiler_pop_fblock(c, LOOP, start);
2088 VISIT_SEQ(c, stmt, s->v.For.orelse); 2175 VISIT_SEQ(c, stmt, s->v.For.orelse);
2089 compiler_use_next_block(c, end); 2176 compiler_use_next_block(c, end);
2090 return 1; 2177 return 1;
2091 } 2178 }
2092 2179
2093 2180
2094 static int 2181 static int
2095 compiler_async_for(struct compiler *c, stmt_ty s) 2182 compiler_async_for(struct compiler *c, stmt_ty s)
2096 { 2183 {
2097 static PyObject *stopiter_error = NULL; 2184 _Py_IDENTIFIER(StopAsyncIteration);
2185
2098 basicblock *try, *except, *end, *after_try, *try_cleanup, 2186 basicblock *try, *except, *end, *after_try, *try_cleanup,
2099 *after_loop, *after_loop_else; 2187 *after_loop, *after_loop_else;
2100 2188
2101 if (stopiter_error == NULL) { 2189 PyObject *stop_aiter_error = _PyUnicode_FromId(&PyId_StopAsyncIteration);
2102 stopiter_error = PyUnicode_InternFromString("StopAsyncIteration"); 2190 if (stop_aiter_error == NULL) {
2103 if (stopiter_error == NULL) 2191 return 0;
2104 return 0;
2105 } 2192 }
2106 2193
2107 try = compiler_new_block(c); 2194 try = compiler_new_block(c);
2108 except = compiler_new_block(c); 2195 except = compiler_new_block(c);
2109 end = compiler_new_block(c); 2196 end = compiler_new_block(c);
2110 after_try = compiler_new_block(c); 2197 after_try = compiler_new_block(c);
2111 try_cleanup = compiler_new_block(c); 2198 try_cleanup = compiler_new_block(c);
2112 after_loop = compiler_new_block(c); 2199 after_loop = compiler_new_block(c);
2113 after_loop_else = compiler_new_block(c); 2200 after_loop_else = compiler_new_block(c);
2114 2201
(...skipping 21 matching lines...) Expand all
2136 ADDOP_O(c, LOAD_CONST, Py_None, consts); 2223 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2137 ADDOP(c, YIELD_FROM); 2224 ADDOP(c, YIELD_FROM);
2138 VISIT(c, expr, s->v.AsyncFor.target); 2225 VISIT(c, expr, s->v.AsyncFor.target);
2139 ADDOP(c, POP_BLOCK); 2226 ADDOP(c, POP_BLOCK);
2140 compiler_pop_fblock(c, EXCEPT, try); 2227 compiler_pop_fblock(c, EXCEPT, try);
2141 ADDOP_JREL(c, JUMP_FORWARD, after_try); 2228 ADDOP_JREL(c, JUMP_FORWARD, after_try);
2142 2229
2143 2230
2144 compiler_use_next_block(c, except); 2231 compiler_use_next_block(c, except);
2145 ADDOP(c, DUP_TOP); 2232 ADDOP(c, DUP_TOP);
2146 ADDOP_O(c, LOAD_GLOBAL, stopiter_error, names); 2233 ADDOP_O(c, LOAD_GLOBAL, stop_aiter_error, names);
2147 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH); 2234 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2148 ADDOP_JABS(c, POP_JUMP_IF_FALSE, try_cleanup); 2235 ADDOP_JABS(c, POP_JUMP_IF_FALSE, try_cleanup);
2149 2236
2150 ADDOP(c, POP_TOP); 2237 ADDOP(c, POP_TOP);
2151 ADDOP(c, POP_TOP); 2238 ADDOP(c, POP_TOP);
2152 ADDOP(c, POP_TOP); 2239 ADDOP(c, POP_TOP);
2153 ADDOP(c, POP_EXCEPT); /* for SETUP_EXCEPT */ 2240 ADDOP(c, POP_EXCEPT); /* for SETUP_EXCEPT */
2154 ADDOP(c, POP_BLOCK); /* for SETUP_LOOP */ 2241 ADDOP(c, POP_BLOCK); /* for SETUP_LOOP */
2155 ADDOP_JABS(c, JUMP_ABSOLUTE, after_loop_else); 2242 ADDOP_JABS(c, JUMP_ABSOLUTE, after_loop_else);
2156 2243
(...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after
2598 } 2685 }
2599 2686
2600 /* build up the names */ 2687 /* build up the names */
2601 for (i = 0; i < n; i++) { 2688 for (i = 0; i < n; i++) {
2602 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);
2603 Py_INCREF(alias->name); 2690 Py_INCREF(alias->name);
2604 PyTuple_SET_ITEM(names, i, alias->name); 2691 PyTuple_SET_ITEM(names, i, alias->name);
2605 } 2692 }
2606 2693
2607 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 &&
2608 !PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module, "__future__")) { 2695 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
2609 Py_DECREF(level); 2696 Py_DECREF(level);
2610 Py_DECREF(names); 2697 Py_DECREF(names);
2611 return compiler_error(c, "from __future__ imports must occur " 2698 return compiler_error(c, "from __future__ imports must occur "
2612 "at the beginning of the file"); 2699 "at the beginning of the file");
2613 } 2700 }
2614 2701
2615 ADDOP_O(c, LOAD_CONST, level, consts); 2702 ADDOP_O(c, LOAD_CONST, level, consts);
2616 Py_DECREF(level); 2703 Py_DECREF(level);
2617 ADDOP_O(c, LOAD_CONST, names, consts); 2704 ADDOP_O(c, LOAD_CONST, names, consts);
2618 Py_DECREF(names); 2705 Py_DECREF(names);
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
2721 2808
2722 switch (s->kind) { 2809 switch (s->kind) {
2723 case FunctionDef_kind: 2810 case FunctionDef_kind:
2724 return compiler_function(c, s, 0); 2811 return compiler_function(c, s, 0);
2725 case ClassDef_kind: 2812 case ClassDef_kind:
2726 return compiler_class(c, s); 2813 return compiler_class(c, s);
2727 case Return_kind: 2814 case Return_kind:
2728 if (c->u->u_ste->ste_type != FunctionBlock) 2815 if (c->u->u_ste->ste_type != FunctionBlock)
2729 return compiler_error(c, "'return' outside function"); 2816 return compiler_error(c, "'return' outside function");
2730 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");
2731 VISIT(c, expr, s->v.Return.value); 2821 VISIT(c, expr, s->v.Return.value);
2732 } 2822 }
2733 else 2823 else
2734 ADDOP_O(c, LOAD_CONST, Py_None, consts); 2824 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2735 ADDOP(c, RETURN_VALUE); 2825 ADDOP(c, RETURN_VALUE);
2736 break; 2826 break;
2737 case Delete_kind: 2827 case Delete_kind:
2738 VISIT_SEQ(c, expr, s->v.Delete.targets) 2828 VISIT_SEQ(c, expr, s->v.Delete.targets)
2739 break; 2829 break;
2740 case Assign_kind: 2830 case Assign_kind:
2741 n = asdl_seq_LEN(s->v.Assign.targets); 2831 n = asdl_seq_LEN(s->v.Assign.targets);
2742 VISIT(c, expr, s->v.Assign.value); 2832 VISIT(c, expr, s->v.Assign.value);
2743 for (i = 0; i < n; i++) { 2833 for (i = 0; i < n; i++) {
2744 if (i < n - 1) 2834 if (i < n - 1)
2745 ADDOP(c, DUP_TOP); 2835 ADDOP(c, DUP_TOP);
2746 VISIT(c, expr, 2836 VISIT(c, expr,
2747 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); 2837 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2748 } 2838 }
2749 break; 2839 break;
2750 case AugAssign_kind: 2840 case AugAssign_kind:
2751 return compiler_augassign(c, s); 2841 return compiler_augassign(c, s);
2842 case AnnAssign_kind:
2843 return compiler_annassign(c, s);
2752 case For_kind: 2844 case For_kind:
2753 return compiler_for(c, s); 2845 return compiler_for(c, s);
2754 case While_kind: 2846 case While_kind:
2755 return compiler_while(c, s); 2847 return compiler_while(c, s);
2756 case If_kind: 2848 case If_kind:
2757 return compiler_if(c, s); 2849 return compiler_if(c, s);
2758 case Raise_kind: 2850 case Raise_kind:
2759 n = 0; 2851 n = 0;
2760 if (s->v.Raise.exc) { 2852 if (s->v.Raise.exc) {
2761 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
2932 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; 3024 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2933 3025
2934 PyObject *dict = c->u->u_names; 3026 PyObject *dict = c->u->u_names;
2935 PyObject *mangled; 3027 PyObject *mangled;
2936 /* XXX AugStore isn't used anywhere! */ 3028 /* XXX AugStore isn't used anywhere! */
2937 3029
2938 mangled = _Py_Mangle(c->u->u_private, name); 3030 mangled = _Py_Mangle(c->u->u_private, name);
2939 if (!mangled) 3031 if (!mangled)
2940 return 0; 3032 return 0;
2941 3033
2942 assert(PyUnicode_CompareWithASCIIString(name, "None") && 3034 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
2943 PyUnicode_CompareWithASCIIString(name, "True") && 3035 !_PyUnicode_EqualToASCIIString(name, "True") &&
2944 PyUnicode_CompareWithASCIIString(name, "False")); 3036 !_PyUnicode_EqualToASCIIString(name, "False"));
2945 3037
2946 op = 0; 3038 op = 0;
2947 optype = OP_NAME; 3039 optype = OP_NAME;
2948 scope = PyST_GetScope(c->u->u_ste, mangled); 3040 scope = PyST_GetScope(c->u->u_ste, mangled);
2949 switch (scope) { 3041 switch (scope) {
2950 case FREE: 3042 case FREE:
2951 dict = c->u->u_freevars; 3043 dict = c->u->u_freevars;
2952 optype = OP_DEREF; 3044 optype = OP_DEREF;
2953 break; 3045 break;
2954 case CELL: 3046 case CELL:
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after
3315 { 3407 {
3316 VISIT(c, expr, e->v.Call.func); 3408 VISIT(c, expr, e->v.Call.func);
3317 return compiler_call_helper(c, 0, 3409 return compiler_call_helper(c, 0,
3318 e->v.Call.args, 3410 e->v.Call.args,
3319 e->v.Call.keywords); 3411 e->v.Call.keywords);
3320 } 3412 }
3321 3413
3322 static int 3414 static int
3323 compiler_joined_str(struct compiler *c, expr_ty e) 3415 compiler_joined_str(struct compiler *c, expr_ty e)
3324 { 3416 {
3325 /* Concatenate parts of a string using ''.join(parts). There are
3326 probably better ways of doing this.
3327
3328 This is used for constructs like "'x=' f'{42}'", which have to
3329 be evaluated at compile time. */
3330
3331 static PyObject *empty_string;
3332 static PyObject *join_string;
3333
3334 if (!empty_string) {
3335 empty_string = PyUnicode_FromString("");
3336 if (!empty_string)
3337 return 0;
3338 }
3339 if (!join_string) {
3340 join_string = PyUnicode_FromString("join");
3341 if (!join_string)
3342 return 0;
3343 }
3344
3345 ADDOP_O(c, LOAD_CONST, empty_string, consts);
3346 ADDOP_NAME(c, LOAD_ATTR, join_string, names);
3347 VISIT_SEQ(c, expr, e->v.JoinedStr.values); 3417 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
3348 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));
3349 ADDOP_I(c, CALL_FUNCTION, 1);
3350 return 1; 3419 return 1;
3351 } 3420 }
3352 3421
3353 /* Used to implement f-strings. Format a single value. */ 3422 /* Used to implement f-strings. Format a single value. */
3354 static int 3423 static int
3355 compiler_formatted_value(struct compiler *c, expr_ty e) 3424 compiler_formatted_value(struct compiler *c, expr_ty e)
3356 { 3425 {
3357 /* Our oparg encodes 2 pieces of information: the conversion 3426 /* Our oparg encodes 2 pieces of information: the conversion
3358 character, and whether or not a format_spec was provided. 3427 character, and whether or not a format_spec was provided.
3359 3428
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
3430 return 1; 3499 return 1;
3431 } 3500 }
3432 3501
3433 /* shared code between compiler_call and compiler_class */ 3502 /* shared code between compiler_call and compiler_class */
3434 static int 3503 static int
3435 compiler_call_helper(struct compiler *c, 3504 compiler_call_helper(struct compiler *c,
3436 int n, /* Args already pushed */ 3505 int n, /* Args already pushed */
3437 asdl_seq *args, 3506 asdl_seq *args,
3438 asdl_seq *keywords) 3507 asdl_seq *keywords)
3439 { 3508 {
3440 int code = 0; 3509 Py_ssize_t i, nseen, nelts, nkwelts;
3441 Py_ssize_t nelts, i, nseen; 3510 int mustdictunpack = 0;
3442 int nkw;
3443 3511
3444 /* the number of tuples and dictionaries on the stack */ 3512 /* the number of tuples and dictionaries on the stack */
3445 Py_ssize_t nsubargs = 0, nsubkwargs = 0; 3513 Py_ssize_t nsubargs = 0, nsubkwargs = 0;
3446 3514
3447 nkw = 0;
3448 nseen = 0; /* the number of positional arguments on the stack */
3449 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 */
3450 for (i = 0; i < nelts; i++) { 3527 for (i = 0; i < nelts; i++) {
3451 expr_ty elt = asdl_seq_GET(args, i); 3528 expr_ty elt = asdl_seq_GET(args, i);
3452 if (elt->kind == Starred_kind) { 3529 if (elt->kind == Starred_kind) {
3453 /* A star-arg. If we've seen positional arguments, 3530 /* A star-arg. If we've seen positional arguments,
3454 pack the positional arguments into a 3531 pack the positional arguments into a tuple. */
3455 tuple. */
3456 if (nseen) { 3532 if (nseen) {
3457 ADDOP_I(c, BUILD_TUPLE, nseen); 3533 ADDOP_I(c, BUILD_TUPLE, nseen);
3458 nseen = 0; 3534 nseen = 0;
3459 nsubargs++; 3535 nsubargs++;
3460 } 3536 }
3461 VISIT(c, expr, elt->v.Starred.value); 3537 VISIT(c, expr, elt->v.Starred.value);
3462 nsubargs++; 3538 nsubargs++;
3463 } 3539 }
3464 else if (nsubargs) { 3540 else {
3465 /* We've seen star-args already, so we
3466 count towards items-to-pack-into-tuple. */
3467 VISIT(c, expr, elt); 3541 VISIT(c, expr, elt);
3468 nseen++; 3542 nseen++;
3469 } 3543 }
3470 else { 3544 }
3471 /* Positional arguments before star-arguments 3545
3472 are left on the stack. */ 3546 /* Same dance again for keyword arguments */
3473 VISIT(c, expr, elt); 3547 if (nsubargs || mustdictunpack) {
3474 n++; 3548 if (nseen) {
3475 } 3549 /* Pack up any trailing positional arguments. */
3476 } 3550 ADDOP_I(c, BUILD_TUPLE, nseen);
3477 if (nseen) { 3551 nsubargs++;
3478 /* Pack up any trailing positional arguments. */ 3552 }
3479 ADDOP_I(c, BUILD_TUPLE, nseen);
3480 nsubargs++;
3481 }
3482 if (nsubargs) {
3483 code |= 1;
3484 if (nsubargs > 1) { 3553 if (nsubargs > 1) {
3485 /* If we ended up with more than one stararg, we need 3554 /* If we ended up with more than one stararg, we need
3486 to concatenate them into a single sequence. */ 3555 to concatenate them into a single sequence. */
3487 ADDOP_I(c, BUILD_LIST_UNPACK, nsubargs); 3556 ADDOP_I(c, BUILD_TUPLE_UNPACK_WITH_CALL, nsubargs);
3488 } 3557 }
3489 } 3558 else if (nsubargs == 0) {
3490 3559 ADDOP_I(c, BUILD_TUPLE, 0);
3491 /* Same dance again for keyword arguments */ 3560 }
3492 nseen = 0; /* the number of keyword arguments on the stack following */ 3561 nseen = 0; /* the number of keyword arguments on the stack following */
3493 nelts = asdl_seq_LEN(keywords); 3562 for (i = 0; i < nkwelts; i++) {
3494 for (i = 0; i < nelts; i++) { 3563 keyword_ty kw = asdl_seq_GET(keywords, i);
3495 keyword_ty kw = asdl_seq_GET(keywords, i); 3564 if (kw->arg == NULL) {
3496 if (kw->arg == NULL) { 3565 /* A keyword argument unpacking. */
3497 /* A keyword argument unpacking. */ 3566 if (nseen) {
3498 if (nseen) {
3499 if (nsubkwargs) {
3500 if (!compiler_subkwargs(c, keywords, i - nseen, i)) 3567 if (!compiler_subkwargs(c, keywords, i - nseen, i))
3501 return 0; 3568 return 0;
3502 nsubkwargs++; 3569 nsubkwargs++;
3570 nseen = 0;
3503 } 3571 }
3504 else { 3572 VISIT(c, expr, kw->value);
3505 Py_ssize_t j; 3573 nsubkwargs++;
3506 for (j = 0; j < nseen; j++) {
3507 VISIT(c, keyword, asdl_seq_GET(keywords, j));
3508 }
3509 nkw = nseen;
3510 }
3511 nseen = 0;
3512 } 3574 }
3513 VISIT(c, expr, kw->value); 3575 else {
3514 nsubkwargs++; 3576 nseen++;
3515 } 3577 }
3516 else { 3578 }
3517 nseen++; 3579 if (nseen) {
3518 }
3519 }
3520 if (nseen) {
3521 if (nsubkwargs) {
3522 /* Pack up any trailing keyword arguments. */ 3580 /* Pack up any trailing keyword arguments. */
3523 if (!compiler_subkwargs(c, keywords, nelts - nseen, nelts)) 3581 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts))
3524 return 0; 3582 return 0;
3525 nsubkwargs++; 3583 nsubkwargs++;
3526 } 3584 }
3527 else {
3528 VISIT_SEQ(c, keyword, keywords);
3529 nkw = nseen;
3530 }
3531 }
3532 if (nsubkwargs) {
3533 code |= 2;
3534 if (nsubkwargs > 1) { 3585 if (nsubkwargs > 1) {
3535 /* Pack it all up */ 3586 /* Pack it all up */
3536 int function_pos = n + (code & 1) + 2 * nkw + 1; 3587 ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs);
3537 ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs | (function_pos << 8)); 3588 }
3538 } 3589 ADDOP_I(c, CALL_FUNCTION_EX, nsubkwargs > 0);
3539 } 3590 return 1;
3540 assert(n < 1<<8); 3591 }
3541 assert(nkw < 1<<24); 3592 else if (nkwelts) {
3542 n |= nkw << 8; 3593 PyObject *names;
3543 3594 VISIT_SEQ(c, keyword, keywords);
3544 switch (code) { 3595 names = PyTuple_New(nkwelts);
3545 case 0: 3596 if (names == NULL) {
3546 ADDOP_I(c, CALL_FUNCTION, n); 3597 return 0;
3547 break; 3598 }
3548 case 1: 3599 for (i = 0; i < nkwelts; i++) {
3549 ADDOP_I(c, CALL_FUNCTION_VAR, n); 3600 keyword_ty kw = asdl_seq_GET(keywords, i);
3550 break; 3601 Py_INCREF(kw->arg);
3551 case 2: 3602 PyTuple_SET_ITEM(names, i, kw->arg);
3552 ADDOP_I(c, CALL_FUNCTION_KW, n); 3603 }
3553 break; 3604 ADDOP_N(c, LOAD_CONST, names, consts);
3554 case 3: 3605 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
3555 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n); 3606 return 1;
3556 break; 3607 }
3557 } 3608 else {
3558 return 1; 3609 ADDOP_I(c, CALL_FUNCTION, n + nelts);
3610 return 1;
3611 }
3559 } 3612 }
3560 3613
3561 3614
3562 /* List and set comprehensions and generator expressions work by creating a 3615 /* List and set comprehensions and generator expressions work by creating a
3563 nested function to perform the actual iteration. This means that the 3616 nested function to perform the actual iteration. This means that the
3564 iteration variables don't leak into the current scope. 3617 iteration variables don't leak into the current scope.
3565 The defined function is called immediately following its definition, with the 3618 The defined function is called immediately following its definition, with the
3566 result of that call being the result of the expression. 3619 result of that call being the result of the expression.
3567 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
3568 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
3569 when the function is called. 3622 when the function is called.
3570 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,
3571 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.
3572 3625
3573 Possible cleanups: 3626 Possible cleanups:
3574 - iterate over the generator sequence instead of using recursion 3627 - iterate over the generator sequence instead of using recursion
3575 */ 3628 */
3576 3629
3630
3577 static int 3631 static int
3578 compiler_comprehension_generator(struct compiler *c, 3632 compiler_comprehension_generator(struct compiler *c,
3579 asdl_seq *generators, int gen_index, 3633 asdl_seq *generators, int gen_index,
3580 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)
3581 { 3651 {
3582 /* generate code for the iterator, then each of the ifs, 3652 /* generate code for the iterator, then each of the ifs,
3583 and then write to the element */ 3653 and then write to the element */
3584 3654
3585 comprehension_ty gen; 3655 comprehension_ty gen;
3586 basicblock *start, *anchor, *skip, *if_cleanup; 3656 basicblock *start, *anchor, *skip, *if_cleanup;
3587 Py_ssize_t i, n; 3657 Py_ssize_t i, n;
3588 3658
3589 start = compiler_new_block(c); 3659 start = compiler_new_block(c);
3590 skip = compiler_new_block(c); 3660 skip = compiler_new_block(c);
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
3658 compiler_use_next_block(c, skip); 3728 compiler_use_next_block(c, skip);
3659 } 3729 }
3660 compiler_use_next_block(c, if_cleanup); 3730 compiler_use_next_block(c, if_cleanup);
3661 ADDOP_JABS(c, JUMP_ABSOLUTE, start); 3731 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3662 compiler_use_next_block(c, anchor); 3732 compiler_use_next_block(c, anchor);
3663 3733
3664 return 1; 3734 return 1;
3665 } 3735 }
3666 3736
3667 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
3668 compiler_comprehension(struct compiler *c, expr_ty e, int type, 3870 compiler_comprehension(struct compiler *c, expr_ty e, int type,
3669 identifier name, asdl_seq *generators, expr_ty elt, 3871 identifier name, asdl_seq *generators, expr_ty elt,
3670 expr_ty val) 3872 expr_ty val)
3671 { 3873 {
3672 PyCodeObject *co = NULL; 3874 PyCodeObject *co = NULL;
3673 expr_ty outermost_iter; 3875 comprehension_ty outermost;
3674 PyObject *qualname = NULL; 3876 PyObject *qualname = NULL;
3675 3877 int is_async_function = c->u->u_ste->ste_coroutine;
3676 outermost_iter = ((comprehension_ty) 3878 int is_async_generator = 0;
3677 asdl_seq_GET(generators, 0))->iter; 3879
3880 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
3678 3881
3679 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, 3882 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
3680 (void *)e, e->lineno)) 3883 (void *)e, e->lineno))
3884 {
3681 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 }
3682 3899
3683 if (type != COMP_GENEXP) { 3900 if (type != COMP_GENEXP) {
3684 int op; 3901 int op;
3685 switch (type) { 3902 switch (type) {
3686 case COMP_LISTCOMP: 3903 case COMP_LISTCOMP:
3687 op = BUILD_LIST; 3904 op = BUILD_LIST;
3688 break; 3905 break;
3689 case COMP_SETCOMP: 3906 case COMP_SETCOMP:
3690 op = BUILD_SET; 3907 op = BUILD_SET;
3691 break; 3908 break;
(...skipping 22 matching lines...) Expand all
3714 Py_INCREF(qualname); 3931 Py_INCREF(qualname);
3715 compiler_exit_scope(c); 3932 compiler_exit_scope(c);
3716 if (co == NULL) 3933 if (co == NULL)
3717 goto error; 3934 goto error;
3718 3935
3719 if (!compiler_make_closure(c, co, 0, qualname)) 3936 if (!compiler_make_closure(c, co, 0, qualname))
3720 goto error; 3937 goto error;
3721 Py_DECREF(qualname); 3938 Py_DECREF(qualname);
3722 Py_DECREF(co); 3939 Py_DECREF(co);
3723 3940
3724 VISIT(c, expr, outermost_iter); 3941 VISIT(c, expr, outermost->iter);
3725 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
3726 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
3727 return 1; 3959 return 1;
3728 error_in_scope: 3960 error_in_scope:
3729 compiler_exit_scope(c); 3961 compiler_exit_scope(c);
3730 error: 3962 error:
3731 Py_XDECREF(qualname); 3963 Py_XDECREF(qualname);
3732 Py_XDECREF(co); 3964 Py_XDECREF(co);
3733 return 0; 3965 return 0;
3734 } 3966 }
3735 3967
3736 static int 3968 static int
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
3791 assert(e->kind == DictComp_kind); 4023 assert(e->kind == DictComp_kind);
3792 return compiler_comprehension(c, e, COMP_DICTCOMP, name, 4024 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
3793 e->v.DictComp.generators, 4025 e->v.DictComp.generators,
3794 e->v.DictComp.key, e->v.DictComp.value); 4026 e->v.DictComp.key, e->v.DictComp.value);
3795 } 4027 }
3796 4028
3797 4029
3798 static int 4030 static int
3799 compiler_visit_keyword(struct compiler *c, keyword_ty k) 4031 compiler_visit_keyword(struct compiler *c, keyword_ty k)
3800 { 4032 {
3801 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3802 VISIT(c, expr, k->value); 4033 VISIT(c, expr, k->value);
3803 return 1; 4034 return 1;
3804 } 4035 }
3805 4036
3806 /* Test whether expression is constant. For constants, report 4037 /* Test whether expression is constant. For constants, report
3807 whether they are true or false. 4038 whether they are true or false.
3808 4039
3809 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.
3810 */ 4041 */
3811 4042
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
4056 return compiler_genexp(c, e); 4287 return compiler_genexp(c, e);
4057 case ListComp_kind: 4288 case ListComp_kind:
4058 return compiler_listcomp(c, e); 4289 return compiler_listcomp(c, e);
4059 case SetComp_kind: 4290 case SetComp_kind:
4060 return compiler_setcomp(c, e); 4291 return compiler_setcomp(c, e);
4061 case DictComp_kind: 4292 case DictComp_kind:
4062 return compiler_dictcomp(c, e); 4293 return compiler_dictcomp(c, e);
4063 case Yield_kind: 4294 case Yield_kind:
4064 if (c->u->u_ste->ste_type != FunctionBlock) 4295 if (c->u->u_ste->ste_type != FunctionBlock)
4065 return compiler_error(c, "'yield' outside function"); 4296 return compiler_error(c, "'yield' outside function");
4066 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
4067 return compiler_error(c, "'yield' inside async function");
4068 if (e->v.Yield.value) { 4297 if (e->v.Yield.value) {
4069 VISIT(c, expr, e->v.Yield.value); 4298 VISIT(c, expr, e->v.Yield.value);
4070 } 4299 }
4071 else { 4300 else {
4072 ADDOP_O(c, LOAD_CONST, Py_None, consts); 4301 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4073 } 4302 }
4074 ADDOP(c, YIELD_VALUE); 4303 ADDOP(c, YIELD_VALUE);
4075 break; 4304 break;
4076 case YieldFrom_kind: 4305 case YieldFrom_kind:
4077 if (c->u->u_ste->ste_type != FunctionBlock) 4306 if (c->u->u_ste->ste_type != FunctionBlock)
4078 return compiler_error(c, "'yield' outside function"); 4307 return compiler_error(c, "'yield' outside function");
4079 4308
4080 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) 4309 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
4081 return compiler_error(c, "'yield from' inside async function"); 4310 return compiler_error(c, "'yield from' inside async function");
4082 4311
4083 VISIT(c, expr, e->v.YieldFrom.value); 4312 VISIT(c, expr, e->v.YieldFrom.value);
4084 ADDOP(c, GET_YIELD_FROM_ITER); 4313 ADDOP(c, GET_YIELD_FROM_ITER);
4085 ADDOP_O(c, LOAD_CONST, Py_None, consts); 4314 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4086 ADDOP(c, YIELD_FROM); 4315 ADDOP(c, YIELD_FROM);
4087 break; 4316 break;
4088 case Await_kind: 4317 case Await_kind:
4089 if (c->u->u_ste->ste_type != FunctionBlock) 4318 if (c->u->u_ste->ste_type != FunctionBlock)
4090 return compiler_error(c, "'await' outside function"); 4319 return compiler_error(c, "'await' outside function");
4091 4320
4092 if (c->u->u_scope_type == COMPILER_SCOPE_COMPREHENSION) 4321 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
4093 return compiler_error( 4322 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION)
4094 c, "'await' expressions in comprehensions are not supported");
4095
4096 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION)
4097 return compiler_error(c, "'await' outside async function"); 4323 return compiler_error(c, "'await' outside async function");
4098 4324
4099 VISIT(c, expr, e->v.Await.value); 4325 VISIT(c, expr, e->v.Await.value);
4100 ADDOP(c, GET_AWAITABLE); 4326 ADDOP(c, GET_AWAITABLE);
4101 ADDOP_O(c, LOAD_CONST, Py_None, consts); 4327 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4102 ADDOP(c, YIELD_FROM); 4328 ADDOP(c, YIELD_FROM);
4103 break; 4329 break;
4104 case Compare_kind: 4330 case Compare_kind:
4105 return compiler_compare(c, e); 4331 return compiler_compare(c, e);
4106 case Call_kind: 4332 case Call_kind:
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
4245 default: 4471 default:
4246 PyErr_Format(PyExc_SystemError, 4472 PyErr_Format(PyExc_SystemError,
4247 "invalid node type (%d) for augmented assignment", 4473 "invalid node type (%d) for augmented assignment",
4248 e->kind); 4474 e->kind);
4249 return 0; 4475 return 0;
4250 } 4476 }
4251 return 1; 4477 return 1;
4252 } 4478 }
4253 4479
4254 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
4255 compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b) 4619 compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
4256 { 4620 {
4257 struct fblockinfo *f; 4621 struct fblockinfo *f;
4258 if (c->u->u_nfblocks >= CO_MAXBLOCKS) { 4622 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
4259 PyErr_SetString(PyExc_SyntaxError, 4623 PyErr_SetString(PyExc_SyntaxError,
4260 "too many statically nested blocks"); 4624 "too many statically nested blocks");
4261 return 0; 4625 return 0;
4262 } 4626 }
4263 f = &c->u->u_fblock[c->u->u_nfblocks++]; 4627 f = &c->u->u_fblock[c->u->u_nfblocks++];
4264 f->fb_type = t; 4628 f->fb_type = t;
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
4542 assemble_init(struct assembler *a, int nblocks, int firstlineno) 4906 assemble_init(struct assembler *a, int nblocks, int firstlineno)
4543 { 4907 {
4544 memset(a, 0, sizeof(struct assembler)); 4908 memset(a, 0, sizeof(struct assembler));
4545 a->a_lineno = firstlineno; 4909 a->a_lineno = firstlineno;
4546 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); 4910 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
4547 if (!a->a_bytecode) 4911 if (!a->a_bytecode)
4548 return 0; 4912 return 0;
4549 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); 4913 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
4550 if (!a->a_lnotab) 4914 if (!a->a_lnotab)
4551 return 0; 4915 return 0;
4552 if ((size_t)nblocks > PY_SIZE_MAX / sizeof(basicblock *)) { 4916 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
4553 PyErr_NoMemory(); 4917 PyErr_NoMemory();
4554 return 0; 4918 return 0;
4555 } 4919 }
4556 a->a_postorder = (basicblock **)PyObject_Malloc( 4920 a->a_postorder = (basicblock **)PyObject_Malloc(
4557 sizeof(basicblock *) * nblocks); 4921 sizeof(basicblock *) * nblocks);
4558 if (!a->a_postorder) { 4922 if (!a->a_postorder) {
4559 PyErr_NoMemory(); 4923 PyErr_NoMemory();
4560 return 0; 4924 return 0;
4561 } 4925 }
4562 return 1; 4926 return 1;
(...skipping 23 matching lines...) Expand all
4586 the instruction's bytecode offset and line number. See 4950 the instruction's bytecode offset and line number. See
4587 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. */
4588 4952
4589 static int 4953 static int
4590 assemble_lnotab(struct assembler *a, struct instr *i) 4954 assemble_lnotab(struct assembler *a, struct instr *i)
4591 { 4955 {
4592 int d_bytecode, d_lineno; 4956 int d_bytecode, d_lineno;
4593 Py_ssize_t len; 4957 Py_ssize_t len;
4594 unsigned char *lnotab; 4958 unsigned char *lnotab;
4595 4959
4596 d_bytecode = a->a_offset - a->a_lineno_off; 4960 d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT);
4597 d_lineno = i->i_lineno - a->a_lineno; 4961 d_lineno = i->i_lineno - a->a_lineno;
4598 4962
4599 assert(d_bytecode >= 0); 4963 assert(d_bytecode >= 0);
4600 4964
4601 if(d_bytecode == 0 && d_lineno == 0) 4965 if(d_bytecode == 0 && d_lineno == 0)
4602 return 1; 4966 return 1;
4603 4967
4604 if (d_bytecode > 255) { 4968 if (d_bytecode > 255) {
4605 int j, nbytes, ncodes = d_bytecode / 255; 4969 int j, nbytes, ncodes = d_bytecode / 255;
4606 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
4693 /* assemble_emit() 5057 /* assemble_emit()
4694 Extend the bytecode with a new instruction. 5058 Extend the bytecode with a new instruction.
4695 Update lnotab if necessary. 5059 Update lnotab if necessary.
4696 */ 5060 */
4697 5061
4698 static int 5062 static int
4699 assemble_emit(struct assembler *a, struct instr *i) 5063 assemble_emit(struct assembler *a, struct instr *i)
4700 { 5064 {
4701 int size, arg = 0; 5065 int size, arg = 0;
4702 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); 5066 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
4703 char *code; 5067 _Py_CODEUNIT *code;
4704 5068
4705 arg = i->i_oparg; 5069 arg = i->i_oparg;
4706 size = instrsize(arg); 5070 size = instrsize(arg);
4707 if (i->i_lineno && !assemble_lnotab(a, i)) 5071 if (i->i_lineno && !assemble_lnotab(a, i))
4708 return 0; 5072 return 0;
4709 if (a->a_offset + size >= len) { 5073 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
4710 if (len > PY_SSIZE_T_MAX / 2) 5074 if (len > PY_SSIZE_T_MAX / 2)
4711 return 0; 5075 return 0;
4712 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) 5076 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
4713 return 0; 5077 return 0;
4714 } 5078 }
4715 code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; 5079 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
4716 a->a_offset += size; 5080 a->a_offset += size;
4717 write_op_arg((unsigned char*)code, i->i_opcode, arg, size); 5081 write_op_arg(code, i->i_opcode, arg, size);
4718 return 1; 5082 return 1;
4719 } 5083 }
4720 5084
4721 static void 5085 static void
4722 assemble_jump_offsets(struct assembler *a, struct compiler *c) 5086 assemble_jump_offsets(struct assembler *a, struct compiler *c)
4723 { 5087 {
4724 basicblock *b; 5088 basicblock *b;
4725 int bsize, totsize, extended_arg_recompile; 5089 int bsize, totsize, extended_arg_recompile;
4726 int i; 5090 int i;
4727 5091
(...skipping 16 matching lines...) Expand all
4744 /* Relative jumps are computed relative to 5108 /* Relative jumps are computed relative to
4745 the instruction pointer after fetching 5109 the instruction pointer after fetching
4746 the jump instruction. 5110 the jump instruction.
4747 */ 5111 */
4748 bsize += isize; 5112 bsize += isize;
4749 if (instr->i_jabs || instr->i_jrel) { 5113 if (instr->i_jabs || instr->i_jrel) {
4750 instr->i_oparg = instr->i_target->b_offset; 5114 instr->i_oparg = instr->i_target->b_offset;
4751 if (instr->i_jrel) { 5115 if (instr->i_jrel) {
4752 instr->i_oparg -= bsize; 5116 instr->i_oparg -= bsize;
4753 } 5117 }
5118 instr->i_oparg *= sizeof(_Py_CODEUNIT);
4754 if (instrsize(instr->i_oparg) != isize) { 5119 if (instrsize(instr->i_oparg) != isize) {
4755 extended_arg_recompile = 1; 5120 extended_arg_recompile = 1;
4756 } 5121 }
4757 } 5122 }
4758 } 5123 }
4759 } 5124 }
4760 5125
4761 /* XXX: This is an awful hack that could hurt performance, but 5126 /* XXX: This is an awful hack that could hurt performance, but
4762 on the bright side it should work until we come up 5127 on the bright side it should work until we come up
4763 with a better solution. 5128 with a better solution.
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
4801 static int 5166 static int
4802 compute_code_flags(struct compiler *c) 5167 compute_code_flags(struct compiler *c)
4803 { 5168 {
4804 PySTEntryObject *ste = c->u->u_ste; 5169 PySTEntryObject *ste = c->u->u_ste;
4805 int flags = 0; 5170 int flags = 0;
4806 Py_ssize_t n; 5171 Py_ssize_t n;
4807 if (ste->ste_type == FunctionBlock) { 5172 if (ste->ste_type == FunctionBlock) {
4808 flags |= CO_NEWLOCALS | CO_OPTIMIZED; 5173 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
4809 if (ste->ste_nested) 5174 if (ste->ste_nested)
4810 flags |= CO_NESTED; 5175 flags |= CO_NESTED;
4811 if (ste->ste_generator) 5176 if (ste->ste_generator && !ste->ste_coroutine)
4812 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;
4813 if (ste->ste_varargs) 5182 if (ste->ste_varargs)
4814 flags |= CO_VARARGS; 5183 flags |= CO_VARARGS;
4815 if (ste->ste_varkeywords) 5184 if (ste->ste_varkeywords)
4816 flags |= CO_VARKEYWORDS; 5185 flags |= CO_VARKEYWORDS;
4817 } 5186 }
4818 5187
4819 /* (Only) inherit compilerflags in PyCF_MASK */ 5188 /* (Only) inherit compilerflags in PyCF_MASK */
4820 flags |= (c->c_flags->cf_flags & PyCF_MASK); 5189 flags |= (c->c_flags->cf_flags & PyCF_MASK);
4821 5190
4822 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
4963 5332
4964 nblocks = 0; 5333 nblocks = 0;
4965 entryblock = NULL; 5334 entryblock = NULL;
4966 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) {
4967 nblocks++; 5336 nblocks++;
4968 entryblock = b; 5337 entryblock = b;
4969 } 5338 }
4970 5339
4971 /* Set firstlineno if it wasn't explicitly set. */ 5340 /* Set firstlineno if it wasn't explicitly set. */
4972 if (!c->u->u_firstlineno) { 5341 if (!c->u->u_firstlineno) {
4973 if (entryblock && entryblock->b_instr) 5342 if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno)
4974 c->u->u_firstlineno = entryblock->b_instr->i_lineno; 5343 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4975 else 5344 else
4976 c->u->u_firstlineno = 1; 5345 c->u->u_firstlineno = 1;
4977 } 5346 }
4978 if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) 5347 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4979 goto error; 5348 goto error;
4980 dfs(c, entryblock, &a); 5349 dfs(c, entryblock, &a);
4981 5350
4982 /* Can't modify the bytecode after computing jump offsets. */ 5351 /* Can't modify the bytecode after computing jump offsets. */
4983 assemble_jump_offsets(&a, c); 5352 assemble_jump_offsets(&a, c);
4984 5353
4985 /* Emit code in reverse postorder from dfs. */ 5354 /* Emit code in reverse postorder from dfs. */
4986 for (i = a.a_nblocks - 1; i >= 0; i--) { 5355 for (i = a.a_nblocks - 1; i >= 0; i--) {
4987 b = a.a_postorder[i]; 5356 b = a.a_postorder[i];
4988 for (j = 0; j < b->b_iused; j++) 5357 for (j = 0; j < b->b_iused; j++)
4989 if (!assemble_emit(&a, &b->b_instr[j])) 5358 if (!assemble_emit(&a, &b->b_instr[j]))
4990 goto error; 5359 goto error;
4991 } 5360 }
4992 5361
4993 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) 5362 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4994 goto error; 5363 goto error;
4995 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset) < 0) 5364 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0)
4996 goto error; 5365 goto error;
4997 5366
4998 co = makecode(c, &a); 5367 co = makecode(c, &a);
4999 error: 5368 error:
5000 assemble_free(&a); 5369 assemble_free(&a);
5001 return co; 5370 return co;
5002 } 5371 }
5003 5372
5004 #undef PyAST_Compile 5373 #undef PyAST_Compile
5005 PyAPI_FUNC(PyCodeObject *) 5374 PyAPI_FUNC(PyCodeObject *)
5006 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, 5375 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
5007 PyArena *arena) 5376 PyArena *arena)
5008 { 5377 {
5009 return PyAST_CompileEx(mod, filename, flags, -1, arena); 5378 return PyAST_CompileEx(mod, filename, flags, -1, arena);
5010 } 5379 }
LEFTRIGHT

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