Skip to content

Commit 008cb5a

Browse files
committed
feat(code): add co_qualname field
1 parent 7569c0f commit 008cb5a

File tree

9 files changed

+1229
-1210
lines changed

9 files changed

+1229
-1210
lines changed

Include/cpython/code.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ struct PyCodeObject {
7575
PyObject *co_localspluskinds; /* Bytes mapping to local kinds (one byte per variable) */
7676
PyObject *co_filename; /* unicode (where it was loaded from) */
7777
PyObject *co_name; /* unicode (name, for reference) */
78+
PyObject *co_qualname; /* unicode (qualname, only for code objects created
79+
by the compiler, otherwise NULL) */
7880
PyObject *co_linetable; /* string (encoding addr<->lineno mapping) See
7981
Objects/lnotab_notes.txt for details. */
8082

Include/internal/pycore_code.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ struct _PyCodeConstructor {
212212
/* metadata */
213213
PyObject *filename;
214214
PyObject *name;
215+
PyObject *qualname;
215216
int flags;
216217

217218
/* the code */
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Added the co_qualname to the PyCodeObject structure to propagate the
2+
qualified name from the compiler to code objects. Code objects that are
3+
created manually have this new field set to NULL.
4+
5+
Patch by Gabriele N. Tornetta

Objects/codeobject.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
296296
co->co_filename = con->filename;
297297
Py_INCREF(con->name);
298298
co->co_name = con->name;
299+
if (con->qualname != NULL)
300+
Py_INCREF(con->qualname);
301+
co->co_qualname = con->qualname;
299302
co->co_flags = con->flags;
300303

301304
Py_INCREF(con->code);
@@ -351,6 +354,9 @@ _PyCode_New(struct _PyCodeConstructor *con)
351354
if (PyUnicode_READY(con->name) < 0) {
352355
return NULL;
353356
}
357+
if (con->qualname != NULL && PyUnicode_READY(con->qualname) < 0) {
358+
return NULL;
359+
}
354360
if (PyUnicode_READY(con->filename) < 0) {
355361
return NULL;
356362
}
@@ -456,6 +462,7 @@ PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargcount,
456462
struct _PyCodeConstructor con = {
457463
.filename = filename,
458464
.name = name,
465+
.qualname = NULL,
459466
.flags = flags,
460467

461468
.code = code,
@@ -549,6 +556,7 @@ PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
549556
struct _PyCodeConstructor con = {
550557
.filename = filename_ob,
551558
.name = funcname_ob,
559+
.qualname = NULL,
552560
.code = emptystring,
553561
.firstlineno = firstlineno,
554562
.linetable = emptystring,
@@ -1146,6 +1154,7 @@ code_dealloc(PyCodeObject *co)
11461154
Py_XDECREF(co->co_cellvars);
11471155
Py_XDECREF(co->co_filename);
11481156
Py_XDECREF(co->co_name);
1157+
Py_XDECREF(co->co_qualname);
11491158
Py_XDECREF(co->co_linetable);
11501159
Py_XDECREF(co->co_exceptiontable);
11511160
if (co->co_weakreflist != NULL)

Python/compile.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7287,6 +7287,7 @@ makecode(struct compiler *c, struct assembler *a, PyObject *constslist,
72877287
struct _PyCodeConstructor con = {
72887288
.filename = c->c_filename,
72897289
.name = c->u->u_name,
7290+
.qualname = c->u->u_qualname,
72907291
.flags = flags,
72917292

72927293
.code = a->a_bytecode,

Python/importlib.h

Lines changed: 397 additions & 397 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/importlib_external.h

Lines changed: 663 additions & 663 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/importlib_zipimport.h

Lines changed: 150 additions & 150 deletions
Large diffs are not rendered by default.

Python/marshal.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,7 @@ r_object(RFILE *p)
13781378
struct _PyCodeConstructor con = {
13791379
.filename = filename,
13801380
.name = name,
1381+
.qualname = NULL,
13811382
.flags = flags,
13821383

13831384
.code = code,

0 commit comments

Comments
 (0)