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

Side by Side Diff: Python/compile.c

Issue 27095: Simplify MAKE_FUNCTION
Patch Set: Created 3 years, 4 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
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 1012 matching lines...) Expand 10 before | Expand all | Expand 10 after
1023 case RAISE_VARARGS: 1023 case RAISE_VARARGS:
1024 return -oparg; 1024 return -oparg;
1025 #define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256)) 1025 #define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
1026 case CALL_FUNCTION: 1026 case CALL_FUNCTION:
1027 return -NARGS(oparg); 1027 return -NARGS(oparg);
1028 case CALL_FUNCTION_VAR: 1028 case CALL_FUNCTION_VAR:
1029 case CALL_FUNCTION_KW: 1029 case CALL_FUNCTION_KW:
1030 return -NARGS(oparg)-1; 1030 return -NARGS(oparg)-1;
1031 case CALL_FUNCTION_VAR_KW: 1031 case CALL_FUNCTION_VAR_KW:
1032 return -NARGS(oparg)-2; 1032 return -NARGS(oparg)-2;
1033 #undef NARGS
1033 case MAKE_FUNCTION: 1034 case MAKE_FUNCTION:
1034 return -1 -NARGS(oparg) - ((oparg >> 16) & 0xffff); 1035 return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1035 case MAKE_CLOSURE: 1036 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
1036 return -2 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
1037 #undef NARGS
1038 case BUILD_SLICE: 1037 case BUILD_SLICE:
1039 if (oparg == 3) 1038 if (oparg == 3)
1040 return -2; 1039 return -2;
1041 else 1040 else
1042 return -1; 1041 return -1;
1043 1042
1044 case LOAD_CLOSURE: 1043 case LOAD_CLOSURE:
1045 return 1; 1044 return 1;
1046 case LOAD_DEREF: 1045 case LOAD_DEREF:
1047 case LOAD_CLASSDEREF: 1046 case LOAD_CLASSDEREF:
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after
1465 if (k == NULL) 1464 if (k == NULL)
1466 return -1; 1465 return -1;
1467 v = PyDict_GetItem(dict, k); 1466 v = PyDict_GetItem(dict, k);
1468 Py_DECREF(k); 1467 Py_DECREF(k);
1469 if (v == NULL) 1468 if (v == NULL)
1470 return -1; 1469 return -1;
1471 return PyLong_AS_LONG(v); 1470 return PyLong_AS_LONG(v);
1472 } 1471 }
1473 1472
1474 static int 1473 static int
1475 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)
1476 { 1475 {
1477 Py_ssize_t i, free = PyCode_GetNumFree(co); 1476 Py_ssize_t i, free = PyCode_GetNumFree(co);
1478 if (qualname == NULL) 1477 if (qualname == NULL)
1479 qualname = co->co_name; 1478 qualname = co->co_name;
1480 1479
1481 if (free == 0) { 1480 if (free) {
1482 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts); 1481 for (i = 0; i < free; ++i) {
1483 ADDOP_O(c, LOAD_CONST, qualname, consts); 1482 /* Bypass com_addop_varname because it will generate
1484 ADDOP_I(c, MAKE_FUNCTION, args); 1483 LOAD_DEREF but LOAD_CLOSURE is needed.
1485 return 1; 1484 */
1485 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1486 int arg, reftype;
1487
1488 /* Special case: If a class contains a method with a
1489 free variable that has the same name as a method,
1490 the name will be considered free *and* local in the
1491 class. It should be handled by the closure, as
1492 well as by the normal name loookup logic.
1493 */
1494 reftype = get_ref_type(c, name);
1495 if (reftype == CELL)
1496 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1497 else /* (reftype == FREE) */
1498 arg = compiler_lookup_arg(c->u->u_freevars, name);
1499 if (arg == -1) {
1500 fprintf(stderr,
1501 "lookup %s in %s %d %d\n"
1502 "freevars of %s: %s\n",
1503 PyUnicode_AsUTF8(PyObject_Repr(name)),
1504 PyUnicode_AsUTF8(c->u->u_name),
1505 reftype, arg,
1506 PyUnicode_AsUTF8(co->co_name),
1507 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));
1508 Py_FatalError("compiler_make_closure()");
1509 }
1510 ADDOP_I(c, LOAD_CLOSURE, arg);
1511 }
1512 flags |= 0x08;
1513 ADDOP_I(c, BUILD_TUPLE, free);
1486 } 1514 }
1487 for (i = 0; i < free; ++i) {
1488 /* Bypass com_addop_varname because it will generate
1489 LOAD_DEREF but LOAD_CLOSURE is needed.
1490 */
1491 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1492 int arg, reftype;
1493
1494 /* Special case: If a class contains a method with a
1495 free variable that has the same name as a method,
1496 the name will be considered free *and* local in the
1497 class. It should be handled by the closure, as
1498 well as by the normal name loookup logic.
1499 */
1500 reftype = get_ref_type(c, name);
1501 if (reftype == CELL)
1502 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1503 else /* (reftype == FREE) */
1504 arg = compiler_lookup_arg(c->u->u_freevars, name);
1505 if (arg == -1) {
1506 fprintf(stderr,
1507 "lookup %s in %s %d %d\n"
1508 "freevars of %s: %s\n",
1509 PyUnicode_AsUTF8(PyObject_Repr(name)),
1510 PyUnicode_AsUTF8(c->u->u_name),
1511 reftype, arg,
1512 PyUnicode_AsUTF8(co->co_name),
1513 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));
1514 Py_FatalError("compiler_make_closure()");
1515 }
1516 ADDOP_I(c, LOAD_CLOSURE, arg);
1517 }
1518 ADDOP_I(c, BUILD_TUPLE, free);
1519 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts); 1515 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1520 ADDOP_O(c, LOAD_CONST, qualname, consts); 1516 ADDOP_O(c, LOAD_CONST, qualname, consts);
1521 ADDOP_I(c, MAKE_CLOSURE, args); 1517 ADDOP_I(c, MAKE_FUNCTION, flags);
1522 return 1; 1518 return 1;
1523 } 1519 }
1524 1520
1525 static int 1521 static int
1526 compiler_decorators(struct compiler *c, asdl_seq* decos) 1522 compiler_decorators(struct compiler *c, asdl_seq* decos)
1527 { 1523 {
1528 int i; 1524 int i;
1529 1525
1530 if (!decos) 1526 if (!decos)
1531 return 1; 1527 return 1;
1532 1528
1533 for (i = 0; i < asdl_seq_LEN(decos); i++) { 1529 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1534 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); 1530 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1535 } 1531 }
1536 return 1; 1532 return 1;
1537 } 1533 }
1538 1534
1539 static int 1535 static Py_ssize_t
1540 compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs, 1536 compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
1541 asdl_seq *kw_defaults) 1537 asdl_seq *kw_defaults)
1542 { 1538 {
1543 int i, default_count = 0; 1539 int i;
1540 PyObject *keys = NULL;
1541
1544 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { 1542 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1545 arg_ty arg = asdl_seq_GET(kwonlyargs, i); 1543 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1546 expr_ty default_ = asdl_seq_GET(kw_defaults, i); 1544 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1547 if (default_) { 1545 if (default_) {
1548 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); 1546 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
1549 if (!mangled) 1547 if (!mangled) {
1550 return -1; 1548 goto error;
1551 ADDOP_O(c, LOAD_CONST, mangled, consts); 1549 }
1552 Py_DECREF(mangled); 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 }
1553 if (!compiler_visit_expr(c, default_)) { 1565 if (!compiler_visit_expr(c, default_)) {
1554 return -1; 1566 goto error;
1555 } 1567 }
1556 default_count++;
1557 } 1568 }
1558 } 1569 }
1559 return default_count; 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;
1560 } 1588 }
1561 1589
1562 static int 1590 static int
1563 compiler_visit_argannotation(struct compiler *c, identifier id, 1591 compiler_visit_argannotation(struct compiler *c, identifier id,
1564 expr_ty annotation, PyObject *names) 1592 expr_ty annotation, PyObject *names)
1565 { 1593 {
1566 if (annotation) { 1594 if (annotation) {
1567 PyObject *mangled; 1595 PyObject *mangled;
1568 VISIT(c, expr, annotation); 1596 VISIT(c, expr, annotation);
1569 mangled = _Py_Mangle(c->u->u_private, id); 1597 mangled = _Py_Mangle(c->u->u_private, id);
1570 if (!mangled) 1598 if (!mangled)
1571 return 0; 1599 return 0;
1572 if (PyList_Append(names, mangled) < 0) { 1600 if (PyList_Append(names, mangled) < 0) {
1573 Py_DECREF(mangled); 1601 Py_DECREF(mangled);
1574 return 0; 1602 return 0;
1575 } 1603 }
1576 Py_DECREF(mangled); 1604 Py_DECREF(mangled);
1577 } 1605 }
1578 return 1; 1606 return 1;
1579 } 1607 }
1580 1608
1581 static int 1609 static int
1582 compiler_visit_argannotations(struct compiler *c, asdl_seq* args, 1610 compiler_visit_argannotations(struct compiler *c, asdl_seq *args,
1583 PyObject *names) 1611 PyObject *names)
1584 { 1612 {
1585 int i; 1613 int i;
1586 for (i = 0; i < asdl_seq_LEN(args); i++) { 1614 for (i = 0; i < asdl_seq_LEN(args); i++) {
1587 arg_ty arg = (arg_ty)asdl_seq_GET(args, i); 1615 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1588 if (!compiler_visit_argannotation( 1616 if (!compiler_visit_argannotation(
1589 c, 1617 c,
1590 arg->arg, 1618 arg->arg,
1591 arg->annotation, 1619 arg->annotation,
1592 names)) 1620 names))
1593 return 0; 1621 return 0;
1594 } 1622 }
1595 return 1; 1623 return 1;
1596 } 1624 }
1597 1625
1598 static int 1626 static Py_ssize_t
1599 compiler_visit_annotations(struct compiler *c, arguments_ty args, 1627 compiler_visit_annotations(struct compiler *c, arguments_ty args,
1600 expr_ty returns) 1628 expr_ty returns)
1601 { 1629 {
1602 /* Push arg annotations and a list of the argument names. Return the # 1630 /* Push arg annotation dict. Return # of items pushed.
1603 of items pushed. The expressions are evaluated out-of-order wrt the 1631 The expressions are evaluated out-of-order wrt the source code.
1604 source code.
1605 1632
1606 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error. 1633 Returns -1 on error.
1607 */ 1634 */
1608 static identifier return_str; 1635 static identifier return_str;
1609 PyObject *names; 1636 PyObject *names;
1610 Py_ssize_t len; 1637 Py_ssize_t len;
1611 names = PyList_New(0); 1638 names = PyList_New(0);
1612 if (!names) 1639 if (!names)
1613 return -1; 1640 return -1;
1614 1641
1615 if (!compiler_visit_argannotations(c, args->args, names)) 1642 if (!compiler_visit_argannotations(c, args->args, names))
1616 goto error; 1643 goto error;
(...skipping 11 matching lines...) Expand all
1628 if (!return_str) { 1655 if (!return_str) {
1629 return_str = PyUnicode_InternFromString("return"); 1656 return_str = PyUnicode_InternFromString("return");
1630 if (!return_str) 1657 if (!return_str)
1631 goto error; 1658 goto error;
1632 } 1659 }
1633 if (!compiler_visit_argannotation(c, return_str, returns, names)) { 1660 if (!compiler_visit_argannotation(c, return_str, returns, names)) {
1634 goto error; 1661 goto error;
1635 } 1662 }
1636 1663
1637 len = PyList_GET_SIZE(names); 1664 len = PyList_GET_SIZE(names);
1638 if (len > 65534) { 1665 if (len) {
1639 /* len must fit in 16 bits, and len is incremented below */ 1666 PyObject *keytuple = PyList_AsTuple(names);
1640 PyErr_SetString(PyExc_SyntaxError, 1667 Py_DECREF(names);
1641 "too many annotations"); 1668 if (keytuple == NULL) {
1642 goto error; 1669 return -1;
1670 }
1671 ADDOP_N(c, LOAD_CONST, keytuple, consts);
1672 ADDOP_I(c, BUILD_CONST_KEY_MAP, len);
1643 } 1673 }
1644 if (len) { 1674 else {
1645 /* convert names to a tuple and place on stack */ 1675 Py_DECREF(names);
1646 PyObject *elt;
1647 Py_ssize_t i;
1648 PyObject *s = PyTuple_New(len);
1649 if (!s)
1650 goto error;
1651 for (i = 0; i < len; i++) {
1652 elt = PyList_GET_ITEM(names, i);
1653 Py_INCREF(elt);
1654 PyTuple_SET_ITEM(s, i, elt);
1655 }
1656 ADDOP_O(c, LOAD_CONST, s, consts);
1657 Py_DECREF(s);
1658 len++; /* include the just-pushed tuple */
1659 } 1676 }
1660 Py_DECREF(names); 1677 return len;
1661
1662 /* We just checked that len <= 65535, see above */
1663 return Py_SAFE_DOWNCAST(len, Py_ssize_t, int);
1664 1678
1665 error: 1679 error:
1666 Py_DECREF(names); 1680 Py_DECREF(names);
1667 return -1; 1681 return -1;
1668 } 1682 }
1669 1683
1684 static Py_ssize_t
1685 compiler_default_arguments(struct compiler *c, arguments_ty args)
1686 {
1687 Py_ssize_t funcflags = 0;
1688 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
1689 VISIT_SEQ(c, expr, args->defaults);
1690 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
1691 funcflags |= 0x01;
1692 }
1693 if (args->kwonlyargs) {
1694 Py_ssize_t res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1695 args->kw_defaults);
1696 if (res < 0) {
1697 return -1;
1698 }
1699 else if (res > 0) {
1700 funcflags |= 0x02;
1701 }
1702 }
1703 return funcflags;
1704 }
1705
1670 static int 1706 static int
1671 compiler_function(struct compiler *c, stmt_ty s, int is_async) 1707 compiler_function(struct compiler *c, stmt_ty s, int is_async)
1672 { 1708 {
1673 PyCodeObject *co; 1709 PyCodeObject *co;
1674 PyObject *qualname, *first_const = Py_None; 1710 PyObject *qualname, *first_const = Py_None;
1675 arguments_ty args; 1711 arguments_ty args;
1676 expr_ty returns; 1712 expr_ty returns;
1677 identifier name; 1713 identifier name;
1678 asdl_seq* decos; 1714 asdl_seq* decos;
1679 asdl_seq *body; 1715 asdl_seq *body;
1680 stmt_ty st; 1716 stmt_ty st;
1681 Py_ssize_t i, n, arglength; 1717 Py_ssize_t i, n, funcflags;
1682 int docstring, kw_default_count = 0; 1718 int docstring;
1683 int num_annotations; 1719 int num_annotations;
1684 int scope_type; 1720 int scope_type;
1685
1686 1721
1687 if (is_async) { 1722 if (is_async) {
1688 assert(s->kind == AsyncFunctionDef_kind); 1723 assert(s->kind == AsyncFunctionDef_kind);
1689 1724
1690 args = s->v.AsyncFunctionDef.args; 1725 args = s->v.AsyncFunctionDef.args;
1691 returns = s->v.AsyncFunctionDef.returns; 1726 returns = s->v.AsyncFunctionDef.returns;
1692 decos = s->v.AsyncFunctionDef.decorator_list; 1727 decos = s->v.AsyncFunctionDef.decorator_list;
1693 name = s->v.AsyncFunctionDef.name; 1728 name = s->v.AsyncFunctionDef.name;
1694 body = s->v.AsyncFunctionDef.body; 1729 body = s->v.AsyncFunctionDef.body;
1695 1730
1696 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; 1731 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
1697 } else { 1732 } else {
1698 assert(s->kind == FunctionDef_kind); 1733 assert(s->kind == FunctionDef_kind);
1699 1734
1700 args = s->v.FunctionDef.args; 1735 args = s->v.FunctionDef.args;
1701 returns = s->v.FunctionDef.returns; 1736 returns = s->v.FunctionDef.returns;
1702 decos = s->v.FunctionDef.decorator_list; 1737 decos = s->v.FunctionDef.decorator_list;
1703 name = s->v.FunctionDef.name; 1738 name = s->v.FunctionDef.name;
1704 body = s->v.FunctionDef.body; 1739 body = s->v.FunctionDef.body;
1705 1740
1706 scope_type = COMPILER_SCOPE_FUNCTION; 1741 scope_type = COMPILER_SCOPE_FUNCTION;
1707 } 1742 }
1708 1743
1709 if (!compiler_decorators(c, decos)) 1744 if (!compiler_decorators(c, decos))
1710 return 0; 1745 return 0;
1711 if (args->defaults) 1746
1712 VISIT_SEQ(c, expr, args->defaults); 1747 funcflags = compiler_default_arguments(c, args);
1713 if (args->kwonlyargs) { 1748 if (funcflags == -1) {
1714 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, 1749 return 0;
1715 args->kw_defaults);
1716 if (res < 0)
1717 return 0;
1718 kw_default_count = res;
1719 } 1750 }
1751
1720 num_annotations = compiler_visit_annotations(c, args, returns); 1752 num_annotations = compiler_visit_annotations(c, args, returns);
1721 if (num_annotations < 0) 1753 if (num_annotations < 0) {
1722 return 0; 1754 return 0;
1723 assert((num_annotations & 0xFFFF) == num_annotations); 1755 }
1756 else if (num_annotations > 0) {
1757 funcflags |= 0x04;
1758 }
1724 1759
1725 if (!compiler_enter_scope(c, name, 1760 if (!compiler_enter_scope(c, name, scope_type, (void *)s, s->lineno)) {
1726 scope_type, (void *)s,
1727 s->lineno))
1728 return 0; 1761 return 0;
1762 }
1729 1763
1730 st = (stmt_ty)asdl_seq_GET(body, 0); 1764 st = (stmt_ty)asdl_seq_GET(body, 0);
1731 docstring = compiler_isdocstring(st); 1765 docstring = compiler_isdocstring(st);
1732 if (docstring && c->c_optimize < 2) { 1766 if (docstring && c->c_optimize < 2) {
1733 if (st->v.Expr.value->kind == Constant_kind) 1767 if (st->v.Expr.value->kind == Constant_kind)
1734 first_const = st->v.Expr.value->v.Constant.value; 1768 first_const = st->v.Expr.value->v.Constant.value;
1735 else 1769 else
1736 first_const = st->v.Expr.value->v.Str.s; 1770 first_const = st->v.Expr.value->v.Str.s;
1737 } 1771 }
1738 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) { 1772 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
(...skipping 12 matching lines...) Expand all
1751 co = assemble(c, 1); 1785 co = assemble(c, 1);
1752 qualname = c->u->u_qualname; 1786 qualname = c->u->u_qualname;
1753 Py_INCREF(qualname); 1787 Py_INCREF(qualname);
1754 compiler_exit_scope(c); 1788 compiler_exit_scope(c);
1755 if (co == NULL) { 1789 if (co == NULL) {
1756 Py_XDECREF(qualname); 1790 Py_XDECREF(qualname);
1757 Py_XDECREF(co); 1791 Py_XDECREF(co);
1758 return 0; 1792 return 0;
1759 } 1793 }
1760 1794
1761 arglength = asdl_seq_LEN(args->defaults);
1762 arglength |= kw_default_count << 8;
1763 arglength |= num_annotations << 16;
1764 if (is_async) 1795 if (is_async)
1765 co->co_flags |= CO_COROUTINE; 1796 co->co_flags |= CO_COROUTINE;
1766 compiler_make_closure(c, co, arglength, qualname); 1797 compiler_make_closure(c, co, funcflags, qualname);
1767 Py_DECREF(qualname); 1798 Py_DECREF(qualname);
1768 Py_DECREF(co); 1799 Py_DECREF(co);
1769 1800
1770 /* decorators */ 1801 /* decorators */
1771 for (i = 0; i < asdl_seq_LEN(decos); i++) { 1802 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1772 ADDOP_I(c, CALL_FUNCTION, 1); 1803 ADDOP_I(c, CALL_FUNCTION, 1);
1773 } 1804 }
1774 1805
1775 return compiler_nameop(c, name, Store); 1806 return compiler_nameop(c, name, Store);
1776 } 1807 }
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
1916 compiler_use_next_block(c, end); 1947 compiler_use_next_block(c, end);
1917 return 1; 1948 return 1;
1918 } 1949 }
1919 1950
1920 static int 1951 static int
1921 compiler_lambda(struct compiler *c, expr_ty e) 1952 compiler_lambda(struct compiler *c, expr_ty e)
1922 { 1953 {
1923 PyCodeObject *co; 1954 PyCodeObject *co;
1924 PyObject *qualname; 1955 PyObject *qualname;
1925 static identifier name; 1956 static identifier name;
1926 int kw_default_count = 0; 1957 Py_ssize_t funcflags;
1927 Py_ssize_t arglength;
1928 arguments_ty args = e->v.Lambda.args; 1958 arguments_ty args = e->v.Lambda.args;
1929 assert(e->kind == Lambda_kind); 1959 assert(e->kind == Lambda_kind);
1930 1960
1931 if (!name) { 1961 if (!name) {
1932 name = PyUnicode_InternFromString("<lambda>"); 1962 name = PyUnicode_InternFromString("<lambda>");
1933 if (!name) 1963 if (!name)
1934 return 0; 1964 return 0;
1935 } 1965 }
1936 1966
1937 if (args->defaults) 1967 funcflags = compiler_default_arguments(c, args);
1938 VISIT_SEQ(c, expr, args->defaults); 1968 if (funcflags == -1) {
1939 if (args->kwonlyargs) { 1969 return 0;
1940 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1941 args->kw_defaults);
1942 if (res < 0) return 0;
1943 kw_default_count = res;
1944 } 1970 }
1971
1945 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, 1972 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
1946 (void *)e, e->lineno)) 1973 (void *)e, e->lineno))
1947 return 0; 1974 return 0;
1948 1975
1949 /* Make None the first constant, so the lambda can't have a 1976 /* Make None the first constant, so the lambda can't have a
1950 docstring. */ 1977 docstring. */
1951 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0) 1978 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)
1952 return 0; 1979 return 0;
1953 1980
1954 c->u->u_argcount = asdl_seq_LEN(args->args); 1981 c->u->u_argcount = asdl_seq_LEN(args->args);
1955 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); 1982 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1956 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); 1983 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1957 if (c->u->u_ste->ste_generator) { 1984 if (c->u->u_ste->ste_generator) {
1958 co = assemble(c, 0); 1985 co = assemble(c, 0);
1959 } 1986 }
1960 else { 1987 else {
1961 ADDOP_IN_SCOPE(c, RETURN_VALUE); 1988 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1962 co = assemble(c, 1); 1989 co = assemble(c, 1);
1963 } 1990 }
1964 qualname = c->u->u_qualname; 1991 qualname = c->u->u_qualname;
1965 Py_INCREF(qualname); 1992 Py_INCREF(qualname);
1966 compiler_exit_scope(c); 1993 compiler_exit_scope(c);
1967 if (co == NULL) 1994 if (co == NULL)
1968 return 0; 1995 return 0;
1969 1996
1970 arglength = asdl_seq_LEN(args->defaults); 1997 compiler_make_closure(c, co, funcflags, qualname);
1971 arglength |= kw_default_count << 8;
1972 compiler_make_closure(c, co, arglength, qualname);
1973 Py_DECREF(qualname); 1998 Py_DECREF(qualname);
1974 Py_DECREF(co); 1999 Py_DECREF(co);
1975 2000
1976 return 1; 2001 return 1;
1977 } 2002 }
1978 2003
1979 static int 2004 static int
1980 compiler_if(struct compiler *c, stmt_ty s) 2005 compiler_if(struct compiler *c, stmt_ty s)
1981 { 2006 {
1982 basicblock *end, *next; 2007 basicblock *end, *next;
(...skipping 2972 matching lines...) Expand 10 before | Expand all | Expand 10 after
4955 return co; 4980 return co;
4956 } 4981 }
4957 4982
4958 #undef PyAST_Compile 4983 #undef PyAST_Compile
4959 PyAPI_FUNC(PyCodeObject *) 4984 PyAPI_FUNC(PyCodeObject *)
4960 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, 4985 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4961 PyArena *arena) 4986 PyArena *arena)
4962 { 4987 {
4963 return PyAST_CompileEx(mod, filename, flags, -1, arena); 4988 return PyAST_CompileEx(mod, filename, flags, -1, arena);
4964 } 4989 }
OLDNEW
« Doc/library/dis.rst ('K') | « Python/ceval.c ('k') | Python/opcode_targets.h » ('j') | no next file with comments »

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