"Fredrik Lundh" <[EMAIL PROTECTED]> writes on Fri, 6 May 2005 00:08:36 +0200:
> > ... lots of "no such file or directory ...
> > Whoa!! After looking at what is being stat'd or
> > open'd, it looks like 'encodings' is new in 2.4 and,
> > even "worse", everything is looked for as a zip first.
> 
> so why not build that ZIP?

We, too, saw this problem -- and we had the "*.zip" files already...

Python is a bit stupid in its import logic.
When, e.g., a package "P" defined in a zip archive "zzz.zip"
contains an "import os", then Python checks whether
"zzz.zip" contains "P.os" (that is okay). *But*, usually
"zzz.zip" does not define "P.os" (as "os" is a builtin module)
and then Python checks in the file system
for "zzz.zip/P/os{,.py,pyc,.so,} and "zzz.zip/P/osmodule.so".
Of course, all of them fail as "zzz.zip" is a Zip archive
and "zzz.zip/something" is not meaningfull as a file system reference.

I improved on this by patching Python's "import.c" with the
attached patch. The patch implements that a "path_hook"
declaring itself to be responsible for a path is authoritative
for both negative as well as positive "find_module" responses.
Earlier, a negative "find_module" response caused Python to
try the default module lookup.


Furthermore, it is vital that your "sys.path" is as small as possible
because a single module lookup can cause file system lookups
in the order of 4 times the number of path elements. 

The standard extension of "sys.path" often contains far more
path elements than necessary (if you defined "python24.zip", you
should remove all other python library directories that do not contain
shared objects).

Dieter

--- Python/import.c~	2004-10-07 08:46:25.000000000 +0200
+++ Python/import.c	2005-05-04 12:52:19.000000000 +0200
@@ -1211,6 +1211,9 @@
 				return NULL;
 			/* Note: importer is a borrowed reference */
 			if (importer != Py_None) {
+			  	/* DM 2005-05-04: ATT: memory leak!
+				   almost surely, we need
+				   a "Py_XDECREF(copy)" */
 				PyObject *loader;
 				loader = PyObject_CallMethod(importer,
 							     "find_module",
@@ -1223,7 +1226,12 @@
 					return &importhookdescr;
 				}
 				Py_DECREF(loader);
-			}
+				/* DM 2005-05-04: do not try the builtin import
+				   when the responsible importer failed.
+				   At least, for zipimport, trying builtin
+				   import would be stupid. */
+				continue;
+						}
 			/* no hook was successful, use builtin import */
 		}
 
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to