Skip to content

Commit 3f2c9e9

Browse files
committed
use co_qualname in function constructor
The MAKE_FUNCTION opcode now needs to pop one less value from the TOS.
1 parent a0252ab commit 3f2c9e9

File tree

11 files changed

+5472
-5575
lines changed

11 files changed

+5472
-5575
lines changed

Doc/c-api/function.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,16 @@ There are a few functions specific to Python functions.
3636
3737
The function's docstring and name are retrieved from the code object. *__module__*
3838
is retrieved from *globals*. The argument defaults, annotations and closure are
39-
set to ``NULL``. *__qualname__* is set to the same value as the function's name.
39+
set to ``NULL``. *__qualname__* is set to the same value as the code object's
40+
``co_qualname`` field.
4041
4142
4243
.. c:function:: PyObject* PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
4344
4445
As :c:func:`PyFunction_New`, but also allows setting the function object's
4546
``__qualname__`` attribute. *qualname* should be a unicode object or ``NULL``;
46-
if ``NULL``, the ``__qualname__`` attribute is set to the same value as its
47-
``__name__`` attribute.
47+
if ``NULL``, the ``__qualname__`` attribute is set to the same value as the
48+
code object's ``co_qualname`` field.
4849
4950
.. versionadded:: 3.3
5051

Lib/test/test_code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
freevars: ()
1818
nlocals: 2
1919
flags: 3
20-
consts: ('None', '<code object g>', "'f.<locals>.g'")
20+
consts: ('None', '<code object g>')
2121
2222
>>> dump(f(4).__code__)
2323
name: g

Lib/test/test_dis.py

