Generate 16+MAX_WBITS decompressable data

Terry Reedy tjreedy at udel.edu
Tue Feb 12 10:27:39 EST 2013


On 2/12/2013 7:47 AM, Fayaz Yusuf Khan wrote:
> I'm trying write unit-tests for some of my old code and have run into this
> piece of code.
>
> dcomp = zlib.decompressobj(16+zlib.MAX_WBITS)

Since zlib.MAX_WBITS is the largest value that should be passed (15), 
adding 16 makes no sense. Since it is also the default, there is also no 
point in providing it explicitly. "Its absolute value should be between 
8 and 15 for the most recent versions of the zlib library".

> chunk = ''.join(f.chunks())
> received_data =  dcomp.decompress(chunk)

Since decompressobj is intended for data that will not all fit in memory 
at once, and since chunk does, just use zlib.decompress(chunk)

> How do I generate the chunk here? From what I've been trying I'm getting
> this exception:
>>>> import zlib
>>>> zlib.compress('hello')
> 'x\x9c\xcbH\xcd\xc9\xc9\x07\x00\x06,\x02\x15'
>>>> zlib.decompress(_, 16+zlib.MAX_WBITS)
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> zlib.error: Error -3 while decompressing data: incorrect header check

You asked for an internal buffer of 2**31 = 2 gigabytes.

> zlib.decompress without the second argument works, but I can't really go
> ahead into my project file and remove it.

-- 
Terry Jan Reedy




More information about the Python-list mailing list