This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author martin.panter
Recipients llllllllll, martin.panter
Date 2016-02-18.10:48:46
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1455792526.72.0.0826322847528.issue26379@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2016-02-18 10:48:46martin.pantersetrecipients: + martin.panter, llllllllll
2016-02-18 10:48:46martin.pantersetmessageid: <1455792526.72.0.0826322847528.issue26379@psf.upfronthosting.co.za>
2016-02-18 10:48:46martin.panterlinkissue26379 messages
2016-02-18 10:48:46martin.pantercreate