Skip to content

gzip.py in 3.12 branch: produces incorrect trailer for memoryviews with itemsize != 1 #141201

@dstndstn

Description

@dstndstn

Bug report

Bug description:

import array
import gzip
# Create a one-element, uint16 array
a=array.array('H', [1])
# The memoryview has len(1) but itemsize=2 and nbytes=2
x=memoryview(a)
d=gzip.compress(x)
gzip.decompress(d)

Or one-liner

python3.12 -c "import array; a=array.array('H', [1]); x=memoryview(a); import gzip; d=gzip.compress(x); gzip.decompress(d)"

prints

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.12/gzip.py", line 657, in decompress
    raise BadGzipFile("Incorrect length of data produced")
gzip.BadGzipFile: Incorrect length of data produced

This works in Python 3.13.0.

The gzip.py is creating the CRC trailer by calling len(data), which gives the number of elements for a memoryview, not the number of bytes. So the trailer is incorrect, and then it fails to decompress.

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions