diff Python/import.c @ 7290:72d531d54572 legacy-trunk

Add case checking feature on import. This is an option for OS-es with case-insensitive but case-preserving filesystems. It is currently supported for Win32 and MacOS. To enable it, #define CHECK_IMPORT_CASE in your platform specific config.h. It is enabled by default on those systems where it is supported. On Win32, it can be disabled at runtime by setting the environment variable PYTHONCASEOK (to any value). When enabled, the feature checks that the case of the requested module name matches that of the filename found in the filesystem, and raises a NameError exception when they don't match.
author Guido van Rossum <guido@python.org>
date Fri, 13 Feb 1998 17:18:36 +0000
parents 74c57288a02b
children 067e4813c6d6
line wrap: on
line diff
--- a/Python/import.c	Fri Feb 13 16:58:49 1998 +0000
+++ b/Python/import.c	Fri Feb 13 17:18:36 1998 +0000
@@ -796,6 +796,10 @@
 extern FILE *PyWin_FindRegisteredModule();
 #endif
 
+#ifdef CHECK_IMPORT_CASE
+static int check_case(char *, int, int, char *);
+#endif
+
 static int find_init_module Py_PROTO((char *)); /* Forward */
 
 static struct filedescr *
@@ -896,8 +900,14 @@
 		if (stat(buf, &statbuf) == 0) {
 			static struct filedescr fd = {"", "", PKG_DIRECTORY};
 			if (S_ISDIR(statbuf.st_mode)) {
-				if (find_init_module(buf))
+				if (find_init_module(buf)) {
+#ifdef CHECK_IMPORT_CASE
+					if (!check_case(buf, len, namelen,
+							name))
+						return NULL;
+#endif
 					return &fd;
+				}
 			}
 		}
 #else
@@ -925,11 +935,78 @@
 			     "No module named %.200s", name);
 		return NULL;
 	}
+#ifdef CHECK_IMPORT_CASE
+	if (!check_case(buf, len, namelen, name)) {
+		fclose(fp);
+		return NULL;
+	}
+#endif
 
 	*p_fp = fp;
 	return fdp;
 }
 
+#ifdef CHECK_IMPORT_CASE
+
+#ifdef MS_WIN32
+#include <windows.h>
+static int
+check_case(char *buf, int len, int namelen, char *name)
+{
+	WIN32_FIND_DATA data;
+	HANDLE h;
+	if (getenv("PYTHONCASEOK") != NULL)
+		return 1;
+	h = FindFirstFile(buf, &data);
+	if (h == INVALID_HANDLE_VALUE) {
+		PyErr_Format(PyExc_NameError,
+		  "Can't find file for module %.100s\n(filename %.300s)",
+		  name, buf);
+		return 0;
+	}
+	FindClose(h);
+	if (strncmp(data.cFileName, name, namelen) != 0) {
+		strcpy(buf+len-namelen, data.cFileName);
+		PyErr_Format(PyExc_NameError,
+		  "Case mismatch for module name %.100s\n(filename %.300s)",
+		  name, buf);
+		return 0;
+	}
+	return 1;
+}
+#endif /* MS_WIN32 */
+
+#ifdef macintosh
+#include <TextUtils.h>
+static int
+check_case(char *buf, int len, int namelen, char *name)
+{
+	FSSpec fss;
+	OSErr err;
+	unsigned char mybuf[MAXPATHLEN+1];
+	
+	strcpy((char *)mybuf, buf);
+	c2pstr((char *)mybuf);
+	err = FSMakeFSSpec(0, 0, mybuf, &fss);
+	if (err) {
+		PyErr_Format(PyExc_NameError,
+		  "Can't find file for module %.100s\n(filename %.300s)",
+		  name, buf);
+		return 0;
+	}
+	p2cstr(fss.name);
+	if ( strncmp(name, (char *)fss.name, namelen) != 0 ) {
+		PyErr_Format(PyExc_NameError,
+		  "Case mismatch for module name %.100s\n(filename %.300s)",
+		  name, fss.name);
+		return 0;
+	}
+	return 1;
+}
+#endif /* macintosh */
+
+#endif CHECK_IMPORT_CASE
+
 #ifdef HAVE_STAT
 /* Helper to look for __init__.py or __init__.py[co] in potential package */
 static int