Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gzip.compress(..., mtime=0) in cpython 3.11+ unexpectedly sets OS byte in gzip header #112346

Closed
Labels
stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
@dennisvang

Description

@dennisvang

Bug report

description

Usinggzip.compress() withmtime=0 in 3.8<=cpython<=3.10, theOS byte, i.e. the 10th byte in theGZIP header, is set to255 "unknown" (also see e.g.#83302):

returnstruct.pack("<BBBBLBB",0x1f,0x8b,8,0,int(mtime),xfl,255)

However, in cpython 3.11 and 3.12, theOS byte is suddenly set to a "known" value, e.g.3 ("Unix") on Ubuntu.

This isnot mentioned in thechangelog for Python 3.11.

This may lead to problems in the context ofreproducible builds. In our case, hash checking fails after decompressing and re-compressing a gzipped archive.

how to reproduce

Here's an example, where byte 10 is\xff in python 3.10 and\x03 in python 3.11:

~ $pythonPython3.10.12 (main,Jun112023,05:26:28) [GCC11.4.0]onlinux>>>importgzip>>>gzip.compress(b'',mtime=0)b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\xff\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00'~ $pyenvshell3.11~ $pythonPython3.11.6 (main,Nov232023,17:30:16) [GCC11.4.0]onlinux>>>importgzip>>>gzip.compress(b'',mtime=0)b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\x03\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00'

cause

I guess this is caused by python 3.11 delegating thegzip.compress() call tozlib ifmtime=0, as mentioned in thedocs:

Changed in version 3.11: Speed is improved by compressing all data at once instead of in a streamed fashion. Calls with mtime set to 0 are delegated tozlib.compress() for better speed.

and source:

cpython/Lib/gzip.py

Lines 609 to 612 in89ddea4

ifmtime==0:
# Use zlib as it creates the header with 0 mtime by default.
# This is faster and with less overhead.
returnzlib.compress(data,level=compresslevel,wbits=31)

Apparentlyzlibdoes set theOS byte.

CPython versions tested on:

3.8, 3.9, 3.10, 3.11, 3.12

Operating systems tested on:

Linux, macOS, Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp