PyAPI_DATA(PyObject *) PyExc_OverflowWarning;
PyAPI_DATA(PyObject *) PyExc_RuntimeWarning;
PyAPI_DATA(PyObject *) PyExc_FutureWarning;
+PyAPI_DATA(PyObject *) PyExc_ImportWarning;
/* Convenience functions */
+-- UserWarning
+-- FutureWarning
+-- OverflowWarning [not generated by the interpreter]
+ +-- ImportWarning
warnings.filterwarnings("ignore", "hex/oct constants", FutureWarning,
"<string>")
+# Ignore ImportWarnings that only occur in the source tree,
+# (because of modules with the same name as source-directories in Modules/)
+for mod in ("ctypes", "gzip", "test.test_zipimport", "test.test_zlib"):
+ warnings.filterwarnings(module=".*%s$" % (mod,),
+ action="ignore", category=ImportWarning)
+
# MacOSX (a.k.a. Darwin) has a default stack size that is too small
# for deeply recursive regular expressions. We see this as crashes in
# the Python test suite when running test_re.py and test_sre.py. The
assert y is test.test_support, y.__name__
test_import_name_binding()
+
+def test_import_initless_directory_warning():
+ import warnings
+ oldfilters = warnings.filters[:]
+ warnings.simplefilter('error', ImportWarning);
+ try:
+ # Just a random non-package directory we always expect to be
+ # somewhere in sys.path...
+ __import__("site-packages")
+ except ImportWarning:
+ pass
+ else:
+ raise AssertionError
+ finally:
+ warnings.filters = oldfilters
+
+test_import_initless_directory_warning()
"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");
\f
/* module global functions */
PyObject *PyExc_OverflowWarning;
PyObject *PyExc_RuntimeWarning;
PyObject *PyExc_FutureWarning;
+PyObject *PyExc_ImportWarning;
\f
RuntimeWarning__doc__},
{"FutureWarning", &PyExc_FutureWarning, &PyExc_Warning,
FutureWarning__doc__},
+ {"ImportWarning", &PyExc_ImportWarning, &PyExc_Warning,
+ ImportWarning__doc__},
/* Sentinel */
{NULL}
};
#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