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

Jim Jewett jimjjewett at gmail.com
Mon May 9 15:00:17 CEST 2011


Are you asserting that all foreign modules (or at least all handled by this) are in C, as opposed to C++ or even Java or Fortran? (And the C won't change?)

Is this ASCII restriction (as opposed to even UTF8) really needed?

Or are you just saying that we need to create an ASCII name for passing to C?

-jJ

On 5/7/11, victor.stinner <python-checkins at python.org> wrote:

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: PyImportLoadDynamicModule() encodes the module name explicitly to ASCII

The name must be encodable to ASCII because dynamic module must have a function called "PyInitNAME", 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 * PyImportLoadDynamicModule(PyObject *name, PyObject *path, FILE *fp) { - PyObject *m; + PyObject *m = NULL; #ifndef MSWINDOWS PyObject *pathbytes; #endif + PyObject *nameascii; char *namestr, *lastdot, *shortname, *packagecontext, *oldcontext; dlfuncptr p0; PyObject* (*p)(void); struct PyModuleDef *def; - namestr = PyUnicodeAsString(name); - if (namestr == NULL) - return NULL; - m = PyImportFindExtensionObject(name, path); if (m != NULL) { PyINCREF(m); return m; } + /* name must be encodable to ASCII because dynamic module must have a + function called "PyInitNAME", they are written in C, and the C language + doesn't accept non-ASCII identifiers. */ + nameascii = PyUnicodeAsEncodedString(name, "ascii", NULL); + if (nameascii == NULL) + return NULL; + + namestr = PyBytesASSTRING(nameascii); + if (namestr == NULL) + goto error; + lastdot = strrchr(namestr, '.'); if (lastdot == NULL) { packagecontext = NULL; @@ -60,34 +65,33 @@ #else pathbytes = PyUnicodeEncodeFSDefault(path); if (pathbytes == NULL) - return NULL; + goto error; p0 = PyImportGetDynLoadFunc(shortname, PyBytesASSTRING(pathbytes), fp); PyDECREF(pathbytes); #endif p = (PyObject*(*)(void))p0; if (PyErrOccurred()) - return NULL; + goto error; if (p == NULL) { PyErrFormat(PyExcImportError, "dynamic module does not define init function" " (PyInit%s)", shortname); - return NULL; + goto error; } oldcontext = PyPackageContext; PyPackageContext = packagecontext; m = (*p)(); PyPackageContext = oldcontext; if (m == NULL) - return NULL; + goto error; if (PyErrOccurred()) { - PyDECREF(m); PyErrFormat(PyExcSystemError, "initialization of %s raised unreported exception", shortname); - return NULL; + goto error; } /* Remember pointer to module init function. */ @@ -101,12 +105,18 @@ PyINCREF(path); if (PyImportFixupExtensionObject(m, name, path) < 0) - return NULL; + goto error; if (PyVerboseFlag) PySysFormatStderr( "import %U # dynamically loaded from %R\n", name, path); + PyDECREF(nameascii); return m; + +error: + PyDECREF(nameascii); + PyXDECREF(m); + return NULL; } #endif /* HAVEDYNAMICLOADING */ -- Repository URL: http://hg.python.org/cpython



More information about the Python-Dev mailing list