r58725 - sandbox/trunk/import_in_py/zipimport_/tests.py sandbox/trunk/import_in_py/zipimport_/zipimport.py
Author: brett.cannon Date: Wed Oct 31 04:51:40 2007 New Revision: 58725 Modified: sandbox/trunk/import_in_py/zipimport_/tests.py sandbox/trunk/import_in_py/zipimport_/zipimport.py Log: zipimport.get_data() might be called with an absolute path to the zip file. 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 Wed Oct 31 04:51:40 2007 @@ -47,7 +47,7 @@ py_compile.compile(code_path, doraise=True) zip_file.write(code_path + bytecode_suffix) zip_file.close() - yield zip_path + yield os.path.abspath(zip_path) finally: zip_file.close() for path in created_paths: @@ -167,6 +167,13 @@ with temp_zipfile(bytecode=False) as zip_path: importer = zipimport.zipimporter(zip_path) self.assertEqual(importer.get_data('_top_level.py'), example_code) + + def test_absolute_path(self): + with temp_zipfile(bytecode=False) as zip_path: + importer = zipimport.zipimporter(zip_path) + path = os.path.join(os.path.abspath(zip_path), '_top_level.py') + self.assertEqual(importer.get_data(path), example_code) + # XXX Test that file reading is done in binary mode. 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 04:51:40 2007 @@ -40,17 +40,17 @@ path = os.path.split(path)[0] else: raise ZipImportError("%s is not a zip file" % archivepath) - self.archive = path # Path to zip file. + self.archive = os.path.abspath(path) # Path to zip file. # XXX C version guarantees 'prefix' ends in a path separator. - self.prefix = archivepath[len(path)+1:] # Package directory. - if not path in _zip_directory_cache: + 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[path] = zip_info_dict - self._files = _zip_directory_cache[path] + _zip_directory_cache[self.archive] = zip_info_dict + self._files = _zip_directory_cache[self.archive] def __repr__(self): if self.prefix: @@ -134,6 +134,8 @@ source or bytecode files. """ + if pathname.startswith(self.archive): + pathname = pathname[len(self.archive)+1:] # Cover path separator. with contextlib.closing(zipfile.ZipFile(self.archive)) as zip_: try: return zip_.open(pathname, 'r').read()
participants (1)
-
brett.cannon