diff Python/import.c @ 52480:80271ce09a66

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.)
author Guido van Rossum <guido@python.org>
date Mon, 30 Mar 2009 22:01:35 +0000
parents 70f5f9113ae1
children 2d80c2797501
line wrap: on
line diff
--- a/Python/import.c	Mon Mar 30 21:54:41 2009 +0000
+++ b/Python/import.c	Mon Mar 30 22:01:35 2009 +0000
@@ -3040,15 +3040,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;
 }
 
@@ -3062,15 +3067,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;
 }
 
@@ -3084,12 +3094,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);
@@ -3102,13 +3116,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) {
@@ -3119,6 +3135,7 @@
 		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 @@
 		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 @@
 {
 	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 *