[Python-checkins] cpython: _PyImport_LoadDynamicModule() encodes the module name explicitly to ASCII

victor.stinner python-checkins at python.org
Sat May 7 12:46:08 CEST 2011


http://hg.python.org/cpython/rev/eb003c3d1770
changeset:   69889:eb003c3d1770
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Sat May 07 12:46:05 2011 +0200
summary:
  _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.

files:
  Python/importdl.c |  40 +++++++++++++++++++++-------------
  1 files changed, 25 insertions(+), 15 deletions(-)


diff --git a/Python/importdl.c b/Python/importdl.c
--- a/Python/importdl.c
+++ b/Python/importdl.c
@@ -20,31 +20,36 @@
                                            const char *pathname, FILE *fp);
 #endif
 
-/* 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(PyObject *name, PyObject *path, FILE *fp)
 {
-    PyObject *m;
+    PyObject *m = NULL;
 #ifndef MS_WINDOWS
     PyObject *pathbytes;
 #endif
+    PyObject *nameascii;
     char *namestr, *lastdot, *shortname, *packagecontext, *oldcontext;
     dl_funcptr p0;
     PyObject* (*p)(void);
     struct PyModuleDef *def;
 
-    namestr = _PyUnicode_AsString(name);
-    if (namestr == NULL)
-        return NULL;
-
     m = _PyImport_FindExtensionObject(name, path);
     if (m != NULL) {
         Py_INCREF(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, '.');
     if (lastdot == NULL) {
         packagecontext = NULL;
@@ -60,34 +65,33 @@
 #else
     pathbytes = PyUnicode_EncodeFSDefault(path);
     if (pathbytes == NULL)
-        return NULL;
+        goto error;
     p0 = _PyImport_GetDynLoadFunc(shortname,
                                   PyBytes_AS_STRING(pathbytes), fp);
     Py_DECREF(pathbytes);
 #endif
     p = (PyObject*(*)(void))p0;
     if (PyErr_Occurred())
-        return NULL;
+        goto error;
     if (p == NULL) {
         PyErr_Format(PyExc_ImportError,
                      "dynamic module does not define init function"
                      " (PyInit_%s)",
                      shortname);
-        return NULL;
+        goto error;
     }
     oldcontext = _Py_PackageContext;
     _Py_PackageContext = packagecontext;
     m = (*p)();
     _Py_PackageContext = oldcontext;
     if (m == NULL)
-        return NULL;
+        goto error;
 
     if (PyErr_Occurred()) {
-        Py_DECREF(m);
         PyErr_Format(PyExc_SystemError,
                      "initialization of %s raised unreported exception",
                      shortname);
-        return NULL;
+        goto error;
     }
 
     /* Remember pointer to module init function. */
@@ -101,12 +105,18 @@
         Py_INCREF(path);
 
     if (_PyImport_FixupExtensionObject(m, name, path) < 0)
-        return NULL;
+        goto error;
     if (Py_VerboseFlag)
         PySys_FormatStderr(
             "import %U # dynamically loaded from %R\n",
             name, path);
+    Py_DECREF(nameascii);
     return m;
+
+error:
+    Py_DECREF(nameascii);
+    Py_XDECREF(m);
+    return NULL;
 }
 
 #endif /* HAVE_DYNAMIC_LOADING */

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list