]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-153037: Make ZstdFile.__next__ raise io.UnsupportedOperation on non-readable files...
authorŁukasz <lukaszlapinski7@gmail.com>
Mon, 13 Jul 2026 02:15:53 +0000 (04:15 +0200)
committerGitHub <noreply@github.com>
Mon, 13 Jul 2026 02:15:53 +0000 (19:15 -0700)
Make `ZstdFile.__next__` raise `io.UnsupportedOperation` on non-readable files, consistent with other compression modules.

Lib/compression/zstd/_zstdfile.py
Lib/test/test_zstd.py
Misc/NEWS.d/next/Library/2026-07-04-20-28-51.gh-issue-153037.XUSp_g.rst [new file with mode: 0644]

index 8d3358152e9a889c68ade8b3e9c3bc3f05f92925..c4477244e8327f996de1fac2af42d447c9d8eba2 100644 (file)
@@ -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
index 6358cc78739cd9deb409c8ca9d464b1a309cf56a..9225c42e0e711aaef5190aff39442a1bd59168bc 100644 (file)
@@ -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 (file)
index 0000000..cbfc62f
--- /dev/null
@@ -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.