From 7d8783bd9fa48eed4046fb1ab0d3e4618d52b5f0 Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Mon, 13 Jul 2026 04:51:38 +0200 Subject: [PATCH] [3.15] gh-153037: Make ZstdFile.__next__ raise io.UnsupportedOperation on non-readable files (GH-153045) (#153638) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit gh-153037: Make ZstdFile.__next__ raise io.UnsupportedOperation on non-readable files (GH-153045) Make `ZstdFile.__next__` raise `io.UnsupportedOperation` on non-readable files, consistent with other compression modules. (cherry picked from commit ed716551e13d1e46a5cd17955657d64b8824626a) Co-authored-by: Łukasz --- Lib/compression/zstd/_zstdfile.py | 1 + Lib/test/test_zstd.py | 2 ++ .../Library/2026-07-04-20-28-51.gh-issue-153037.XUSp_g.rst | 3 +++ 3 files changed, 6 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-07-04-20-28-51.gh-issue-153037.XUSp_g.rst diff --git a/Lib/compression/zstd/_zstdfile.py b/Lib/compression/zstd/_zstdfile.py index 8d3358152e9a..c4477244e832 100644 --- a/Lib/compression/zstd/_zstdfile.py +++ b/Lib/compression/zstd/_zstdfile.py @@ -246,6 +246,7 @@ class ZstdFile(_streams.BaseStream): return self._buffer.peek(size) def __next__(self): + self._check_can_read() if ret := self._buffer.readline(): return ret raise StopIteration diff --git a/Lib/test/test_zstd.py b/Lib/test/test_zstd.py index 6358cc78739c..9225c42e0e71 100644 --- a/Lib/test/test_zstd.py +++ b/Lib/test/test_zstd.py @@ -2373,6 +2373,8 @@ class FileTestCase(unittest.TestCase): f.read(100) with self.assertRaises(io.UnsupportedOperation): f.seek(100) + with self.assertRaises(io.UnsupportedOperation): + next(iter(f)) self.assertEqual(f.closed, True) with self.assertRaises(ValueError): f.readable() diff --git a/Misc/NEWS.d/next/Library/2026-07-04-20-28-51.gh-issue-153037.XUSp_g.rst b/Misc/NEWS.d/next/Library/2026-07-04-20-28-51.gh-issue-153037.XUSp_g.rst new file mode 100644 index 000000000000..cbfc62f75184 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-04-20-28-51.gh-issue-153037.XUSp_g.rst @@ -0,0 +1,3 @@ +Fix :class:`~compression.zstd.ZstdFile` raising :exc:`AttributeError` +instead of :exc:`io.UnsupportedOperation` when iterating over a file that +is not open for reading. -- 2.47.3