mirror of
https://github.com/python/cpython.git
synced 2026-01-06 15:32:22 +00:00
gh-117953: Add Internal struct _Py_ext_module_loader_info (gh-118194)
This helps with a later change that splits up _PyImport_LoadDynamicModuleWithSpec().
This commit is contained in:
parent
9b280ab0ab
commit
5865fa5f9b
3 changed files with 180 additions and 94 deletions
|
|
@ -93,57 +93,108 @@ get_encoded_name(PyObject *name, const char **hook_prefix) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
_PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
|
||||
void
|
||||
_Py_ext_module_loader_info_clear(struct _Py_ext_module_loader_info *info)
|
||||
{
|
||||
Py_CLEAR(info->filename);
|
||||
#ifndef MS_WINDOWS
|
||||
PyObject *filename_bytes = NULL;
|
||||
const char *filename_buf;
|
||||
Py_CLEAR(info->filename_encoded);
|
||||
#endif
|
||||
PyObject *name_unicode = NULL, *name = NULL, *filename = NULL, *m = NULL;
|
||||
const char *name_buf, *hook_prefix;
|
||||
const char *oldcontext, *newcontext;
|
||||
dl_funcptr exportfunc;
|
||||
PyModuleDef *def;
|
||||
PyModInitFunction p0;
|
||||
Py_CLEAR(info->name);
|
||||
Py_CLEAR(info->name_encoded);
|
||||
}
|
||||
|
||||
name_unicode = PyObject_GetAttrString(spec, "name");
|
||||
if (name_unicode == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (!PyUnicode_Check(name_unicode)) {
|
||||
int
|
||||
_Py_ext_module_loader_info_init(struct _Py_ext_module_loader_info *p_info,
|
||||
PyObject *name, PyObject *filename)
|
||||
{
|
||||
struct _Py_ext_module_loader_info info = {0};
|
||||
|
||||
assert(name != NULL);
|
||||
if (!PyUnicode_Check(name)) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"spec.name must be a string");
|
||||
goto error;
|
||||
"module name must be a string");
|
||||
_Py_ext_module_loader_info_clear(&info);
|
||||
return -1;
|
||||
}
|
||||
newcontext = PyUnicode_AsUTF8(name_unicode);
|
||||
if (newcontext == NULL) {
|
||||
goto error;
|
||||
info.name = Py_NewRef(name);
|
||||
|
||||
info.name_encoded = get_encoded_name(info.name, &info.hook_prefix);
|
||||
if (info.name_encoded == NULL) {
|
||||
_Py_ext_module_loader_info_clear(&info);
|
||||
return -1;
|
||||
}
|
||||
|
||||
name = get_encoded_name(name_unicode, &hook_prefix);
|
||||
info.newcontext = PyUnicode_AsUTF8(info.name);
|
||||
if (info.newcontext == NULL) {
|
||||
_Py_ext_module_loader_info_clear(&info);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (filename != NULL) {
|
||||
if (!PyUnicode_Check(filename)) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"module filename must be a string");
|
||||
_Py_ext_module_loader_info_clear(&info);
|
||||
return -1;
|
||||
}
|
||||
info.filename = Py_NewRef(filename);
|
||||
|
||||
#ifndef MS_WINDOWS
|
||||
info.filename_encoded = PyUnicode_EncodeFSDefault(info.filename);
|
||||
if (info.filename_encoded == NULL) {
|
||||
_Py_ext_module_loader_info_clear(&info);
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
info.path = info.filename;
|
||||
}
|
||||
else {
|
||||
info.path = info.name;
|
||||
}
|
||||
|
||||
*p_info = info;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
_Py_ext_module_loader_info_init_from_spec(
|
||||
struct _Py_ext_module_loader_info *p_info,
|
||||
PyObject *spec)
|
||||
{
|
||||
PyObject *name = PyObject_GetAttrString(spec, "name");
|
||||
if (name == NULL) {
|
||||
goto error;
|
||||
return -1;
|
||||
}
|
||||
name_buf = PyBytes_AS_STRING(name);
|
||||
|
||||
filename = PyObject_GetAttrString(spec, "origin");
|
||||
PyObject *filename = PyObject_GetAttrString(spec, "origin");
|
||||
if (filename == NULL) {
|
||||
goto error;
|
||||
return -1;
|
||||
}
|
||||
return _Py_ext_module_loader_info_init(p_info, name, filename);
|
||||
}
|
||||
|
||||
|
||||
PyObject *
|
||||
_PyImport_LoadDynamicModuleWithSpec(struct _Py_ext_module_loader_info *info,
|
||||
PyObject *spec, FILE *fp)
|
||||
{
|
||||
PyObject *m = NULL;
|
||||
const char *name_buf = PyBytes_AS_STRING(info->name_encoded);
|
||||
const char *oldcontext;
|
||||
dl_funcptr exportfunc;
|
||||
PyModInitFunction p0;
|
||||
PyModuleDef *def;
|
||||
|
||||
#ifdef MS_WINDOWS
|
||||
exportfunc = _PyImport_FindSharedFuncptrWindows(
|
||||
hook_prefix, name_buf, filename, fp);
|
||||
info->hook_prefix, name_buf, info->filename, fp);
|
||||
#else
|
||||
filename_bytes = PyUnicode_EncodeFSDefault(filename);
|
||||
if (filename_bytes == NULL) {
|
||||
goto error;
|
||||
{
|
||||
const char *path_buf = PyBytes_AS_STRING(info->filename_encoded);
|
||||
exportfunc = _PyImport_FindSharedFuncptr(
|
||||
info->hook_prefix, name_buf, path_buf, fp);
|
||||
}
|
||||
filename_buf = PyBytes_AS_STRING(filename_bytes);
|
||||
exportfunc = _PyImport_FindSharedFuncptr(
|
||||
hook_prefix, name_buf, filename_buf, fp);
|
||||
Py_DECREF(filename_bytes);
|
||||
#endif
|
||||
|
||||
if (exportfunc == NULL) {
|
||||
|
|
@ -152,11 +203,11 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
|
|||
msg = PyUnicode_FromFormat(
|
||||
"dynamic module does not define "
|
||||
"module export function (%s_%s)",
|
||||
hook_prefix, name_buf);
|
||||
if (msg == NULL)
|
||||
goto error;
|
||||
PyErr_SetImportError(msg, name_unicode, filename);
|
||||
Py_DECREF(msg);
|
||||
info->hook_prefix, name_buf);
|
||||
if (msg != NULL) {
|
||||
PyErr_SetImportError(msg, info->name, info->filename);
|
||||
Py_DECREF(msg);
|
||||
}
|
||||
}
|
||||
goto error;
|
||||
}
|
||||
|
|
@ -164,7 +215,7 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
|
|||
p0 = (PyModInitFunction)exportfunc;
|
||||
|
||||
/* Package context is needed for single-phase init */
|
||||
oldcontext = _PyImport_SwapPackageContext(newcontext);
|
||||
oldcontext = _PyImport_SwapPackageContext(info->newcontext);
|
||||
m = p0();
|
||||
_PyImport_SwapPackageContext(oldcontext);
|
||||
|
||||
|
|
@ -195,9 +246,6 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
|
|||
goto error;
|
||||
}
|
||||
if (PyObject_TypeCheck(m, &PyModuleDef_Type)) {
|
||||
Py_DECREF(name_unicode);
|
||||
Py_DECREF(name);
|
||||
Py_DECREF(filename);
|
||||
return PyModule_FromDefAndSpec((PyModuleDef*)m, spec);
|
||||
}
|
||||
|
||||
|
|
@ -207,7 +255,7 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
|
|||
goto error;
|
||||
}
|
||||
|
||||
if (hook_prefix == nonascii_prefix) {
|
||||
if (info->hook_prefix == nonascii_prefix) {
|
||||
/* don't allow legacy init for non-ASCII module names */
|
||||
PyErr_Format(
|
||||
PyExc_SystemError,
|
||||
|
|
@ -227,24 +275,20 @@ _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->filename) < 0) {
|
||||
PyErr_Clear(); /* Not important enough to report */
|
||||
}
|
||||
|
||||
PyObject *modules = PyImport_GetModuleDict();
|
||||
if (_PyImport_FixupExtensionObject(m, name_unicode, filename, modules) < 0)
|
||||
if (_PyImport_FixupExtensionObject(
|
||||
m, info->name, info->filename, modules) < 0)
|
||||
{
|
||||
goto error;
|
||||
|
||||
Py_DECREF(name_unicode);
|
||||
Py_DECREF(name);
|
||||
Py_DECREF(filename);
|
||||
}
|
||||
|
||||
return m;
|
||||
|
||||
error:
|
||||
Py_DECREF(name_unicode);
|
||||
Py_XDECREF(name);
|
||||
Py_XDECREF(filename);
|
||||
Py_XDECREF(m);
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue