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

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 1565 matching lines...) Expand 10 before | Expand all | Expand 10 after
1576 } 1576 }
1577 ADDOP_N(c, LOAD_CONST, keys_tuple, consts); 1577 ADDOP_N(c, LOAD_CONST, keys_tuple, consts);
1578 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count); 1578 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
1579 return default_count; 1579 return default_count;
1580 } 1580 }
1581 else { 1581 else {
1582 return 0; 1582 return 0;
1583 } 1583 }
1584 1584
1585 error: 1585 error:
1586 if (keys != NULL) { 1586 Py_XDECREF(keys);
storchaka 2016/06/12 06:55:56 Just Py_XDECREF(keys).
1587 Py_DECREF(keys);
1588 }
1589 return -1; 1587 return -1;
1590 } 1588 }
1591 1589
1592 static int 1590 static int
1593 compiler_visit_argannotation(struct compiler *c, identifier id, 1591 compiler_visit_argannotation(struct compiler *c, identifier id,
1594 expr_ty annotation, PyObject* keylist) 1592 expr_ty annotation, PyObject *names)
storchaka 2016/06/12 06:55:56 It looks to me that this function can be left unch
1595 { 1593 {
1596 if (annotation) { 1594 if (annotation) {
1597 int res; 1595 PyObject *mangled;
1598 PyObject *mangled = _Py_Mangle(c->u->u_private, id); 1596 VISIT(c, expr, annotation);
1599 if (!mangled) { 1597 mangled = _Py_Mangle(c->u->u_private, id);
1600 return 0; 1598 if (!mangled)
1601 } 1599 return 0;
1602 res = PyList_Append(keylist, mangled); 1600 if (PyList_Append(names, mangled) < 0) {
1601 Py_DECREF(mangled);
1602 return 0;
1603 }
1603 Py_DECREF(mangled); 1604 Py_DECREF(mangled);
1604 if (res == -1) { 1605 }
1605 return 0; 1606 return 1;
1606 } 1607 }
1607 VISIT(c, expr, annotation); 1608
1608 } 1609 static int
1609 return 1; 1610 compiler_visit_argannotations(struct compiler *c, asdl_seq *args,
1610 } 1611 PyObject *names)
1611
1612 static int
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
1614 { 1612 {
1615 int i; 1613 int i;
1616 Py_ssize_t arglen = asdl_seq_LEN(args); 1614 for (i = 0; i < asdl_seq_LEN(args); i++) {
1617 for (i = 0; i < arglen; i++) {
1618 arg_ty arg = (arg_ty)asdl_seq_GET(args, i); 1615 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1619 int res = compiler_visit_argannotation(c, arg->arg, 1616 if (!compiler_visit_argannotation(
1620 arg->annotation, keylist); 1617 c,
1621 if (res == 0) { 1618 arg->arg,
1622 return 0; 1619 arg->annotation,
1623 } 1620 names))
1624 } 1621 return 0;
1625 1622 }
1626 return 1; 1623 return 1;
1627 } 1624 }
1628 1625
1629 static Py_ssize_t 1626 static Py_ssize_t
1630 compiler_visit_annotations(struct compiler *c, arguments_ty args, 1627 compiler_visit_annotations(struct compiler *c, arguments_ty args,
1631 expr_ty returns) 1628 expr_ty returns)
1632 { 1629 {
1633 /* Push arg annotation dict. Return # of items pushed. 1630 /* Push arg annotation dict. Return # of items pushed.
1634 The expressions are evaluated out-of-order wrt the source code. 1631 The expressions are evaluated out-of-order wrt the source code.
1635 1632
1636 Returns -1 on error. 1633 Returns -1 on error.
1637 */ 1634 */
1638 static identifier return_str; 1635 static identifier return_str;
1636 PyObject *names;
1639 Py_ssize_t len; 1637 Py_ssize_t len;
1640 PyObject* keylist = PyList_New(0); 1638 names = PyList_New(0);
storchaka 2016/06/12 06:55:56 I think the large part of this function can be lef
1641 1639 if (!names)
1642 if (keylist == NULL) {
1643 return -1; 1640 return -1;
1644 } 1641
1645 1642 if (!compiler_visit_argannotations(c, args->args, names))
1646 if (compiler_visit_argannotations(c, args->args, keylist) == 0) {
1647 goto error; 1643 goto error;
1648 }
1649
1650 if (args->vararg && args->vararg->annotation && 1644 if (args->vararg && args->vararg->annotation &&
1651 compiler_visit_argannotation(c, args->vararg->arg, 1645 !compiler_visit_argannotation(c, args->vararg->arg,
1652 args->vararg->annotation, keylist) == 0) { 1646 args->vararg->annotation, names))
1653 goto error; 1647 goto error;
1654 } 1648 if (!compiler_visit_argannotations(c, args->kwonlyargs, names))
1655
1656 if (compiler_visit_argannotations(c, args->kwonlyargs, keylist) == 0) {
1657 goto error; 1649 goto error;
1658 }
1659
1660 if (args->kwarg && args->kwarg->annotation && 1650 if (args->kwarg && args->kwarg->annotation &&
1661 compiler_visit_argannotation(c, args->kwarg->arg, 1651 !compiler_visit_argannotation(c, args->kwarg->arg,
1662 args->kwarg->annotation, keylist) == 0) { 1652 args->kwarg->annotation, names))
1663 goto error; 1653 goto error;
1664 }
1665 1654
1666 if (!return_str) { 1655 if (!return_str) {
1667 return_str = PyUnicode_InternFromString("return"); 1656 return_str = PyUnicode_InternFromString("return");
1668 if (!return_str) { 1657 if (!return_str)
1669 goto error; 1658 goto error;
1670 } 1659 }
1671 } 1660 if (!compiler_visit_argannotation(c, return_str, returns, names)) {
1672
1673 if (compiler_visit_argannotation(c, return_str, returns, keylist) == 0) {
1674 goto error; 1661 goto error;
1675 } 1662 }
1676 1663
1677 len = PyList_Size(keylist); 1664 len = PyList_GET_SIZE(names);
1678 if (len != 0) { 1665 if (len) {
1679 PyObject *keytuple = PyList_AsTuple(keylist); 1666 PyObject *keytuple = PyList_AsTuple(names);
1667 Py_DECREF(names);
1680 if (keytuple == NULL) { 1668 if (keytuple == NULL) {
1681 goto error; 1669 return -1;
1682 } 1670 }
1683 ADDOP_N(c, LOAD_CONST, keytuple, consts); 1671 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); 1672 ADDOP_I(c, BUILD_CONST_KEY_MAP, len);
1685 } 1673 }
1686 Py_DECREF(keylist); 1674 else {
1675 Py_DECREF(names);
1676 }
1687 return len; 1677 return len;
1688 1678
1689 error: 1679 error:
1690 Py_DECREF(keylist); 1680 Py_DECREF(names);
1691 return -1; 1681 return -1;
1692 } 1682 }
1693 1683
1694 static Py_ssize_t 1684 static Py_ssize_t
1695 compiler_default_arguments(struct compiler *c, arguments_ty args) 1685 compiler_default_arguments(struct compiler *c, arguments_ty args)
1696 { 1686 {
1697 Py_ssize_t funcflags = 0; 1687 Py_ssize_t funcflags = 0;
1698 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { 1688 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
1699 VISIT_SEQ(c, expr, args->defaults); 1689 VISIT_SEQ(c, expr, args->defaults);
1700 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); 1690 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
(...skipping 1814 matching lines...) Expand 10 before | Expand all | Expand 10 after
3515 } 3505 }
3516 else { 3506 else {
3517 VISIT_SEQ(c, keyword, keywords); 3507 VISIT_SEQ(c, keyword, keywords);
3518 nkw = nseen; 3508 nkw = nseen;
3519 } 3509 }
3520 } 3510 }
3521 if (nsubkwargs) { 3511 if (nsubkwargs) {
3522 code |= 2; 3512 code |= 2;
3523 if (nsubkwargs > 1) { 3513 if (nsubkwargs > 1) {
3524 /* Pack it all up */ 3514 /* Pack it all up */
3525 int function_pos = n + (code & 1) + nkw + 1; 3515 int function_pos = n + (code & 1) + 2 * nkw + 1;
3526 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));
3527 } 3517 }
3528 } 3518 }
3529 assert(n < 1<<8); 3519 assert(n < 1<<8);
3530 assert(nkw < 1<<24); 3520 assert(nkw < 1<<24);
3531 n |= nkw << 8; 3521 n |= nkw << 8;
3532 3522
3533 switch (code) { 3523 switch (code) {
3534 case 0: 3524 case 0:
3535 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
4990 return co; 4980 return co;
4991 } 4981 }
4992 4982
4993 #undef PyAST_Compile 4983 #undef PyAST_Compile
4994 PyAPI_FUNC(PyCodeObject *) 4984 PyAPI_FUNC(PyCodeObject *)
4995 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, 4985 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4996 PyArena *arena) 4986 PyArena *arena)
4997 { 4987 {
4998 return PyAST_CompileEx(mod, filename, flags, -1, arena); 4988 return PyAST_CompileEx(mod, filename, flags, -1, arena);
4999 } 4989 }
LEFTRIGHT

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