Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions path/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,8 @@ def chunks(self, size, *args, **kwargs):

This will read the file by chunks of 8192 bytes.
"""
if size <= 0:
raise ValueError("size must be positive")
with self.open(*args, **kwargs) as f:
yield from iter(lambda: f.read(size) or None, None)

Expand Down
9 changes: 9 additions & 0 deletions tests/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,15 @@ def test_chunks(self, tmpdir):

assert i == len(txt) / size - 1

def test_chunks_rejects_nonpositive_size(self, tmpdir):
p = (TempDir() / 'test.txt').touch()
p.write_text("0123456789", encoding='utf-8')
with pytest.raises(ValueError, match="size must be positive"):
list(p.chunks(0, encoding='utf-8'))
with pytest.raises(ValueError, match="size must be positive"):
list(p.chunks(-1, encoding='utf-8'))
assert list(p.chunks(5, encoding='utf-8')) == ["01234", "56789"]

def test_samefile(self, tmpdir):
f1 = (TempDir() / '1.txt').touch()
f1.write_text('foo')
Expand Down