From f7041971cdf50f4a50c50556d03ebe3cbd98e137 Mon Sep 17 00:00:00 2001 From: Guido Imperiale Date: Wed, 18 Sep 2019 14:17:51 +0100 Subject: [PATCH 1/4] Allow weakref --- doc/whats-new.rst | 8 ++++++++ xarray/core/dataarray.py | 10 +++++++++- xarray/core/dataset.py | 1 + xarray/tests/test_dataarray.py | 10 ++++++++++ xarray/tests/test_dataset.py | 10 ++++++++++ 5 files changed, 38 insertions(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 42da3fa8e63..a65ebfb7c7f 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -20,6 +20,14 @@ v0.13.1 (unreleased) .. _whats-new.0.13.0: +Bug fixes +~~~~~~~~~ +- Reintroduce support for :mod:`weakref` (broken in v0.13.0). Support has been + reinstated for :class:`DataArray` and :class:`Dataset` objects only. Internal xarray + objects remain unaddressable by weakref in order to save RAM. + (:issue:`3317`) by `Guido Imperiale `_. + + v0.13.0 (17 Sep 2019) --------------------- diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index d9e98839419..13a3a507c7b 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -248,7 +248,15 @@ class DataArray(AbstractArray, DataWithCoords): Dictionary for holding arbitrary metadata. """ - __slots__ = ("_accessors", "_coords", "_file_obj", "_name", "_indexes", "_variable") + __slots__ = ( + "_accessors", + "_coords", + "_file_obj", + "_name", + "_indexes", + "_variable", + "__weakref__", + ) _groupby_cls = groupby.DataArrayGroupBy _rolling_cls = rolling.DataArrayRolling diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 693e94e22dd..3ba1bd9e3d8 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -420,6 +420,7 @@ class Dataset(Mapping, ImplementsDatasetReduce, DataWithCoords): "_file_obj", "_indexes", "_variables", + "__weakref__", ) _groupby_cls = groupby.DatasetGroupBy diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 01e92bdd7be..8ba66760c9c 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -4672,3 +4672,13 @@ class MyArray(DataArray): pass assert str(e.value) == "MyArray must explicitly define __slots__" + + +def test_weakref(): + """Classes with __slots__ are incompatible with the weakref module unless they + explicitly state __weakref__ among their slots + """ + from weakref import ref + a = DataArray(1) + r = ref(a) + assert r() is a diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 7d2b11d02c9..9a34593389c 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -5786,3 +5786,13 @@ class MyDS(Dataset): pass assert str(e.value) == "MyDS must explicitly define __slots__" + + +def test_weakref(): + """Classes with __slots__ are incompatible with the weakref module unless they + explicitly state __weakref__ among their slots + """ + from weakref import ref + ds = Dataset() + r = ref(ds) + assert r() is ds From b74e615c52ac3fc580f65a3571c1cd54a975b250 Mon Sep 17 00:00:00 2001 From: Guido Imperiale Date: Wed, 18 Sep 2019 14:19:04 +0100 Subject: [PATCH 2/4] black --- xarray/tests/test_dataarray.py | 1 + xarray/tests/test_dataset.py | 1 + 2 files changed, 2 insertions(+) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 8ba66760c9c..9ba3eecc5a0 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -4679,6 +4679,7 @@ def test_weakref(): explicitly state __weakref__ among their slots """ from weakref import ref + a = DataArray(1) r = ref(a) assert r() is a diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 9a34593389c..f02990a1be9 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -5793,6 +5793,7 @@ def test_weakref(): explicitly state __weakref__ among their slots """ from weakref import ref + ds = Dataset() r = ref(ds) assert r() is ds From 1d86f4d00f27e0643ca4313dbd7241073ff644eb Mon Sep 17 00:00:00 2001 From: Guido Imperiale Date: Wed, 18 Sep 2019 14:20:09 +0100 Subject: [PATCH 3/4] What's New tweak --- doc/whats-new.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index a65ebfb7c7f..6a1dd7972dc 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -18,8 +18,6 @@ What's New v0.13.1 (unreleased) -------------------- -.. _whats-new.0.13.0: - Bug fixes ~~~~~~~~~ - Reintroduce support for :mod:`weakref` (broken in v0.13.0). Support has been @@ -28,6 +26,8 @@ Bug fixes (:issue:`3317`) by `Guido Imperiale `_. +.. _whats-new.0.13.0: + v0.13.0 (17 Sep 2019) --------------------- From cda02a58ada386307dedc9499c3ae67c4994d847 Mon Sep 17 00:00:00 2001 From: crusaderky Date: Wed, 18 Sep 2019 16:29:10 +0100 Subject: [PATCH 4/4] Update doc/whats-new.rst Co-Authored-By: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> --- doc/whats-new.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 6a1dd7972dc..eeb768224e6 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -22,7 +22,7 @@ Bug fixes ~~~~~~~~~ - Reintroduce support for :mod:`weakref` (broken in v0.13.0). Support has been reinstated for :class:`DataArray` and :class:`Dataset` objects only. Internal xarray - objects remain unaddressable by weakref in order to save RAM. + objects remain unaddressable by weakref in order to save memory. (:issue:`3317`) by `Guido Imperiale `_.