From 1c817f941de6728c93d6e2b6819b836336f9893f Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 28 Oct 2026 20:07:16 -0500 Subject: [PATCH 1/3] gh-160860: Fix value of GzipFIle.mtime of writable stream --- Doc/whatsnew/3.16.rst | 2 ++ Lib/gzip.py | 2 ++ Lib/test/test_gzip.py | 1 + .../Library/2026-10-28-20-23-15.gh-issue-160860.ozPoph.rst | 3 +++ 4 files changed, 8 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-10-28-20-23-15.gh-issue-160860.ozPoph.rst diff --git a/Doc/whatsnew/3.16.rst b/Doc/whatsnew/3.16.rst index e342aa71c7924ea..1469ce6a28459b4 100644 --- a/Doc/whatsnew/3.16.rst +++ b/Doc/whatsnew/3.16.rst @@ -457,6 +457,8 @@ gzip * :func:`gzip.open` now accepts an optional argument ``mtime`` which is passed on to the constructor of the :class:`~gzip.GzipFile` class. (Contributed by Marin Misur in :gh:`91372`.) +* The write-only property :attr:`gzip.GzipFile.mtime` now returns the + constructor value for :class:`~gzip.GzipFile` files in write mode. imaplib diff --git a/Lib/gzip.py b/Lib/gzip.py index 5b6ebcfef45c3ad..eeb2a633b05fab8 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -262,6 +262,8 @@ def __init__(self, filename=None, mode=None, @property def mtime(self): """Last modification time read from stream, or None""" + if self.mode == WRITE: + return self._write_mtime return self._buffer.raw._last_mtime def __repr__(self): diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index 8bc8e507683cb50..e149d5d78157460 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -343,6 +343,7 @@ def test_paddedfile_getattr(self): def test_mtime(self): mtime = 123456789 with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite: + self.assertEqual(fWrite.mtime, mtime) fWrite.write(data1) with gzip.GzipFile(self.filename) as fRead: self.assertHasAttr(fRead, 'mtime') diff --git a/Misc/NEWS.d/next/Library/2026-10-28-20-23-15.gh-issue-160860.ozPoph.rst b/Misc/NEWS.d/next/Library/2026-10-28-20-23-15.gh-issue-160860.ozPoph.rst new file mode 100644 index 000000000000000..f74d4a22e6cf6bf --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-28-20-23-15.gh-issue-160860.ozPoph.rst @@ -0,0 +1,3 @@ +The write-only property :attr:`gzip.GzipFile.mtime` now returns the +constructor value for :class:`~gzip.GzipFile` files in write mode. Formerly, +it was ``None``. From 455cfd46557fce3c5355f43211a8112546b863f7 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 28 Oct 2026 20:46:01 -0500 Subject: [PATCH 2/3] update gzip docs accordingly --- Doc/library/gzip.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst index f32a54f0e35a02c..85d7ee641b270ca 100644 --- a/Doc/library/gzip.rst +++ b/Doc/library/gzip.rst @@ -163,6 +163,10 @@ The module defines the following items: since the Unix epoch (00:00:00 UTC, January 1, 1970). The initial value before reading any headers is ``None``. + .. versionchanged:: 3.16 + When compressing, this attribute is set to the value passed to the constructor. + In older versions, it was always ``None`` when compressing. + .. attribute:: name The path to the gzip file on disk, as a :class:`str` or :class:`bytes`. From e857a1b5293f0a51be37e0581a950b713d4bb940 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 29 Oct 2026 12:01:19 -0500 Subject: [PATCH 3/3] Correct documentation to correctly describe prior behavior --- Doc/library/gzip.rst | 5 +++-- .../Library/2026-10-28-20-23-15.gh-issue-160860.ozPoph.rst | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst index 85d7ee641b270ca..f1206c4bfabfb13 100644 --- a/Doc/library/gzip.rst +++ b/Doc/library/gzip.rst @@ -163,9 +163,10 @@ The module defines the following items: since the Unix epoch (00:00:00 UTC, January 1, 1970). The initial value before reading any headers is ``None``. - .. versionchanged:: 3.16 When compressing, this attribute is set to the value passed to the constructor. - In older versions, it was always ``None`` when compressing. + + .. versionchanged:: 3.16 + In previous versions, an :exc:`AttributeError` occured for compressing streams. .. attribute:: name diff --git a/Misc/NEWS.d/next/Library/2026-10-28-20-23-15.gh-issue-160860.ozPoph.rst b/Misc/NEWS.d/next/Library/2026-10-28-20-23-15.gh-issue-160860.ozPoph.rst index f74d4a22e6cf6bf..28fde67dd840884 100644 --- a/Misc/NEWS.d/next/Library/2026-10-28-20-23-15.gh-issue-160860.ozPoph.rst +++ b/Misc/NEWS.d/next/Library/2026-10-28-20-23-15.gh-issue-160860.ozPoph.rst @@ -1,3 +1,3 @@ The write-only property :attr:`gzip.GzipFile.mtime` now returns the constructor value for :class:`~gzip.GzipFile` files in write mode. Formerly, -it was ``None``. +this caused an :exc:`AttributeError`.