Skip to content

Commit d827294

Browse files
authored
Update co_filename if needed (#49)
Sometimes co_filename is set to something different than what was written -- it is changed (using `_imp.fix_co_filename`) to match the file from which it was loaded. Here we ensure that this is propagated through hydration.
1 parent 41cf743 commit d827294

File tree

3 files changed

+31
-26
lines changed

3 files changed

+31
-26
lines changed

Include/internal/pycore_code.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ struct _PyCodeConstructor {
261261
PyAPI_FUNC(int) _PyCode_Validate(struct _PyCodeConstructor *);
262262
PyAPI_FUNC(PyCodeObject *) _PyCode_New(struct _PyCodeConstructor *);
263263
PyAPI_FUNC(PyCodeObject *) _PyCode_Update(struct _PyCodeConstructor *, PyCodeObject *);
264+
PyAPI_FUNC(void) _PyCode_UpdateFilenames(PyCodeObject *co, PyObject *oldname, PyObject *newname);
264265

265266

266267
/* Private API */

Objects/codeobject.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,28 @@ _PyCode_New(struct _PyCodeConstructor *con)
430430
return co;
431431
}
432432

433+
void
434+
_PyCode_UpdateFilenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
435+
{
436+
PyObject *constants, *tmp;
437+
Py_ssize_t i, n;
438+
439+
if (PyUnicode_Compare(co->co_filename, oldname))
440+
return;
441+
442+
Py_INCREF(newname);
443+
Py_XSETREF(co->co_filename, newname);
444+
445+
constants = co->co_consts;
446+
n = constants != NULL ? PyTuple_GET_SIZE(constants) : 0;
447+
for (i = 0; i < n; i++) {
448+
tmp = PyTuple_GET_ITEM(constants, i);
449+
if (PyCode_Check(tmp))
450+
_PyCode_UpdateFilenames((PyCodeObject *)tmp,
451+
oldname, newname);
452+
}
453+
}
454+
433455
PyCodeObject *
434456
_PyCode_Update(struct _PyCodeConstructor *con, PyCodeObject *code)
435457
{
@@ -447,12 +469,16 @@ _PyCode_Update(struct _PyCodeConstructor *con, PyCodeObject *code)
447469
con->columntable = Py_None;
448470
}
449471

450-
Py_XDECREF(code->co_filename);
451-
Py_XDECREF(code->co_name);
452-
Py_XDECREF(code->co_qualname);
472+
PyObject *newname = code->co_filename;
473+
Py_DECREF(code->co_name);
474+
Py_DECREF(code->co_qualname);
453475

454476
init_code(code, con);
455477

478+
assert(newname);
479+
_PyCode_UpdateFilenames(code, code->co_filename, newname);
480+
Py_DECREF(newname);
481+
456482
return code;
457483
}
458484

Python/import.c

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -823,28 +823,6 @@ PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
823823
}
824824

825825

826-
static void
827-
update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
828-
{
829-
PyObject *constants, *tmp;
830-
Py_ssize_t i, n;
831-
832-
if (PyUnicode_Compare(co->co_filename, oldname))
833-
return;
834-
835-
Py_INCREF(newname);
836-
Py_XSETREF(co->co_filename, newname);
837-
838-
constants = co->co_consts;
839-
n = constants != NULL ? PyTuple_GET_SIZE(constants) : 0;
840-
for (i = 0; i < n; i++) {
841-
tmp = PyTuple_GET_ITEM(constants, i);
842-
if (PyCode_Check(tmp))
843-
update_code_filenames((PyCodeObject *)tmp,
844-
oldname, newname);
845-
}
846-
}
847-
848826
static void
849827
update_compiled_module(PyCodeObject *co, PyObject *newname)
850828
{
@@ -855,7 +833,7 @@ update_compiled_module(PyCodeObject *co, PyObject *newname)
855833

856834
oldname = co->co_filename;
857835
Py_INCREF(oldname);
858-
update_code_filenames(co, oldname, newname);
836+
_PyCode_UpdateFilenames(co, oldname, newname);
859837
Py_DECREF(oldname);
860838
}
861839

0 commit comments

Comments
 (0)