Lines changed: 44 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,16 @@ def bug1333982(x=[]):
149149
dis_bug1333982 = """\
150150
%3d 0 LOAD_ASSERTION_ERROR
151151
2 LOAD_CONST 2 (<code object <listcomp> at 0x..., file "%s", line %d>)
152-
4 LOAD_CONST 3 ('bug1333982.<locals>.<listcomp>')
153-
6 MAKE_FUNCTION 0
154-
8 LOAD_FAST 0 (x)
155-
10 GET_ITER
156-
12 CALL_FUNCTION 1
152+
4 MAKE_FUNCTION 0
153+
6 LOAD_FAST 0 (x)
154+
8 GET_ITER
155+
10 CALL_FUNCTION 1
157156
158-
%3d 14 LOAD_CONST 4 (1)
157+
%3d 12 LOAD_CONST 3 (1)
159158
160-
%3d 16 BINARY_ADD
161-
18 CALL_FUNCTION 1
162-
20 RAISE_VARARGS 1
159+
%3d 14 BINARY_ADD
160+
16 CALL_FUNCTION 1
161+
18 RAISE_VARARGS 1
163162
""" % (bug1333982.__code__.co_firstlineno + 1,
164163
__file__,
165164
bug1333982.__code__.co_firstlineno + 1,
@@ -432,12 +431,11 @@ def foo(x):
432431
%3d 2 LOAD_CLOSURE 0 (y)
433432
4 BUILD_TUPLE 1
434433
6 LOAD_CONST 1 (<code object foo at 0x..., file "%s", line %d>)
435-
8 LOAD_CONST 2 ('_h.<locals>.foo')
436-
10 MAKE_FUNCTION 8 (closure)
437-
12 STORE_FAST 1 (foo)
434+
8 MAKE_FUNCTION 8 (closure)
435+
10 STORE_FAST 1 (foo)
438436
439-
%3d 14 LOAD_FAST 1 (foo)
440-
16 RETURN_VALUE
437+
%3d 12 LOAD_FAST 1 (foo)
438+
14 RETURN_VALUE
441439
""" % (_h.__code__.co_firstlineno + 1,
442440
__file__,
443441
_h.__code__.co_firstlineno + 1,
@@ -451,12 +449,11 @@ def foo(x):
451449
%3d 2 LOAD_CLOSURE 0 (x)
452450
4 BUILD_TUPLE 1
453451
6 LOAD_CONST 1 (<code object <listcomp> at 0x..., file "%s", line %d>)
454-
8 LOAD_CONST 2 ('_h.<locals>.foo.<locals>.<listcomp>')
455-
10 MAKE_FUNCTION 8 (closure)
456-
12 LOAD_DEREF 1 (y)
457-
14 GET_ITER
458-
16 CALL_FUNCTION 1
459-
18 RETURN_VALUE
452+
8 MAKE_FUNCTION 8 (closure)
453+
10 LOAD_DEREF 1 (y)
454+
12 GET_ITER
455+
14 CALL_FUNCTION 1
456+
16 RETURN_VALUE
460457
""" % (dis_nested_0,
461458
__file__,
462459
_h.__code__.co_firstlineno + 1,
@@ -747,7 +744,6 @@ def f(c=c):
747744
Constants:
748745
0: None
749746
1: <code object f at (.*), file "(.*)", line (.*)>
750-
2: 'tricky.<locals>.f'
751747
Variable names:
752748
0: a
753749
1: b
@@ -968,50 +964,48 @@ def jumpy():
968964
expected_opinfo_outer = [
969965
Instruction(opname='MAKE_CELL', opcode=135, arg=0, argval='a', argrepr='a', offset=0, starts_line=None, is_jump_target=False),
970966
Instruction(opname='MAKE_CELL', opcode=135, arg=1, argval='b', argrepr='b', offset=2, starts_line=None, is_jump_target=False),
971-
Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval=(3, 4), argrepr='(3, 4)', offset=4, starts_line=2, is_jump_target=False),
967+
Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=(3, 4), argrepr='(3, 4)', offset=4, starts_line=2, is_jump_target=False),
972968
Instruction(opname='LOAD_CLOSURE', opcode=136, arg=0, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
973969
Instruction(opname='LOAD_CLOSURE', opcode=136, arg=1, argval='b', argrepr='b', offset=8, starts_line=None, is_jump_target=False),
974970
Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=10, starts_line=None, is_jump_target=False),
975971
Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_f, argrepr=repr(code_object_f), offset=12, starts_line=None, is_jump_target=False),
976-
Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f', argrepr="'outer.<locals>.f'", offset=14, starts_line=None, is_jump_target=False),
977-
Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='defaults, closure', offset=16, starts_line=None, is_jump_target=False),
978-
Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=18, starts_line=None, is_jump_target=False),
979-
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=20, starts_line=7, is_jump_target=False),
980-
Instruction(opname='LOAD_DEREF', opcode=137, arg=0, argval='a', argrepr='a', offset=22, starts_line=None, is_jump_target=False),
981-
Instruction(opname='LOAD_DEREF', opcode=137, arg=1, argval='b', argrepr='b', offset=24, starts_line=None, is_jump_target=False),
982-
Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=26, starts_line=None, is_jump_target=False),
983-
Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=28, starts_line=None, is_jump_target=False),
984-
Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=30, starts_line=None, is_jump_target=False),
985-
Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=32, starts_line=None, is_jump_target=False),
986-
Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval='Hello world!', argrepr="'Hello world!'", offset=34, starts_line=None, is_jump_target=False),
987-
Instruction(opname='CALL_FUNCTION', opcode=131, arg=7, argval=7, argrepr='', offset=36, starts_line=None, is_jump_target=False),
988-
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=38, starts_line=None, is_jump_target=False),
989-
Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=40, starts_line=8, is_jump_target=False),
990-
Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=42, starts_line=None, is_jump_target=False),
972+
Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='defaults, closure', offset=14, starts_line=None, is_jump_target=False),
973+
Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=16, starts_line=None, is_jump_target=False),
974+
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=18, starts_line=7, is_jump_target=False),
975+
Instruction(opname='LOAD_DEREF', opcode=137, arg=0, argval='a', argrepr='a', offset=20, starts_line=None, is_jump_target=False),
976+
Instruction(opname='LOAD_DEREF', opcode=137, arg=1, argval='b', argrepr='b', offset=22, starts_line=None, is_jump_target=False),
977+
Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='', argrepr="''", offset=24, starts_line=None, is_jump_target=False),
978+
Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=26, starts_line=None, is_jump_target=False),
979+
Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=28, starts_line=None, is_jump_target=False),
980+
Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=30, starts_line=None, is_jump_target=False),
981+
Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval='Hello world!', argrepr="'Hello world!'", offset=32, starts_line=None, is_jump_target=False),
982+
Instruction(opname='CALL_FUNCTION', opcode=131, arg=7, argval=7, argrepr='', offset=34, starts_line=None, is_jump_target=False),
983+
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=36, starts_line=None, is_jump_target=False),
984+
Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=38, starts_line=8, is_jump_target=False),
985+
Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=40, starts_line=None, is_jump_target=False),
991986
]
992987

