test_call_policies.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 import pytest
3 
4 import env # noqa: F401
5 
6 from pybind11_tests import call_policies as m
7 from pybind11_tests import ConstructorStats
8 
9 
10 @pytest.mark.xfail("env.PYPY", reason="sometimes comes out 1 off on PyPy", strict=False)
12  n_inst = ConstructorStats.detail_reg_inst()
13  with capture:
14  p = m.Parent()
15  assert capture == "Allocating parent."
16  with capture:
17  p.addChild(m.Child())
18  assert ConstructorStats.detail_reg_inst() == n_inst + 1
19  assert capture == """
20  Allocating child.
21  Releasing child.
22  """
23  with capture:
24  del p
25  assert ConstructorStats.detail_reg_inst() == n_inst
26  assert capture == "Releasing parent."
27 
28  with capture:
29  p = m.Parent()
30  assert capture == "Allocating parent."
31  with capture:
32  p.addChildKeepAlive(m.Child())
33  assert ConstructorStats.detail_reg_inst() == n_inst + 2
34  assert capture == "Allocating child."
35  with capture:
36  del p
37  assert ConstructorStats.detail_reg_inst() == n_inst
38  assert capture == """
39  Releasing parent.
40  Releasing child.
41  """
42 
43 
45  n_inst = ConstructorStats.detail_reg_inst()
46  with capture:
47  p = m.Parent()
48  assert capture == "Allocating parent."
49  with capture:
50  p.returnChild()
51  assert ConstructorStats.detail_reg_inst() == n_inst + 1
52  assert capture == """
53  Allocating child.
54  Releasing child.
55  """
56  with capture:
57  del p
58  assert ConstructorStats.detail_reg_inst() == n_inst
59  assert capture == "Releasing parent."
60 
61  with capture:
62  p = m.Parent()
63  assert capture == "Allocating parent."
64  with capture:
65  p.returnChildKeepAlive()
66  assert ConstructorStats.detail_reg_inst() == n_inst + 2
67  assert capture == "Allocating child."
68  with capture:
69  del p
70  assert ConstructorStats.detail_reg_inst() == n_inst
71  assert capture == """
72  Releasing parent.
73  Releasing child.
74  """
75 
76 
77 # https://foss.heptapod.net/pypy/pypy/-/issues/2447
78 @pytest.mark.xfail("env.PYPY", reason="_PyObject_GetDictPtr is unimplemented")
79 def test_alive_gc(capture):
80  n_inst = ConstructorStats.detail_reg_inst()
81  p = m.ParentGC()
82  p.addChildKeepAlive(m.Child())
83  assert ConstructorStats.detail_reg_inst() == n_inst + 2
84  lst = [p]
85  lst.append(lst) # creates a circular reference
86  with capture:
87  del p, lst
88  assert ConstructorStats.detail_reg_inst() == n_inst
89  assert capture == """
90  Releasing parent.
91  Releasing child.
92  """
93 
94 
95 def test_alive_gc_derived(capture):
96  class Derived(m.Parent):
97  pass
98 
99  n_inst = ConstructorStats.detail_reg_inst()
100  p = Derived()
101  p.addChildKeepAlive(m.Child())
102  assert ConstructorStats.detail_reg_inst() == n_inst + 2
103  lst = [p]
104  lst.append(lst) # creates a circular reference
105  with capture:
106  del p, lst
107  assert ConstructorStats.detail_reg_inst() == n_inst
108  assert capture == """
109  Releasing parent.
110  Releasing child.
111  """
112 
113 
115  class Derived(m.Parent, m.Child):
116  def __init__(self):
117  m.Parent.__init__(self)
118  m.Child.__init__(self)
119 
120  n_inst = ConstructorStats.detail_reg_inst()
121  p = Derived()
122  p.addChildKeepAlive(m.Child())
123  # +3 rather than +2 because Derived corresponds to two registered instances
124  assert ConstructorStats.detail_reg_inst() == n_inst + 3
125  lst = [p]
126  lst.append(lst) # creates a circular reference
127  with capture:
128  del p, lst
129  assert ConstructorStats.detail_reg_inst() == n_inst
130  assert capture == """
131  Releasing parent.
132  Releasing child.
133  Releasing child.
134  """
135 
136 
137 def test_return_none(capture):
138  n_inst = ConstructorStats.detail_reg_inst()
139  with capture:
140  p = m.Parent()
141  assert capture == "Allocating parent."
142  with capture:
143  p.returnNullChildKeepAliveChild()
144  assert ConstructorStats.detail_reg_inst() == n_inst + 1
145  assert capture == ""
146  with capture:
147  del p
148  assert ConstructorStats.detail_reg_inst() == n_inst
149  assert capture == "Releasing parent."
150 
151  with capture:
152  p = m.Parent()
153  assert capture == "Allocating parent."
154  with capture:
155  p.returnNullChildKeepAliveParent()
156  assert ConstructorStats.detail_reg_inst() == n_inst + 1
157  assert capture == ""
158  with capture:
159  del p
160  assert ConstructorStats.detail_reg_inst() == n_inst
161  assert capture == "Releasing parent."
162 
163 
165  n_inst = ConstructorStats.detail_reg_inst()
166 
167  with capture:
168  p = m.Parent(m.Child())
169  assert ConstructorStats.detail_reg_inst() == n_inst + 2
170  assert capture == """
171  Allocating child.
172  Allocating parent.
173  """
174  with capture:
175  del p
176  assert ConstructorStats.detail_reg_inst() == n_inst
177  assert capture == """
178  Releasing parent.
179  Releasing child.
180  """
181 
182 
184  assert m.unguarded_call() == "unguarded"
185  assert m.guarded_call() == "guarded"
186 
187  assert m.multiple_guards_correct_order() == "guarded & guarded"
188  assert m.multiple_guards_wrong_order() == "unguarded & guarded"
189 
190  if hasattr(m, "with_gil"):
191  assert m.with_gil() == "GIL held"
192  assert m.without_gil() == "GIL released"
bool hasattr(handle obj, handle name)
Definition: pytypes.h:403
def test_keep_alive_constructor(capture)
def test_alive_gc_multi_derived(capture)
def test_keep_alive_return_value(capture)
def test_return_none(capture)
def test_alive_gc(capture)
def test_keep_alive_argument(capture)
def test_alive_gc_derived(capture)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:03