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

Side by Side Diff: Python/compile.c

Issue 27095: Simplify MAKE_FUNCTION
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:
View unified diff | Download patch
« no previous file with comments | « Python/ceval.c ('k') | Python/opcode_targets.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 if (keys != NULL) {
storchaka 2016/06/12 06:55:56 Just Py_XDECREF(keys).
1587 Py_DECREF(keys);
1588 }
1589 return -1;
1560 } 1590 }
1561 1591
1562 static int 1592 static int
1563 compiler_visit_argannotation(struct compiler *c, identifier id, 1593 compiler_visit_argannotation(struct compiler *c, identifier id,
1564 expr_ty annotation, PyObject *names) 1594 expr_ty annotation, PyObject* keylist)
storchaka 2016/06/12 06:55:56 It looks to me that this function can be left unch
1565 { 1595 {
1566 if (annotation) { 1596 if (annotation) {
1567 PyObject *mangled; 1597 int res;
1568 VISIT(c, expr, annotation); 1598 PyObject *mangled = _Py_Mangle(c->u->u_private, id);
1569 mangled = _Py_Mangle(c->u->u_private, id); 1599 if (!mangled) {
1570 if (!mangled)
1571 return 0;
1572 if (PyList_Append(names, mangled) < 0) {
1573 Py_DECREF(mangled);
1574 return 0; 1600 return 0;
1575 } 1601 }
1602 res = PyList_Append(keylist, mangled);
1576 Py_DECREF(mangled); 1603 Py_DECREF(mangled);
1604 if (res == -1) {
1605 return 0;
1606 }
1607 VISIT(c, expr, annotation);
1577 } 1608 }
1578 return 1; 1609 return 1;
1579 } 1610 }
1580 1611
1581 static int 1612 static int
1582 compiler_visit_argannotations(struct compiler *c, asdl_seq* args, 1613 compiler_visit_argannotations(struct compiler *c, asdl_seq *args, PyObject *keyl ist)
storchaka 2016/06/12 06:55:56 It looks to me that this function can be left unch
1583 PyObject *names)
1584 { 1614 {
1585 int i; 1615 int i;
1586 for (i = 0; i < asdl_seq_LEN(args); i++) { 1616 Py_ssize_t arglen = asdl_seq_LEN(args);
1617 for (i = 0; i < arglen; i++) {
1587 arg_ty arg = (arg_ty)asdl_seq_GET(args, i); 1618 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1588 if (!compiler_visit_argannotation( 1619 int res = compiler_visit_argannotation(c, arg->arg,
1589 c, 1620 arg->annotation, keylist);
1590 arg->arg, 1621 if (res == 0) {
1591 arg->annotation,
1592 names))
1593 return 0; 1622 return 0;
1623 }
1594 } 1624 }
1625
1595 return 1; 1626 return 1;
1596 } 1627 }
1597 1628
1598 static int 1629 static Py_ssize_t
1599 compiler_visit_annotations(struct compiler *c, arguments_ty args, 1630 compiler_visit_annotations(struct compiler *c, arguments_ty args,
1600 expr_ty returns) 1631 expr_ty returns)
1601 { 1632 {
1602 /* Push arg annotations and a list of the argument names. Return the # 1633 /* Push arg annotation dict. Return # of items pushed.
1603 of items pushed. The expressions are evaluated out-of-order wrt the 1634 The expressions are evaluated out-of-order wrt the source code.
1604 source code.
1605 1635
1606 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error. 1636 Returns -1 on error.
1607 */ 1637 */
1608 static identifier return_str; 1638 static identifier return_str;
1609 PyObject *names;
1610 Py_ssize_t len; 1639 Py_ssize_t len;
1611 names = PyList_New(0); 1640 PyObject* keylist = PyList_New(0);
storchaka 2016/06/12 06:55:56 I think the large part of this function can be lef
1612 if (!names) 1641
1642 if (keylist == NULL) {
1613 return -1; 1643 return -1;
1644 }
1614 1645
1615 if (!compiler_visit_argannotations(c, args->args, names)) 1646 if (compiler_visit_argannotations(c, args->args, keylist) == 0) {
1616 goto error; 1647 goto error;
1648 }
1649
1617 if (args->vararg && args->vararg->annotation && 1650 if (args->vararg && args->vararg->annotation &&
1618 !compiler_visit_argannotation(c, args->vararg->arg, 1651 compiler_visit_argannotation(c, args->vararg->arg,
1619 args->vararg->annotation, names)) 1652 args->vararg->annotation, keylist) == 0) {
1620 goto error; 1653 goto error;
1621 if (!compiler_visit_argannotations(c, args->kwonlyargs, names)) 1654 }
1655
1656 if (compiler_visit_argannotations(c, args->kwonlyargs, keylist) == 0) {
1622 goto error; 1657 goto error;
1658 }
1659
1623 if (args->kwarg && args->kwarg->annotation && 1660 if (args->kwarg && args->kwarg->annotation &&
1624 !compiler_visit_argannotation(c, args->kwarg->arg, 1661 compiler_visit_argannotation(c, args->kwarg->arg,
1625 args->kwarg->annotation, names)) 1662 args->kwarg->annotation, keylist) == 0) {
1626 goto error; 1663 goto error;
1664 }
1627 1665
1628 if (!return_str) { 1666 if (!return_str) {
1629 return_str = PyUnicode_InternFromString("return"); 1667 return_str = PyUnicode_InternFromString("return");
1630 if (!return_str) 1668 if (!return_str) {
1631 goto error; 1669 goto error;
1670 }
1632 } 1671 }
1633 if (!compiler_visit_argannotation(c, return_str, returns, names)) { 1672
1673 if (compiler_visit_argannotation(c, return_str, returns, keylist) == 0) {
1634 goto error; 1674 goto error;
1635 } 1675 }
1636 1676
1637 len = PyList_GET_SIZE(names); 1677 len = PyList_Size(keylist);
1638 if (len > 65534) { 1678 if (len != 0) {
1639 /* len must fit in 16 bits, and len is incremented below */ 1679 PyObject *keytuple = PyList_AsTuple(keylist);
1640 PyErr_SetString(PyExc_SyntaxError, 1680 if (keytuple == NULL) {
1641 "too many annotations"); 1681 goto error;
1642 goto error; 1682 }
1683 ADDOP_N(c, LOAD_CONST, keytuple, consts);
storchaka 2016/06/12 06:55:56 ADDOP_N and ADDOP_I can return from the function a
1684 ADDOP_I(c, BUILD_CONST_KEY_MAP, len);
1643 } 1685 }
1644 if (len) { 1686 Py_DECREF(keylist);
1645 /* convert names to a tuple and place on stack */ 1687 return len;
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 }
1660 Py_DECREF(names);
1661
1662 /* We just checked that len <= 65535, see above */
1663 return Py_SAFE_DOWNCAST(len, Py_ssize_t, int);
1664 1688
1665 error: 1689 error:
1666 Py_DECREF(names); 1690 Py_DECREF(keylist);
1667 return -1; 1691 return -1;
1692 }
1693
1694 static Py_ssize_t
1695 compiler_default_arguments(struct compiler *c, arguments_ty args)
1696 {
1697 Py_ssize_t funcflags = 0;
1698 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
1699 VISIT_SEQ(c, expr, args->defaults);
1700 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
1701 funcflags |= 0x01;
1702 }
1703 if (args->kwonlyargs) {
1704 Py_ssize_t res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1705 args->kw_defaults);
1706 if (res < 0) {
1707 return -1;
1708 }
1709 else if (res > 0) {
1710 funcflags |= 0x02;
1711 }
1712 }
1713 return funcflags;
1668 } 1714 }
1669 1715
1670 static int 1716 static int
1671 compiler_function(struct compiler *c, stmt_ty s, int is_async) 1717 compiler_function(struct compiler *c, stmt_ty s, int is_async)
1672 { 1718 {
1673 PyCodeObject *co; 1719 PyCodeObject *co;
1674 PyObject *qualname, *first_const = Py_None; 1720 PyObject *qualname, *first_const = Py_None;
1675 arguments_ty args; 1721 arguments_ty args;
1676 expr_ty returns; 1722 expr_ty returns;
1677 identifier name; 1723 identifier name;
1678 asdl_seq* decos; 1724 asdl_seq* decos;
1679 asdl_seq *body; 1725 asdl_seq *body;
1680 stmt_ty st; 1726 stmt_ty st;
1681 Py_ssize_t i, n, arglength; 1727 Py_ssize_t i, n, funcflags;
1682 int docstring, kw_default_count = 0; 1728 int docstring;
1683 int num_annotations; 1729 int num_annotations;
1684 int scope_type; 1730 int scope_type;
1685
1686 1731
1687 if (is_async) { 1732 if (is_async) {
1688 assert(s->kind == AsyncFunctionDef_kind); 1733 assert(s->kind == AsyncFunctionDef_kind);
1689 1734
1690 args = s->v.AsyncFunctionDef.args; 1735 args = s->v.AsyncFunctionDef.args;
1691 returns = s->v.AsyncFunctionDef.returns; 1736 returns = s->v.AsyncFunctionDef.returns;
1692 decos = s->v.AsyncFunctionDef.decorator_list; 1737 decos = s->v.AsyncFunctionDef.decorator_list;
1693 name = s->v.AsyncFunctionDef.name; 1738 name = s->v.AsyncFunctionDef.name;
1694 body = s->v.AsyncFunctionDef.body; 1739 body = s->v.AsyncFunctionDef.body;
1695 1740
1696 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; 1741 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
1697 } else { 1742 } else {
1698 assert(s->kind == FunctionDef_kind); 1743 assert(s->kind == FunctionDef_kind);
1699 1744
1700 args = s->v.FunctionDef.args; 1745 args = s->v.FunctionDef.args;
1701 returns = s->v.FunctionDef.returns; 1746 returns = s->v.FunctionDef.returns;
1702 decos = s->v.FunctionDef.decorator_list; 1747 decos = s->v.FunctionDef.decorator_list;
1703 name = s->v.FunctionDef.name; 1748 name = s->v.FunctionDef.name;
1704 body = s->v.FunctionDef.body; 1749 body = s->v.FunctionDef.body;
1705 1750
1706 scope_type = COMPILER_SCOPE_FUNCTION; 1751 scope_type = COMPILER_SCOPE_FUNCTION;
1707 } 1752 }
1708 1753
1709 if (!compiler_decorators(c, decos)) 1754 if (!compiler_decorators(c, decos))
1710 return 0; 1755 return 0;
1711 if (args->defaults) 1756
1712 VISIT_SEQ(c, expr, args->defaults); 1757 funcflags = compiler_default_arguments(c, args);
1713 if (args->kwonlyargs) { 1758 if (funcflags == -1) {
1714 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, 1759 return 0;
1715 args->kw_defaults);
1716 if (res < 0)
1717 return 0;
1718 kw_default_count = res;
1719 } 1760 }
1761
1720 num_annotations = compiler_visit_annotations(c, args, returns); 1762 num_annotations = compiler_visit_annotations(c, args, returns);
1721 if (num_annotations < 0) 1763 if (num_annotations < 0) {
1722 return 0; 1764 return 0;
1723 assert((num_annotations & 0xFFFF) == num_annotations); 1765 }
1766 else if (num_annotations > 0) {
1767 funcflags |= 0x04;
1768 }
1724 1769
1725 if (!compiler_enter_scope(c, name, 1770 if (!compiler_enter_scope(c, name, scope_type, (void *)s, s->lineno)) {
1726 scope_type, (void *)s,
1727 s->lineno))
1728 return 0; 1771 return 0;
1772 }
1729 1773
1730 st = (stmt_ty)asdl_seq_GET(body, 0); 1774 st = (stmt_ty)asdl_seq_GET(body, 0);
1731 docstring = compiler_isdocstring(st); 1775 docstring = compiler_isdocstring(st);
1732 if (docstring && c->c_optimize < 2) { 1776 if (docstring && c->c_optimize < 2) {
1733 if (st->v.Expr.value->kind == Constant_kind) 1777 if (st->v.Expr.value->kind == Constant_kind)
1734 first_const = st->v.Expr.value->v.Constant.value; 1778 first_const = st->v.Expr.value->v.Constant.value;
1735 else 1779 else
1736 first_const = st->v.Expr.value->v.Str.s; 1780 first_const = st->v.Expr.value->v.Str.s;
1737 } 1781 }
1738 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) { 1782 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
(...skipping 12 matching lines...) Expand all
1751 co = assemble(c, 1); 1795 co = assemble(c, 1);
1752 qualname = c->u->u_qualname; 1796 qualname = c->u->u_qualname;
1753 Py_INCREF(qualname); 1797 Py_INCREF(qualname);
1754 compiler_exit_scope(c); 1798 compiler_exit_scope(c);
1755 if (co == NULL) { 1799 if (co == NULL) {
1756 Py_XDECREF(qualname); 1800 Py_XDECREF(qualname);
1757 Py_XDECREF(co); 1801 Py_XDECREF(co);
1758 return 0; 1802 return 0;
1759 } 1803 }
1760 1804
1761 arglength = asdl_seq_LEN(args->defaults);
1762 arglength |= kw_default_count << 8;
1763 arglength |= num_annotations << 16;
1764 if (is_async) 1805 if (is_async)
1765 co->co_flags |= CO_COROUTINE; 1806 co->co_flags |= CO_COROUTINE;
1766 compiler_make_closure(c, co, arglength, qualname); 1807 compiler_make_closure(c, co, funcflags, qualname);
1767 Py_DECREF(qualname); 1808 Py_DECREF(qualname);
1768 Py_DECREF(co); 1809 Py_DECREF(co);
1769 1810
1770 /* decorators */ 1811 /* decorators */
1771 for (i = 0; i < asdl_seq_LEN(decos); i++) { 1812 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1772 ADDOP_I(c, CALL_FUNCTION, 1); 1813 ADDOP_I(c, CALL_FUNCTION, 1);
1773 } 1814 }
1774 1815
1775 return compiler_nameop(c, name, Store); 1816 return compiler_nameop(c, name, Store);
1776 } 1817 }
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
1916 compiler_use_next_block(c, end); 1957 compiler_use_next_block(c, end);
1917 return 1; 1958 return 1;
1918 } 1959 }
1919 1960
1920 static int 1961 static int
1921 compiler_lambda(struct compiler *c, expr_ty e) 1962 compiler_lambda(struct compiler *c, expr_ty e)
1922 { 1963 {
1923 PyCodeObject *co; 1964 PyCodeObject *co;
1924 PyObject *qualname; 1965 PyObject *qualname;
1925 static identifier name; 1966 static identifier name;
1926 int kw_default_count = 0; 1967 Py_ssize_t funcflags;
1927 Py_ssize_t arglength;
1928 arguments_ty args = e->v.Lambda.args; 1968 arguments_ty args = e->v.Lambda.args;
1929 assert(e->kind == Lambda_kind); 1969 assert(e->kind == Lambda_kind);
1930 1970
1931 if (!name) { 1971 if (!name) {
1932 name = PyUnicode_InternFromString("<lambda>"); 1972 name = PyUnicode_InternFromString("<lambda>");
1933 if (!name) 1973 if (!name)
1934 return 0; 1974 return 0;
1935 } 1975 }
1936 1976
1937 if (args->defaults) 1977 funcflags = compiler_default_arguments(c, args);
1938 VISIT_SEQ(c, expr, args->defaults); 1978 if (funcflags == -1) {
1939 if (args->kwonlyargs) { 1979 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 } 1980 }
1981
1945 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, 1982 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
1946 (void *)e, e->lineno)) 1983 (void *)e, e->lineno))
1947 return 0; 1984 return 0;
1948 1985
1949 /* Make None the first constant, so the lambda can't have a 1986 /* Make None the first constant, so the lambda can't have a
1950 docstring. */ 1987 docstring. */
1951 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0) 1988 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)
1952 return 0; 1989 return 0;
1953 1990
1954 c->u->u_argcount = asdl_seq_LEN(args->args); 1991 c->u->u_argcount = asdl_seq_LEN(args->args);
1955 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); 1992 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1956 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); 1993 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1957 if (c->u->u_ste->ste_generator) { 1994 if (c->u->u_ste->ste_generator) {
1958 co = assemble(c, 0); 1995 co = assemble(c, 0);
1959 } 1996 }
1960 else { 1997 else {
1961 ADDOP_IN_SCOPE(c, RETURN_VALUE); 1998 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1962 co = assemble(c, 1); 1999 co = assemble(c, 1);
1963 } 2000 }
1964 qualname = c->u->u_qualname; 2001 qualname = c->u->u_qualname;
1965 Py_INCREF(qualname); 2002 Py_INCREF(qualname);
1966 compiler_exit_scope(c); 2003 compiler_exit_scope(c);
1967 if (co == NULL) 2004 if (co == NULL)
1968 return 0; 2005 return 0;
1969 2006
1970 arglength = asdl_seq_LEN(args->defaults); 2007 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); 2008 Py_DECREF(qualname);
1974 Py_DECREF(co); 2009 Py_DECREF(co);
1975 2010
1976 return 1; 2011 return 1;
1977 } 2012 }
1978 2013
1979 static int 2014 static int
1980 compiler_if(struct compiler *c, stmt_ty s) 2015 compiler_if(struct compiler *c, stmt_ty s)
1981 { 2016 {
1982 basicblock *end, *next; 2017 basicblock *end, *next;
(...skipping 2972 matching lines...) Expand 10 before | Expand all | Expand 10 after
4955 return co; 4990 return co;
4956 } 4991 }
4957 4992
4958 #undef PyAST_Compile 4993 #undef PyAST_Compile
4959 PyAPI_FUNC(PyCodeObject *) 4994 PyAPI_FUNC(PyCodeObject *)
4960 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, 4995 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4961 PyArena *arena) 4996 PyArena *arena)
4962 { 4997 {
4963 return PyAST_CompileEx(mod, filename, flags, -1, arena); 4998 return PyAST_CompileEx(mod, filename, flags, -1, arena);
4964 } 4999 }
OLDNEW
« no previous file with comments | « Python/ceval.c ('k') | Python/opcode_targets.h » ('j') | no next file with comments »

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