Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_precompute_compress_rawcontent(self):
d = zstd.ZstdCompressionDict(
b"dictcontent" * 64, dict_type=zstd.DICT_TYPE_RAWCONTENT
)
d.precompute_compress(level=1)
d = zstd.ZstdCompressionDict(
b"dictcontent" * 64, dict_type=zstd.DICT_TYPE_FULLDICT
)
with self.assertRaisesRegex(zstd.ZstdError, "unable to precompute dictionary"):
d.precompute_compress(level=1)
def test_invalid_frame(self):
with self.assertRaisesRegex(zstd.ZstdError, "Unknown frame descriptor"):
zstd.get_frame_parameters(b"foobarbaz")
def test_compress_after_finish(self):
cctx = zstd.ZstdCompressor()
chunker = cctx.chunker()
list(chunker.compress(b"foo"))
list(chunker.finish())
with self.assertRaisesRegex(
zstd.ZstdError, r"cannot call compress\(\) after compression finished"
):
list(chunker.compress(b"foo"))
def test_too_small(self):
with self.assertRaisesRegex(
zstd.ZstdError,
"could not determine frame header size: Src size " "is incorrect",
):
zstd.frame_header_size(b"foob")
def test_invalid_type(self):
with self.assertRaises(TypeError):
zstd.get_frame_parameters(None)
# Python 3 doesn't appear to convert unicode to Py_buffer.
if sys.version_info[0] >= 3:
with self.assertRaises(TypeError):
zstd.get_frame_parameters(u"foobarbaz")
else:
# CPython will convert unicode to Py_buffer. But CFFI won't.
if zstd.backend == "cffi":
with self.assertRaises(TypeError):
zstd.get_frame_parameters(u"foobarbaz")
else:
with self.assertRaises(zstd.ZstdError):
zstd.get_frame_parameters(u"foobarbaz")
dctx = zstd.ZstdDecompressor()
if not hasattr(dctx, "multi_decompress_to_buffer"):
self.skipTest("multi_decompress_to_buffer not available")
with self.assertRaisesRegex(
zstd.ZstdError,
"error decompressing item 1: ("
"Corrupted block|"
"Destination buffer is too small)",
):
dctx.multi_decompress_to_buffer(frames)
with self.assertRaisesRegex(
zstd.ZstdError,
"error decompressing item 1: ("
"Corrupted block|"
"Destination buffer is too small)",
):
dctx.multi_decompress_to_buffer(frames, threads=2)
def decode_zstd(content: bytes) -> bytes:
if not content:
return b""
zstd_ctx = zstd.ZstdDecompressor()
try:
return zstd_ctx.decompress(content)
except zstd.ZstdError:
# If the zstd stream is streamed without a size header,
# try decoding with a 10MiB output buffer
return zstd_ctx.decompress(content, max_output_size=10 * 2**20)