From 6e553d61be75c212a07305083ebe768bec5f70ef Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Sun, 19 Jan 2025 06:07:46 -0500 Subject: [PATCH] Fix additional architecture-specific bugs --- ...st_doc_example-on-big-endian-systems.patch | 34 ++++++++++++ ...afe-casts-from-float-to-unsigned-int.patch | 53 +++++++++++++++++++ python-xarray.spec | 5 ++ 3 files changed, 92 insertions(+) create mode 100644 0001-Fix-test_doc_example-on-big-endian-systems.patch create mode 100644 0002-Avoid-unsafe-casts-from-float-to-unsigned-int.patch diff --git a/0001-Fix-test_doc_example-on-big-endian-systems.patch b/0001-Fix-test_doc_example-on-big-endian-systems.patch new file mode 100644 index 0000000..d6ea5ea --- /dev/null +++ b/0001-Fix-test_doc_example-on-big-endian-systems.patch @@ -0,0 +1,34 @@ +From f41100ae4202dce5891854143ba3a31f4e2d5a6d Mon Sep 17 00:00:00 2001 +From: Elliott Sales de Andrade +Date: Tue, 14 Jan 2025 02:46:39 -0500 +Subject: [PATCH 1/2] Fix test_doc_example on big-endian systems + +... by using a fixed-endian input. + +Signed-off-by: Elliott Sales de Andrade +--- + xarray/tests/test_datatree.py | 8 ++++++-- + 1 file changed, 6 insertions(+), 2 deletions(-) + +diff --git a/xarray/tests/test_datatree.py b/xarray/tests/test_datatree.py +index 7b295128..21870050 100644 +--- a/xarray/tests/test_datatree.py ++++ b/xarray/tests/test_datatree.py +@@ -1240,8 +1240,12 @@ class TestRepr: + ) + def test_doc_example(self) -> None: + # regression test for https://github.com/pydata/xarray/issues/9499 +- time = xr.DataArray(data=["2022-01", "2023-01"], dims="time") +- stations = xr.DataArray(data=list("abcdef"), dims="station") ++ time = xr.DataArray( ++ data=np.array(["2022-01", "2023-01"], dtype=" +Date: Sun, 19 Jan 2025 04:47:23 -0500 +Subject: [PATCH 2/2] Avoid unsafe casts from float to unsigned int + +Fixes #9815 + +Signed-off-by: Elliott Sales de Andrade +--- + xarray/coding/variables.py | 5 ++++- + xarray/core/duck_array_ops.py | 10 ++++++++++ + 2 files changed, 14 insertions(+), 1 deletion(-) + +diff --git a/xarray/coding/variables.py b/xarray/coding/variables.py +index 8154f044..355f3b0b 100644 +--- a/xarray/coding/variables.py ++++ b/xarray/coding/variables.py +@@ -426,7 +426,10 @@ class CFMaskCoder(VariableCoder): + if fill_value is not None and has_unsigned: + pop_to(encoding, attrs, "_Unsigned") + # XXX: Is this actually needed? Doesn't the backend handle this? +- data = duck_array_ops.astype(duck_array_ops.around(data), dtype) ++ signed_dtype = np.dtype(f"i{dtype.itemsize}") ++ data = duck_array_ops.view( ++ duck_array_ops.astype(duck_array_ops.around(data), signed_dtype), dtype ++ ) + attrs["_FillValue"] = fill_value + + return Variable(dims, data, attrs, encoding, fastpath=True) +diff --git a/xarray/core/duck_array_ops.py b/xarray/core/duck_array_ops.py +index 7e7333fd..be842b66 100644 +--- a/xarray/core/duck_array_ops.py ++++ b/xarray/core/duck_array_ops.py +@@ -236,6 +236,16 @@ def astype(data, dtype, **kwargs): + return data.astype(dtype, **kwargs) + + ++def view(data, *args, **kwargs): ++ if hasattr(data, "__array_namespace__"): ++ xp = get_array_namespace(data) ++ if xp == np: ++ # numpy currently doesn't have a view: ++ return data.view(*args, **kwargs) ++ return xp.view(data, *args, **kwargs) ++ return data.view(*args, **kwargs) ++ ++ + def asarray(data, xp=np, dtype=None): + converted = data if is_duck_array(data) else xp.asarray(data) + +-- +2.47.0 + diff --git a/python-xarray.spec b/python-xarray.spec index bfa2384..601ed47 100644 --- a/python-xarray.spec +++ b/python-xarray.spec @@ -12,6 +12,11 @@ Source: %pypi_source %{srcname} # Fix test_dask_da_groupby_quantile. Patch: https://github.com/pydata/xarray/pull/9945.patch +# https://github.com/pydata/xarray/pull/9949 +Patch: 0001-Fix-test_doc_example-on-big-endian-systems.patch +# https://github.com/pydata/xarray/pull/9964 +Patch: 0002-Avoid-unsafe-casts-from-float-to-unsigned-int.patch + BuildArch: noarch BuildRequires: python3-devel