From: Ɓukasz Date: Mon, 13 Jul 2026 02:15:53 +0000 (+0200) Subject: gh-153037: Make ZstdFile.__next__ raise io.UnsupportedOperation on non-readable files... X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=ed716551e13d1e46a5cd17955657d64b8824626a;p=thirdparty%2FPython%2Fcpython.git gh-153037: Make ZstdFile.__next__ raise io.UnsupportedOperation on non-readable files (#153045) Make `ZstdFile.__next__` raise `io.UnsupportedOperation` on non-readable files, consistent with other compression modules. --- 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.