本文整理汇总了Python中zipimport.ZipImportError方法的典型用法代码示例。如果您正苦于以下问题:Python zipimport.ZipImportError方法的具体用法?Python zipimport.ZipImportError怎么用?Python zipimport.ZipImportError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类zipimport
的用法示例。
在下文中一共展示了zipimport.ZipImportError方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _precache_zipimporters
# 需要导入模块: import zipimport [as 别名]
# 或者: from zipimport import ZipImportError [as 别名]
def _precache_zipimporters(path=None):
pic = sys.path_importer_cache
# When measured, despite having the same complexity (O(n)),
# converting to tuples and then caching the conversion to sets
# and the set difference is faster than converting to sets
# and then only caching the set difference.
req_paths = tuple(path or sys.path)
cached_paths = tuple(pic)
new_paths = _cached_set_diff(req_paths, cached_paths)
for entry_path in new_paths:
try:
pic[entry_path] = zipimport.zipimporter(entry_path)
except zipimport.ZipImportError:
continue
return pic
示例2: get_zip_bytes
# 需要导入模块: import zipimport [as 别名]
# 或者: from zipimport import ZipImportError [as 别名]
def get_zip_bytes(filename):
"""Get data from `filename` if it is a zip file path.
Returns the bytestring data read from the zip file, or None if no zip file
could be found or `filename` isn't in it. The data returned will be
an empty string if the file is empty.
"""
markers = ['.zip'+os.sep, '.egg'+os.sep, '.pex'+os.sep]
for marker in markers:
if marker in filename:
parts = filename.split(marker)
try:
zi = zipimport.zipimporter(parts[0]+marker[:-1])
except zipimport.ZipImportError:
continue
try:
data = zi.get_data(parts[1])
except IOError:
continue
return data
return None
示例3: assertZipFailure
# 需要导入模块: import zipimport [as 别名]
# 或者: from zipimport import ZipImportError [as 别名]
def assertZipFailure(self, filename):
self.assertRaises(zipimport.ZipImportError,
zipimport.zipimporter, filename)
示例4: _testBogusZipFile
# 需要导入模块: import zipimport [as 别名]
# 或者: from zipimport import ZipImportError [as 别名]
def _testBogusZipFile(self):
test_support.unlink(TESTMOD)
fp = open(TESTMOD, 'w+')
fp.write(struct.pack('=I', 0x06054B50))
fp.write('a' * 18)
fp.close()
z = zipimport.zipimporter(TESTMOD)
try:
self.assertRaises(TypeError, z.find_module, None)
self.assertRaises(TypeError, z.load_module, None)
self.assertRaises(TypeError, z.is_package, None)
self.assertRaises(TypeError, z.get_code, None)
self.assertRaises(TypeError, z.get_data, None)
self.assertRaises(TypeError, z.get_source, None)
error = zipimport.ZipImportError
self.assertEqual(z.find_module('abc'), None)
self.assertRaises(error, z.load_module, 'abc')
self.assertRaises(error, z.get_code, 'abc')
self.assertRaises(IOError, z.get_data, 'abc')
self.assertRaises(error, z.get_source, 'abc')
self.assertRaises(error, z.is_package, 'abc')
finally:
zipimport._zip_directory_cache.clear()
示例5: testFileUnreadable
# 需要导入模块: import zipimport [as 别名]
# 或者: from zipimport import ZipImportError [as 别名]
def testFileUnreadable(self):
support.unlink(TESTMOD)
fd = os.open(TESTMOD, os.O_CREAT, 000)
try:
os.close(fd)
with self.assertRaises(zipimport.ZipImportError) as cm:
zipimport.zipimporter(TESTMOD)
finally:
# If we leave "the read-only bit" set on Windows, nothing can
# delete TESTMOD, and later tests suffer bogus failures.
os.chmod(TESTMOD, 0o666)
support.unlink(TESTMOD)
示例6: _testBogusZipFile
# 需要导入模块: import zipimport [as 别名]
# 或者: from zipimport import ZipImportError [as 别名]
def _testBogusZipFile(self):
support.unlink(TESTMOD)
fp = open(TESTMOD, 'w+')
fp.write(struct.pack('=I', 0x06054B50))
fp.write('a' * 18)
fp.close()
z = zipimport.zipimporter(TESTMOD)
try:
self.assertRaises(TypeError, z.find_module, None)
self.assertRaises(TypeError, z.load_module, None)
self.assertRaises(TypeError, z.is_package, None)
self.assertRaises(TypeError, z.get_code, None)
self.assertRaises(TypeError, z.get_data, None)
self.assertRaises(TypeError, z.get_source, None)
error = zipimport.ZipImportError
self.assertEqual(z.find_module('abc'), None)
self.assertRaises(error, z.load_module, 'abc')
self.assertRaises(error, z.get_code, 'abc')
self.assertRaises(OSError, z.get_data, 'abc')
self.assertRaises(error, z.get_source, 'abc')
self.assertRaises(error, z.is_package, 'abc')
finally:
zipimport._zip_directory_cache.clear()
示例7: __init__
# 需要导入模块: import zipimport [as 别名]
# 或者: from zipimport import ZipImportError [as 别名]
def __init__(self, path):
try:
self.__zip = zipimport.zipimporter(path)
except zipimport.ZipImportError, e:
raise OwnerError('%s: %s' % (str(e), path))
示例8: getmod
# 需要导入模块: import zipimport [as 别名]
# 或者: from zipimport import ZipImportError [as 别名]
def getmod(self, nm, newmod=imp.new_module):
# We cannot simply use zipimport.load_module here
# because it both loads (= create module object)
# and imports (= execute bytecode). Instead, our
# getmod() functions are supposed to only load the modules.
# Note that imp.load_module() does the right thing, instead.
debug('zipimport try: %s within %s' % (nm, self.__zip))
try:
bytecode = self.__zip.get_code(nm)
mod = newmod(nm)
mod.__file__ = bytecode.co_filename
if self.__zip.is_package(nm):
mod.__path__ = [_os_path_join(self.path, nm)]
subimporter = PathImportDirector(mod.__path__)
mod.__importsub__ = subimporter.getmod
if self.path.endswith(".egg"):
# Fixup some additional special attribute so that
# pkg_resources works correctly.
# TODO: couldn't we fix these attributes always,
# for all zip files?
mod.__file__ = _os_path_join(
_os_path_join(self.path, nm), "__init__.py")
mod.__loader__ = self.__zip
mod.__co__ = bytecode
return mod
except zipimport.ZipImportError:
debug('zipimport not found %s' % nm)
return None
# Define order where to look for Python modules first.
# 1. PYZOwner: look in executable created by PyInstaller.
# (_pyi_bootstrap.py will insert it (archive.PYZOwner) in front later.)
# 2. ZipOwner: zip files (.egg files)
# 3. DirOwner: file system
# 4. Owner: module not found