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

warn and return bytes undecoded in case of UnicodeDecodeError in h5netcdf-backend #8874

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 6 additions & 4 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,17 @@ Bug fixes
`CFMaskCoder`/`CFScaleOffsetCoder` (:issue:`2304`, :issue:`5597`,
:issue:`7691`, :pull:`8713`, see also discussion in :pull:`7654`).
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.
- do not cast `_FillValue`/`missing_value` in `CFMaskCoder` if `_Unsigned` is provided
- Do not cast `_FillValue`/`missing_value` in `CFMaskCoder` if `_Unsigned` is provided
(:issue:`8844`, :pull:`8852`).
- Adapt handling of copy keyword argument for numpy >= 2.0dev
(:issue:`8844`, :pull:`8851`, :pull:`8865``).
(:issue:`8844`, :pull:`8851`, :pull:`8865`).
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.
- import trapz/trapezoid depending on numpy version.
- Import trapz/trapezoid depending on numpy version
(:issue:`8844`, :pull:`8865`).
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.

- Warn and return bytes undecoded in case of UnicodeDecodeError in h5netcdf-backend
(:issue:`5563`, :pull:`8874`).
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.


Documentation
Expand Down
12 changes: 9 additions & 3 deletions xarray/backends/h5netcdf_.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from xarray.core import indexing
from xarray.core.utils import (
FrozenDict,
emit_user_level_warning,
is_remote_uri,
read_magic_number_from_file,
try_read_magic_number_from_file_or_path,
Expand Down Expand Up @@ -60,9 +61,14 @@ def _getitem(self, key):

def maybe_decode_bytes(txt):
if isinstance(txt, bytes):
return txt.decode("utf-8")
else:
return txt
try:
kmuehlbauer marked this conversation as resolved.
Show resolved Hide resolved
return txt.decode("utf-8")
except UnicodeDecodeError:
emit_user_level_warning(
"'utf-8' codec can't decode bytes, " "returning bytes undecoded.",
UnicodeWarning,
)
return txt


def _read_attributes(h5netcdf_var):
Expand Down
9 changes: 9 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -3560,6 +3560,15 @@ def test_dump_encodings_h5py(self) -> None:
assert actual.x.encoding["compression"] == "lzf"
assert actual.x.encoding["compression_opts"] is None

def test_decode_utf8_warning(self) -> None:
title = b"\xc3"
with create_tmp_file() as tmp_file:
with nc4.Dataset(tmp_file, "w") as f:
f.title = title
with pytest.warns(UnicodeWarning, match="returning bytes undecoded"):
ds = xr.load_dataset(tmp_file, engine="h5netcdf")
assert ds.title == title


@requires_h5netcdf
@requires_netCDF4
Expand Down
Loading