Skip to content

gh-133998: Fix gzip file creation when time is out of range - #134278

Merged
serhiy-storchaka merged 11 commits into
python:mainfrom
adang1345:fix-gzip-mtime
May 22, 2026
Merged

serhiy-storchaka merged 11 commits into
python:mainfrom
adang1345:fix-gzip-mtime

Conversation

@adang1345

@adang1345 adang1345 commented May 19, 2025 •

Copy link
Copy Markdown
Contributor

If the system clock is set so that time.time() falls outside of the range 0 through 2**32-1, then creation of a GZIP-compressed file using gzip.GzipFile() would fail with the exception struct.error: 'L' format requires 0 <= number <= 4294967295. Fix this error.

If the mtime argument to gzip.GzipFile() is explicitly set to an out-of-range value, then the behavior is unchanged, and we continue raising the exception.


📚 Documentation preview 📚: https://cpython-previews--134278.org.readthedocs.build/

Comment thread Doc/library/gzip.rst Resolved
Comment thread Doc/library/gzip.rst Resolved
Comment thread Doc/library/gzip.rst Resolved
Comment thread Lib/test/test_gzip.py Resolved
Comment thread Lib/gzip.py Resolved
Comment thread Doc/library/gzip.rst
however, if the current time is outside the range 00:00:00 UTC, January 1,
1970 through 06:28:15 UTC, February 7, 2106, then the value ``0`` is used
instead.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs are slightly wrong now that GzipFile.__init__ will raise a ValueError if the specified value is out of range.
I know you've documented it in the docstring for __init__, but I think it's worth clarifying here since we're already in the area.

Comment thread Lib/gzip.py Outdated
Comment on lines +191 to +195
Set mtime to 0 to generate a compressed stream that does not depend on
creation time. If mtime is omitted or None, the current time is used;
however, if the current time is outside the range 00:00:00 UTC, January
1, 1970 through 06:28:15 UTC, February 7, 2106, then the value 0 is used
instead.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto, see previous comment

@@ -0,0 +1 @@
Fix ``struct.error`` exception when creating a file with ``gzip.GzipFile()`` if the system time is outside the range 00:00:00 UTC, January 1, 1970 through 06:28:15 UTC, February 7, 2106.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should also be tweaked since the behaviour of providing an out-of-range mtime has changed

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test also for 2**32-0.1.

I do not think that raising ValueError in the GzipFile constructor is right. This does not solve the problem of the user passing stat().st_mtime or time.time().

There is other occurrence of this problem in compress().

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 25, 2026
@serhiy-storchaka serhiy-storchaka self-assigned this May 21, 2026
@read-the-docs-community

read-the-docs-community Bot commented May 21, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #32800180 | 📁 Comparing e4e7d53 against main (65f9932)

  🔍 Preview build  

2 files changed
± library/gzip.html
± whatsnew/changelog.html
@serhiy-storchaka
serhiy-storchaka merged commit 1daad8a into python:main May 22, 2026
90 of 93 checks passed
@serhiy-storchaka serhiy-storchaka added the needs backport to 3.15 bugs and security fixes label May 22, 2026
@miss-islington-app

Copy link
Copy Markdown

Thanks @adang1345 for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented May 22, 2026

Copy link
Copy Markdown

GH-150221 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label May 22, 2026
@bedevere-bot

Copy link
Copy Markdown

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot AMD64 Debian root 3.x (tier-1) has failed when building commit 1daad8a.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/345/builds/14303) and take a look at the build logs.
  4. Check if the failure is related to this commit (1daad8a) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#/builders/345/builds/14303

Summary of the results of the build (if available):

==

Click to see traceback logs
Traceback (most recent call last):
  File "/root/buildarea/3.x.angelico-debian-amd64/build/Lib/test/support/__init__.py", line 948, in gc_collect
    gc.collect()
    ~~~~~~~~~~^^
ResourceWarning: unclosed file <_io.FileIO name=13 mode='wb' closefd=True>


