Skip to content
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

gh-117953: Split Up _PyImport_LoadDynamicModuleWithSpec() #118203

Merged
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
Factor out create_dynamic().
  • Loading branch information
ericsnowcurrently committed Apr 24, 2024
commit daf7295237c10bb544cc582618bb798af3c3d0b6
142 changes: 87 additions & 55 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,90 @@ clear_singlephase_extension(PyInterpreterState *interp,
return 0;
}

static PyObject *
create_dynamic(PyThreadState *tstate, struct _Py_ext_module_loader_info *info,
PyObject *file, PyObject *spec)
{
PyObject *mod = NULL;

/* We would move this (and the fclose() below) into
* _PyImport_GetModInitFunc(), but it isn't clear if the intervening
* code relies on fp still being open. */
FILE *fp;
if (file != NULL) {
fp = _Py_fopen_obj(info->filename, "r");
if (fp == NULL) {
goto finally;
}
}
else {
fp = NULL;
}

PyModInitFunction p0 = _PyImport_GetModInitFunc(info, fp);
if (p0 == NULL) {
goto finally;
}

struct _Py_ext_module_loader_result res;
if (_PyImport_RunModInitFunc(p0, info, &res) < 0) {
assert(PyErr_Occurred());
goto finally;
}

if (res.module == NULL) {
//assert(!is_singlephase(res.def));
mod = PyModule_FromDefAndSpec(res.def, spec);
}
else {
assert(is_singlephase(res.def));
assert(!is_core_module(tstate->interp, info->name, info->filename));
assert(!is_core_module(tstate->interp, info->name, info->name));
mod = Py_NewRef(res.module);

const char *name_buf = PyBytes_AS_STRING(info->name_encoded);
if (_PyImport_CheckSubinterpIncompatibleExtensionAllowed(name_buf) < 0) {
Py_CLEAR(mod);
goto finally;
}

/* Remember the filename as the __file__ attribute */
if (PyModule_AddObjectRef(mod, "__file__", info->filename) < 0) {
PyErr_Clear(); /* Not important enough to report */
}

struct singlephase_global_update singlephase = {0};
// gh-88216: Extensions and def->m_base.m_copy can be updated
// when the extension module doesn't support sub-interpreters.
if (res.def->m_size == -1) {
singlephase.m_dict = PyModule_GetDict(mod);
assert(singlephase.m_dict != NULL);
}
if (update_global_state_for_extension(
tstate, info->filename, info->name, res.def, &singlephase) < 0)
{
Py_CLEAR(mod);
goto finally;
}

PyObject *modules = get_modules_dict(tstate, true);
if (finish_singlephase_extension(
tstate, mod, res.def, info->name, modules) < 0)
{
Py_CLEAR(mod);
goto finally;
}
}

// XXX Shouldn't this happen in the error cases too.
if (fp) {
fclose(fp);
}

finally:
return mod;
}


/*******************/
/* builtin modules */
Expand Down Expand Up @@ -3921,64 +4005,12 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
goto finally;
}

/* We would move this (and the fclose() below) into
* _PyImport_GetModInitFunc(), but it isn't clear if the intervening
* code relies on fp still being open. */
FILE *fp;
if (file != NULL) {
fp = _Py_fopen_obj(info.filename, "r");
if (fp == NULL) {
goto finally;
}
}
else {
fp = NULL;
}

PyModInitFunction p0 = _PyImport_GetModInitFunc(&info, fp);
if (p0 == NULL) {
/* Is multi-phase init or this is the first time being loaded. */
mod = create_dynamic(tstate, &info, file, spec);
if (mod == NULL) {
goto finally;
}

struct _Py_ext_module_loader_result res;
if (_PyImport_RunModInitFunc(p0, &info, &res) < 0) {
assert(PyErr_Occurred());
return NULL;
}

if (res.module == NULL) {
//assert(!is_singlephase(res.def));
mod = PyModule_FromDefAndSpec(res.def, spec);
}
else {
assert(is_singlephase(res.def));
mod = Py_NewRef(res.module);

const char *name_buf = PyBytes_AS_STRING(info.name_encoded);
if (_PyImport_CheckSubinterpIncompatibleExtensionAllowed(name_buf) < 0) {
Py_CLEAR(mod);
goto finally;
}

/* Remember the filename as the __file__ attribute */
if (PyModule_AddObjectRef(mod, "__file__", info.filename) < 0) {
PyErr_Clear(); /* Not important enough to report */
}

PyObject *modules = get_modules_dict(tstate, true);
if (_PyImport_FixupExtensionObject(
mod, info.name, info.filename, modules) < 0)
{
Py_CLEAR(mod);
goto finally;
}
}

// XXX Shouldn't this happen in the error cases too.
if (fp) {
fclose(fp);
}

finally:
_Py_ext_module_loader_info_clear(&info);
return mod;
Expand Down