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

gh-133998: Fix gzip file creation when time is out of range#134278
serhiy-storchaka merged 11 commits into
python:mainfrom
adang1345:fix-gzip-mtime

Conversation

@adang1345
Copy link
Copy Markdown
Contributor

@adang1345 adang1345 commented May 19, 2025

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 Outdated
Comment thread Doc/library/gzip.rst Outdated
Comment thread Doc/library/gzip.rst Outdated
Comment thread Lib/test/test_gzip.py
Comment thread Lib/gzip.py Outdated
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

Copy link
Copy Markdown
Member

@serhiy-storchaka serhiy-storchaka left a comment

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
Copy link
Copy Markdown

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

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
94 of 97 checks passed
@serhiy-storchaka serhiy-storchaka added the needs backport to 3.15 pre-release feature fixes, 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
Copy link
Copy Markdown

bedevere-app Bot commented May 22, 2026

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 pre-release feature fixes, 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
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants