diff Python/import.c @ 52475:780d6401eed6 3.0

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 Andrew Svetlov!
author Guido van Rossum <guido@python.org>
date Mon, 30 Mar 2009 21:43:25 +0000
parents 194fd401922e
children
line wrap: on
line diff
--- a/Python/import.c	Mon Mar 30 14:55:02 2009 +0000
+++ b/Python/import.c	Mon Mar 30 21:43:25 2009 +0000
@@ -3006,15 +3006,20 @@
 	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;
 }
 
@@ -3028,15 +3033,20 @@
 	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;
 }
 
@@ -3050,12 +3060,16 @@
 	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);
@@ -3068,13 +3082,15 @@
 	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) {
@@ -3085,6 +3101,7 @@
 		if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
 			PyErr_Format(PyExc_ValueError,
 				     "invalid file open mode %.200s", mode);
+			PyMem_Free(pathname);
 			return NULL;
 		}
 	}
@@ -3092,10 +3109,14 @@
 		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 *
@@ -3103,9 +3124,13 @@
 {
 	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 *