Compare compressed bytes only after decompression
Also published in our Blogger archive.
Two gzip byte sequences can represent the same uncompressed fixture while still differing as byte strings. This example compresses one payload twice: once at level 1 with timestamp zero and once at level 9 with timestamp one. The changed timestamp alone is enough to make the gzip representations distinct, and the compression-level choice also describes different encoder settings.
The first assertion intentionally confirms that fast and other are not equal. It is not a failure condition: it shows why raw compressed-byte equality is the wrong comparison when the intended contract is content equality. Each stream is decompressed, and each restored byte string is compared with the original payload. The only output, logical-equality=True, reports that content-level conclusion rather than a size or timing measurement.
This comparison does not authenticate either stream, prove that arbitrary inputs are safe, or indicate which compressor configuration should be used in production. If a test truly requires reproducible gzip bytes, it must define all relevant encoding details and test that stricter contract separately. gzip.compress gained its mtime parameter in Python 3.8, which is the minimum version for this exact code. The documentation describes mtime=0 as suitable for reproducible output and explains that gzip.decompress returns uncompressed bytes. See gzip.compress and gzip.decompress.
import gzip
payload = b"same logical fixture\n"
fast = gzip.compress(payload, compresslevel=1, mtime=0)
other = gzip.compress(payload, compresslevel=9, mtime=1)
assert fast != other
assert gzip.decompress(fast) == payload
assert gzip.decompress(other) == payload
print("logical-equality=True")
logical-equality=True
AI assistance disclosure: this article was drafted with AI assistance and should be reviewed in the context of its project.