summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorThomas Wouters <thomas@python.org>2006-04-27 23:13:20 (GMT)
committerThomas Wouters <thomas@python.org>2006-04-27 23:13:20 (GMT)
commit9df4e6f6735af274813cf1b611ee1a342955ad63 (patch)
treec0ab6329dd61fc03a46932435828210b3d0d3db8 /Python
parent2c1e63f8a48bf0f530a922219e85443c4e12882d (diff)
downloadcpython-9df4e6f6735af274813cf1b611ee1a342955ad63.zip
cpython-9df4e6f6735af274813cf1b611ee1a342955ad63.tar.gz
cpython-9df4e6f6735af274813cf1b611ee1a342955ad63.tar.bz2
- 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.
Diffstat (limited to 'Python')
-rw-r--r--Python/exceptions.c5
-rw-r--r--Python/import.c37
2 files changed, 35 insertions, 7 deletions
diff --git a/Python/exceptions.c b/Python/exceptions.c
index 5c824e6..31fb53e 100644
--- a/Python/exceptions.c
+++ b/Python/exceptions.c
@@ -1647,6 +1647,8 @@ PyDoc_STRVAR(FutureWarning__doc__,
"Base class for warnings about constructs that will change semantically "
"in the future.");
+PyDoc_STRVAR(ImportWarning__doc__,
+"Base class for warnings about probable mistakes in module imports");
/* module global functions */
@@ -1719,6 +1721,7 @@ PyObject *PyExc_SyntaxWarning;
PyObject *PyExc_OverflowWarning;
PyObject *PyExc_RuntimeWarning;
PyObject *PyExc_FutureWarning;
+PyObject *PyExc_ImportWarning;
@@ -1818,6 +1821,8 @@ static struct {
RuntimeWarning__doc__},
{"FutureWarning", &PyExc_FutureWarning, &PyExc_Warning,
FutureWarning__doc__},
+ {"ImportWarning", &PyExc_ImportWarning, &PyExc_Warning,
+ ImportWarning__doc__},
/* Sentinel */
{NULL}
};
diff --git a/Python/import.c b/Python/import.c
index 81027d8..6642082 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -1271,19 +1271,42 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
#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