]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-153037: Make ZstdFile.__next__ raise io.UnsupportedOperation on non-readabl...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 13 Jul 2026 02:51:30 +0000 (04:51 +0200)
committerGitHub <noreply@github.com>
Mon, 13 Jul 2026 02:51:30 +0000 (19:51 -0700)
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 <lukaszlapinski7@gmail.com>
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 cf618534add387cccda5f2b9aa5f063ed91f5417..6de63d840898ac7a5bcc5bb7b6b81f5d6a290434 100644 (file)
@@ -2374,6 +2374,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.