993988
expected_opinfo_f = [
994989
Instruction(opname='MAKE_CELL', opcode=135, arg=0, argval='c', argrepr='c', offset=0, starts_line=None, is_jump_target=False),
995990
Instruction(opname='MAKE_CELL', opcode=135, arg=1, argval='d', argrepr='d', offset=2, starts_line=None, is_jump_target=False),
996-
Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=(5, 6), argrepr='(5, 6)', offset=4, starts_line=3, is_jump_target=False),
991+
Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval=(5, 6), argrepr='(5, 6)', offset=4, starts_line=3, is_jump_target=False),
997992
Instruction(opname='LOAD_CLOSURE', opcode=136, arg=3, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
998993
Instruction(opname='LOAD_CLOSURE', opcode=136, arg=4, argval='b', argrepr='b', offset=8, starts_line=None, is_jump_target=False),
999994
Instruction(opname='LOAD_CLOSURE', opcode=136, arg=0, argval='c', argrepr='c', offset=10, starts_line=None, is_jump_target=False),
1000995
Instruction(opname='LOAD_CLOSURE', opcode=136, arg=1, argval='d', argrepr='d', offset=12, starts_line=None, is_jump_target=False),
1001996
Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=14, starts_line=None, is_jump_target=False),
1002997
Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_inner, argrepr=repr(code_object_inner), offset=16, starts_line=None, is_jump_target=False),
1003-
Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f.<locals>.inner', argrepr="'outer.<locals>.f.<locals>.inner'", offset=18, starts_line=None, is_jump_target=False),
1004-
Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='defaults, closure', offset=20, starts_line=None, is_jump_target=False),
1005-
Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=22, starts_line=None, is_jump_target=False),
1006-
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=24, starts_line=5, is_jump_target=False),
1007-
Instruction(opname='LOAD_DEREF', opcode=137, arg=3, argval='a', argrepr='a', offset=26, starts_line=None, is_jump_target=False),
1008-
Instruction(opname='LOAD_DEREF', opcode=137, arg=4, argval='b', argrepr='b', offset=28, starts_line=None, is_jump_target=False),
1009-
Instruction(opname='LOAD_DEREF', opcode=137, arg=0, argval='c', argrepr='c', offset=30, starts_line=None, is_jump_target=False),
1010-
Instruction(opname='LOAD_DEREF', opcode=137, arg=1, argval='d', argrepr='d', offset=32, starts_line=None, is_jump_target=False),
1011-
Instruction(opname='CALL_FUNCTION', opcode=131, arg=4, argval=4, argrepr='', offset=34, starts_line=None, is_jump_target=False),
1012-
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=36, starts_line=None, is_jump_target=False),
1013-
Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=38, starts_line=6, is_jump_target=False),
1014-
Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=40, starts_line=None, is_jump_target=False),
998+
Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='defaults, closure', offset=18, starts_line=None, is_jump_target=False),
999+
Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=20, starts_line=None, is_jump_target=False),
1000+
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=22, starts_line=5, is_jump_target=False),
1001+
Instruction(opname='LOAD_DEREF', opcode=137, arg=3, argval='a', argrepr='a', offset=24, starts_line=None, is_jump_target=False),
1002+
Instruction(opname='LOAD_DEREF', opcode=137, arg=4, argval='b', argrepr='b', offset=26, starts_line=None, is_jump_target=False),
1003+
Instruction(opname='LOAD_DEREF', opcode=137, arg=0, argval='c', argrepr='c', offset=28, starts_line=None, is_jump_target=False),
1004+
Instruction(opname='LOAD_DEREF', opcode=137, arg=1, argval='d', argrepr='d', offset=30, starts_line=None, is_jump_target=False),
1005+
Instruction(opname='CALL_FUNCTION', opcode=131, arg=4, argval=4, argrepr='', offset=32, starts_line=None, is_jump_target=False),
1006+
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=34, starts_line=None, is_jump_target=False),
1007+
Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=36, starts_line=6, is_jump_target=False),
1008+
Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=38, starts_line=None, is_jump_target=False),
10151009
]
10161010

