comparison Python/compile.c @ 86521:bb2affc1e317

cleanup the construction of __qualname__ (closes #19301 again)
author Benjamin Peterson <benjamin@python.org>
date Sun, 20 Oct 2013 17:50:28 -0400
parents 84a8b797c5c5
children ea373a14f9e9
comparison
equal deleted inserted replaced
86520:8ad3e9cf9590 86521:bb2affc1e317
92 92
93 enum { 93 enum {
94 COMPILER_SCOPE_MODULE, 94 COMPILER_SCOPE_MODULE,
95 COMPILER_SCOPE_CLASS, 95 COMPILER_SCOPE_CLASS,
96 COMPILER_SCOPE_FUNCTION, 96 COMPILER_SCOPE_FUNCTION,
97 COMPILER_SCOPE_LAMBDA,
97 COMPILER_SCOPE_COMPREHENSION, 98 COMPILER_SCOPE_COMPREHENSION,
98 }; 99 };
99 100
100 /* The following items change on entry and exit of code blocks. 101 /* The following items change on entry and exit of code blocks.
101 They must be saved and restored when returning to a block. 102 They must be saved and restored when returning to a block.
102 */ 103 */
103 struct compiler_unit { 104 struct compiler_unit {
104 PySTEntryObject *u_ste; 105 PySTEntryObject *u_ste;
105 106
106 PyObject *u_name; 107 PyObject *u_name;
108 PyObject *u_qualname; /* dot-separated qualified name (lazy) */
107 int u_scope_type; 109 int u_scope_type;
108 110
109 /* The following fields are dicts that map objects to 111 /* The following fields are dicts that map objects to
110 the index of them in co_XXX. The index is used as 112 the index of them in co_XXX. The index is used as
111 the argument for opcodes that refer to those collections. 113 the argument for opcodes that refer to those collections.
197 asdl_seq *args, 199 asdl_seq *args,
198 asdl_seq *keywords, 200 asdl_seq *keywords,
199 expr_ty starargs, 201 expr_ty starargs,
200 expr_ty kwargs); 202 expr_ty kwargs);
201 static int compiler_try_except(struct compiler *, stmt_ty); 203 static int compiler_try_except(struct compiler *, stmt_ty);
204 static int compiler_set_qualname(struct compiler *);
202 205
203 static PyCodeObject *assemble(struct compiler *, int addNone); 206 static PyCodeObject *assemble(struct compiler *, int addNone);
204 static PyObject *__doc__; 207 static PyObject *__doc__;
205 208
206 #define COMPILER_CAPSULE_NAME_COMPILER_UNIT "compile.c compiler unit" 209 #define COMPILER_CAPSULE_NAME_COMPILER_UNIT "compile.c compiler unit"
504 PyObject_Free((void *)b); 507 PyObject_Free((void *)b);
505 b = next; 508 b = next;
506 } 509 }
507 Py_CLEAR(u->u_ste); 510 Py_CLEAR(u->u_ste);
508 Py_CLEAR(u->u_name); 511 Py_CLEAR(u->u_name);
512 Py_CLEAR(u->u_qualname);
509 Py_CLEAR(u->u_consts); 513 Py_CLEAR(u->u_consts);
510 Py_CLEAR(u->u_names); 514 Py_CLEAR(u->u_names);
511 Py_CLEAR(u->u_varnames); 515 Py_CLEAR(u->u_varnames);
512 Py_CLEAR(u->u_freevars); 516 Py_CLEAR(u->u_freevars);
513 Py_CLEAR(u->u_cellvars); 517 Py_CLEAR(u->u_cellvars);
617 c->u = u; 621 c->u = u;
618 622
619 c->c_nestlevel++; 623 c->c_nestlevel++;
620 if (compiler_use_new_block(c) == NULL) 624 if (compiler_use_new_block(c) == NULL)
621 return 0; 625 return 0;
626
627 if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
628 if (!compiler_set_qualname(c))
629 return 0;
630 }
622 631
623 return 1; 632 return 1;
624 } 633 }
625 634
626 static void 635 static void
645 else 654 else
646 c->u = NULL; 655 c->u = NULL;
647 656
648 } 657 }
649 658
650 static PyObject * 659 static int
651 compiler_scope_qualname(struct compiler *c, identifier scope_name) 660 compiler_set_qualname(struct compiler *c)
652 { 661 {
662 _Py_static_string(dot, ".");
663 _Py_static_string(dot_locals, ".<locals>");
653 Py_ssize_t stack_size; 664 Py_ssize_t stack_size;
654 int global_scope; 665 struct compiler_unit *u = c->u;
655 _Py_static_string(dot, "."); 666 PyObject *name, *base, *dot_str, *dot_locals_str;
656 _Py_static_string(locals, "<locals>"); 667
657 struct compiler_unit *u; 668 base = NULL;
658 PyObject *capsule, *name, *seq, *dot_str, *locals_str;
659
660 u = c->u;
661 seq = PyList_New(0);
662 if (seq == NULL)
663 return NULL;
664
665 stack_size = PyList_GET_SIZE(c->c_stack); 669 stack_size = PyList_GET_SIZE(c->c_stack);
666 assert(stack_size >= 1); 670 assert(stack_size >= 1);
667 global_scope = stack_size == 1; 671 if (stack_size > 1) {
668 if (scope_name != NULL && !global_scope) { 672 int scope, force_global = 0;
669 int scope; 673 struct compiler_unit *parent;
670 PyObject *mangled; 674 PyObject *mangled, *capsule;
675
671 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1); 676 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
672 u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CAPSULE_NAME_COMPILER_UNIT); 677 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CAPSULE_NAME_COMPILER_UNIT);
673 assert(u); 678 assert(parent);
674 mangled = _Py_Mangle(u->u_private, scope_name); 679
675 if (!mangled) 680 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION || u->u_scope_type == COMPILER_SCOPE_CLASS) {
676 return NULL; 681 assert(u->u_name);
677 scope = PyST_GetScope(u->u_ste, mangled); 682 mangled = _Py_Mangle(parent->u_private, u->u_name);
678 Py_DECREF(mangled); 683 if (!mangled)
679 assert(scope != GLOBAL_IMPLICIT); 684 return 0;
680 if (scope == GLOBAL_EXPLICIT) 685 scope = PyST_GetScope(parent->u_ste, mangled);
681 global_scope = 1; 686 Py_DECREF(mangled);
682 } 687 assert(scope != GLOBAL_IMPLICIT);
683 if (!global_scope) { 688 if (scope == GLOBAL_EXPLICIT)
684 Py_ssize_t i; 689 force_global = 1;
685 for (i = 1; i < stack_size; i++) { 690 }
686 capsule = PyList_GET_ITEM(c->c_stack, i); 691
687 u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CAPSULE_NAME_COMPILER_UNIT); 692 if (!force_global) {
688 assert(u); 693 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
689 assert(u->u_scope_type != COMPILER_SCOPE_MODULE); 694 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
690 if (PyList_Append(seq, u->u_name)) 695 dot_locals_str = _PyUnicode_FromId(&dot_locals);
691 goto _error; 696 if (dot_locals_str == NULL)
692 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION) { 697 return 0;
693 locals_str = _PyUnicode_FromId(&locals); 698 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
694 if (locals_str == NULL) 699 if (base == NULL)
695 goto _error; 700 return 0;
696 if (PyList_Append(seq, locals_str))
697 goto _error;
698 } 701 }
699 } 702 else {
700 } 703 Py_INCREF(parent->u_qualname);
701 704 base = parent->u_qualname;
702 u = c->u; 705 }
703 if (PyList_Append(seq, u->u_name)) 706 }
704 goto _error; 707 }
705 dot_str = _PyUnicode_FromId(&dot); 708
706 if (dot_str == NULL) 709 if (base != NULL) {
707 goto _error; 710 dot_str = _PyUnicode_FromId(&dot);
708 name = PyUnicode_Join(dot_str, seq); 711 if (dot_str == NULL) {
709 Py_DECREF(seq); 712 Py_DECREF(base);
710 return name; 713 return 0;
711 714 }
712 _error: 715 name = PyUnicode_Concat(base, dot_str);
713 Py_XDECREF(seq); 716 Py_DECREF(base);
714 return NULL; 717 if (name == NULL)
718 return 0;
719 PyUnicode_Append(&name, u->u_name);
720 if (name == NULL)
721 return 0;
722 }
723 else {
724 Py_INCREF(u->u_name);
725 name = u->u_name;
726 }
727 u->u_qualname = name;
728
729 return 1;
715 } 730 }
716 731
717 /* Allocate a new block and return a pointer to it. 732 /* Allocate a new block and return a pointer to it.
718 Returns NULL on error. 733 Returns NULL on error.
719 */ 734 */
1659 for (i = docstring; i < n; i++) { 1674 for (i = docstring; i < n; i++) {
1660 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i); 1675 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1661 VISIT_IN_SCOPE(c, stmt, st); 1676 VISIT_IN_SCOPE(c, stmt, st);
1662 } 1677 }
1663 co = assemble(c, 1); 1678 co = assemble(c, 1);
1664 qualname = compiler_scope_qualname(c, s->v.FunctionDef.name); 1679 qualname = c->u->u_qualname;
1680 Py_INCREF(qualname);
1665 compiler_exit_scope(c); 1681 compiler_exit_scope(c);
1666 if (qualname == NULL || co == NULL) { 1682 if (co == NULL) {
1667 Py_XDECREF(qualname); 1683 Py_XDECREF(qualname);
1668 Py_XDECREF(co); 1684 Py_XDECREF(co);
1669 return 0; 1685 return 0;
1670 } 1686 }
1671 1687
1731 Py_XDECREF(str); 1747 Py_XDECREF(str);
1732 compiler_exit_scope(c); 1748 compiler_exit_scope(c);
1733 return 0; 1749 return 0;
1734 } 1750 }
1735 Py_DECREF(str); 1751 Py_DECREF(str);
1736 /* store the __qualname__ */ 1752 assert(c->u->u_qualname);
1737 str = compiler_scope_qualname(c, s->v.ClassDef.name); 1753 ADDOP_O(c, LOAD_CONST, c->u->u_qualname, consts);
1738 if (!str) {
1739 compiler_exit_scope(c);
1740 return 0;
1741 }
1742 ADDOP_O(c, LOAD_CONST, str, consts);
1743 Py_DECREF(str);
1744 str = PyUnicode_InternFromString("__qualname__"); 1754 str = PyUnicode_InternFromString("__qualname__");
1745 if (!str || !compiler_nameop(c, str, Store)) { 1755 if (!str || !compiler_nameop(c, str, Store)) {
1746 Py_XDECREF(str); 1756 Py_XDECREF(str);
1747 compiler_exit_scope(c); 1757 compiler_exit_scope(c);
1748 return 0; 1758 return 0;
1853 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, 1863 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1854 args->kw_defaults); 1864 args->kw_defaults);
1855 if (res < 0) return 0; 1865 if (res < 0) return 0;
1856 kw_default_count = res; 1866 kw_default_count = res;
1857 } 1867 }
1858 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_FUNCTION, 1868 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
1859 (void *)e, e->lineno)) 1869 (void *)e, e->lineno))
1860 return 0; 1870 return 0;
1861 1871
1862 /* Make None the first constant, so the lambda can't have a 1872 /* Make None the first constant, so the lambda can't have a
1863 docstring. */ 1873 docstring. */
1872 } 1882 }
1873 else { 1883 else {
1874 ADDOP_IN_SCOPE(c, RETURN_VALUE); 1884 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1875 } 1885 }
1876 co = assemble(c, 1); 1886 co = assemble(c, 1);
1877 qualname = compiler_scope_qualname(c, NULL); 1887 qualname = c->u->u_qualname;
1888 Py_INCREF(qualname);
1878 compiler_exit_scope(c); 1889 compiler_exit_scope(c);
1879 if (qualname == NULL || co == NULL) 1890 if (co == NULL)
1880 return 0; 1891 return 0;
1881 1892
1882 arglength = asdl_seq_LEN(args->defaults); 1893 arglength = asdl_seq_LEN(args->defaults);
1883 arglength |= kw_default_count << 8; 1894 arglength |= kw_default_count << 8;
1884 compiler_make_closure(c, co, arglength, qualname); 1895 compiler_make_closure(c, co, arglength, qualname);
3149 if (type != COMP_GENEXP) { 3160 if (type != COMP_GENEXP) {
3150 ADDOP(c, RETURN_VALUE); 3161 ADDOP(c, RETURN_VALUE);
3151 } 3162 }
3152 3163
3153 co = assemble(c, 1); 3164 co = assemble(c, 1);
3154 qualname = compiler_scope_qualname(c, NULL); 3165 qualname = c->u->u_qualname;
3166 Py_INCREF(qualname);
3155 compiler_exit_scope(c); 3167 compiler_exit_scope(c);
3156 if (qualname == NULL || co == NULL) 3168 if (co == NULL)
3157 goto error; 3169 goto error;
3158 3170
3159 if (!compiler_make_closure(c, co, 0, qualname)) 3171 if (!compiler_make_closure(c, co, 0, qualname))
3160 goto error; 3172 goto error;
3161 Py_DECREF(qualname); 3173 Py_DECREF(qualname);