Skip to content

Commit

Permalink
merge maybe_decode_bytes function into _read_attributes, add attribut…
Browse files Browse the repository at this point in the history
…e and variable name to warning
  • Loading branch information
kmuehlbauer committed Mar 25, 2024
1 parent 9fd4f58 commit e13b586
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
23 changes: 10 additions & 13 deletions xarray/backends/h5netcdf_.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,23 @@ def _getitem(self, key):
return array[key]


def maybe_decode_bytes(txt):
if isinstance(txt, bytes):
try:
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):
# GH451
# to ensure conventions decoding works properly on Python 3, decode all
# bytes attributes to strings
attrs = {}
for k, v in h5netcdf_var.attrs.items():
if k not in ["_FillValue", "missing_value"]:
v = maybe_decode_bytes(v)
if isinstance(v, bytes):
try:
v = v.decode("utf-8")
except UnicodeDecodeError:
emit_user_level_warning(
f"'utf-8' codec can't decode bytes for attribute "
f"{k!r} of h5netcdf object {h5netcdf_var.name!r}, "
f"returning bytes undecoded.",
UnicodeWarning,
)
attrs[k] = v
return attrs

Expand Down
3 changes: 2 additions & 1 deletion xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -3565,9 +3565,10 @@ def test_decode_utf8_warning(self) -> None:
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"):
with pytest.warns(UnicodeWarning, match="returning bytes undecoded") as w:
ds = xr.load_dataset(tmp_file, engine="h5netcdf")
assert ds.title == title
assert "attribute 'title' of h5netcdf object '/'" in str(w[0].message)


@requires_h5netcdf
Expand Down

0 comments on commit e13b586

Please sign in to comment.