Commit 65b7eff7 authored by Victor Stinner's avatar Victor Stinner

_PyImport_LoadDynamicModule() encodes the module name explicitly to ASCII

The name must be encodable to ASCII because dynamic module must have a function
called "PyInit_NAME", they are written in C, and the C language doesn't accept
non-ASCII identifiers.
parent 7bba62fd
...@@ -20,31 +20,36 @@ extern dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname, ...@@ -20,31 +20,36 @@ extern dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
const char *pathname, FILE *fp); const char *pathname, FILE *fp);
#endif #endif
/* name should be ASCII only because the C language doesn't accept non-ASCII
identifiers, and dynamic modules are written in C. */
PyObject * PyObject *
_PyImport_LoadDynamicModule(PyObject *name, PyObject *path, FILE *fp) _PyImport_LoadDynamicModule(PyObject *name, PyObject *path, FILE *fp)
{ {
PyObject *m; PyObject *m = NULL;
#ifndef MS_WINDOWS #ifndef MS_WINDOWS
PyObject *pathbytes; PyObject *pathbytes;
#endif #endif
PyObject *nameascii;
char *namestr, *lastdot, *shortname, *packagecontext, *oldcontext; char *namestr, *lastdot, *shortname, *packagecontext, *oldcontext;
dl_funcptr p0; dl_funcptr p0;
PyObject* (*p)(void); PyObject* (*p)(void);
struct PyModuleDef *def; struct PyModuleDef *def;
namestr = _PyUnicode_AsString(name);
if (namestr == NULL)
return NULL;
m = _PyImport_FindExtensionObject(name, path); m = _PyImport_FindExtensionObject(name, path);
if (m != NULL) { if (m != NULL) {
Py_INCREF(m); Py_INCREF(m);
return m; return m;
} }
/* name must be encodable to ASCII because dynamic module must have a
function called "PyInit_NAME", they are written in C, and the C language
doesn't accept non-ASCII identifiers. */
nameascii = PyUnicode_AsEncodedString(name, "ascii", NULL);
if (nameascii == NULL)
return NULL;
namestr = PyBytes_AS_STRING(nameascii);
if (namestr == NULL)
goto error;
lastdot = strrchr(namestr, '.'); lastdot = strrchr(namestr, '.');
if (lastdot == NULL) { if (lastdot == NULL) {
packagecontext = NULL; packagecontext = NULL;
...@@ -60,34 +65,33 @@ _PyImport_LoadDynamicModule(PyObject *name, PyObject *path, FILE *fp) ...@@ -60,34 +65,33 @@ _PyImport_LoadDynamicModule(PyObject *name, PyObject *path, FILE *fp)
#else #else
pathbytes = PyUnicode_EncodeFSDefault(path); pathbytes = PyUnicode_EncodeFSDefault(path);
if (pathbytes == NULL) if (pathbytes == NULL)
return NULL; goto error;
p0 = _PyImport_GetDynLoadFunc(shortname, p0 = _PyImport_GetDynLoadFunc(shortname,
PyBytes_AS_STRING(pathbytes), fp); PyBytes_AS_STRING(pathbytes), fp);
Py_DECREF(pathbytes); Py_DECREF(pathbytes);
#endif #endif
p = (PyObject*(*)(void))p0; p = (PyObject*(*)(void))p0;
if (PyErr_Occurred()) if (PyErr_Occurred())
return NULL; goto error;
if (p == NULL) { if (p == NULL) {
PyErr_Format(PyExc_ImportError, PyErr_Format(PyExc_ImportError,
"dynamic module does not define init function" "dynamic module does not define init function"
" (PyInit_%s)", " (PyInit_%s)",
shortname); shortname);
return NULL; goto error;
} }
oldcontext = _Py_PackageContext; oldcontext = _Py_PackageContext;
_Py_PackageContext = packagecontext; _Py_PackageContext = packagecontext;
m = (*p)(); m = (*p)();
_Py_PackageContext = oldcontext; _Py_PackageContext = oldcontext;
if (m == NULL) if (m == NULL)
return NULL; goto error;
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
Py_DECREF(m);
PyErr_Format(PyExc_SystemError, PyErr_Format(PyExc_SystemError,
"initialization of %s raised unreported exception", "initialization of %s raised unreported exception",
shortname); shortname);
return NULL; goto error;
} }
/* Remember pointer to module init function. */ /* Remember pointer to module init function. */
...@@ -101,12 +105,18 @@ _PyImport_LoadDynamicModule(PyObject *name, PyObject *path, FILE *fp) ...@@ -101,12 +105,18 @@ _PyImport_LoadDynamicModule(PyObject *name, PyObject *path, FILE *fp)
Py_INCREF(path); Py_INCREF(path);
if (_PyImport_FixupExtensionObject(m, name, path) < 0) if (_PyImport_FixupExtensionObject(m, name, path) < 0)
return NULL; goto error;
if (Py_VerboseFlag) if (Py_VerboseFlag)
PySys_FormatStderr( PySys_FormatStderr(
"import %U # dynamically loaded from %R\n", "import %U # dynamically loaded from %R\n",
name, path); name, path);
Py_DECREF(nameascii);
return m; return m;
error:
Py_DECREF(nameascii);
Py_XDECREF(m);
return NULL;
} }
#endif /* HAVE_DYNAMIC_LOADING */ #endif /* HAVE_DYNAMIC_LOADING */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment