summaryrefslogtreecommitdiff
path: root/Python/importdl.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2011-03-14 15:54:07 -0400
committerVictor Stinner <victor.stinner@haypocalc.com>2011-03-14 15:54:07 -0400
commitfefd70c40d16b779a4c378a795b5577470af07d8 (patch)
tree3967bf0de03825f3fa6e6a0e7adf920e7a278c91 /Python/importdl.c
parent4d6c1c476aee975867c5375f9fc175f69d4b6266 (diff)
downloadcpython-git-fefd70c40d16b779a4c378a795b5577470af07d8.tar.gz
Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
Document also that dynamic module names are ASCII only
Diffstat (limited to 'Python/importdl.c')
-rw-r--r--Python/importdl.c70
1 files changed, 29 insertions, 41 deletions
diff --git a/Python/importdl.c b/Python/importdl.c
index f0e1f55d27..629f3e294e 100644
--- a/Python/importdl.c
+++ b/Python/importdl.c
@@ -15,67 +15,68 @@
extern dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
const char *pathname, FILE *fp);
-
+/* name should be ASCII only because the C language doesn't accept non-ASCII
+ identifiers, and dynamic modules are written in C. */
PyObject *
-_PyImport_LoadDynamicModule(char *name, char *pathname, FILE *fp)
+_PyImport_LoadDynamicModule(PyObject *name, PyObject *path, FILE *fp)
{
PyObject *m;
- PyObject *path;
- char *lastdot, *shortname, *packagecontext, *oldcontext;
+ PyObject *pathbytes;
+ char *namestr, *lastdot, *shortname, *packagecontext, *oldcontext;
dl_funcptr p0;
PyObject* (*p)(void);
struct PyModuleDef *def;
- PyObject *nameobj, *result;
- path = PyUnicode_DecodeFSDefault(pathname);
- if (path == NULL)
+ namestr = _PyUnicode_AsString(name);
+ if (namestr == NULL)
return NULL;
- nameobj = PyUnicode_FromString(name);
- if (nameobj == NULL)
- return NULL;
- m = _PyImport_FindExtensionObject(nameobj, path);
+ m = _PyImport_FindExtensionObject(name, path);
if (m != NULL) {
- Py_DECREF(nameobj);
Py_INCREF(m);
- result = m;
- goto finally;
+ return m;
}
- Py_DECREF(nameobj);
- lastdot = strrchr(name, '.');
+
+ lastdot = strrchr(namestr, '.');
if (lastdot == NULL) {
packagecontext = NULL;
- shortname = name;
+ shortname = namestr;
}
else {
- packagecontext = name;
+ packagecontext = namestr;
shortname = lastdot+1;
}
- p0 = _PyImport_GetDynLoadFunc(shortname, pathname, fp);
+ pathbytes = PyUnicode_EncodeFSDefault(path);
+ if (pathbytes == NULL)
+ return NULL;
+ p0 = _PyImport_GetDynLoadFunc(shortname,
+ PyBytes_AS_STRING(pathbytes), fp);
+ Py_DECREF(pathbytes);
p = (PyObject*(*)(void))p0;
if (PyErr_Occurred())
- goto error;
+ return NULL;
if (p == NULL) {
PyErr_Format(PyExc_ImportError,
- "dynamic module does not define init function (PyInit_%.200s)",
+ "dynamic module does not define init function"
+ " (PyInit_%s)",
shortname);
- goto error;
+ return NULL;
}
oldcontext = _Py_PackageContext;
_Py_PackageContext = packagecontext;
m = (*p)();
_Py_PackageContext = oldcontext;
if (m == NULL)
- goto error;
+ return NULL;
if (PyErr_Occurred()) {
Py_DECREF(m);
PyErr_Format(PyExc_SystemError,
"initialization of %s raised unreported exception",
shortname);
- goto error;
+ return NULL;
}
/* Remember pointer to module init function. */
@@ -88,26 +89,13 @@ _PyImport_LoadDynamicModule(char *name, char *pathname, FILE *fp)
else
Py_INCREF(path);
- nameobj = PyUnicode_FromString(name);
- if (nameobj == NULL)
+ if (_PyImport_FixupExtensionObject(m, name, path) < 0)
return NULL;
- if (_PyImport_FixupExtensionObject(m, nameobj, path) < 0) {
- Py_DECREF(nameobj);
- goto error;
- }
if (Py_VerboseFlag)
PySys_FormatStderr(
- "import %U # dynamically loaded from %s\n",
- nameobj, pathname);
- Py_DECREF(nameobj);
- result = m;
- goto finally;
-
-error:
- result = NULL;
-finally:
- Py_DECREF(path);
- return result;
+ "import %U # dynamically loaded from %R\n",
+ name, path);
+ return m;
}
#endif /* HAVE_DYNAMIC_LOADING */