]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path
authorVictor Stinner <victor.stinner@haypocalc.com>
Mon, 14 Mar 2011 19:54:07 +0000 (15:54 -0400)
committerVictor Stinner <victor.stinner@haypocalc.com>
Mon, 14 Mar 2011 19:54:07 +0000 (15:54 -0400)
Document also that dynamic module names are ASCII only

Python/import.c
Python/importdl.c
Python/importdl.h

index 8fab810eecd1633e8e1257dbff11505478b992f3..4e5cb2ddbf0fccf48eee9f0c6aca656d259754d6 100644 (file)
@@ -2173,9 +2173,21 @@ load_module(char *name, FILE *fp, char *pathname, int type, PyObject *loader)
         break;
 
 #ifdef HAVE_DYNAMIC_LOADING
-    case C_EXTENSION:
-        m = _PyImport_LoadDynamicModule(name, pathname, fp);
+    case C_EXTENSION: {
+        PyObject *nameobj, *pathobj;
+        nameobj = PyUnicode_FromString(name);
+        if (nameobj == NULL)
+            return NULL;
+        pathobj = PyUnicode_DecodeFSDefault(pathname);
+        if (pathobj == NULL) {
+            Py_DECREF(nameobj);
+            return NULL;
+        }
+        m = _PyImport_LoadDynamicModule(nameobj, pathobj, fp);
+        Py_DECREF(nameobj);
+        Py_DECREF(pathobj);
         break;
+    }
 #endif
 
     case PKG_DIRECTORY:
@@ -2185,11 +2197,10 @@ load_module(char *name, FILE *fp, char *pathname, int type, PyObject *loader)
     case C_BUILTIN:
     case PY_FROZEN: {
         PyObject *nameobj = PyUnicode_FromString(name);
-        if (nameobj != NULL) {
-            m = load_builtin(nameobj, type);
-            Py_DECREF(nameobj);
-        } else
-            m = NULL;
+        if (nameobj == NULL)
+            return NULL;
+        m = load_builtin(nameobj, type);
+        Py_DECREF(nameobj);
         break;
     }
 
@@ -3443,28 +3454,23 @@ imp_load_compiled(PyObject *self, PyObject *args)
 static PyObject *
 imp_load_dynamic(PyObject *self, PyObject *args)
 {
-    char *name;
-    PyObject *pathbytes;
-    char *pathname;
-    PyObject *fob = NULL;
-    PyObject *m;
-    FILE *fp = NULL;
-    if (!PyArg_ParseTuple(args, "sO&|O:load_dynamic",
-                          &name, PyUnicode_FSConverter, &pathbytes, &fob))
+    PyObject *name, *pathname, *fob = NULL, *mod;
+    FILE *fp;
+
+    if (!PyArg_ParseTuple(args, "UO&|O:load_dynamic",
+                          &name, PyUnicode_FSDecoder, &pathname, &fob))
         return NULL;
-    pathname = PyBytes_AS_STRING(pathbytes);
-    if (fob) {
-        fp = get_file(pathname, fob, "r");
-        if (fp == NULL) {
-            Py_DECREF(pathbytes);
+    if (fob != NULL) {
+        fp = get_file(NULL, fob, "r");
+        if (fp == NULL)
             return NULL;
-        }
     }
-    m = _PyImport_LoadDynamicModule(name, pathname, fp);
-    Py_DECREF(pathbytes);
+    else
+        fp = NULL;
+    mod = _PyImport_LoadDynamicModule(name, pathname, fp);
     if (fp)
         fclose(fp);
-    return m;
+    return mod;
 }
 
 #endif /* HAVE_DYNAMIC_LOADING */
index f0e1f55d2730572f6ba94bc9e27ec4927f73560f..629f3e294ec5dbfb84164325af5f6294c5a29a46 100644 (file)
 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 */
index b4d21be6f01520ba188f851496d62f4c4df45cc2..e51e551ff952799885a3c27e9b1b25a36c41c9d0 100644 (file)
@@ -28,7 +28,7 @@ struct filedescr {
 extern struct filedescr * _PyImport_Filetab;
 extern const struct filedescr _PyImport_DynLoadFiletab[];
 
-extern PyObject *_PyImport_LoadDynamicModule(char *name, char *pathname,
+extern PyObject *_PyImport_LoadDynamicModule(PyObject *name, PyObject *pathname,
                                              FILE *);
 
 /* Max length of module suffix searched for -- accommodates "module.slb" */