[Python-checkins] r57447 - sandbox/trunk/import_in_py/zipimport_/tests.py sandbox/trunk/import_in_py/zipimport_/zipimport.py
brett.cannon
python-checkins at python.org
Sat Aug 25 05:56:54 CEST 2007
Author: brett.cannon
Date: Sat Aug 25 05:56:53 2007
New Revision: 57447
Modified:
sandbox/trunk/import_in_py/zipimport_/tests.py
sandbox/trunk/import_in_py/zipimport_/zipimport.py
Log:
Change zipimporter's constructor to accept package paths (i.e., path with the
zip file at the beginning with a trailing package path).
Modified: sandbox/trunk/import_in_py/zipimport_/tests.py
==============================================================================
--- sandbox/trunk/import_in_py/zipimport_/tests.py (original)
+++ sandbox/trunk/import_in_py/zipimport_/tests.py Sat Aug 25 05:56:53 2007
@@ -17,8 +17,11 @@
zip_file = zipfile.ZipFile(zip_path, 'w')
try:
os.mkdir('_pkg')
+ os.mkdir(os.path.join('_pkg', '_subpkg'))
for module_path in ['_top_level.py', os.path.join('_pkg', '__init__.py'),
- os.path.join('_pkg', 'submodule.py')]:
+ os.path.join('_pkg', 'submodule.py'),
+ os.path.join('_pkg', '_subpkg', '__init__.py'),
+ os.path.join('_pkg', '_subpkg', 'submodule.py')]:
with open(module_path, 'w') as temp_file:
temp_file.write(example_code)
try:
@@ -33,6 +36,8 @@
zip_file.close()
yield zip_path
finally:
+ if os.path.exists(os.path.join('_pkg', '_subpkg')):
+ os.rmdir(os.path.join('_pkg', '_subpkg'))
if os.path.exists('_pkg'):
os.rmdir('_pkg')
os.unlink(zip_path)
@@ -61,12 +66,20 @@
finally:
test_support.unlink(test_support.TESTFN)
- def test_zipfile(self):
+ def test_direct_path(self):
# A zipfile should return an instance of zipimporter.
with temp_zipfile() as zip_path:
zip_importer = zipimport.zipimporter(zip_path)
self.assert_(isinstance(zip_importer, zipimport.zipimporter))
+ def test_pkg_path(self):
+ # Thanks to __path__, need to be able to work off of a path with a zip
+ # file at the front and a path for the rest.
+ with temp_zipfile() as zip_path:
+ path = os.path.join(zip_path, '_pkg')
+ zip_importer = zipimport.zipimporter(path)
+ self.assert_(isinstance(zip_importer, zipimport.zipimporter))
+
class FindModule(unittest.TestCase):
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 Sat Aug 25 05:56:53 2007
@@ -26,18 +26,23 @@
than a zip file is passed in then ZipImportError is raised.
"""
+ path = os.path.abspath(archivepath)
# XXX Need to tweak to handle zip archive package info like
# _pkg.zip/pkg
- if not zipfile.is_zipfile(archivepath):
+ while path:
+ if zipfile.is_zipfile(path):
+ break
+ path = path.rsplit(os.sep, 1)[0]
+ else:
raise ZipImportError("the specified path must be a zip file")
- self._path = archivepath
+ self._zip_path = path
self._path_cache = {}
def _check_paths(self, base_path):
source_suffixes = importlib.suffix_list(imp.PY_SOURCE)
bytecode_suffixes = importlib.suffix_list(imp.PY_COMPILED)
source, bytecode = None, None
- with contextlib.closing(zipfile.ZipFile(self._path)) as zip_:
+ with contextlib.closing(zipfile.ZipFile(self._zip_path)) as zip_:
for suffix in source_suffixes:
path = base_path + suffix
try:
@@ -94,12 +99,12 @@
source or bytecode files.
"""
- with contextlib.closing(zipfile.ZipFile(self._path)) as zip_:
+ with contextlib.closing(zipfile.ZipFile(self._zip_path)) as zip_:
try:
return zip_.open(pathname, 'r').read()
except KeyError:
raise IOError('%s does not exist in the zip file %s' % (pathname,
- self._path))
+ self._zip_path))
def get_source(self, fullname):
"""Get the source code for the specified module, raising ZipImportError
@@ -110,7 +115,7 @@
raise ZipImportError("%s is not known" % fullname)
if info[0] is None:
return None
- with importlib.closing(zipfile.ZipFile(self._path)) as zip_:
+ with importlib.closing(zipfile.ZipFile(self._zip_path)) as zip_:
return zip_.open(info[0], 'U').read()
def is_package(self, fullname):
@@ -129,7 +134,7 @@
info = self._path_cache[fullname]
except KeyError:
raise ZipImportError('%s is not known' % name)
- with contextlib.closing(zipfile.ZipFile(self._path)) as zip_:
+ with contextlib.closing(zipfile.ZipFile(self._zip_path)) as zip_:
file_info = zip_.getinfo(info[0])
file_mtime = datetime.datetime(*file_info.date_time)
return int(time.mktime(file_time.timetuple()))
@@ -140,7 +145,7 @@
bytecode_path = self._path_cache[name][1]
except KeyError:
raise ZipImportError('%s is not known' % name)
- with contextlib.closing(zipfile.ZipFile(self._path)) as zip_:
+ with contextlib.closing(zipfile.ZipFile(self._zip_path)) as zip_:
return zip_.open(bytecode_path, 'r').read()
def write_bytecode(self, name, timestamp, data):
More information about the Python-checkins
mailing list