Skip to content

Commit 368aebd

Browse files
miss-islingtonlkk7
andauthored
[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 ed71655) Co-authored-by: Łukasz <lukaszlapinski7@gmail.com>
1 parent bb29dc2 commit 368aebd

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
@@ -2374,6 +2374,8 @@ def read(self, size):
23742374
f.read(100)
23752375
with self.assertRaises(io.UnsupportedOperation):
23762376
f.seek(100)
2377+
with self.assertRaises(io.UnsupportedOperation):
2378+
next(iter(f))
23772379
self.assertEqual(f.closed, True)
23782380
with self.assertRaises(ValueError):
23792381
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)