diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst index f32a54f0e35a02c..f1206c4bfabfb13 100644 --- a/Doc/library/gzip.rst +++ b/Doc/library/gzip.rst @@ -163,6 +163,11 @@ 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``. + When compressing, this attribute is set to the value passed to the constructor. + + .. versionchanged:: 3.16 + In previous versions, an :exc:`AttributeError` occured for compressing streams. + .. attribute:: name The path to the gzip file on disk, as a :class:`str` or :class:`bytes`. diff --git a/Doc/whatsnew/3.16.rst b/Doc/whatsnew/3.16.rst index ce001d07731bcc5..83db0243f48e02b 100644 --- a/Doc/whatsnew/3.16.rst +++ b/Doc/whatsnew/3.16.rst @@ -461,6 +461,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..28fde67dd840884 --- /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, +this caused an :exc:`AttributeError`.