From 5c612c421c4e7cf436de9d637b86eda47d15b28b Mon Sep 17 00:00:00 2001 From: Andy Babic Date: Wed, 19 Apr 2023 22:29:54 +0100 Subject: [PATCH 1/4] Make Local instances clearable (without deleting) --- asgiref/local.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/asgiref/local.py b/asgiref/local.py index 17314d4e..95650e22 100644 --- a/asgiref/local.py +++ b/asgiref/local.py @@ -84,7 +84,7 @@ def _get_storage(self): self._context_refs.add(context_obj) return getattr(context_obj, self._attr_name) - def __del__(self): + def clear(self): try: for context_obj in self._context_refs: try: @@ -96,6 +96,9 @@ def __del__(self): # to _IterationGuard being None. pass + def __del__(self): + self.clear() + def __getattr__(self, key): with self._thread_lock: storage = self._get_storage() From bf84d7b151ccfe90cdff873b01ccfa92859bd048 Mon Sep 17 00:00:00 2001 From: Andy Babic Date: Thu, 20 Apr 2023 12:58:26 +0100 Subject: [PATCH 2/4] Update tests --- tests/test_local.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/tests/test_local.py b/tests/test_local.py index 035c99f9..7d8504a0 100644 --- a/tests/test_local.py +++ b/tests/test_local.py @@ -307,7 +307,7 @@ def run(self): assert test_local.counter == 6 -def test_local_del_swallows_type_error(monkeypatch): +def test_local_clear_swallows_type_error(monkeypatch): test_local = Local() blow_up_calls = 0 @@ -319,6 +319,22 @@ def blow_up(self): monkeypatch.setattr("weakref.WeakSet.__iter__", blow_up) - test_local.__del__() + test_local.clear() assert blow_up_calls == 1 + + +def test_local_clears_on_deletion(monkeypatch): + test_local = Local() + + clear_patched_call_count = 0 + + def clear_patched(self): + nonlocal clear_patched_call_count + clear_patched_call_count += 1 + + monkeypatch.setattr(test_local.clear, clear_patched) + + del test_local + + assert clear_patched_call_count == 1 From d287a88e61693cc564c71b3308f28aef0e447817 Mon Sep 17 00:00:00 2001 From: Andy Babic Date: Fri, 21 Apr 2023 06:40:48 +0100 Subject: [PATCH 3/4] Use monkeypatch correctly --- tests/test_local.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_local.py b/tests/test_local.py index 7d8504a0..1e8978c5 100644 --- a/tests/test_local.py +++ b/tests/test_local.py @@ -333,7 +333,7 @@ def clear_patched(self): nonlocal clear_patched_call_count clear_patched_call_count += 1 - monkeypatch.setattr(test_local.clear, clear_patched) + monkeypatch.setattr(test_local, "clear", clear_patched) del test_local From eba8944838a972e49742cb48fd536a037d33abc3 Mon Sep 17 00:00:00 2001 From: Andy Babic Date: Wed, 3 May 2023 19:01:22 +0100 Subject: [PATCH 4/4] Switch back to invoking __del__() directly --- tests/test_local.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_local.py b/tests/test_local.py index 1e8978c5..330cc12d 100644 --- a/tests/test_local.py +++ b/tests/test_local.py @@ -335,6 +335,6 @@ def clear_patched(self): monkeypatch.setattr(test_local, "clear", clear_patched) - del test_local + test_local.__del__() assert clear_patched_call_count == 1