Skip to content

Commit

Permalink
Update co_filename if needed (#49)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
iritkatriel authored Jul 27, 2021
1 parent 41cf743 commit d827294
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
1 change: 1 addition & 0 deletions Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ struct _PyCodeConstructor {
PyAPI_FUNC(int) _PyCode_Validate(struct _PyCodeConstructor *);
PyAPI_FUNC(PyCodeObject *) _PyCode_New(struct _PyCodeConstructor *);
PyAPI_FUNC(PyCodeObject *) _PyCode_Update(struct _PyCodeConstructor *, PyCodeObject *);
PyAPI_FUNC(void) _PyCode_UpdateFilenames(PyCodeObject *co, PyObject *oldname, PyObject *newname);


/* Private API */
Expand Down
32 changes: 29 additions & 3 deletions Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,28 @@ _PyCode_New(struct _PyCodeConstructor *con)
return co;
}

void
_PyCode_UpdateFilenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
{
PyObject *constants, *tmp;
Py_ssize_t i, n;

if (PyUnicode_Compare(co->co_filename, oldname))
return;

Py_INCREF(newname);
Py_XSETREF(co->co_filename, newname);

constants = co->co_consts;
n = constants != NULL ? PyTuple_GET_SIZE(constants) : 0;
for (i = 0; i < n; i++) {
tmp = PyTuple_GET_ITEM(constants, i);
if (PyCode_Check(tmp))
_PyCode_UpdateFilenames((PyCodeObject *)tmp,
oldname, newname);
}
}

PyCodeObject *
_PyCode_Update(struct _PyCodeConstructor *con, PyCodeObject *code)
{
Expand All @@ -447,12 +469,16 @@ _PyCode_Update(struct _PyCodeConstructor *con, PyCodeObject *code)
con->columntable = Py_None;
}

Py_XDECREF(code->co_filename);
Py_XDECREF(code->co_name);
Py_XDECREF(code->co_qualname);
PyObject *newname = code->co_filename;
Py_DECREF(code->co_name);
Py_DECREF(code->co_qualname);

init_code(code, con);

assert(newname);
_PyCode_UpdateFilenames(code, code->co_filename, newname);
Py_DECREF(newname);

return code;
}

Expand Down
24 changes: 1 addition & 23 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -823,28 +823,6 @@ PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
}


static void
update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
{
PyObject *constants, *tmp;
Py_ssize_t i, n;

if (PyUnicode_Compare(co->co_filename, oldname))
return;

Py_INCREF(newname);
Py_XSETREF(co->co_filename, newname);

constants = co->co_consts;
n = constants != NULL ? PyTuple_GET_SIZE(constants) : 0;
for (i = 0; i < n; i++) {
tmp = PyTuple_GET_ITEM(constants, i);
if (PyCode_Check(tmp))
update_code_filenames((PyCodeObject *)tmp,
oldname, newname);
}
}

static void
update_compiled_module(PyCodeObject *co, PyObject *newname)
{
Expand All @@ -855,7 +833,7 @@ update_compiled_module(PyCodeObject *co, PyObject *newname)

oldname = co->co_filename;
Py_INCREF(oldname);
update_code_filenames(co, oldname, newname);
_PyCode_UpdateFilenames(co, oldname, newname);
Py_DECREF(oldname);
}

Expand Down

0 comments on commit d827294

Please sign in to comment.