diff Python/import.c @ 38018:b5e7d5b3c442 legacy-trunk

- Add new Warning class, ImportWarning - Warn-raise ImportWarning when importing would have picked up a directory as package, if only it'd had an __init__.py. This swaps two tests (for case-ness and __init__-ness), but case-test is not really more expensive, and it's not in a speed-critical section. - Test for the new warning by importing a common non-package directory on sys.path: site-packages - In regrtest.py, silence warnings generated by the build-environment because Modules/ (which is added to sys.path for Setup-created modules) has 'zlib' and '_ctypes' directories without __init__.py's.
author Thomas Wouters <thomas@python.org>
date Thu, 27 Apr 2006 23:13:20 +0000
parents 2f3f2e32dfd9
children 1fdf29a88f0e
line wrap: on
line diff
--- a/Python/import.c	Thu Apr 27 22:53:05 2006 +0000
+++ b/Python/import.c	Thu Apr 27 23:13:20 2006 +0000
@@ -1271,19 +1271,42 @@
 #ifdef HAVE_STAT
 		if (stat(buf, &statbuf) == 0 &&         /* it exists */
 		    S_ISDIR(statbuf.st_mode) &&         /* it's a directory */
-		    find_init_module(buf) &&            /* it has __init__.py */
-		    case_ok(buf, len, namelen, name)) { /* and case matches */
-			Py_XDECREF(copy);
-			return &fd_package;
+		    case_ok(buf, len, namelen, name)) { /* case matches */
+			if (find_init_module(buf)) { /* and has __init__.py */
+				Py_XDECREF(copy);
+				return &fd_package;
+			}
+			else {
+				char warnstr[MAXPATHLEN+80];
+				sprintf(warnstr, "Not importing directory "
+					"'%.*s': missing __init__.py", 
+					MAXPATHLEN, buf);
+				if (PyErr_Warn(PyExc_ImportWarning,
+					       warnstr)) {
+					Py_XDECREF(copy);
+					return NULL;
+				}
+			}
 		}
 #else
 		/* XXX How are you going to test for directories? */
 #ifdef RISCOS
 		if (isdir(buf) &&
-		    find_init_module(buf) &&
 		    case_ok(buf, len, namelen, name)) {
-			Py_XDECREF(copy);
-			return &fd_package;
+			if (find_init_module(buf)) {
+				Py_XDECREF(copy);
+				return &fd_package;
+			}
+			else {
+				char warnstr[MAXPATHLEN+80];
+				sprintf(warnstr, "Not importing directory "
+					"'%.*s': missing __init__.py", 
+					MAXPATHLEN, buf);
+				if (PyErr_Warn(PyExc_ImportWarning,
+					       warnstr)) {
+					Py_XDECREF(copy);
+					return NULL;
+				}
 		}
 #endif
 #endif