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

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 1010 matching lines...) Expand 10 before | Expand all | Expand 10 after
1021 case RAISE_VARARGS: 1021 case RAISE_VARARGS:
1022 return -oparg; 1022 return -oparg;
1023 #define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256)) 1023 #define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
1024 case CALL_FUNCTION: 1024 case CALL_FUNCTION:
1025 return -NARGS(oparg); 1025 return -NARGS(oparg);
1026 case CALL_FUNCTION_VAR: 1026 case CALL_FUNCTION_VAR:
1027 case CALL_FUNCTION_KW: 1027 case CALL_FUNCTION_KW:
1028 return -NARGS(oparg)-1; 1028 return -NARGS(oparg)-1;
1029 case CALL_FUNCTION_VAR_KW: 1029 case CALL_FUNCTION_VAR_KW:
1030 return -NARGS(oparg)-2; 1030 return -NARGS(oparg)-2;
1031 #undef NARGS
1031 case MAKE_FUNCTION: 1032 case MAKE_FUNCTION:
1032 return -1 -NARGS(oparg) - ((oparg >> 16) & 0xffff); 1033 return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1033 case MAKE_CLOSURE: 1034 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
1034 return -2 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
1035 #undef NARGS
1036 case BUILD_SLICE: 1035 case BUILD_SLICE:
1037 if (oparg == 3) 1036 if (oparg == 3)
1038 return -2; 1037 return -2;
1039 else 1038 else
1040 return -1; 1039 return -1;
1041 1040
1042 case LOAD_CLOSURE: 1041 case LOAD_CLOSURE:
1043 return 1; 1042 return 1;
1044 case LOAD_DEREF: 1043 case LOAD_DEREF:
1045 case LOAD_CLASSDEREF: 1044 case LOAD_CLASSDEREF:
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after
1416 if (k == NULL) 1415 if (k == NULL)
1417 return -1; 1416 return -1;
1418 v = PyDict_GetItem(dict, k); 1417 v = PyDict_GetItem(dict, k);
1419 Py_DECREF(k); 1418 Py_DECREF(k);
1420 if (v == NULL) 1419 if (v == NULL)
1421 return -1; 1420 return -1;
1422 return PyLong_AS_LONG(v); 1421 return PyLong_AS_LONG(v);
1423 } 1422 }
1424 1423
1425 static int 1424 static int
1426 compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t args, PyO bject *qualname) 1425 compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t args, PyO bject *qualname)
storchaka 2016/05/27 22:13:11 Maybe rename args to flags?
1427 { 1426 {
1428 Py_ssize_t i, free = PyCode_GetNumFree(co); 1427 Py_ssize_t i, free = PyCode_GetNumFree(co);
1429 if (qualname == NULL) 1428 if (qualname == NULL)
1430 qualname = co->co_name; 1429 qualname = co->co_name;
1431 1430
1432 if (free == 0) { 1431 if (free) {
storchaka 2016/05/27 22:13:11 The patch would be smaller if move "if (free) {" a
1433 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts); 1432 for (i = 0; i < free; ++i) {
1434 ADDOP_O(c, LOAD_CONST, qualname, consts); 1433 /* Bypass com_addop_varname because it will generate
1435 ADDOP_I(c, MAKE_FUNCTION, args); 1434 LOAD_DEREF but LOAD_CLOSURE is needed.
1436 return 1; 1435 */
1436 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1437 int arg, reftype;
1438
1439 /* Special case: If a class contains a method with a
1440 free variable that has the same name as a method,
1441 the name will be considered free *and* local in the
1442 class. It should be handled by the closure, as
1443 well as by the normal name loookup logic.
1444 */
1445 reftype = get_ref_type(c, name);
1446 if (reftype == CELL)
1447 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1448 else /* (reftype == FREE) */
1449 arg = compiler_lookup_arg(c->u->u_freevars, name);
1450 if (arg == -1) {
1451 fprintf(stderr,
1452 "lookup %s in %s %d %d\n"
1453 "freevars of %s: %s\n",
1454 PyUnicode_AsUTF8(PyObject_Repr(name)),
1455 PyUnicode_AsUTF8(c->u->u_name),
1456 reftype, arg,
1457 PyUnicode_AsUTF8(co->co_name),
1458 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));
1459 Py_FatalError("compiler_make_closure()");
1460 }
1461 ADDOP_I(c, LOAD_CLOSURE, arg);
1462 }
1463 args |= 0x08;
1464 ADDOP_I(c, BUILD_TUPLE, free);
1437 } 1465 }
1438 for (i = 0; i < free; ++i) {
1439 /* Bypass com_addop_varname because it will generate
1440 LOAD_DEREF but LOAD_CLOSURE is needed.
1441 */
1442 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1443 int arg, reftype;
1444
1445 /* Special case: If a class contains a method with a
1446 free variable that has the same name as a method,
1447 the name will be considered free *and* local in the
1448 class. It should be handled by the closure, as
1449 well as by the normal name loookup logic.
1450 */
1451 reftype = get_ref_type(c, name);
1452 if (reftype == CELL)
1453 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1454 else /* (reftype == FREE) */
1455 arg = compiler_lookup_arg(c->u->u_freevars, name);
1456 if (arg == -1) {
1457 fprintf(stderr,
1458 "lookup %s in %s %d %d\n"
1459 "freevars of %s: %s\n",
1460 PyUnicode_AsUTF8(PyObject_Repr(name)),
1461 PyUnicode_AsUTF8(c->u->u_name),
1462 reftype, arg,
1463 PyUnicode_AsUTF8(co->co_name),
1464 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));
1465 Py_FatalError("compiler_make_closure()");
1466 }
1467 ADDOP_I(c, LOAD_CLOSURE, arg);
1468 }
1469 ADDOP_I(c, BUILD_TUPLE, free);
1470 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts); 1466 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1471 ADDOP_O(c, LOAD_CONST, qualname, consts); 1467 ADDOP_O(c, LOAD_CONST, qualname, consts);
1472 ADDOP_I(c, MAKE_CLOSURE, args); 1468 ADDOP_I(c, MAKE_FUNCTION, args);
1473 return 1; 1469 return 1;
1474 } 1470 }
1475 1471
1476 static int 1472 static int
1477 compiler_decorators(struct compiler *c, asdl_seq* decos) 1473 compiler_decorators(struct compiler *c, asdl_seq* decos)
1478 { 1474 {
1479 int i; 1475 int i;
1480 1476
1481 if (!decos) 1477 if (!decos)
1482 return 1; 1478 return 1;
1483 1479
1484 for (i = 0; i < asdl_seq_LEN(decos); i++) { 1480 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1485 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); 1481 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1486 } 1482 }
1487 return 1; 1483 return 1;
1488 } 1484 }
1489 1485
1490 static int 1486 static int
1491 compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs, 1487 compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
1492 asdl_seq *kw_defaults) 1488 asdl_seq *kw_defaults)
1493 { 1489 {
1494 int i, default_count = 0; 1490 int i, default_count = 0;
1495 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { 1491 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1496 arg_ty arg = asdl_seq_GET(kwonlyargs, i); 1492 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1497 expr_ty default_ = asdl_seq_GET(kw_defaults, i); 1493 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1498 if (default_) { 1494 if (default_) {
1499 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); 1495 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
1500 if (!mangled) 1496 if (!mangled) {
storchaka 2016/05/27 22:13:11 This is unrelated change (since the body of the lo
1501 return -1; 1497 return -1;
1498 }
1502 ADDOP_O(c, LOAD_CONST, mangled, consts); 1499 ADDOP_O(c, LOAD_CONST, mangled, consts);
1503 Py_DECREF(mangled); 1500 Py_DECREF(mangled);
1504 if (!compiler_visit_expr(c, default_)) { 1501 if (!compiler_visit_expr(c, default_)) {
1505 return -1; 1502 return -1;
1506 } 1503 }
1507 default_count++; 1504 default_count++;
1508 } 1505 }
1506 }
1507 if (default_count > 0) {
1508 ADDOP_I(c, BUILD_MAP, default_count);
1509 } 1509 }
1510 return default_count; 1510 return default_count;
1511 } 1511 }
1512 1512
1513 static int 1513 static int
1514 compiler_visit_argannotation(struct compiler *c, identifier id, 1514 compiler_visit_argannotation(struct compiler *c, identifier id,
1515 expr_ty annotation, PyObject *names) 1515 expr_ty annotation)
1516 { 1516 {
1517 if (annotation) { 1517 if (annotation) {
1518 PyObject *mangled; 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);
1519 VISIT(c, expr, annotation); 1524 VISIT(c, expr, annotation);
1520 mangled = _Py_Mangle(c->u->u_private, id); 1525 return 1;
1521 if (!mangled) 1526 }
1522 return 0; 1527 return 0;
1523 if (PyList_Append(names, mangled) < 0) { 1528 }
1524 Py_DECREF(mangled); 1529
1525 return 0; 1530 static Py_ssize_t
1531 compiler_visit_argannotations(struct compiler *c, asdl_seq* args)
1532 {
1533 int i;
1534 Py_ssize_t annonum = 0, arglen = asdl_seq_LEN(args);
1535 for (i = 0; i < arglen; i++) {
1536 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1537 Py_ssize_t annoret = compiler_visit_argannotation(c, arg->arg,
1538 arg->annotation);
1539 if (annoret == -1) {
1540 return -1;
1526 } 1541 }
1527 Py_DECREF(mangled); 1542 annonum += annoret;
1528 } 1543 }
1529 return 1; 1544
1545 return annonum;
1546 }
1547
1548 static Py_ssize_t
1549 compiler_visit_annotations(struct compiler *c, arguments_ty args,
1550 expr_ty returns)
1551 {
1552 /* Push arg annotation dict. Return # of items pushed.
1553 The expressions are evaluated out-of-order wrt the source code.
1554
1555 Returns -1 on error.
1556 */
1557 static identifier return_str;
1558 Py_ssize_t len = 0, annoret;
1559
1560 annoret = compiler_visit_argannotations(c, args->args);
1561 if (annoret == -1) {
1562 return -1;
1563 }
1564 len += annoret;
1565
1566 if (args->vararg && args->vararg->annotation) {
1567 annoret = compiler_visit_argannotation(c, args->vararg->arg,
1568 args->vararg->annotation);
1569 if (annoret == -1) {
1570 return -1;
1571 }
1572 len += annoret;
1573 }
1574
1575 annoret = compiler_visit_argannotations(c, args->kwonlyargs);
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
1590 if (!return_str) {
1591 return_str = PyUnicode_InternFromString("return");
1592 if (!return_str) {
1593 return -1;
1594 }
1595 }
1596
1597 annoret = compiler_visit_argannotation(c, return_str, returns);
1598 if (annoret == -1) {
1599 return -1;
1600 }
1601 len += annoret;
1602
1603 if (len != 0) {
1604 ADDOP_I(c, BUILD_MAP, len);
1605 }
1606 return len;
1607 }
1608
1609 static Py_ssize_t
1610 compiler_default_arguments(struct compiler *c, arguments_ty args)
1611 {
1612 Py_ssize_t funcflags = 0;
1613 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
1614 VISIT_SEQ(c, expr, args->defaults);
1615 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
1616 funcflags |= 0x01;
1617 }
1618 if (args->kwonlyargs) {
1619 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1620 args->kw_defaults);
1621 if (res < 0) {
1622 return -1;
1623 }
1624 else if (res > 0) {
1625 funcflags |= 0x02;
1626 }
1627 }
1628 return funcflags;
1530 } 1629 }
1531 1630
1532 static int 1631 static int
1533 compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1534 PyObject *names)
1535 {
1536 int i;
1537 for (i = 0; i < asdl_seq_LEN(args); i++) {
1538 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1539 if (!compiler_visit_argannotation(
1540 c,
1541 arg->arg,
1542 arg->annotation,
1543 names))
1544 return 0;
1545 }
1546 return 1;
1547 }
1548
1549 static int
1550 compiler_visit_annotations(struct compiler *c, arguments_ty args,
1551 expr_ty returns)
1552 {
1553 /* Push arg annotations and a list of the argument names. Return the #
1554 of items pushed. The expressions are evaluated out-of-order wrt the
1555 source code.
1556
1557 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1558 */
1559 static identifier return_str;
1560 PyObject *names;
1561 Py_ssize_t len;
1562 names = PyList_New(0);
1563 if (!names)
1564 return -1;
1565
1566 if (!compiler_visit_argannotations(c, args->args, names))
1567 goto error;
1568 if (args->vararg && args->vararg->annotation &&
1569 !compiler_visit_argannotation(c, args->vararg->arg,
1570 args->vararg->annotation, names))
1571 goto error;
1572 if (!compiler_visit_argannotations(c, args->kwonlyargs, names))
1573 goto error;
1574 if (args->kwarg && args->kwarg->annotation &&
1575 !compiler_visit_argannotation(c, args->kwarg->arg,
1576 args->kwarg->annotation, names))
1577 goto error;
1578
1579 if (!return_str) {
1580 return_str = PyUnicode_InternFromString("return");
1581 if (!return_str)
1582 goto error;
1583 }
1584 if (!compiler_visit_argannotation(c, return_str, returns, names)) {
1585 goto error;
1586 }
1587
1588 len = PyList_GET_SIZE(names);
1589 if (len > 65534) {
1590 /* len must fit in 16 bits, and len is incremented below */
1591 PyErr_SetString(PyExc_SyntaxError,
1592 "too many annotations");
1593 goto error;
1594 }
1595 if (len) {
1596 /* convert names to a tuple and place on stack */
1597 PyObject *elt;
1598 Py_ssize_t i;
1599 PyObject *s = PyTuple_New(len);
1600 if (!s)
1601 goto error;
1602 for (i = 0; i < len; i++) {
1603 elt = PyList_GET_ITEM(names, i);
1604 Py_INCREF(elt);
1605 PyTuple_SET_ITEM(s, i, elt);
1606 }
1607 ADDOP_O(c, LOAD_CONST, s, consts);
1608 Py_DECREF(s);
1609 len++; /* include the just-pushed tuple */
1610 }
1611 Py_DECREF(names);
1612
1613 /* We just checked that len <= 65535, see above */
1614 return Py_SAFE_DOWNCAST(len, Py_ssize_t, int);
1615
1616 error:
1617 Py_DECREF(names);
1618 return -1;
1619 }
1620
1621 static int
1622 compiler_function(struct compiler *c, stmt_ty s, int is_async) 1632 compiler_function(struct compiler *c, stmt_ty s, int is_async)
1623 { 1633 {
1624 PyCodeObject *co; 1634 PyCodeObject *co;
1625 PyObject *qualname, *first_const = Py_None; 1635 PyObject *qualname, *first_const = Py_None;
1626 arguments_ty args; 1636 arguments_ty args;
1627 expr_ty returns; 1637 expr_ty returns;
1628 identifier name; 1638 identifier name;
1629 asdl_seq* decos; 1639 asdl_seq* decos;
1630 asdl_seq *body; 1640 asdl_seq *body;
1631 stmt_ty st; 1641 stmt_ty st;
1632 Py_ssize_t i, n, arglength; 1642 Py_ssize_t i, n, funcflags;
1633 int docstring, kw_default_count = 0; 1643 int docstring;
1634 int num_annotations; 1644 int num_annotations;
1635 int scope_type; 1645 int scope_type;
1636
1637 1646
1638 if (is_async) { 1647 if (is_async) {
1639 assert(s->kind == AsyncFunctionDef_kind); 1648 assert(s->kind == AsyncFunctionDef_kind);
1640 1649
1641 args = s->v.AsyncFunctionDef.args; 1650 args = s->v.AsyncFunctionDef.args;
1642 returns = s->v.AsyncFunctionDef.returns; 1651 returns = s->v.AsyncFunctionDef.returns;
1643 decos = s->v.AsyncFunctionDef.decorator_list; 1652 decos = s->v.AsyncFunctionDef.decorator_list;
1644 name = s->v.AsyncFunctionDef.name; 1653 name = s->v.AsyncFunctionDef.name;
1645 body = s->v.AsyncFunctionDef.body; 1654 body = s->v.AsyncFunctionDef.body;
1646 1655
1647 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; 1656 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
1648 } else { 1657 } else {
1649 assert(s->kind == FunctionDef_kind); 1658 assert(s->kind == FunctionDef_kind);
1650 1659
1651 args = s->v.FunctionDef.args; 1660 args = s->v.FunctionDef.args;
1652 returns = s->v.FunctionDef.returns; 1661 returns = s->v.FunctionDef.returns;
1653 decos = s->v.FunctionDef.decorator_list; 1662 decos = s->v.FunctionDef.decorator_list;
1654 name = s->v.FunctionDef.name; 1663 name = s->v.FunctionDef.name;
1655 body = s->v.FunctionDef.body; 1664 body = s->v.FunctionDef.body;
1656 1665
1657 scope_type = COMPILER_SCOPE_FUNCTION; 1666 scope_type = COMPILER_SCOPE_FUNCTION;
1658 } 1667 }
1659 1668
1660 if (!compiler_decorators(c, decos)) 1669 if (!compiler_decorators(c, decos))
1661 return 0; 1670 return 0;
1662 if (args->defaults) 1671
1663 VISIT_SEQ(c, expr, args->defaults); 1672 funcflags = compiler_default_arguments(c, args);
1664 if (args->kwonlyargs) { 1673 if (funcflags == -1) {
1665 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, 1674 return 0;
1666 args->kw_defaults);
1667 if (res < 0)
1668 return 0;
1669 kw_default_count = res;
1670 } 1675 }
1676
1671 num_annotations = compiler_visit_annotations(c, args, returns); 1677 num_annotations = compiler_visit_annotations(c, args, returns);
1672 if (num_annotations < 0) 1678 if (num_annotations < 0) {
1673 return 0; 1679 return 0;
1674 assert((num_annotations & 0xFFFF) == num_annotations); 1680 }
1681 else if (num_annotations > 0) {
1682 funcflags |= 0x04;
1683 }
1675 1684
1676 if (!compiler_enter_scope(c, name, 1685 if (!compiler_enter_scope(c, name, scope_type, (void *)s, s->lineno)) {
1677 scope_type, (void *)s,
1678 s->lineno))
1679 return 0; 1686 return 0;
1687 }
1680 1688
1681 st = (stmt_ty)asdl_seq_GET(body, 0); 1689 st = (stmt_ty)asdl_seq_GET(body, 0);
1682 docstring = compiler_isdocstring(st); 1690 docstring = compiler_isdocstring(st);
1683 if (docstring && c->c_optimize < 2) { 1691 if (docstring && c->c_optimize < 2) {
1684 if (st->v.Expr.value->kind == Constant_kind) 1692 if (st->v.Expr.value->kind == Constant_kind)
1685 first_const = st->v.Expr.value->v.Constant.value; 1693 first_const = st->v.Expr.value->v.Constant.value;
1686 else 1694 else
1687 first_const = st->v.Expr.value->v.Str.s; 1695 first_const = st->v.Expr.value->v.Str.s;
1688 } 1696 }
1689 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) { 1697 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
(...skipping 12 matching lines...) Expand all
1702 co = assemble(c, 1); 1710 co = assemble(c, 1);
1703 qualname = c->u->u_qualname; 1711 qualname = c->u->u_qualname;
1704 Py_INCREF(qualname); 1712 Py_INCREF(qualname);
1705 compiler_exit_scope(c); 1713 compiler_exit_scope(c);
1706 if (co == NULL) { 1714 if (co == NULL) {
1707 Py_XDECREF(qualname); 1715 Py_XDECREF(qualname);
1708 Py_XDECREF(co); 1716 Py_XDECREF(co);
1709 return 0; 1717 return 0;
1710 } 1718 }
1711 1719
1712 arglength = asdl_seq_LEN(args->defaults);
1713 arglength |= kw_default_count << 8;
1714 arglength |= num_annotations << 16;
1715 if (is_async) 1720 if (is_async)
1716 co->co_flags |= CO_COROUTINE; 1721 co->co_flags |= CO_COROUTINE;
1717 compiler_make_closure(c, co, arglength, qualname); 1722 compiler_make_closure(c, co, funcflags, qualname);
1718 Py_DECREF(qualname); 1723 Py_DECREF(qualname);
1719 Py_DECREF(co); 1724 Py_DECREF(co);
1720 1725
1721 /* decorators */ 1726 /* decorators */
1722 for (i = 0; i < asdl_seq_LEN(decos); i++) { 1727 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1723 ADDOP_I(c, CALL_FUNCTION, 1); 1728 ADDOP_I(c, CALL_FUNCTION, 1);
1724 } 1729 }
1725 1730
1726 return compiler_nameop(c, name, Store); 1731 return compiler_nameop(c, name, Store);
1727 } 1732 }
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
1867 compiler_use_next_block(c, end); 1872 compiler_use_next_block(c, end);
1868 return 1; 1873 return 1;
1869 } 1874 }
1870 1875
1871 static int 1876 static int
1872 compiler_lambda(struct compiler *c, expr_ty e) 1877 compiler_lambda(struct compiler *c, expr_ty e)
1873 { 1878 {
1874 PyCodeObject *co; 1879 PyCodeObject *co;
1875 PyObject *qualname; 1880 PyObject *qualname;
1876 static identifier name; 1881 static identifier name;
1877 int kw_default_count = 0; 1882 Py_ssize_t funcflags;
1878 Py_ssize_t arglength;
1879 arguments_ty args = e->v.Lambda.args; 1883 arguments_ty args = e->v.Lambda.args;
1880 assert(e->kind == Lambda_kind); 1884 assert(e->kind == Lambda_kind);
1881 1885
1882 if (!name) { 1886 if (!name) {
1883 name = PyUnicode_InternFromString("<lambda>"); 1887 name = PyUnicode_InternFromString("<lambda>");
1884 if (!name) 1888 if (!name)
1885 return 0; 1889 return 0;
1886 } 1890 }
1887 1891
1888 if (args->defaults) 1892 funcflags = compiler_default_arguments(c, args);
1889 VISIT_SEQ(c, expr, args->defaults); 1893 if (funcflags == -1) {
1890 if (args->kwonlyargs) { 1894 return 0;
1891 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1892 args->kw_defaults);
1893 if (res < 0) return 0;
1894 kw_default_count = res;
1895 } 1895 }
1896
1896 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, 1897 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
1897 (void *)e, e->lineno)) 1898 (void *)e, e->lineno))
1898 return 0; 1899 return 0;
1899 1900
1900 /* Make None the first constant, so the lambda can't have a 1901 /* Make None the first constant, so the lambda can't have a
1901 docstring. */ 1902 docstring. */
1902 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0) 1903 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)
1903 return 0; 1904 return 0;
1904 1905
1905 c->u->u_argcount = asdl_seq_LEN(args->args); 1906 c->u->u_argcount = asdl_seq_LEN(args->args);
1906 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); 1907 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1907 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); 1908 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1908 if (c->u->u_ste->ste_generator) { 1909 if (c->u->u_ste->ste_generator) {
1909 co = assemble(c, 0); 1910 co = assemble(c, 0);
1910 } 1911 }
1911 else { 1912 else {
1912 ADDOP_IN_SCOPE(c, RETURN_VALUE); 1913 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1913 co = assemble(c, 1); 1914 co = assemble(c, 1);
1914 } 1915 }
1915 qualname = c->u->u_qualname; 1916 qualname = c->u->u_qualname;
1916 Py_INCREF(qualname); 1917 Py_INCREF(qualname);
1917 compiler_exit_scope(c); 1918 compiler_exit_scope(c);
1918 if (co == NULL) 1919 if (co == NULL)
1919 return 0; 1920 return 0;
1920 1921
1921 arglength = asdl_seq_LEN(args->defaults); 1922 compiler_make_closure(c, co, funcflags, qualname);
1922 arglength |= kw_default_count << 8;
1923 compiler_make_closure(c, co, arglength, qualname);
1924 Py_DECREF(qualname); 1923 Py_DECREF(qualname);
1925 Py_DECREF(co); 1924 Py_DECREF(co);
1926 1925
1927 return 1; 1926 return 1;
1928 } 1927 }
1929 1928
1930 static int 1929 static int
1931 compiler_if(struct compiler *c, stmt_ty s) 1930 compiler_if(struct compiler *c, stmt_ty s)
1932 { 1931 {
1933 basicblock *end, *next; 1932 basicblock *end, *next;
(...skipping 2894 matching lines...) Expand 10 before | Expand all | Expand 10 after
4828 return co; 4827 return co;
4829 } 4828 }
4830 4829
4831 #undef PyAST_Compile 4830 #undef PyAST_Compile
4832 PyAPI_FUNC(PyCodeObject *) 4831 PyAPI_FUNC(PyCodeObject *)
4833 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, 4832 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4834 PyArena *arena) 4833 PyArena *arena)
4835 { 4834 {
4836 return PyAST_CompileEx(mod, filename, flags, -1, arena); 4835 return PyAST_CompileEx(mod, filename, flags, -1, arena);
4837 } 4836 }
OLDNEW
« Python/ceval.c ('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+