Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset file pointer to 0 when reading file stream #7304

Merged
merged 8 commits into from
Dec 1, 2022
Merged
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 doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ Bug fixes
By `Michael Niklas <https://github.com/headtr1ck>`_.
- Fix static typing of :py:meth:`xr.polyval` (:issue:`7312`, :pull:`7315`).
By `Michael Niklas <https://github.com/headtr1ck>`_.
- Fix multiple reads on fsspec S3 files by resetting file pointer to 0 when reading file streams (:issue:`6813`, :pull:`7304`).
By `David Hoese <https://github.com/djhoese>`_ and `Wei Ji Leong <https://github.com/weiji14>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
8 changes: 2 additions & 6 deletions xarray/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,15 +654,11 @@ def read_magic_number_from_file(filename_or_obj, count=8) -> bytes:
magic_number = filename_or_obj[:count]
elif isinstance(filename_or_obj, io.IOBase):
if filename_or_obj.tell() != 0:
raise ValueError(
"cannot guess the engine, "
"file-like object read/write pointer not at the start of the file, "
"please close and reopen, or use a context manager"
)
filename_or_obj.seek(0)
magic_number = filename_or_obj.read(count)
filename_or_obj.seek(0)
else:
raise TypeError(f"cannot read the magic number form {type(filename_or_obj)}")
raise TypeError(f"cannot read the magic number from {type(filename_or_obj)}")
return magic_number


Expand Down
21 changes: 6 additions & 15 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -3159,13 +3159,12 @@ def test_open_badbytes(self) -> None:
def test_open_twice(self) -> None:
expected = create_test_data()
expected.attrs["foo"] = "bar"
with pytest.raises(ValueError, match=r"read/write pointer not at the start"):
with create_tmp_file() as tmp_file:
expected.to_netcdf(tmp_file, engine="h5netcdf")
with open(tmp_file, "rb") as f:
with create_tmp_file() as tmp_file:
expected.to_netcdf(tmp_file, engine="h5netcdf")
with open(tmp_file, "rb") as f:
with open_dataset(f, engine="h5netcdf"):
with open_dataset(f, engine="h5netcdf"):
with open_dataset(f, engine="h5netcdf"):
pass
pass

@requires_scipy
def test_open_fileobj(self) -> None:
Expand Down Expand Up @@ -3197,15 +3196,7 @@ def test_open_fileobj(self) -> None:
# `raises_regex`?). Ref https://github.com/pydata/xarray/pull/5191
with open(tmp_file, "rb") as f:
f.seek(8)
with pytest.raises(
ValueError,
match="match in any of xarray's currently installed IO",
):
with pytest.warns(
RuntimeWarning,
match=re.escape("'h5netcdf' fails while guessing"),
):
open_dataset(f)
open_dataset(f)


@requires_h5netcdf
Expand Down