[Python-checkins] cpython: Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path (original) (raw)

victor.stinner python-checkins at python.org
Sun Mar 20 04:13:36 CET 2011


http://hg.python.org/cpython/rev/ef2b6305d395 changeset: 68716:ef2b6305d395 user: Victor Stinner <victor.stinner at haypocalc.com> date: Mon Mar 14 15:54:07 2011 -0400 summary: Issue #3080: _PyImport_LoadDynamicModule() uses Unicode for name and path

Document also that dynamic module names are ASCII only

files: Python/import.c Python/importdl.c Python/importdl.h

diff --git a/Python/import.c b/Python/import.c --- a/Python/import.c +++ b/Python/import.c @@ -2173,9 +2173,21 @@ 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 @@ 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 @@ 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 */ diff --git a/Python/importdl.c b/Python/importdl.c --- 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 @@ 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:

-finally:

}

#endif /* HAVE_DYNAMIC_LOADING */ diff --git a/Python/importdl.h b/Python/importdl.h --- a/Python/importdl.h +++ b/Python/importdl.h @@ -28,7 +28,7 @@ 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" */

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



More information about the Python-checkins mailing list