1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
/* Support for dynamic loading of extension modules */
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
#include "Python.h"
|
2022-02-08 16:39:07 -04:00
|
|
|
#include "pycore_call.h"
|
2023-02-15 21:16:00 -04:00
|
|
|
#include "pycore_import.h"
|
2023-07-03 07:48:50 -03:00
|
|
|
#include "pycore_pyerrors.h" // _PyErr_FormatFromCause()
|
2022-02-08 16:39:07 -04:00
|
|
|
#include "pycore_pystate.h"
|
|
|
|
#include "pycore_runtime.h"
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1999-12-22 10:09:35 -04:00
|
|
|
/* ./configure sets HAVE_DYNAMIC_LOADING if dynamic loading of modules is
|
|
|
|
supported on this platform. configure will then compile and link in one
|
|
|
|
of the dynload_*.c files, as appropriate. We will call a function in
|
|
|
|
those modules to get a function pointer to the module's init function.
|
1995-01-02 15:04:15 -04:00
|
|
|
*/
|
1999-12-20 17:20:42 -04:00
|
|
|
#ifdef HAVE_DYNAMIC_LOADING
|
1995-01-02 15:04:15 -04:00
|
|
|
|
2023-10-04 19:50:29 -03:00
|
|
|
#include "pycore_importdl.h"
|
1999-12-22 10:09:35 -04:00
|
|
|
|
2011-04-04 18:05:53 -03:00
|
|
|
#ifdef MS_WINDOWS
|
2015-05-23 09:24:10 -03:00
|
|
|
extern dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
|
|
|
|
const char *shortname,
|
|
|
|
PyObject *pathname,
|
|
|
|
FILE *fp);
|
2011-04-04 18:05:53 -03:00
|
|
|
#else
|
2015-05-23 09:24:10 -03:00
|
|
|
extern dl_funcptr _PyImport_FindSharedFuncptr(const char *prefix,
|
|
|
|
const char *shortname,
|
|
|
|
const char *pathname, FILE *fp);
|
2011-04-04 18:05:53 -03:00
|
|
|
#endif
|
1997-11-22 17:53:48 -04:00
|
|
|
|
2015-12-25 13:53:18 -04:00
|
|
|
static const char * const ascii_only_prefix = "PyInit";
|
|
|
|
static const char * const nonascii_prefix = "PyInitU";
|
2015-05-23 09:24:10 -03:00
|
|
|
|
|
|
|
/* Get the variable part of a module's export symbol name.
|
|
|
|
* Returns a bytes instance. For non-ASCII-named modules, the name is
|
|
|
|
* encoded as per PEP 489.
|
|
|
|
* The hook_prefix pointer is set to either ascii_only_prefix or
|
|
|
|
* nonascii_prefix, as appropriate.
|
|
|
|
*/
|
|
|
|
static PyObject *
|
|
|
|
get_encoded_name(PyObject *name, const char **hook_prefix) {
|
|
|
|
PyObject *tmp;
|
|
|
|
PyObject *encoded = NULL;
|
2015-05-29 19:10:30 -03:00
|
|
|
PyObject *modname = NULL;
|
|
|
|
Py_ssize_t name_len, lastdot;
|
2015-05-23 09:24:10 -03:00
|
|
|
|
|
|
|
/* Get the short name (substring after last dot) */
|
|
|
|
name_len = PyUnicode_GetLength(name);
|
2021-10-11 08:08:38 -03:00
|
|
|
if (name_len < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-05-23 09:24:10 -03:00
|
|
|
lastdot = PyUnicode_FindChar(name, '.', 0, name_len, -1);
|
|
|
|
if (lastdot < -1) {
|
|
|
|
return NULL;
|
|
|
|
} else if (lastdot >= 0) {
|
2015-05-26 08:48:17 -03:00
|
|
|
tmp = PyUnicode_Substring(name, lastdot + 1, name_len);
|
2015-05-23 09:24:10 -03:00
|
|
|
if (tmp == NULL)
|
|
|
|
return NULL;
|
|
|
|
name = tmp;
|
|
|
|
/* "name" now holds a new reference to the substring */
|
|
|
|
} else {
|
|
|
|
Py_INCREF(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Encode to ASCII or Punycode, as needed */
|
|
|
|
encoded = PyUnicode_AsEncodedString(name, "ascii", NULL);
|
|
|
|
if (encoded != NULL) {
|
|
|
|
*hook_prefix = ascii_only_prefix;
|
|
|
|
} else {
|
|
|
|
if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) {
|
|
|
|
PyErr_Clear();
|
|
|
|
encoded = PyUnicode_AsEncodedString(name, "punycode", NULL);
|
|
|
|
if (encoded == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
*hook_prefix = nonascii_prefix;
|
|
|
|
} else {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-29 19:10:30 -03:00
|
|
|
/* Replace '-' by '_' */
|
2022-02-08 16:39:07 -04:00
|
|
|
modname = _PyObject_CallMethod(encoded, &_Py_ID(replace), "cc", '-', '_');
|
2015-05-29 19:10:30 -03:00
|
|
|
if (modname == NULL)
|
|
|
|
goto error;
|
2015-05-23 09:24:10 -03:00
|
|
|
|
|
|
|
Py_DECREF(name);
|
2015-05-29 19:10:30 -03:00
|
|
|
Py_DECREF(encoded);
|
|
|
|
return modname;
|
2015-05-23 09:24:10 -03:00
|
|
|
error:
|
|
|
|
Py_DECREF(name);
|
|
|
|
Py_XDECREF(encoded);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-04-24 14:42:01 -03:00
|
|
|
void
|
|
|
|
_Py_ext_module_loader_info_clear(struct _Py_ext_module_loader_info *info)
|
1995-01-02 15:04:15 -04:00
|
|
|
{
|
2024-04-24 14:42:01 -03:00
|
|
|
Py_CLEAR(info->filename);
|
2011-04-04 18:05:53 -03:00
|
|
|
#ifndef MS_WINDOWS
|
2024-04-24 14:42:01 -03:00
|
|
|
Py_CLEAR(info->filename_encoded);
|
2011-04-04 18:05:53 -03:00
|
|
|
#endif
|
2024-04-24 14:42:01 -03:00
|
|
|
Py_CLEAR(info->name);
|
|
|
|
Py_CLEAR(info->name_encoded);
|
|
|
|
}
|
2015-05-23 09:24:10 -03:00
|
|
|
|
2024-04-24 14:42:01 -03:00
|
|
|
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)) {
|
2017-09-19 08:39:47 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
2024-04-24 14:42:01 -03:00
|
|
|
"module name must be a string");
|
|
|
|
_Py_ext_module_loader_info_clear(&info);
|
|
|
|
return -1;
|
2017-09-19 08:39:47 -03:00
|
|
|
}
|
2024-04-24 14:42:01 -03:00
|
|
|
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;
|
2023-02-15 18:32:31 -04:00
|
|
|
}
|
2011-05-07 07:46:05 -03:00
|
|
|
|
2024-04-24 14:42:01 -03:00
|
|
|
info.newcontext = PyUnicode_AsUTF8(info.name);
|
|
|
|
if (info.newcontext == NULL) {
|
|
|
|
_Py_ext_module_loader_info_clear(&info);
|
|
|
|
return -1;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2015-05-23 09:24:10 -03:00
|
|
|
|
2024-04-24 14:42:01 -03:00
|
|
|
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) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
PyObject *filename = PyObject_GetAttrString(spec, "origin");
|
2024-04-23 11:25:50 -03:00
|
|
|
if (filename == NULL) {
|
2024-04-24 14:42:01 -03:00
|
|
|
return -1;
|
2019-05-23 12:45:22 -03:00
|
|
|
}
|
2024-04-24 14:42:01 -03:00
|
|
|
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;
|
2019-05-23 12:45:22 -03:00
|
|
|
|
2011-04-04 18:05:53 -03:00
|
|
|
#ifdef MS_WINDOWS
|
2024-04-23 11:25:50 -03:00
|
|
|
exportfunc = _PyImport_FindSharedFuncptrWindows(
|
2024-04-24 14:42:01 -03:00
|
|
|
info->hook_prefix, name_buf, info->filename, fp);
|
2011-04-04 18:05:53 -03:00
|
|
|
#else
|
2024-04-24 14:42:01 -03:00
|
|
|
{
|
|
|
|
const char *path_buf = PyBytes_AS_STRING(info->filename_encoded);
|
|
|
|
exportfunc = _PyImport_FindSharedFuncptr(
|
|
|
|
info->hook_prefix, name_buf, path_buf, fp);
|
2024-04-23 11:25:50 -03:00
|
|
|
}
|
2011-04-04 18:05:53 -03:00
|
|
|
#endif
|
2015-05-23 09:24:10 -03:00
|
|
|
|
|
|
|
if (exportfunc == NULL) {
|
|
|
|
if (!PyErr_Occurred()) {
|
|
|
|
PyObject *msg;
|
|
|
|
msg = PyUnicode_FromFormat(
|
|
|
|
"dynamic module does not define "
|
|
|
|
"module export function (%s_%s)",
|
2024-04-24 14:42:01 -03:00
|
|
|
info->hook_prefix, name_buf);
|
|
|
|
if (msg != NULL) {
|
|
|
|
PyErr_SetImportError(msg, info->name, info->filename);
|
|
|
|
Py_DECREF(msg);
|
|
|
|
}
|
2015-05-23 09:24:10 -03:00
|
|
|
}
|
2011-05-07 07:46:05 -03:00
|
|
|
goto error;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2015-05-23 09:24:10 -03:00
|
|
|
|
2022-03-30 16:28:33 -03:00
|
|
|
p0 = (PyModInitFunction)exportfunc;
|
2015-05-23 09:24:10 -03:00
|
|
|
|
|
|
|
/* Package context is needed for single-phase init */
|
2024-04-24 14:42:01 -03:00
|
|
|
oldcontext = _PyImport_SwapPackageContext(info->newcontext);
|
2023-06-29 21:20:49 -03:00
|
|
|
m = p0();
|
2023-02-15 18:32:31 -04:00
|
|
|
_PyImport_SwapPackageContext(oldcontext);
|
1995-01-02 15:04:15 -04:00
|
|
|
|
2015-05-23 09:24:10 -03:00
|
|
|
if (m == NULL) {
|
|
|
|
if (!PyErr_Occurred()) {
|
|
|
|
PyErr_Format(
|
|
|
|
PyExc_SystemError,
|
|
|
|
"initialization of %s failed without raising an exception",
|
|
|
|
name_buf);
|
|
|
|
}
|
|
|
|
goto error;
|
|
|
|
} else if (PyErr_Occurred()) {
|
2022-12-23 19:43:19 -04:00
|
|
|
_PyErr_FormatFromCause(
|
2015-05-23 09:24:10 -03:00
|
|
|
PyExc_SystemError,
|
|
|
|
"initialization of %s raised unreported exception",
|
|
|
|
name_buf);
|
|
|
|
m = NULL;
|
|
|
|
goto error;
|
|
|
|
}
|
2020-03-04 09:15:20 -04:00
|
|
|
if (Py_IS_TYPE(m, NULL)) {
|
2015-05-23 09:24:10 -03:00
|
|
|
/* This can happen when a PyModuleDef is returned without calling
|
|
|
|
* PyModuleDef_Init on it
|
|
|
|
*/
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_Format(PyExc_SystemError,
|
2015-05-23 09:24:10 -03:00
|
|
|
"init function of %s returned uninitialized object",
|
|
|
|
name_buf);
|
|
|
|
m = NULL; /* prevent segfault in DECREF */
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (PyObject_TypeCheck(m, &PyModuleDef_Type)) {
|
|
|
|
return PyModule_FromDefAndSpec((PyModuleDef*)m, spec);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fall back to single-phase init mechanism */
|
|
|
|
|
2023-02-15 21:16:00 -04:00
|
|
|
if (_PyImport_CheckSubinterpIncompatibleExtensionAllowed(name_buf) < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2024-04-24 14:42:01 -03:00
|
|
|
if (info->hook_prefix == nonascii_prefix) {
|
2015-05-23 09:24:10 -03:00
|
|
|
/* don't allow legacy init for non-ASCII module names */
|
|
|
|
PyErr_Format(
|
|
|
|
PyExc_SystemError,
|
2021-10-12 04:20:04 -03:00
|
|
|
"initialization of %s did not return PyModuleDef",
|
2015-05-23 09:24:10 -03:00
|
|
|
name_buf);
|
2011-05-07 07:46:05 -03:00
|
|
|
goto error;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2008-06-11 02:26:20 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Remember pointer to module init function. */
|
|
|
|
def = PyModule_GetDef(m);
|
2013-07-11 08:02:30 -03:00
|
|
|
if (def == NULL) {
|
|
|
|
PyErr_Format(PyExc_SystemError,
|
|
|
|
"initialization of %s did not return an extension "
|
2015-05-23 09:24:10 -03:00
|
|
|
"module", name_buf);
|
2013-07-11 06:22:21 -03:00
|
|
|
goto error;
|
2013-07-11 08:02:30 -03:00
|
|
|
}
|
2015-05-23 09:24:10 -03:00
|
|
|
def->m_base.m_init = p0;
|
2008-06-11 02:26:20 -03:00
|
|
|
|
2024-04-24 12:55:48 -03:00
|
|
|
/* Remember the filename as the __file__ attribute */
|
2024-04-24 14:42:01 -03:00
|
|
|
if (PyModule_AddObjectRef(m, "__file__", info->filename) < 0) {
|
2024-04-24 12:55:48 -03:00
|
|
|
PyErr_Clear(); /* Not important enough to report */
|
|
|
|
}
|
|
|
|
|
2017-09-14 15:18:12 -03:00
|
|
|
PyObject *modules = PyImport_GetModuleDict();
|
2024-04-24 14:42:01 -03:00
|
|
|
if (_PyImport_FixupExtensionObject(
|
|
|
|
m, info->name, info->filename, modules) < 0)
|
|
|
|
{
|
2011-05-07 07:46:05 -03:00
|
|
|
goto error;
|
2024-04-24 14:42:01 -03:00
|
|
|
}
|
2015-05-23 09:24:10 -03:00
|
|
|
|
2011-03-14 16:54:07 -03:00
|
|
|
return m;
|
2011-05-07 07:46:05 -03:00
|
|
|
|
|
|
|
error:
|
|
|
|
Py_XDECREF(m);
|
|
|
|
return NULL;
|
1998-08-04 19:46:29 -03:00
|
|
|
}
|
1999-12-22 10:09:35 -04:00
|
|
|
|
|
|
|
#endif /* HAVE_DYNAMIC_LOADING */
|