summaryrefslogtreecommitdiff
path: root/Python
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2006-05-26 18:03:31 +0000
committerGeorg Brandl <georg@python.org>2006-05-26 18:03:31 +0000
commit89452d2437221cae6aaef5192a859d93eb55040c (patch)
tree780bd913dee2531504db3673f6ee1d9ba2a8efaa /Python
parent0951d7015ee6d2657d501c3277fcd925ef86c9ef (diff)
downloadcpython-89452d2437221cae6aaef5192a859d93eb55040c.tar.gz
Need for speed: Patch #921466 : sys.path_importer_cache is now used to cache valid and
invalid file paths for the built-in import machinery which leads to fewer open calls on startup. Also fix issue with PEP 302 style import hooks which lead to more open() calls than necessary.
Diffstat (limited to 'Python')
-rw-r--r--Python/import.c32
1 files changed, 30 insertions, 2 deletions
diff --git a/Python/import.c b/Python/import.c
index 862f33c703..e09365b8c9 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -1240,7 +1240,33 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
if (importer == NULL)
return NULL;
/* Note: importer is a borrowed reference */
- if (importer != Py_None) {
+ if (importer == Py_False) {
+ /* Cached as not being a valid dir. */
+ Py_XDECREF(copy);
+ continue;
+ }
+ else if (importer == Py_True) {
+ /* Cached as being a valid dir, so just
+ * continue below. */
+ }
+ else if (importer == Py_None) {
+ /* No importer was found, so it has to be a file.
+ * Check if the directory is valid. */
+#ifdef HAVE_STAT
+ if (stat(buf, &statbuf) != 0) {
+ /* Directory does not exist. */
+ PyDict_SetItem(path_importer_cache,
+ v, Py_False);
+ Py_XDECREF(copy);
+ continue;
+ } else {
+ PyDict_SetItem(path_importer_cache,
+ v, Py_True);
+ }
+#endif
+ }
+ else {
+ /* A real import hook importer was found. */
PyObject *loader;
loader = PyObject_CallMethod(importer,
"find_module",
@@ -1253,9 +1279,11 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
return &importhookdescr;
}
Py_DECREF(loader);
+ Py_XDECREF(copy);
+ continue;
}
- /* no hook was successful, use builtin import */
}
+ /* no hook was found, use builtin import */
if (len > 0 && buf[len-1] != SEP
#ifdef ALTSEP