[Python-checkins] r58726 - sandbox/trunk/import_in_py/zipimport_/zipimport.py
brett.cannon
python-checkins at python.org
Wed Oct 31 05:29:14 CET 2007
Author: brett.cannon
Date: Wed Oct 31 05:29:13 2007
New Revision: 58726
Modified:
sandbox/trunk/import_in_py/zipimport_/zipimport.py
Log:
Consolidate the list of differences between this version of zipimport and what
ships currently with Python. Also raise a TypeError if a false value is passed
in as an argument to zipimport.__init__.
Modified: sandbox/trunk/import_in_py/zipimport_/zipimport.py
==============================================================================
--- sandbox/trunk/import_in_py/zipimport_/zipimport.py (original)
+++ sandbox/trunk/import_in_py/zipimport_/zipimport.py Wed Oct 31 05:29:13 2007
@@ -1,4 +1,15 @@
-"""A re-implementation of zipimport to use importlib."""
+"""A re-implementation of zipimport to use importlib.
+
+XXX Differences with 2.x version:
+
+ + _zip_directory_cache uses instances of zipfile.ZipInfo as values instead
+ of tuples.
+ + zipimporter.prefix does not necessarily end in a path separator.
+ + ZipImporterError is raised if the argument to zipimporter.__init__ does
+ not contain a path to a zip file; 2.x version allows for opening a any
+ file but raises errors later on during usage.
+
+"""
import importlib
import contextlib
@@ -9,11 +20,12 @@
import zipfile
-# XXX Import lock prevents concurrency issues during importation use, but does
-# not make any guarantees for non-import uses.
-# XXX Use zipfile.ZipInfo instances for values of file paths (C version uses
-# tuples).
-# XXX Prevents any dynamic update to the zip file from being detected.
+# XXX _zip_directory_cache issues:
+# * Import lock prevents concurrency issues during importation use, but does
+# not make any guarantees for non-import uses.
+# * Uses zipfile.ZipInfo instances for values of file paths (C version uses
+# tuples).
+# * Prevents any dynamic update to the zip file from being detected.
_zip_directory_cache = {}
@@ -33,6 +45,8 @@
than a zip file is passed in then ZipImportError is raised.
"""
+ if not archivepath:
+ raise TypeError("argument must represent a path to a zip file")
path = archivepath
while path and path != os.sep:
if zipfile.is_zipfile(path):
@@ -41,12 +55,10 @@
else:
raise ZipImportError("%s is not a zip file" % archivepath)
self.archive = os.path.abspath(path) # Path to zip file.
- # XXX C version guarantees 'prefix' ends in a path separator.
self.prefix = archivepath[len(self.archive)+1:] # Package directory.
if not self.archive in _zip_directory_cache:
with contextlib.closing(zipfile.ZipFile(path)) as zip_file:
zip_info_list = zip_file.infolist()
- # XXX Need to duplicate tuple from original zipimport?
zip_info_dict = dict((info.filename, info)
for info in zip_info_list)
_zip_directory_cache[self.archive] = zip_info_dict
More information about the Python-checkins
mailing list