def test_deflate_set_dictionary(): text = 'abcabc' zdict = 'abc' stream = rzlib.deflateInit() rzlib.deflateSetDictionary(stream, zdict) bytes = rzlib.compress(stream, text, rzlib.Z_FINISH) rzlib.deflateEnd(stream) stream2 = rzlib.inflateInit() from rpython.rtyper.lltypesystem import lltype, rffi, rstr from rpython.rtyper.annlowlevel import llstr from rpython.rlib.rstring import StringBuilder with lltype.scoped_alloc(rffi.CCHARP.TO, len(bytes)) as inbuf: rstr.copy_string_to_raw(llstr(bytes), inbuf, 0, len(bytes)) stream2.c_next_in = rffi.cast(rzlib.Bytefp, inbuf) rffi.setintfield(stream2, 'c_avail_in', len(bytes)) with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as outbuf: stream2.c_next_out = rffi.cast(rzlib.Bytefp, outbuf) bufsize = 100 rffi.setintfield(stream2, 'c_avail_out', bufsize) err = rzlib._inflate(stream2, rzlib.Z_SYNC_FLUSH) assert err == rzlib.Z_NEED_DICT rzlib.inflateSetDictionary(stream2, zdict) rzlib._inflate(stream2, rzlib.Z_SYNC_FLUSH) avail_out = rffi.cast(lltype.Signed, stream2.c_avail_out) result = StringBuilder() result.append_charpsize(outbuf, bufsize - avail_out) rzlib.inflateEnd(stream2) assert result.build() == text
def test_compress_copy(): """ inflateCopy produces an independent copy of a stream. """ stream = rzlib.deflateInit() bytes1 = rzlib.compress(stream, expanded[:10]) assert bytes1 copied = rzlib.deflateCopy(stream) bytes_stream = rzlib.compress( stream, expanded[10:], rzlib.Z_FINISH, ) assert bytes1 + bytes_stream == compressed rzlib.deflateEnd(stream) bytes_copy = rzlib.compress( copied, expanded[10:], rzlib.Z_FINISH, ) rzlib.deflateEnd(copied) assert bytes1 + bytes_copy == compressed
def Compress___new__( space, w_subtype, level=rzlib.Z_DEFAULT_COMPRESSION, method=rzlib.Z_DEFLATED, # \ wbits=rzlib.MAX_WBITS, # \ undocumented memLevel=rzlib.DEF_MEM_LEVEL, # / parameters strategy=rzlib.Z_DEFAULT_STRATEGY, # / w_zdict=None): """ Create a new z_stream and call its initializer. """ if space.is_none(w_zdict): zdict = None else: zdict = space.charbuf_w(w_zdict) w_stream = space.allocate_instance(Compress, w_subtype) w_stream = space.interp_w(Compress, w_stream) try: stream = rzlib.deflateInit(level, method, wbits, memLevel, strategy, zdict=zdict) except rzlib.RZlibError as e: raise zlib_error(space, e.msg) except ValueError: raise oefmt(space.w_ValueError, "Invalid initialization option") Compress.__init__(w_stream, space, stream) return w_stream
def test_compression(): """ Once we have got a deflate stream, rzlib.compress() should allow us to compress bytes. """ stream = rzlib.deflateInit() bytes = rzlib.compress(stream, expanded) bytes += rzlib.compress(stream, "", rzlib.Z_FINISH) rzlib.deflateEnd(stream) assert bytes == compressed
def Compress_init(): try: level = rzlib.Z_DEFAULT_COMPRESSION method = rzlib.Z_DEFLATED wbits = rzlib.MAX_WBITS memLevel = rzlib.DEF_MEM_LEVEL strategy = rzlib.Z_DEFAULT_STRATEGY stream = rzlib.deflateInit(level, method, wbits, memLevel, strategy) return Compress(stream) except rzlib.RZlibError as e: raise zlib_error(e.msg)
def test_compression_lots_of_data(): """ Test compression of more data that fits in a single internal output buffer. """ expanded = repr(range(20000)) compressed = zlib.compress(expanded) print len(expanded), '=>', len(compressed) stream = rzlib.deflateInit() bytes = rzlib.compress(stream, expanded, rzlib.Z_FINISH) rzlib.deflateEnd(stream) assert bytes == compressed
def __init__(self, space, level=rzlib.Z_DEFAULT_COMPRESSION, method=rzlib.Z_DEFLATED, # \ wbits=rzlib.MAX_WBITS, # \ undocumented memLevel=rzlib.DEF_MEM_LEVEL, # / parameters strategy=rzlib.Z_DEFAULT_STRATEGY): # / ZLibObject.__init__(self, space) try: self.stream = rzlib.deflateInit(level, method, wbits, memLevel, strategy) except rzlib.RZlibError, e: raise zlib_error(space, e.msg)
def f(i, check): bytes = "s" * i if check == 1: for j in range(3): stream = rzlib.deflateInit() bytes = rzlib.compress(stream, bytes, rzlib.Z_FINISH) rzlib.deflateEnd(stream) return bytes if check == 2: return str(rzlib.adler32(bytes)) if check == 3: return str(rzlib.crc32(bytes)) return '?'
def __init__(self, space, level=rzlib.Z_DEFAULT_COMPRESSION, method=rzlib.Z_DEFLATED, # \ wbits=rzlib.MAX_WBITS, # \ undocumented memLevel=rzlib.DEF_MEM_LEVEL, # / parameters strategy=rzlib.Z_DEFAULT_STRATEGY): # / ZLibObject.__init__(self, space) try: self.stream = rzlib.deflateInit(level, method, wbits, memLevel, strategy) except rzlib.RZlibError as e: raise zlib_error(space, e.msg) except ValueError: raise oefmt(space.w_ValueError, "Invalid initialization option") self.register_finalizer(space)
def __init__( self, space, level=rzlib.Z_DEFAULT_COMPRESSION, method=rzlib.Z_DEFLATED, # \ wbits=rzlib.MAX_WBITS, # \ undocumented memLevel=rzlib.DEF_MEM_LEVEL, # / parameters strategy=rzlib.Z_DEFAULT_STRATEGY): # / ZLibObject.__init__(self, space) try: self.stream = rzlib.deflateInit(level, method, wbits, memLevel, strategy) except rzlib.RZlibError, e: raise zlib_error(space, e.msg)
def compress(space, string, level=rzlib.Z_DEFAULT_COMPRESSION): """ compress(string[, level]) -- Returned compressed string. Optional arg level is the compression level, in 1-9. """ try: try: stream = rzlib.deflateInit(level) except ValueError: raise zlib_error(space, "Bad compression level") try: result = rzlib.compress(stream, string, rzlib.Z_FINISH) finally: rzlib.deflateEnd(stream) except rzlib.RZlibError, e: raise zlib_error(space, e.msg)
def test_unsuccessful_compress_copy(): """ Errors during unsuccesful deflateCopy operations raise RZlibErrors. """ stream = rzlib.deflateInit() # From zlib.h: # # "deflateCopy returns [...] Z_STREAM_ERROR if the source stream # state was inconsistent (such as zalloc being Z_NULL)" from rpython.rtyper.lltypesystem import rffi, lltype stream.c_zalloc = rffi.cast(lltype.typeOf(stream.c_zalloc), rzlib.Z_NULL) exc = py.test.raises(rzlib.RZlibError, rzlib.deflateCopy, stream) msg = "Error -2 while copying compression object: inconsistent stream state" assert str(exc.value) == msg rzlib.deflateEnd(stream)
def test_cornercases(): """ Test degenerate arguments. """ stream = rzlib.deflateInit() bytes = rzlib.compress(stream, "") bytes += rzlib.compress(stream, "") bytes += rzlib.compress(stream, "", rzlib.Z_FINISH) assert zlib.decompress(bytes) == "" rzlib.deflateEnd(stream) stream = rzlib.inflateInit() data, finished, unused = rzlib.decompress(stream, "") assert data == "" assert finished is False assert unused == 0 buf = compressed for i in range(10): data, finished, unused = rzlib.decompress(stream, buf, max_length=0) assert data == "" assert finished is False assert unused > 0 buf = buf[-unused:] rzlib.deflateEnd(stream)
def _encode(data, level, encoding): stream = rzlib.deflateInit(level=level, wbits=encoding) bytes = rzlib.compress(stream, data) bytes += rzlib.compress(stream, "", rzlib.Z_FINISH) rzlib.deflateEnd(stream) return bytes
def test_deflate_init_end(): """ deflateInit() followed by deflateEnd() should work and do nothing. """ stream = rzlib.deflateInit() rzlib.deflateEnd(stream)