本文整理汇总了Python中lzma.LZMAError方法的典型用法代码示例。如果您正苦于以下问题:Python lzma.LZMAError方法的具体用法?Python lzma.LZMAError怎么用?Python lzma.LZMAError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lzma
的用法示例。
在下文中一共展示了lzma.LZMAError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: xzopen
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAError [as 别名]
def xzopen(cls, name, mode="r", fileobj=None, preset=None, **kwargs):
"""Open lzma compressed tar archive name for reading or writing.
Appending is not allowed.
"""
if mode not in ("r", "w"):
raise ValueError("mode must be 'r' or 'w'")
try:
import lzma
except ImportError:
raise CompressionError("lzma module is not available")
fileobj = lzma.LZMAFile(fileobj or name, mode, preset=preset)
try:
t = cls.taropen(name, mode, fileobj, **kwargs)
except (lzma.LZMAError, EOFError):
fileobj.close()
raise ReadError("not an lzma file")
t._extfileobj = False
return t
# All *open() methods are registered here.
示例2: check_if_committed
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAError [as 别名]
def check_if_committed(self, filename: str) -> bool:
"""Checks to see if the current post has been committed.
A post is considered committed if its json metadata file exists and is not malformed.
.. versionadded:: 4.2
"""
if os.path.isfile(filename + '.json.xz'):
filename += '.json.xz'
elif os.path.isfile(filename + '.json'):
filename += '.json'
else:
return False
try:
load_structure_from_file(self.context, filename)
return True
except (FileNotFoundError, lzma.LZMAError, json.decoder.JSONDecodeError):
return False
示例3: test_init_bad_check
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAError [as 别名]
def test_init_bad_check(self):
with self.assertRaises(TypeError):
LZMAFile(BytesIO(), "w", check=b"asd")
# CHECK_UNKNOWN and anything above CHECK_ID_MAX should be invalid.
with self.assertRaises(LZMAError):
LZMAFile(BytesIO(), "w", check=lzma.CHECK_UNKNOWN)
with self.assertRaises(LZMAError):
LZMAFile(BytesIO(), "w", check=lzma.CHECK_ID_MAX + 3)
# Cannot specify a check with mode="r".
with self.assertRaises(ValueError):
LZMAFile(BytesIO(COMPRESSED_XZ), check=lzma.CHECK_NONE)
with self.assertRaises(ValueError):
LZMAFile(BytesIO(COMPRESSED_XZ), check=lzma.CHECK_CRC32)
with self.assertRaises(ValueError):
LZMAFile(BytesIO(COMPRESSED_XZ), check=lzma.CHECK_CRC64)
with self.assertRaises(ValueError):
LZMAFile(BytesIO(COMPRESSED_XZ), check=lzma.CHECK_SHA256)
with self.assertRaises(ValueError):
LZMAFile(BytesIO(COMPRESSED_XZ), check=lzma.CHECK_UNKNOWN)
示例4: test_init_bad_preset
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAError [as 别名]
def test_init_bad_preset(self):
with self.assertRaises(TypeError):
LZMAFile(BytesIO(), "w", preset=4.39)
with self.assertRaises(LZMAError):
LZMAFile(BytesIO(), "w", preset=10)
with self.assertRaises(LZMAError):
LZMAFile(BytesIO(), "w", preset=23)
with self.assertRaises(OverflowError):
LZMAFile(BytesIO(), "w", preset=-1)
with self.assertRaises(OverflowError):
LZMAFile(BytesIO(), "w", preset=-7)
with self.assertRaises(TypeError):
LZMAFile(BytesIO(), "w", preset="foo")
# Cannot specify a preset with mode="r".
with self.assertRaises(ValueError):
LZMAFile(BytesIO(COMPRESSED_XZ), preset=3)
示例5: test__encode_filter_properties
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAError [as 别名]
def test__encode_filter_properties(self):
with self.assertRaises(TypeError):
lzma._encode_filter_properties(b"not a dict")
with self.assertRaises(ValueError):
lzma._encode_filter_properties({"id": 0x100})
with self.assertRaises(ValueError):
lzma._encode_filter_properties({"id": lzma.FILTER_LZMA2, "junk": 12})
with self.assertRaises(lzma.LZMAError):
lzma._encode_filter_properties({"id": lzma.FILTER_DELTA,
"dist": 9001})
# Test with parameters used by zipfile module.
props = lzma._encode_filter_properties({
"id": lzma.FILTER_LZMA1,
"pb": 2,
"lp": 0,
"lc": 3,
"dict_size": 8 << 20,
})
self.assertEqual(props, b"]\x00\x00\x80\x00")
示例6: decompress_lzma
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAError [as 别名]
def decompress_lzma(data):
results = []
len(data)
while True:
decomp = LZMADecompressor(FORMAT_AUTO, None, None)
try:
res = decomp.decompress(data)
except LZMAError:
if results:
break # Leftover data is not a valid LZMA/XZ stream; ignore it.
else:
raise # Error on the first iteration; bail out.
results.append(res)
data = decomp.unused_data
if not data:
break
if not decomp.eof:
raise LZMAError("Compressed data ended before the end-of-stream marker was reached")
return b"".join(results)
示例7: decompress
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAError [as 别名]
def decompress(decomp, buf):
'''Decompress *buf* using *decomp*
This method encapsulates exception handling for different
decompressors.
'''
try:
return decomp.decompress(buf)
except IOError as exc:
if exc.args[0].lower().startswith('invalid data stream'):
raise CorruptedObjectError('Invalid compressed stream')
raise
except lzma.LZMAError as exc:
if (exc.args[0].lower().startswith('corrupt input data')
or exc.args[0].startswith('Input format not supported')):
raise CorruptedObjectError('Invalid compressed stream')
raise
except zlib.error as exc:
if exc.args[0].lower().startswith('error -3 while decompressing'):
raise CorruptedObjectError('Invalid compressed stream')
raise
示例8: download_clusterblast
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAError [as 别名]
def download_clusterblast(db_dir: str) -> None:
"""Download the clusterblast database."""
archive_filename = os.path.join(db_dir, CLUSTERBLAST_URL.rpartition("/")[2])
fasta_filename = os.path.join(db_dir, "clusterblast", "proteins.fasta")
if present_and_checksum_matches(fasta_filename, CLUSTERBLAST_FASTA_CHECKSUM):
print("ClusterBlast fasta file present and checked")
return
print("Downloading ClusterBlast database.")
check_diskspace(CLUSTERBLAST_URL)
download_if_not_present(CLUSTERBLAST_URL, archive_filename, CLUSTERBLAST_ARCHIVE_CHECKSUM)
filename = unzip_file(archive_filename, lzma, lzma.LZMAError)
untar_file(filename)
delete_file(filename)
delete_file(filename + ".xz")
示例9: replace_syslinux_modules
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAError [as 别名]
def replace_syslinux_modules(syslinux_version, under_this_dir):
# Replace modules files extracted from iso with corresponding
# version provided by multibootusb.
modules_src_dir = os.path.join(
multibootusb_host_dir(), "syslinux", "modules", syslinux_version)
for dirpath, dirnames, filenames in os.walk(under_this_dir):
for fname in filenames:
if not fname.lower().endswith('.c32'):
continue
dst_path = os.path.join(under_this_dir, dirpath, fname)
src_path = os.path.join(modules_src_dir, fname)
if not os.path.exists(src_path):
log("Suitable replacement of '%s' is not bundled. "
"Trying to unlzma." % fname)
try:
with lzma.open(dst_path) as f:
expanded = f.read()
except lzma.LZMAError:
continue
except (OSError, IOError) as e:
log("%s while accessing %s." % (e, dst_path))
continue
with open(dst_path, 'wb') as f:
f.write(expanded)
log("Successfully decompressed %s." % fname)
continue
try:
os.remove(dst_path)
shutil.copy(src_path, dst_path)
log("Replaced %s module" % fname)
except (OSError, IOError) as err:
log(err)
log("Could not update " + fname)
示例10: xzopen
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAError [as 别名]
def xzopen(cls, name, mode="r", fileobj=None, preset=None, **kwargs):
"""Open lzma compressed tar archive name for reading or writing.
Appending is not allowed.
"""
if mode not in ("r", "w", "x"):
raise ValueError("mode must be 'r', 'w' or 'x'")
try:
import lzma
except ImportError:
raise CompressionError("lzma module is not available")
fileobj = lzma.LZMAFile(fileobj or name, mode, preset=preset)
try:
t = cls.taropen(name, mode, fileobj, **kwargs)
except (lzma.LZMAError, EOFError):
fileobj.close()
if mode == 'r':
raise ReadError("not an lzma file")
raise
except:
fileobj.close()
raise
t._extfileobj = False
return t
# All *open() methods are registered here.
示例11: test_decompressor_memlimit
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAError [as 别名]
def test_decompressor_memlimit(self):
lzd = LZMADecompressor(memlimit=1024)
self.assertRaises(LZMAError, lzd.decompress, COMPRESSED_XZ)
lzd = LZMADecompressor(lzma.FORMAT_XZ, memlimit=1024)
self.assertRaises(LZMAError, lzd.decompress, COMPRESSED_XZ)
lzd = LZMADecompressor(lzma.FORMAT_ALONE, memlimit=1024)
self.assertRaises(LZMAError, lzd.decompress, COMPRESSED_ALONE)
# Test LZMADecompressor on known-good input data.
示例12: test_decompressor_bad_input
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAError [as 别名]
def test_decompressor_bad_input(self):
lzd = LZMADecompressor()
self.assertRaises(LZMAError, lzd.decompress, COMPRESSED_RAW_1)
lzd = LZMADecompressor(lzma.FORMAT_XZ)
self.assertRaises(LZMAError, lzd.decompress, COMPRESSED_ALONE)
lzd = LZMADecompressor(lzma.FORMAT_ALONE)
self.assertRaises(LZMAError, lzd.decompress, COMPRESSED_XZ)
lzd = LZMADecompressor(lzma.FORMAT_RAW, filters=FILTERS_RAW_1)
self.assertRaises(LZMAError, lzd.decompress, COMPRESSED_XZ)
# Test that LZMACompressor->LZMADecompressor preserves the input data.
示例13: test_decompress_memlimit
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAError [as 别名]
def test_decompress_memlimit(self):
with self.assertRaises(LZMAError):
lzma.decompress(COMPRESSED_XZ, memlimit=1024)
with self.assertRaises(LZMAError):
lzma.decompress(
COMPRESSED_XZ, format=lzma.FORMAT_XZ, memlimit=1024)
with self.assertRaises(LZMAError):
lzma.decompress(
COMPRESSED_ALONE, format=lzma.FORMAT_ALONE, memlimit=1024)
# Test LZMADecompressor on known-good input data.
示例14: test_decompress_incomplete_input
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAError [as 别名]
def test_decompress_incomplete_input(self):
self.assertRaises(LZMAError, lzma.decompress, COMPRESSED_XZ[:128])
self.assertRaises(LZMAError, lzma.decompress, COMPRESSED_ALONE[:128])
self.assertRaises(LZMAError, lzma.decompress, COMPRESSED_RAW_1[:128],
format=lzma.FORMAT_RAW, filters=FILTERS_RAW_1)
self.assertRaises(LZMAError, lzma.decompress, COMPRESSED_RAW_2[:128],
format=lzma.FORMAT_RAW, filters=FILTERS_RAW_2)
self.assertRaises(LZMAError, lzma.decompress, COMPRESSED_RAW_3[:128],
format=lzma.FORMAT_RAW, filters=FILTERS_RAW_3)
self.assertRaises(LZMAError, lzma.decompress, COMPRESSED_RAW_4[:128],
format=lzma.FORMAT_RAW, filters=FILTERS_RAW_4)
示例15: test_read_bad_data
# 需要导入模块: import lzma [as 别名]
# 或者: from lzma import LZMAError [as 别名]
def test_read_bad_data(self):
with LZMAFile(BytesIO(COMPRESSED_BOGUS)) as f:
self.assertRaises(LZMAError, f.read)