Martin Panter added the comment:

This is an unusual technique. I can’t think of any other standard library 
routine that returns a new bytearray instead of a bytes object.

Normally there are sister functions with an -into() suffix that accept a 
pre-allocated buffer. Examples are BufferedIOBase.readinto(), 
struct.pack_into(), socket.recv_into(). What do you think about adding a 
decompressobj.decompress_into(input, output) method instead? If necessary you 
could use it in a wrapper, something like:

def decompress_as_bytearray(data, wbits=15, bufsize=16384):
    decompressor = zlib.decompressobj(wbits=wbits)
    buffer = bytearray(bufsize + 1)  # Overallocate to help detect EOF
    offset = 0
    while True:
        with memoryview(buffer) as view:
            offset += decompressor.decompress_into(data, view[offset:])
        if offset < len(buffer):
            break
        data = decompressor.unconsumed_tail
        buffer *= 2  # Expand the buffer
    del buffer[offset:]
    if not decompressor.eof:
        raise zlib.error("Incomplete data")
    return buffer

If anything is added for zlib decompression, it would be good to add equivalent 
functionality in the bz2 and LZMA modules.

----------
nosy: +martin.panter

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue26379>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to