Message260447
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. |
|
Date |
User |
Action |
Args |
2016-02-18 10:48:46 | martin.panter | set | recipients:
+ martin.panter, llllllllll |
2016-02-18 10:48:46 | martin.panter | set | messageid: <1455792526.72.0.0826322847528.issue26379@psf.upfronthosting.co.za> |
2016-02-18 10:48:46 | martin.panter | link | issue26379 messages |
2016-02-18 10:48:46 | martin.panter | create | |
|