Skip to content

Commit 68475be

Browse files
lhoestqWauplin
authored andcommitted
[HfFileSystem] Fix end-of-file read() (#3080)
* fix hffs oef read * add test * fix test --------- Co-authored-by: Lucain <lucain@huggingface.co>
1 parent 2e5dc96 commit 68475be

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/huggingface_hub/hf_file_system.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,9 @@ def read(self, length=-1):
10091009
"""
10101010
if self.mode == "rb" and (length is None or length == -1) and self.loc == 0:
10111011
with self.fs.open(self.path, "rb", block_size=0) as f: # block_size=0 enables fast streaming
1012-
return f.read()
1012+
out = f.read()
1013+
self.loc += len(out)
1014+
return out
10131015
return super().read(length)
10141016

10151017
def url(self) -> str:
@@ -1057,7 +1059,7 @@ def seek(self, loc: int, whence: int = 0):
10571059

10581060
def read(self, length: int = -1):
10591061
read_args = (length,) if length >= 0 else ()
1060-
if self.response is None or self.response.raw.isclosed():
1062+
if self.response is None:
10611063
url = hf_hub_url(
10621064
repo_id=self.resolved_path.repo_id,
10631065
revision=self.resolved_path.revision,

tests/test_hf_file_system.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,13 @@ def test_read_file(self):
175175
self.assertIsInstance(f, io.TextIOWrapper)
176176
self.assertIsInstance(f.buffer, HfFileSystemFile)
177177
self.assertEqual(f.read(), "dummy text data")
178+
self.assertEqual(f.read(), "")
178179

179180
def test_stream_file(self):
180181
with self.hffs.open(self.hf_path + "/data/binary_data.bin", block_size=0) as f:
181182
self.assertIsInstance(f, HfFileSystemStreamFile)
182183
self.assertEqual(f.read(), b"dummy binary data")
184+
self.assertEqual(f.read(), b"")
183185

184186
def test_stream_file_retry(self):
185187
with self.hffs.open(self.hf_path + "/data/binary_data.bin", block_size=0) as f:

0 commit comments

Comments
 (0)