Traceback (most recent call last):
  File "/root/buildarea/3.x.angelico-debian-amd64/build/Lib/test/support/__init__.py", line 948, in gc_collect
    gc.collect()
    ~~~~~~~~~~^^
ResourceWarning: unclosed file <_io.FileIO name=11 mode='wb' closefd=True>
@huwcbjones

Copy link
Copy Markdown

Thanks for getting the fix over the line @serhiy-storchaka! 😊

serhiy-storchaka added a commit that referenced this pull request May 22, 2026
…H-134278) (GH-150221)

(cherry picked from commit 1daad8a)

Co-authored-by: adang1345 <adang1345@gmail.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
grantlouisherman pushed a commit to grantlouisherman/cpython that referenced this pull request May 22, 2026
grantlouisherman pushed a commit to grantlouisherman/cpython that referenced this pull request May 22, 2026
@serhiy-storchaka serhiy-storchaka removed their assignment Jul 15, 2026
vstinner added a commit that referenced this pull request Oct 16, 2026
…159592)

[3.15] gh-133998: Fix gzip file creation when time is out of range (GH-134278) (GH-150221)

(cherry picked from commit 1daad8a)



(cherry picked from commit b039d1b)

Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Co-authored-by: adang1345 <adang1345@gmail.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
vanschelven added a commit to bugsink/zlib-rust that referenced this pull request Nov 26, 2026
Copy Lib/gzip.py, Lib/compression/_common/_streams.py and
Lib/test/test_gzip.py from CPython main at:
b0b000527e895a2882259a6a4b5bdfbf5618c54a (2026-11-26)
https://github.com/python/cpython/tree/b0b000527e895a2882259a6a4b5bdfbf5618c54a

The previous copy was v3.14.0 at
ebf955df7a89ed0c7968f79faec1de49f61ed7cb. The intervening fixes are:

* Possibly unbound buf in the gzip reader: gh-137571 / GH-150222.
  python/cpython#137571
  python/cpython#150222
* Header checksum validation: bpo-45509 / GH-29028.
  https://bugs.python.org/issue45509
  python/cpython#29028
* Large-header memory use: gh-149945 / GH-149979.
  python/cpython#149945
  python/cpython#149979
* Out-of-range creation times: gh-133998 / GH-134278.
  python/cpython#133998
  python/cpython#134278
* Quadratic decompression of many members: gh-160310 / GH-160311.
  python/cpython#160310
  python/cpython#160311
* Damaged later streams in the shared helpers: gh-160314 / GH-160315.
  python/cpython#160314
  python/cpython#160315
* CLI compression flags for file arguments: gh-161086 / GH-161087.
  python/cpython#161086
  python/cpython#161087
* CLI output replacement and permissions: gh-161088 / GH-161089.
  python/cpython#161088
  python/cpython#161089
* CLI module shadowing: gh-161705 / GH-161706.
  python/cpython#161705
  python/cpython#161706

The import also includes the default compression level change to 6
(gh-91349 / GH-131470 and GH-159439), gzip.open(mtime=...)
(gh-91372 / GH-32310), unused-field removal (GH-138123), docstring
formatting (gh-150285 / GH-150366), CLI help formatting
(gh-142389 / GH-149384), and CLI color-test setup
(gh-139946 / GH-140695), and unused test imports
(gh-134357 / GH-134340):
python/cpython#131470
python/cpython#159439
python/cpython#32310
python/cpython#138123
python/cpython#150366
python/cpython#149384
python/cpython#140695
python/cpython#134340

Keep the package imports and compatibility with Python 3.11 onward.
Older interpreters need a fallback for runpy._remove_sys_path0(),
equivalent unittest assertions, environment-based CLI color control,
and the earlier implicit write-mode cases (gh-69528 / GH-137834).
Document these adaptations in tests/upstream/cpython/SOURCE.md.
python/cpython#137834
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale Stale PR or inactive for long period of time.

5 participants