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


gtsam
Author(s):
autogenerated on Thu Jul 4 2024 03:06:00