[Python-checkins] r57340 - sandbox/trunk/import_in_py/zipimport_/zipimport.py

brett.cannon python-checkins at python.org
Thu Aug 23 22:21:16 CEST 2007


Author: brett.cannon
Date: Thu Aug 23 22:21:16 2007
New Revision: 57340
Modified:
 sandbox/trunk/import_in_py/zipimport_/zipimport.py
Log:
Rewrite find_module to use imp for proper file suffix lookup.
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	Thu Aug 23 22:21:16 2007
@@ -1,6 +1,7 @@
 """A re-implementation of zipimport to use importlib."""
 import importlib
 
+import imp
 import os
 import zipfile
 
@@ -29,15 +30,20 @@
 """Check if the specified module is contained within the zip file,
 returning self if it is or None if it is not."""
 path_name = fullname.replace('.', os.sep)
- # XXX Really should use imp.getprefixes().
- path_name_source = path_name + '.py'
- path_name_bytecode = path_name_source + ('c' if __debug__ else 'o')
- path_pkg_name_source = os.path.join(path_name, '__init__.py')
- path_pkg_name_bytecode = path_pkg_name_source + ('c' if __debug__ else 'o')
- for possible_name in (path_pkg_name_source, path_pkg_name_bytecode,
- path_name_bytecode, path_name_source):
+ suffixes = importlib.suffix_list(imp.PY_COMPILED)
+ suffixes += importlib.suffix_list(imp.PY_SOURCE)
+ for suffix in suffixes:
+ pkg_init_path = os.path.join(path_name, '__init__' + suffix)
 try:
- self._zip.getinfo(possible_name)
+ self._zip.getinfo(pkg_init_path)
+ except KeyError:
+ continue
+ else:
+ return self
+ for suffix in suffixes:
+ file_path = path_name + suffix
+ try:
+ self._zip.getinfo(file_path)
 except KeyError:
 continue
 else:
@@ -45,6 +51,7 @@
 else:
 return None
 
+
 def get_code(self, fullname):
 """Return the code object for the module, raising ZipImportError if the
 module is not found."""


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /