test_custom_type_setup.py
Go to the documentation of this file.
1 import gc
2 import weakref
3 
4 import pytest
5 
6 import env # noqa: F401
7 from pybind11_tests import custom_type_setup as m
8 
9 
10 @pytest.fixture()
11 def gc_tester():
12  """Tests that an object is garbage collected.
13 
14  Assumes that any unreferenced objects are fully collected after calling
15  `gc.collect()`. That is true on CPython, but does not appear to reliably
16  hold on PyPy.
17  """
18 
19  weak_refs = []
20 
21  def add_ref(obj):
22  # PyPy does not support `gc.is_tracked`.
23  if hasattr(gc, "is_tracked"):
24  assert gc.is_tracked(obj)
25  weak_refs.append(weakref.ref(obj))
26 
27  yield add_ref
28 
29  gc.collect()
30  for ref in weak_refs:
31  assert ref() is None
32 
33 
34 # PyPy does not seem to reliably garbage collect.
35 @pytest.mark.skipif("env.PYPY")
36 def test_self_cycle(gc_tester):
37  obj = m.OwnsPythonObjects()
38  obj.value = obj
39  gc_tester(obj)
40 
41 
42 # PyPy does not seem to reliably garbage collect.
43 @pytest.mark.skipif("env.PYPY")
44 def test_indirect_cycle(gc_tester):
45  obj = m.OwnsPythonObjects()
46  obj_list = [obj]
47  obj.value = obj_list
48  gc_tester(obj)
test_custom_type_setup.gc_tester
def gc_tester()
Definition: test_custom_type_setup.py:11
hasattr
bool hasattr(handle obj, handle name)
Definition: pytypes.h:853
test_custom_type_setup.test_self_cycle
def test_self_cycle(gc_tester)
Definition: test_custom_type_setup.py:36
ref
Reference counting helper.
Definition: object.h:67
test_custom_type_setup.test_indirect_cycle
def test_indirect_cycle(gc_tester)
Definition: test_custom_type_setup.py:44


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:05:28