Skip to content

Commit

Permalink
Fix read_bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisburr committed Mar 3, 2024
1 parent 551692a commit b7515a7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/fsspec_xrootd/xrootd.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,9 @@ async def _ls(self, path: str, detail: bool = True, **kwargs: Any) -> list[Any]:
else:
return [os.path.basename(item["name"].rstrip("/")) for item in listing]

async def _cat_file(self, path: str, start: int, end: int, **kwargs: Any) -> Any:
async def _cat_file(
self, path: str, start: int | None, end: int | None, **kwargs: Any
) -> Any:
_myFile = client.File()
try:
status, _n = await _async_wrap(
Expand All @@ -397,10 +399,15 @@ async def _cat_file(self, path: str, start: int, end: int, **kwargs: Any) -> Any
)
if not status.ok:
raise OSError(f"File failed to read: {status.message}")

n_bytes = end
if start is not None and end is not None:
n_bytes = end - start

status, data = await _async_wrap(
_myFile.read,
start,
end - start,
start or 0,
n_bytes or 0,
self.timeout,
)
if not status.ok:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_basicio.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,17 @@ def test_write_fsspec(localserver, clear_server):
assert f.read() == TESTDATA1


@pytest.mark.parametrize("start, end", [(None, None), (None, 10), (1, None), (1, 10)])
def test_read_bytes_fsspec(localserver, clear_server, start, end):
remoteurl, localpath = localserver
with open(localpath + "/testfile.txt", "w") as fout:
fout.write(TESTDATA1)

fs, _ = fsspec.core.url_to_fs(remoteurl)
data = fs.read_bytes(localpath + "/testfile.txt", start=start, end=end)
assert data == TESTDATA1.encode("utf-8")[start:end]


def test_append_fsspec(localserver, clear_server):
remoteurl, localpath = localserver
with open(localpath + "/testfile.txt", "w") as fout:
Expand Down

0 comments on commit b7515a7

Please sign in to comment.