Issue #5604: non-ASCII characters in module name passed to

imp.find_module() were converted to UTF-8 while the path is
converted to the default filesystem encoding, causing nonsense.
Thanks to Andrew Svetlov.

(This time to the right branch.  Will block duplicate merge to 3.0.2.)
This commit is contained in:
Guido van Rossum 2009-03-30 22:01:35 +00:00
parent eaaec27b6e
commit 0ad59d467d
4 changed files with 117 additions and 16 deletions

View file

@ -3040,15 +3040,20 @@ imp_load_compiled(PyObject *self, PyObject *args)
PyObject *fob = NULL;
PyObject *m;
FILE *fp;
if (!PyArg_ParseTuple(args, "ss|O:load_compiled",
&name, &pathname, &fob))
if (!PyArg_ParseTuple(args, "ses|O:load_compiled",
&name,
Py_FileSystemDefaultEncoding, &pathname,
&fob))
return NULL;
fp = get_file(pathname, fob, "rb");
if (fp == NULL)
if (fp == NULL) {
PyMem_Free(pathname);
return NULL;
}
m = load_compiled_module(name, pathname, fp);
if (fob == NULL)
fclose(fp);
PyMem_Free(pathname);
return m;
}
@ -3062,15 +3067,20 @@ imp_load_dynamic(PyObject *self, PyObject *args)
PyObject *fob = NULL;
PyObject *m;
FILE *fp = NULL;
if (!PyArg_ParseTuple(args, "ss|O:load_dynamic",
&name, &pathname, &fob))
if (!PyArg_ParseTuple(args, "ses|O:load_dynamic",
&name,
Py_FileSystemDefaultEncoding, &pathname,
&fob))
return NULL;
if (fob) {
fp = get_file(pathname, fob, "r");
if (fp == NULL)
if (fp == NULL) {
PyMem_Free(pathname);
return NULL;
}
}
m = _PyImport_LoadDynamicModule(name, pathname, fp);
PyMem_Free(pathname);
return m;
}
@ -3084,12 +3094,16 @@ imp_load_source(PyObject *self, PyObject *args)
PyObject *fob = NULL;
PyObject *m;
FILE *fp;
if (!PyArg_ParseTuple(args, "ss|O:load_source",
&name, &pathname, &fob))
if (!PyArg_ParseTuple(args, "ses|O:load_source",
&name,
Py_FileSystemDefaultEncoding, &pathname,
&fob))
return NULL;
fp = get_file(pathname, fob, "r");
if (fp == NULL)
if (fp == NULL) {
PyMem_Free(pathname);
return NULL;
}
m = load_source_module(name, pathname, fp);
if (fob == NULL)
fclose(fp);
@ -3102,13 +3116,15 @@ imp_load_module(PyObject *self, PyObject *args)
char *name;
PyObject *fob;
char *pathname;
PyObject * ret;
char *suffix; /* Unused */
char *mode;
int type;
FILE *fp;
if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
&name, &fob, &pathname,
if (!PyArg_ParseTuple(args, "sOes(ssi):load_module",
&name, &fob,
Py_FileSystemDefaultEncoding, &pathname,
&suffix, &mode, &type))
return NULL;
if (*mode) {
@ -3119,6 +3135,7 @@ imp_load_module(PyObject *self, PyObject *args)
if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
PyErr_Format(PyExc_ValueError,
"invalid file open mode %.200s", mode);
PyMem_Free(pathname);
return NULL;
}
}
@ -3126,10 +3143,14 @@ imp_load_module(PyObject *self, PyObject *args)
fp = NULL;
else {
fp = get_file(NULL, fob, mode);
if (fp == NULL)
if (fp == NULL) {
PyMem_Free(pathname);
return NULL;
}
return load_module(name, fp, pathname, type, NULL);
}
}
ret = load_module(name, fp, pathname, type, NULL);
PyMem_Free(pathname);
return ret;
}
static PyObject *
@ -3137,9 +3158,13 @@ imp_load_package(PyObject *self, PyObject *args)
{
char *name;
char *pathname;
if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
PyObject * ret;
if (!PyArg_ParseTuple(args, "ses:load_package",
&name, Py_FileSystemDefaultEncoding, &pathname))
return NULL;
return load_package(name, pathname);
ret = load_package(name, pathname);
PyMem_Free(pathname);
return ret;
}
static PyObject *