https://github.com/python/cpython/commit/368aebd1b5560ea589bef87a6e08760b6bbb4961
commit: 368aebd1b5560ea589bef87a6e08760b6bbb4961
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: emmatyping <[email protected]>
date: 2026-07-12T19:51:30-07:00
summary:

[3.14] gh-153037: Make ZstdFile.__next__ raise io.UnsupportedOperation on 
non-readable files (GH-153045) (#153639)

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 <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-07-04-20-28-51.gh-issue-153037.XUSp_g.rst
M Lib/compression/zstd/_zstdfile.py
M Lib/test/test_zstd.py

diff --git a/Lib/compression/zstd/_zstdfile.py 
b/Lib/compression/zstd/_zstdfile.py
index 8d3358152e9a889..c4477244e8327f9 100644
--- a/Lib/compression/zstd/_zstdfile.py
+++ b/Lib/compression/zstd/_zstdfile.py
@@ -246,6 +246,7 @@ def peek(self, size=-1):
         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 cf618534add387c..6de63d840898ac7 100644
--- a/Lib/test/test_zstd.py
+++ b/Lib/test/test_zstd.py
@@ -2374,6 +2374,8 @@ def read(self, size):
                 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 000000000000000..cbfc62f75184706
--- /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.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to