8000 gh-117953: Add Internal struct _Py_ext_module_loader_info by ericsnowcurrently · Pull Request #118194 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-117953: Add Internal struct _Py_ext_module_loader_info #118194

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
Prev Previous commit
Next Next commit
Pass info to _PyImport_LoadDynamicModuleWithSpec().
  • Loading branch information
ericsnowcurrently committed Apr 24, 2024
commit c9600ba17a8dd12fff6b3505b4550ecc72bd5e0a
5 changes: 4 additions & 1 deletion Include/internal/pycore_importdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ extern int _Py_ext_module_loader_info_from_spec(
PyObject *spec,
struct _Py_ext_module_loader_info *info);

extern PyObject *_PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *);
extern PyObject *_PyImport_LoadDynamicModuleWithSpec(
struct _Py_ext_module_loader_info *info,
PyObject *spec,
FILE *fp);


/* Max length of module suffix searched for -- accommodates "module.slb" */
Expand Down
23 changes: 8 additions & 15 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -3878,28 +3878,22 @@ static PyObject *
_imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
/*[clinic end generated code: output=83249b827a4fde77 input=c31b954f4cf4e09d]*/
{
PyObject *mod, *name, *filename;
PyObject *mod = NULL;
FILE *fp;

name = PyObject_GetAttrString(spec, "name");
if (name == NULL) {
return NULL;
}

filename = PyObject_GetAttrString(spec, "origin");
if (filename == NULL) {
Py_DECREF(name);
struct _Py_ext_module_loader_info info;
if (_Py_ext_module_loader_info_from_spec(spec, &info) < 0) {
return NULL;
}

PyThreadState *tstate = _PyThreadState_GET();
mod = import_find_extension(tstate, name, filename);
mod = import_find_extension(tstate, info.name, info.path);
if (mod != NULL || _PyErr_Occurred(tstate)) {
assert(mod == NULL || !_PyErr_Occurred(tstate));
goto finally;
}

if (PySys_Audit("import", "OOOOO", name, filename,
if (PySys_Audit("import", "OOOOO", info.name, info.path,
Py_None, Py_None, Py_None) < 0)
{
goto finally;
Expand All @@ -3911,7 +3905,7 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
* _PyImport_GetModInitFunc(), but it isn't clear if the intervening
* code relies on fp still being open. */
if (file != NULL) {
fp = _Py_fopen_obj(filename, "r");
fp = _Py_fopen_obj(info.path, "r");
if (fp == NULL) {
goto finally;
}
Expand All @@ -3920,16 +3914,15 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
fp = NULL;
}

mod = _PyImport_LoadDynamicModuleWithSpec(spec, fp);
mod = _PyImport_LoadDynamicModuleWithSpec(&info, spec, fp);

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

finally:
Py_DECREF(name);
Py_DECREF(filename);
_Py_ext_module_loader_info_clear(&info);
return mod;
}

Expand Down
31 changes: 12 additions & 19 deletions Python/importdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,40 +142,36 @@ _Py_ext_module_loader_info_from_spec(PyObject *spec,
}

PyObject *
_PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
_PyImport_LoadDynamicModuleWithSpec(struct _Py_ext_module_loader_info *info,
PyObject *spec, FILE *fp)
{
struct _Py_ext_module_loader_info info;
if (_Py_ext_module_loader_info_from_spec(spec, &info) < 0) {
return NULL;
}

#ifndef MS_WINDOWS
PyObject *pathbytes = NULL;
const char *path_buf;
#endif
PyObject *m = NULL;
const char *name_buf = PyBytes_AS_STRING(info.name_encoded);
const char *name_buf = PyBytes_AS_STRING(info->name_encoded);
const char *oldcontext, *newcontext;
dl_funcptr exportfunc;
PyModInitFunction p0;
PyModuleDef *def;

newcontext = PyUnicode_AsUTF8(info.name);
newcontext = PyUnicode_AsUTF8(info->name);
if (newcontext == NULL) {
8000 goto error;
}

#ifdef MS_WINDOWS
exportfunc = _PyImport_FindSharedFuncptrWindows(
info.hook_prefix, name_buf, info.path, fp);
info->hook_prefix, name_buf, info->path, fp);
#else
pathbytes = PyUnicode_EncodeFSDefault(info.path);
pathbytes = PyUnicode_EncodeFSDefault(info->path);
if (pathbytes == NULL) {
goto error;
}
path_buf = PyBytes_AS_STRING(pathbytes);
exportfunc = _PyImport_FindSharedFuncptr(
info.hook_prefix, name_buf, path_buf, fp);
info->hook_prefix, name_buf, path_buf, fp);
Py_DECREF(pathbytes);
#endif

Expand All @@ -185,9 +181,9 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
msg = PyUnicode_FromFormat(
"dynamic module does not define "
"module export function (%s_%s)",
info.hook_prefix, name_buf);
info->hook_prefix, name_buf);
if (msg != NULL) {
PyErr_SetImportError(msg, info.name, info.path);
PyErr_SetImportError(msg, info->name, info->path);
Py_DECREF(msg);
}
}
Expand Down Expand Up @@ -228,7 +224,6 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
goto error;
}
if (PyObject_TypeCheck(m, &PyModuleDef_Type)) {
_Py_ext_module_loader_info_clear(&info);
return PyModule_FromDefAndSpec((PyModuleDef*)m, spec);
}

Expand All @@ -238,7 +233,7 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
goto error;
}

if (info.hook_prefix == nonascii_prefix) {
if (info->hook_prefix == nonascii_prefix) {
/* don't allow legacy init for non-ASCII module names */
PyErr_Format(
PyExc_SystemError,
Expand All @@ -258,20 +253,18 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
def->m_base.m_init = p0;

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

PyObject *modules = PyImport_GetModuleDict();
if (_PyImport_FixupExtensionObject(m, info.name, info.path, modules) < 0) {
if (_PyImport_FixupExtensionObject(m, info->name, info->path, modules) < 0) {
goto error;
}

_Py_ext_module_loader_info_clear(&info);
return m;

error:
_Py_ext_module_loader_info_clear(&info);
Py_XDECREF(m);
return NULL;
}
Expand Down
0