8000 gh-117953: Cleanups For fix_up_extension() in import.c by ericsnowcurrently · Pull Request #118192 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-117953: Cleanups For fix_up_extension() in import.c #118192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Pass tstate through to fix_up_extension().
  • Loading branch information
ericsnowcurrently committed Apr 23, 2024
commit b85c5afb269197d4724be688050b99605ce9e985
1 change: 1 addition & 0 deletions Include/internal/pycore_import.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extern int _PyImport_ReleaseLock(PyInterpreterState *interp);

// This is used exclusively for the sys and builtins modules:
extern int _PyImport_FixupBuiltin(
PyThreadState *tstate,
PyObject *mod,
const char *name, /* UTF-8 encoded string */
PyObject *modules
Expand Down
25 changes: 13 additions & 12 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,7 @@ is_core_module(PyInterpreterState *interp, PyObject *name, PyObject *path)
}

static int
fix_up_extension_for_interpreter(PyInterpreterState *interp,
fix_up_extension_for_interpreter(PyThreadState *tstate,
PyObject *mod, PyModuleDef *def,
PyObject *name, PyObject *path,
PyObject *modules)
Expand All @@ -1174,13 +1174,13 @@ fix_up_extension_for_interpreter(PyInterpreterState *interp,
assert(def == PyModule_GetDef(mod));

if (modules == NULL) {
modules = get_modules_dict(interp);
modules = get_modules_dict(tstate->interp);
}
if (PyObject_SetItem(modules, name, mod) < 0) {
return -1;
}

if (_modules_by_index_set(interp, def, mod) < 0) {
if (_modules_by_index_set(tstate->interp, def, mod) < 0) {
goto error;
}

Expand All @@ -1193,11 +1193,10 @@ fix_up_extension_for_interpreter(PyInterpreterState *interp,


static int
fix_up_extension(PyObject *mod, PyModuleDef *def,
fix_up_extension(PyThreadState *tstate, PyObject *mod, PyModuleDef *def,
PyObject *name, PyObject *path,
PyObject *modules)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
if (def == NULL) {
def = PyModule_GetDef(mod);
if (def == NULL) {
Expand All @@ -1207,15 +1206,15 @@ fix_up_extension(PyObject *mod, PyModuleDef *def,
}

if (fix_up_extension_for_interpreter(
interp, mod, def, name, path, modules) < 0)
tstate, mod, def, name, path, modules) < 0)
{
return -1;
}

// bpo-44050: Extensions and def->m_base.m_copy can be updated
// when the extension module doesn't support sub-interpreters.
if (def->m_size == -1) {
if (!is_core_module(interp, name, path)) {
if (!is_core_module(tstate->interp, name, path)) {
assert(PyUnicode_CompareWithASCIIString(name, "sys") != 0);
assert(PyUnicode_CompareWithASCIIString(name, "builtins") != 0);
if (def->m_base.m_copy) {
Expand All @@ -1236,7 +1235,7 @@ fix_up_extension(PyObject *mod, PyModuleDef *def,
}

// XXX Why special-case the main interpreter?
if (_Py_IsMainInterpreter(interp) || def->m_size == -1) {
if (_Py_IsMainInterpreter(tstate->interp) || def->m_size == -1) {
#ifndef NDEBUG
PyModuleDef *cached = _extensions_cache_get(path, name);
assert(cached == NULL || cached == def);
Expand All @@ -1262,7 +1261,8 @@ _PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
PyErr_BadInternalCall();
return -1;
}
if (fix_up_extension(mod, NULL, name, filename, modules) < 0) {
PyThreadState *tstate = _PyThreadState_GET();
if (fix_up_extension(tstate, mod, NULL, name, filename, modules) < 0) {
return -1;
}
return 0;
Expand Down Expand Up @@ -1377,7 +1377,8 @@ clear_singlephase_extension(PyInterpreterState *interp,
/*******************/

int
_PyImport_FixupBuiltin(PyObject *mod, const char *name, PyObject *modules)
_PyImport_FixupBuiltin(PyThreadState *tstate, PyObject *mod, const char *name,
PyObject *modules)
{
int res = -1;
assert(mod != NULL && PyModule_Check(mod));
Expand All @@ -1394,7 +1395,7 @@ _PyImport_FixupBuiltin(PyObject *mod, const char *name, PyObject *modules)
goto finally;
}

if (fix_up_extension(mod, def, nameobj, nameobj, modules) < 0) {
if (fix_up_extension(tstate, mod, def, nameobj, nameobj, modules) < 0) {
goto finally;
}

Expand Down Expand Up @@ -1468,7 +1469,7 @@ create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec)
def->m_base.m_init = p0;

PyObject *modules = MODULES(tstate->interp);
if (fix_up_extension(mod, def, name, name, modules) < 0) {
if (fix_up_extension(tstate, mod, def, name, name, modules) < 0) {
return NULL;
}
return mod;
Expand Down
2 changes: 1 addition & 1 deletion Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ pycore_init_builtins(PyThreadState *tstate)
}

PyObject *modules = _PyImport_GetModules(interp);
if (_PyImport_FixupBuiltin(bimod, "builtins", modules) < 0) {
if (_PyImport_FixupBuiltin(tstate, bimod, "builtins", modules) < 0) {
goto error;
}

Expand Down
2 changes: 1 addition & 1 deletion Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3764,7 +3764,7 @@ _PySys_Create(PyThreadState *tstate, PyObject **sysmod_p)
return status;
}

if (_PyImport_FixupBuiltin(sysmod, "sys", modules) < 0) {
if (_PyImport_FixupBuiltin(tstate, sysmod, "sys", modules) < 0) {
goto error;
}

Expand Down
0