def test_signal_weakref_context_manager_delete_during_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2, 3] del foo del foo2 gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4]
def test_signal_weakref_context_manager_delete_during(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] with on_ctx_test(before=3, after=4): assert result == [1, 2, 3] del foo gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4]
def test_signal_weakref_context_manager_delete_after(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] del foo gc.collect() with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2]
def test_ctx_with_identifier(): class Foo(): @contextmanager def on_ctx_test_identifier(self, before, after): self.result.append(before) yield self.result.append(after) f1 = Foo() f1.obj = 'obj1' f1.result = [] f2 = Foo() f2.obj = 'obj2' f2.result = [] register_object(f1) register_object(f2) with on_ctx_test_identifier(before=1, after=2, obj='obj1'): assert f1.result == [1] assert f2.result == [] assert f1.result == [1, 2] assert f2.result == [] with on_ctx_test_identifier(before=3, after=4, obj='obj2'): assert f1.result == [1, 2] assert f2.result == [3] assert f1.result == [1, 2] assert f2.result == [3, 4]
def test_signal_weakref_complex_descriptors(): import gc from easypy.lockstep import lockstep class Foo: @lockstep def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c')
def test_signal_weakref_with_identifier(): import gc class Foo: def on_test_identifier(self, a, b): a / b foo = Foo() foo.obj = 'obj' register_object(foo) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') del foo gc.collect() on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj')
def test_signal_weakref(): """ Test that signals handlers of methods are deleted when their objects get collected """ import gc class Foo: def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c')
def test_ctx(): result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) assert result == [] with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] unregister_object(foo) with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2]