All, I tried to implement Jean-Paul Calderone's idea for the following patch, plagiarizing Ralf W. Grosse-Kunstleve's error text. It delays import warning until end of search for modules, but remembers how many potential modules (candidates without __init__.py) it didn't import. I didn't really try to analyze any conditions, instead I simply assumed that wherever ImportWarning would be issued, we have a suitable candidate, and saved it on the stack. If nothing is found, Python emits ImportWarning right before ImportError, and explains what happened. Please let me know if this would work and if anything needs to be done for this patch to be accepted. Thank you!
Sergey.
Index: Python/import.c =================================================================== --- Python/import.c (revision 47190) +++ Python/import.c (working copy) @@ -1203,6 +1203,10 @@ return NULL; } + /* prepare to find a directory without __init__.py and report it */ + char candidate_path[MAXPATHLEN]; + int candidate_count = 0; + npath = PyList_Size(path); namelen = strlen(name); for (i = 0; i < npath; i++) { @@ -1307,15 +1311,8 @@ 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; - } + if (candidate_count++ == 0) + strncpy(candidate_path, buf, MAXPATHLEN); } } #else @@ -1328,15 +1325,9 @@ 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; - } + if (candidate_count++ == 0) + strncpy(candidate_path, buf, MAXPATHLEN); + } } #endif #endif @@ -1409,6 +1400,16 @@ break; } if (fp == NULL) { + if (candidate_count > 0) { + char warnstr[MAXPATHLEN+256]; + sprintf(warnstr, "Found but not imported %d directory(ies)" + " due to missing __init__.py. First such directory follows\n" + "%.*s\n" + "See the section on \"Packages\" in the Python tutorial for" + " details (http://www.python.org/doc/tut/).", + candidate_count, MAXPATHLEN, candidate_path); + PyErr_Warn(PyExc_ImportWarning, warnstr); + } PyErr_Format(PyExc_ImportError, "No module named %.200s", name); return NULL;
_______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com