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

Delta Between Two Patch Sets: Python/compile.c

Issue 27095: Simplify MAKE_FUNCTION
Left Patch Set: Created 3 years, 3 months ago
Right Patch Set: Created 3 years, 3 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 /* 1 /*
2 * This file compiles an abstract syntax tree (AST) into Python bytecode. 2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
3 * 3 *
4 * The primary entry point is PyAST_Compile(), which returns a 4 * The primary entry point is PyAST_Compile(), which returns a
5 * PyCodeObject. The compiler makes several passes to build the code 5 * PyCodeObject. The compiler makes several passes to build the code
6 * object: 6 * object:
7 * 1. Checks for future statements. See future.c 7 * 1. Checks for future statements. See future.c
8 * 2. Builds a symbol table. See symtable.c. 8 * 2. Builds a symbol table. See symtable.c.
9 * 3. Generate code for basic blocks. See compiler_mod() in this file. 9 * 3. Generate code for basic blocks. See compiler_mod() in this file.
10 * 4. Assemble the basic blocks into final code. See assemble() in 10 * 4. Assemble the basic blocks into final code. See assemble() in
(...skipping 962 matching lines...) Expand 10 before | Expand all | Expand 10 after
973 return 1-oparg; 973 return 1-oparg;
974 case BUILD_LIST_UNPACK: 974 case BUILD_LIST_UNPACK:
975 case BUILD_TUPLE_UNPACK: 975 case BUILD_TUPLE_UNPACK:
976 case BUILD_SET_UNPACK: 976 case BUILD_SET_UNPACK:
977 case BUILD_MAP_UNPACK: 977 case BUILD_MAP_UNPACK:
978 return 1 - oparg; 978 return 1 - oparg;
979 case BUILD_MAP_UNPACK_WITH_CALL: 979 case BUILD_MAP_UNPACK_WITH_CALL:
980 return 1 - (oparg & 0xFF); 980 return 1 - (oparg & 0xFF);
981 case BUILD_MAP: 981 case BUILD_MAP:
982 return 1 - 2*oparg; 982 return 1 - 2*oparg;
983 case BUILD_CONST_KEY_MAP:
984 return -oparg;
983 case LOAD_ATTR: 985 case LOAD_ATTR:
984 return 0; 986 return 0;
985 case COMPARE_OP: 987 case COMPARE_OP:
986 return -1; 988 return -1;
987 case IMPORT_NAME: 989 case IMPORT_NAME:
988 return -1; 990 return -1;
989 case IMPORT_FROM: 991 case IMPORT_FROM:
990 return 1; 992 return 1;
991 993
992 case JUMP_FORWARD: 994 case JUMP_FORWARD:
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
1226 compiler_exit_scope(c); \ 1228 compiler_exit_scope(c); \
1227 return 0; \ 1229 return 0; \
1228 } \ 1230 } \
1229 } 1231 }
1230 1232
1231 #define ADDOP_O(C, OP, O, TYPE) { \ 1233 #define ADDOP_O(C, OP, O, TYPE) { \
1232 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ 1234 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1233 return 0; \ 1235 return 0; \
1234 } 1236 }
1235 1237
1238 /* Same as ADDOP_O, but steals a reference. */
1239 #define ADDOP_N(C, OP, O, TYPE) { \
1240 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1241 Py_DECREF((O)); \
1242 return 0; \
1243 } \
1244 Py_DECREF((O)); \
1245 }
1246
1236 #define ADDOP_NAME(C, OP, O, TYPE) { \ 1247 #define ADDOP_NAME(C, OP, O, TYPE) { \
1237 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ 1248 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1238 return 0; \ 1249 return 0; \
1239 } 1250 }
1240 1251
1241 #define ADDOP_I(C, OP, O) { \ 1252 #define ADDOP_I(C, OP, O) { \
1242 if (!compiler_addop_i((C), (OP), (O))) \ 1253 if (!compiler_addop_i((C), (OP), (O))) \
1243 return 0; \ 1254 return 0; \
1244 } 1255 }
1245 1256
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1299 static int 1310 static int
1300 compiler_isdocstring(stmt_ty s) 1311 compiler_isdocstring(stmt_ty s)
1301 { 1312 {
1302 if (s->kind != Expr_kind) 1313 if (s->kind != Expr_kind)
1303 return 0; 1314 return 0;
1304 if (s->v.Expr.value->kind == Str_kind) 1315 if (s->v.Expr.value->kind == Str_kind)
1305 return 1; 1316 return 1;
1306 if (s->v.Expr.value->kind == Constant_kind) 1317 if (s->v.Expr.value->kind == Constant_kind)
1307 return PyUnicode_CheckExact(s->v.Expr.value->v.Constant.value); 1318 return PyUnicode_CheckExact(s->v.Expr.value->v.Constant.value);
1308 return 0; 1319 return 0;
1320 }
1321
1322 static int
1323 is_const(expr_ty e)
1324 {
1325 switch (e->kind) {
1326 case Constant_kind:
1327 case Num_kind:
1328 case Str_kind:
1329 case Bytes_kind:
1330 case Ellipsis_kind:
1331 case NameConstant_kind:
1332 return 1;
1333 default:
1334 return 0;
1335 }
1336 }
1337
1338 static PyObject *
1339 get_const_value(expr_ty e)
1340 {
1341 switch (e->kind) {
1342 case Constant_kind:
1343 return e->v.Constant.value;
1344 case Num_kind:
1345 return e->v.Num.n;
1346 case Str_kind:
1347 return e->v.Str.s;
1348 case Bytes_kind:
1349 return e->v.Bytes.s;
1350 case Ellipsis_kind:
1351 return Py_Ellipsis;
1352 case NameConstant_kind:
1353 return e->v.NameConstant.value;
1354 default:
1355 assert(!is_const(e));
1356 return NULL;
1357 }
1309 } 1358 }
1310 1359
1311 /* Compile a sequence of statements, checking for a docstring. */ 1360 /* Compile a sequence of statements, checking for a docstring. */
1312 1361
1313 static int 1362 static int
1314 compiler_body(struct compiler *c, asdl_seq *stmts) 1363 compiler_body(struct compiler *c, asdl_seq *stmts)
1315 { 1364 {
1316 int i = 0; 1365 int i = 0;
1317 stmt_ty st; 1366 stmt_ty st;
1318 1367
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
1415 if (k == NULL) 1464 if (k == NULL)
1416 return -1; 1465 return -1;
1417 v = PyDict_GetItem(dict, k); 1466 v = PyDict_GetItem(dict, k);
1418 Py_DECREF(k); 1467 Py_DECREF(k);
1419 if (v == NULL) 1468 if (v == NULL)
1420 return -1; 1469 return -1;
1421 return PyLong_AS_LONG(v); 1470 return PyLong_AS_LONG(v);
1422 } 1471 }
1423 1472
1424 static int 1473 static int
1425 compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t args, PyO bject *qualname) 1474 compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, Py Object *qualname)
storchaka 2016/05/27 22:13:11 Maybe rename args to flags?
1426 { 1475 {
1427 Py_ssize_t i, free = PyCode_GetNumFree(co); 1476 Py_ssize_t i, free = PyCode_GetNumFree(co);
1428 if (qualname == NULL) 1477 if (qualname == NULL)
1429 qualname = co->co_name; 1478 qualname = co->co_name;
1430 1479
1431 if (free) { 1480 if (free) {
storchaka 2016/05/27 22:13:11 The patch would be smaller if move "if (free) {" a
1432 for (i = 0; i < free; ++i) { 1481 for (i = 0; i < free; ++i) {
1433 /* Bypass com_addop_varname because it will generate 1482 /* Bypass com_addop_varname because it will generate
1434 LOAD_DEREF but LOAD_CLOSURE is needed. 1483 LOAD_DEREF but LOAD_CLOSURE is needed.
1435 */ 1484 */
1436 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); 1485 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1437 int arg, reftype; 1486 int arg, reftype;
1438 1487
1439 /* Special case: If a class contains a method with a 1488 /* Special case: If a class contains a method with a
1440 free variable that has the same name as a method, 1489 free variable that has the same name as a method,
1441 the name will be considered free *and* local in the 1490 the name will be considered free *and* local in the
(...skipping 11 matching lines...) Expand all
1453 "freevars of %s: %s\n", 1502 "freevars of %s: %s\n",
1454 PyUnicode_AsUTF8(PyObject_Repr(name)), 1503 PyUnicode_AsUTF8(PyObject_Repr(name)),
1455 PyUnicode_AsUTF8(c->u->u_name), 1504 PyUnicode_AsUTF8(c->u->u_name),
1456 reftype, arg, 1505 reftype, arg,
1457 PyUnicode_AsUTF8(co->co_name), 1506 PyUnicode_AsUTF8(co->co_name),
1458 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars))); 1507 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));
1459 Py_FatalError("compiler_make_closure()"); 1508 Py_FatalError("compiler_make_closure()");
1460 } 1509 }
1461 ADDOP_I(c, LOAD_CLOSURE, arg); 1510 ADDOP_I(c, LOAD_CLOSURE, arg);
1462 } 1511 }
1463 args |= 0x08; 1512 flags |= 0x08;
1464 ADDOP_I(c, BUILD_TUPLE, free); 1513 ADDOP_I(c, BUILD_TUPLE, free);
1465 } 1514 }
1466 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts); 1515 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1467 ADDOP_O(c, LOAD_CONST, qualname, consts); 1516 ADDOP_O(c, LOAD_CONST, qualname, consts);
1468 ADDOP_I(c, MAKE_FUNCTION, args); 1517 ADDOP_I(c, MAKE_FUNCTION, flags);
1469 return 1; 1518 return 1;
1470 } 1519 }
1471 1520
1472 static int 1521 static int
1473 compiler_decorators(struct compiler *c, asdl_seq* decos) 1522 compiler_decorators(struct compiler *c, asdl_seq* decos)
1474 { 1523 {
1475 int i; 1524 int i;
1476 1525
1477 if (!decos) 1526 if (!decos)
1478 return 1; 1527 return 1;
1479 1528
1480 for (i = 0; i < asdl_seq_LEN(decos); i++) { 1529 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1481 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); 1530 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1482 } 1531 }
1483 return 1; 1532 return 1;
1484 } 1533 }
1485 1534
1486 static int 1535 static Py_ssize_t
1487 compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs, 1536 compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
1488 asdl_seq *kw_defaults) 1537 asdl_seq *kw_defaults)
1489 { 1538 {
1490 int i, default_count = 0; 1539 int i;
1540 PyObject *keys = NULL;
1541
1491 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { 1542 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1492 arg_ty arg = asdl_seq_GET(kwonlyargs, i); 1543 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1493 expr_ty default_ = asdl_seq_GET(kw_defaults, i); 1544 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1494 if (default_) { 1545 if (default_) {
1495 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); 1546 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
1496 if (!mangled) { 1547 if (!mangled) {
storchaka 2016/05/27 22:13:11 This is unrelated change (since the body of the lo
1497 return -1; 1548 goto error;
1498 } 1549 }
1499 ADDOP_O(c, LOAD_CONST, mangled, consts); 1550 if (keys == NULL) {
1551 keys = PyList_New(1);
1552 if (keys == NULL) {
1553 Py_DECREF(mangled);
1554 return -1;
1555 }
1556 PyList_SET_ITEM(keys, 0, mangled);
1557 }
1558 else {
1559 int res = PyList_Append(keys, mangled);
1560 Py_DECREF(mangled);
1561 if (res == -1) {
1562 goto error;
1563 }
1564 }
1565 if (!compiler_visit_expr(c, default_)) {
1566 goto error;
1567 }
1568 }
1569 }
1570 if (keys != NULL) {
1571 Py_ssize_t default_count = PyList_Size(keys);
1572 PyObject *keys_tuple = PyList_AsTuple(keys);
1573 Py_DECREF(keys);
1574 if (keys_tuple == NULL) {
1575 return -1;
1576 }
1577 ADDOP_N(c, LOAD_CONST, keys_tuple, consts);
1578 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
1579 return default_count;
1580 }
1581 else {
1582 return 0;
1583 }
1584
1585 error:
1586 Py_XDECREF(keys);
1587 return -1;
1588 }
1589
1590 static int
1591 compiler_visit_argannotation(struct compiler *c, identifier id,
1592 expr_ty annotation, PyObject *names)
1593 {
1594 if (annotation) {
1595 PyObject *mangled;
1596 VISIT(c, expr, annotation);
1597 mangled = _Py_Mangle(c->u->u_private, id);
1598 if (!mangled)
1599 return 0;
1600 if (PyList_Append(names, mangled) < 0) {
1500 Py_DECREF(mangled); 1601 Py_DECREF(mangled);
1501 if (!compiler_visit_expr(c, default_)) { 1602 return 0;
1502 return -1; 1603 }
1503 }
1504 default_count++;
1505 }
1506 }
1507 if (default_count > 0) {
1508 ADDOP_I(c, BUILD_MAP, default_count);
1509 }
1510 return default_count;
1511 }
1512
1513 static int
1514 compiler_visit_argannotation(struct compiler *c, identifier id,
1515 expr_ty annotation)
1516 {
1517 if (annotation) {
1518 PyObject *mangled = _Py_Mangle(c->u->u_private, id);
1519 if (!mangled) {
1520 return -1;
1521 }
1522 ADDOP_O(c, LOAD_CONST, mangled, consts);
1523 Py_DECREF(mangled); 1604 Py_DECREF(mangled);
1524 VISIT(c, expr, annotation); 1605 }
1525 return 1; 1606 return 1;
1526 } 1607 }
1527 return 0; 1608
1528 } 1609 static int
1529 1610 compiler_visit_argannotations(struct compiler *c, asdl_seq *args,
1530 static Py_ssize_t 1611 PyObject *names)
1531 compiler_visit_argannotations(struct compiler *c, asdl_seq* args)
1532 { 1612 {
1533 int i; 1613 int i;
1534 Py_ssize_t annonum = 0, arglen = asdl_seq_LEN(args); 1614 for (i = 0; i < asdl_seq_LEN(args); i++) {
1535 for (i = 0; i < arglen; i++) {
1536 arg_ty arg = (arg_ty)asdl_seq_GET(args, i); 1615 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1537 Py_ssize_t annoret = compiler_visit_argannotation(c, arg->arg, 1616 if (!compiler_visit_argannotation(
1538 arg->annotation); 1617 c,
1539 if (annoret == -1) { 1618 arg->arg,
1540 return -1; 1619 arg->annotation,
1541 } 1620 names))
1542 annonum += annoret; 1621 return 0;
1543 } 1622 }
1544 1623 return 1;
1545 return annonum;
1546 } 1624 }
1547 1625
1548 static Py_ssize_t 1626 static Py_ssize_t
1549 compiler_visit_annotations(struct compiler *c, arguments_ty args, 1627 compiler_visit_annotations(struct compiler *c, arguments_ty args,
1550 expr_ty returns) 1628 expr_ty returns)
1551 { 1629 {
1552 /* Push arg annotation dict. Return # of items pushed. 1630 /* Push arg annotation dict. Return # of items pushed.
1553 The expressions are evaluated out-of-order wrt the source code. 1631 The expressions are evaluated out-of-order wrt the source code.
1554 1632
1555 Returns -1 on error. 1633 Returns -1 on error.
1556 */ 1634 */
1557 static identifier return_str; 1635 static identifier return_str;
1558 Py_ssize_t len = 0, annoret; 1636 PyObject *names;
1559 1637 Py_ssize_t len;
1560 annoret = compiler_visit_argannotations(c, args->args); 1638 names = PyList_New(0);
1561 if (annoret == -1) { 1639 if (!names)
1562 return -1; 1640 return -1;
1563 } 1641
1564 len += annoret; 1642 if (!compiler_visit_argannotations(c, args->args, names))
1565 1643 goto error;
1566 if (args->vararg && args->vararg->annotation) { 1644 if (args->vararg && args->vararg->annotation &&
1567 annoret = compiler_visit_argannotation(c, args->vararg->arg, 1645 !compiler_visit_argannotation(c, args->vararg->arg,
1568 args->vararg->annotation); 1646 args->vararg->annotation, names))
1569 if (annoret == -1) { 1647 goto error;
1570 return -1; 1648 if (!compiler_visit_argannotations(c, args->kwonlyargs, names))
1571 } 1649 goto error;
1572 len += annoret; 1650 if (args->kwarg && args->kwarg->annotation &&
1573 } 1651 !compiler_visit_argannotation(c, args->kwarg->arg,
1574 1652 args->kwarg->annotation, names))
1575 annoret = compiler_visit_argannotations(c, args->kwonlyargs); 1653 goto error;
1576 if (annoret == -1) {
1577 return -1;
1578 }
1579 len += annoret;
1580
1581 if (args->kwarg && args->kwarg->annotation) {
1582 annoret = compiler_visit_argannotation(c, args->kwarg->arg,
1583 args->kwarg->annotation);
1584 if (annoret == -1) {
1585 return -1;
1586 }
1587 len += annoret;
1588 }
1589 1654
1590 if (!return_str) { 1655 if (!return_str) {
1591 return_str = PyUnicode_InternFromString("return"); 1656 return_str = PyUnicode_InternFromString("return");
1592 if (!return_str) { 1657 if (!return_str)
1593 return -1; 1658 goto error;
1594 } 1659 }
1595 } 1660 if (!compiler_visit_argannotation(c, return_str, returns, names)) {
1596 1661 goto error;
1597 annoret = compiler_visit_argannotation(c, return_str, returns); 1662 }
1598 if (annoret == -1) { 1663
1599 return -1; 1664 len = PyList_GET_SIZE(names);
1600 } 1665 if (len) {
1601 len += annoret; 1666 PyObject *keytuple = PyList_AsTuple(names);
1602 1667 Py_DECREF(names);
1603 if (len != 0) { 1668 if (keytuple == NULL) {
1604 ADDOP_I(c, BUILD_MAP, len); 1669 return -1;
1670 }
1671 ADDOP_N(c, LOAD_CONST, keytuple, consts);
1672 ADDOP_I(c, BUILD_CONST_KEY_MAP, len);
1673 }
1674 else {
1675 Py_DECREF(names);
1605 } 1676 }
1606 return len; 1677 return len;
1678
1679 error:
1680 Py_DECREF(names);
1681 return -1;
1607 } 1682 }
1608 1683
1609 static Py_ssize_t 1684 static Py_ssize_t
1610 compiler_default_arguments(struct compiler *c, arguments_ty args) 1685 compiler_default_arguments(struct compiler *c, arguments_ty args)
1611 { 1686 {
1612 Py_ssize_t funcflags = 0; 1687 Py_ssize_t funcflags = 0;
1613 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { 1688 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
1614 VISIT_SEQ(c, expr, args->defaults); 1689 VISIT_SEQ(c, expr, args->defaults);
1615 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); 1690 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
1616 funcflags |= 0x01; 1691 funcflags |= 0x01;
1617 } 1692 }
1618 if (args->kwonlyargs) { 1693 if (args->kwonlyargs) {
1619 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, 1694 Py_ssize_t res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1620 args->kw_defaults); 1695 args->kw_defaults);
1621 if (res < 0) { 1696 if (res < 0) {
1622 return -1; 1697 return -1;
1623 } 1698 }
1624 else if (res > 0) { 1699 else if (res > 0) {
1625 funcflags |= 0x02; 1700 funcflags |= 0x02;
1626 } 1701 }
1627 } 1702 }
1628 return funcflags; 1703 return funcflags;
1629 } 1704 }
(...skipping 966 matching lines...) Expand 10 before | Expand all | Expand 10 after
2596 2671
2597 static int 2672 static int
2598 compiler_visit_stmt_expr(struct compiler *c, expr_ty value) 2673 compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
2599 { 2674 {
2600 if (c->c_interactive && c->c_nestlevel <= 1) { 2675 if (c->c_interactive && c->c_nestlevel <= 1) {
2601 VISIT(c, expr, value); 2676 VISIT(c, expr, value);
2602 ADDOP(c, PRINT_EXPR); 2677 ADDOP(c, PRINT_EXPR);
2603 return 1; 2678 return 1;
2604 } 2679 }
2605 2680
2606 switch (value->kind) 2681 if (is_const(value)) {
2607 {
2608 case Str_kind:
2609 case Num_kind:
2610 case Ellipsis_kind:
2611 case Bytes_kind:
2612 case NameConstant_kind:
2613 case Constant_kind:
2614 /* ignore constant statement */ 2682 /* ignore constant statement */
2615 return 1; 2683 return 1;
2616
2617 default:
2618 break;
2619 } 2684 }
2620 2685
2621 VISIT(c, expr, value); 2686 VISIT(c, expr, value);
2622 ADDOP(c, POP_TOP); 2687 ADDOP(c, POP_TOP);
2623 return 1; 2688 return 1;
2624 } 2689 }
2625 2690
2626 static int 2691 static int
2627 compiler_visit_stmt(struct compiler *c, stmt_ty s) 2692 compiler_visit_stmt(struct compiler *c, stmt_ty s)
2628 { 2693 {
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after
3088 } 3153 }
3089 3154
3090 static int 3155 static int
3091 compiler_set(struct compiler *c, expr_ty e) 3156 compiler_set(struct compiler *c, expr_ty e)
3092 { 3157 {
3093 return starunpack_helper(c, e->v.Set.elts, BUILD_SET, 3158 return starunpack_helper(c, e->v.Set.elts, BUILD_SET,
3094 BUILD_SET, BUILD_SET_UNPACK); 3159 BUILD_SET, BUILD_SET_UNPACK);
3095 } 3160 }
3096 3161
3097 static int 3162 static int
3163 are_all_items_const(asdl_seq *seq, Py_ssize_t begin, Py_ssize_t end)
3164 {
3165 Py_ssize_t i;
3166 for (i = begin; i < end; i++) {
3167 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
3168 if (key == NULL || !is_const(key))
3169 return 0;
3170 }
3171 return 1;
3172 }
3173
3174 static int
3175 compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end )
3176 {
3177 Py_ssize_t i, n = end - begin;
3178 PyObject *keys, *key;
3179 if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) {
3180 for (i = begin; i < end; i++) {
3181 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3182 }
3183 keys = PyTuple_New(n);
3184 if (keys == NULL) {
3185 return 0;
3186 }
3187 for (i = begin; i < end; i++) {
3188 key = get_const_value((expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3189 Py_INCREF(key);
3190 PyTuple_SET_ITEM(keys, i - begin, key);
3191 }
3192 ADDOP_N(c, LOAD_CONST, keys, consts);
3193 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3194 }
3195 else {
3196 for (i = begin; i < end; i++) {
3197 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3198 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3199 }
3200 ADDOP_I(c, BUILD_MAP, n);
3201 }
3202 return 1;
3203 }
3204
3205 static int
3098 compiler_dict(struct compiler *c, expr_ty e) 3206 compiler_dict(struct compiler *c, expr_ty e)
3099 { 3207 {
3100 Py_ssize_t i, n, elements; 3208 Py_ssize_t i, n, elements;
3101 int containers; 3209 int containers;
3102 int is_unpacking = 0; 3210 int is_unpacking = 0;
3103 n = asdl_seq_LEN(e->v.Dict.values); 3211 n = asdl_seq_LEN(e->v.Dict.values);
3104 containers = 0; 3212 containers = 0;
3105 elements = 0; 3213 elements = 0;
3106 for (i = 0; i < n; i++) { 3214 for (i = 0; i < n; i++) {
3107 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL; 3215 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
3108 if (elements == 0xFFFF || (elements && is_unpacking)) { 3216 if (elements == 0xFFFF || (elements && is_unpacking)) {
3109 ADDOP_I(c, BUILD_MAP, elements); 3217 if (!compiler_subdict(c, e, i - elements, i))
3218 return 0;
3110 containers++; 3219 containers++;
3111 elements = 0; 3220 elements = 0;
3112 } 3221 }
3113 if (is_unpacking) { 3222 if (is_unpacking) {
3114 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); 3223 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3115 containers++; 3224 containers++;
3116 } 3225 }
3117 else { 3226 else {
3118 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3119 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3120 elements++; 3227 elements++;
3121 } 3228 }
3122 } 3229 }
3123 if (elements || containers == 0) { 3230 if (elements || containers == 0) {
3124 ADDOP_I(c, BUILD_MAP, elements); 3231 if (!compiler_subdict(c, e, n - elements, n))
3232 return 0;
3125 containers++; 3233 containers++;
3126 } 3234 }
3127 /* If there is more than one dict, they need to be merged into a new 3235 /* If there is more than one dict, they need to be merged into a new
3128 * dict. If there is one dict and it's an unpacking, then it needs 3236 * dict. If there is one dict and it's an unpacking, then it needs
3129 * to be copied into a new dict." */ 3237 * to be copied into a new dict." */
3130 while (containers > 1 || is_unpacking) { 3238 while (containers > 1 || is_unpacking) {
3131 int oparg = containers < 255 ? containers : 255; 3239 int oparg = containers < 255 ? containers : 255;
3132 ADDOP_I(c, BUILD_MAP_UNPACK, oparg); 3240 ADDOP_I(c, BUILD_MAP_UNPACK, oparg);
3133 containers -= (oparg - 1); 3241 containers -= (oparg - 1);
3134 is_unpacking = 0; 3242 is_unpacking = 0;
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
3255 return 0; 3363 return 0;
3256 } 3364 }
3257 if (e->v.FormattedValue.format_spec) { 3365 if (e->v.FormattedValue.format_spec) {
3258 /* Evaluate the format spec, and update our opcode arg. */ 3366 /* Evaluate the format spec, and update our opcode arg. */
3259 VISIT(c, expr, e->v.FormattedValue.format_spec); 3367 VISIT(c, expr, e->v.FormattedValue.format_spec);
3260 oparg |= FVS_HAVE_SPEC; 3368 oparg |= FVS_HAVE_SPEC;
3261 } 3369 }
3262 3370
3263 /* And push our opcode and oparg */ 3371 /* And push our opcode and oparg */
3264 ADDOP_I(c, FORMAT_VALUE, oparg); 3372 ADDOP_I(c, FORMAT_VALUE, oparg);
3373 return 1;
3374 }
3375
3376 static int
3377 compiler_subkwargs(struct compiler *c, asdl_seq *keywords, Py_ssize_t begin, Py_ ssize_t end)
3378 {
3379 Py_ssize_t i, n = end - begin;
3380 keyword_ty kw;
3381 PyObject *keys, *key;
3382 assert(n > 0);
3383 if (n > 1) {
3384 for (i = begin; i < end; i++) {
3385 kw = asdl_seq_GET(keywords, i);
3386 VISIT(c, expr, kw->value);
3387 }
3388 keys = PyTuple_New(n);
3389 if (keys == NULL) {
3390 return 0;
3391 }
3392 for (i = begin; i < end; i++) {
3393 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
3394 Py_INCREF(key);
3395 PyTuple_SET_ITEM(keys, i - begin, key);
3396 }
3397 ADDOP_N(c, LOAD_CONST, keys, consts);
3398 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3399 }
3400 else {
3401 /* a for loop only executes once */
3402 for (i = begin; i < end; i++) {
3403 kw = asdl_seq_GET(keywords, i);
3404 ADDOP_O(c, LOAD_CONST, kw->arg, consts);
3405 VISIT(c, expr, kw->value);
3406 }
3407 ADDOP_I(c, BUILD_MAP, n);
3408 }
3265 return 1; 3409 return 1;
3266 } 3410 }
3267 3411
3268 /* shared code between compiler_call and compiler_class */ 3412 /* shared code between compiler_call and compiler_class */
3269 static int 3413 static int
3270 compiler_call_helper(struct compiler *c, 3414 compiler_call_helper(struct compiler *c,
3271 int n, /* Args already pushed */ 3415 int n, /* Args already pushed */
3272 asdl_seq *args, 3416 asdl_seq *args,
3273 asdl_seq *keywords) 3417 asdl_seq *keywords)
3274 { 3418 {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
3324 } 3468 }
3325 3469
3326 /* Same dance again for keyword arguments */ 3470 /* Same dance again for keyword arguments */
3327 nseen = 0; /* the number of keyword arguments on the stack following */ 3471 nseen = 0; /* the number of keyword arguments on the stack following */
3328 nelts = asdl_seq_LEN(keywords); 3472 nelts = asdl_seq_LEN(keywords);
3329 for (i = 0; i < nelts; i++) { 3473 for (i = 0; i < nelts; i++) {
3330 keyword_ty kw = asdl_seq_GET(keywords, i); 3474 keyword_ty kw = asdl_seq_GET(keywords, i);
3331 if (kw->arg == NULL) { 3475 if (kw->arg == NULL) {
3332 /* A keyword argument unpacking. */ 3476 /* A keyword argument unpacking. */
3333 if (nseen) { 3477 if (nseen) {
3334 ADDOP_I(c, BUILD_MAP, nseen); 3478 if (nsubkwargs) {
3479 if (!compiler_subkwargs(c, keywords, i - nseen, i))
3480 return 0;
3481 nsubkwargs++;
3482 }
3483 else {
3484 Py_ssize_t j;
3485 for (j = 0; j < nseen; j++) {
3486 VISIT(c, keyword, asdl_seq_GET(keywords, j));
3487 }
3488 nkw = nseen;
3489 }
3335 nseen = 0; 3490 nseen = 0;
3336 nsubkwargs++;
3337 } 3491 }
3338 VISIT(c, expr, kw->value); 3492 VISIT(c, expr, kw->value);
3339 nsubkwargs++; 3493 nsubkwargs++;
3340 } 3494 }
3341 else if (nsubkwargs) { 3495 else {
3342 /* A keyword argument and we already have a dict. */
3343 ADDOP_O(c, LOAD_CONST, kw->arg, consts);
3344 VISIT(c, expr, kw->value);
3345 nseen++; 3496 nseen++;
3346 } 3497 }
3498 }
3499 if (nseen) {
3500 if (nsubkwargs) {
3501 /* Pack up any trailing keyword arguments. */
3502 if (!compiler_subkwargs(c, keywords, nelts - nseen, nelts))
3503 return 0;
3504 nsubkwargs++;
3505 }
3347 else { 3506 else {
3348 /* keyword argument */ 3507 VISIT_SEQ(c, keyword, keywords);
3349 VISIT(c, keyword, kw) 3508 nkw = nseen;
3350 nkw++; 3509 }
3351 }
3352 }
3353 if (nseen) {
3354 /* Pack up any trailing keyword arguments. */
3355 ADDOP_I(c, BUILD_MAP, nseen);
3356 nsubkwargs++;
3357 } 3510 }
3358 if (nsubkwargs) { 3511 if (nsubkwargs) {
3359 code |= 2; 3512 code |= 2;
3360 if (nsubkwargs > 1) { 3513 if (nsubkwargs > 1) {
3361 /* Pack it all up */ 3514 /* Pack it all up */
3362 int function_pos = n + (code & 1) + nkw + 1; 3515 int function_pos = n + (code & 1) + 2 * nkw + 1;
3363 ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs | (function_pos << 8)); 3516 ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs | (function_pos << 8));
3364 } 3517 }
3365 } 3518 }
3366 assert(n < 1<<8); 3519 assert(n < 1<<8);
3367 assert(nkw < 1<<24); 3520 assert(nkw < 1<<24);
3368 n |= nkw << 8; 3521 n |= nkw << 8;
3369 3522
3370 switch (code) { 3523 switch (code) {
3371 case 0: 3524 case 0:
3372 ADDOP_I(c, CALL_FUNCTION, n); 3525 ADDOP_I(c, CALL_FUNCTION, n);
(...skipping 1454 matching lines...) Expand 10 before | Expand all | Expand 10 after
4827 return co; 4980 return co;
4828 } 4981 }
4829 4982
4830 #undef PyAST_Compile 4983 #undef PyAST_Compile
4831 PyAPI_FUNC(PyCodeObject *) 4984 PyAPI_FUNC(PyCodeObject *)
4832 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, 4985 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4833 PyArena *arena) 4986 PyArena *arena)
4834 { 4987 {
4835 return PyAST_CompileEx(mod, filename, flags, -1, arena); 4988 return PyAST_CompileEx(mod, filename, flags, -1, arena);
4836 } 4989 }
LEFTRIGHT

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