10171011
expected_opinfo_inner = [

Objects/codeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ _PyCode_New(struct _PyCodeConstructor *con)
354354
if (PyUnicode_READY(con->name) < 0) {
355355
return NULL;
356356
}
357-
if (con->qualname != NULL && PyUnicode_READY(con->qualname) < 0) {
357+
if (PyUnicode_READY(con->qualname) < 0) {
358358
return NULL;
359359
}
360360
if (PyUnicode_READY(con->filename) < 0) {

Objects/funcobject.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
2222
PyObject *name = code_obj->co_name;
2323
assert(name != NULL);
2424
Py_INCREF(name);
25+
2526
if (!qualname) {
26-
qualname = name;
27+
qualname = code_obj->co_qualname;
2728
}
29+
assert(qualname != NULL);
2830
Py_INCREF(qualname);
2931

3032
PyObject *consts = code_obj->co_consts;

Objects/genobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ gen_new_with_qualname(PyTypeObject *type, PyFrameObject *f,
840840
if (qualname != NULL)
841841
gen->gi_qualname = qualname;
842842
else
843-
gen->gi_qualname = gen->gi_name;
843+
gen->gi_qualname = gen->gi_code->co_qualname;
844844
Py_INCREF(gen->gi_qualname);
845845
_PyObject_GC_TRACK(gen);
846846
return (PyObject *)gen;

Python/ceval.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4136,13 +4136,11 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
41364136
}
41374137

41384138
case TARGET(MAKE_FUNCTION): {
4139-
PyObject *qualname = POP();
41404139
PyObject *codeobj = POP();
41414140
PyFunctionObject *func = (PyFunctionObject *)
4142-
PyFunction_NewWithQualName(codeobj, GLOBALS(), qualname);
4141+
PyFunction_New(codeobj, GLOBALS());
41434142

41444143
Py_DECREF(codeobj);
4145-
Py_DECREF(qualname);
41464144
if (func == NULL) {
41474145
goto error;
41484146
}

Python/compile.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,7 @@ stack_effect(int opcode, int oparg, int jump)
11781178
case CALL_FUNCTION_EX:
11791179
return -1 - ((oparg & 0x01) != 0);
11801180
case MAKE_FUNCTION:
1181-
return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1181+
return 0 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
11821182
((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
11831183
case BUILD_SLICE:
11841184
if (oparg == 3)
@@ -2101,7 +2101,6 @@ compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags,
21012101
ADDOP_I(c, BUILD_TUPLE, co->co_nfreevars);
21022102
}
21032103
ADDOP_LOAD_CONST(c, (PyObject*)co);
2104-
ADDOP_LOAD_CONST(c, qualname);
21052104
ADDOP_I(c, MAKE_FUNCTION, flags);
21062105
return 1;
21072106
}

0 commit comments

Comments
 (0)