Skip to content

Commit 99fde4f

Browse files
committed
gh-153037: Fix AttributeError when iterating over a non-readable ZstdFile
Make `ZstdFile.__next__` raise `io.UnsupportedOperation` on non-readable files, consistent with the other read methods and compression modules.
1 parent 66c76fa commit 99fde4f

3 files changed

Lines changed: 6 additions & 0 deletions

File tree

Lib/compression/zstd/_zstdfile.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ def peek(self, size=-1):
246246
return self._buffer.peek(size)
247247

248248
def __next__(self):
249+
self._check_can_read()
249250
if ret := self._buffer.readline():
250251
return ret
251252
raise StopIteration

Lib/test/test_zstd.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2373,6 +2373,8 @@ def read(self, size):
23732373
f.read(100)
23742374
with self.assertRaises(io.UnsupportedOperation):
23752375
f.seek(100)
2376+
with self.assertRaises(io.UnsupportedOperation):
2377+
next(iter(f))
23762378
self.assertEqual(f.closed, True)
23772379
with self.assertRaises(ValueError):
23782380
f.readable()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix :class:`~compression.zstd.ZstdFile` raising :exc:`AttributeError`
2+
instead of :exc:`io.UnsupportedOperation` when iterating over a file that
3+
is not open for reading.

0 commit comments

Comments
 (0)