μHAL (v2.8.17)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
test_call_policies.py
Go to the documentation of this file.
1# -*- coding: utf-8 -*-
2import pytest
3
4import env # noqa: F401
5from pybind11_tests import ConstructorStats
6from pybind11_tests import call_policies as m
7
8
9@pytest.mark.xfail("env.PYPY", reason="sometimes comes out 1 off on PyPy", strict=False)
11 n_inst = ConstructorStats.detail_reg_inst()
12 with capture:
13 p = m.Parent()
14 assert capture == "Allocating parent."
15 with capture:
16 p.addChild(m.Child())
17 assert ConstructorStats.detail_reg_inst() == n_inst + 1
18 assert (
19 capture
20 == """
21 Allocating child.
22 Releasing child.
23 """
24 )
25 with capture:
26 del p
27 assert ConstructorStats.detail_reg_inst() == n_inst
28 assert capture == "Releasing parent."
29
30 with capture:
31 p = m.Parent()
32 assert capture == "Allocating parent."
33 with capture:
34 p.addChildKeepAlive(m.Child())
35 assert ConstructorStats.detail_reg_inst() == n_inst + 2
36 assert capture == "Allocating child."
37 with capture:
38 del p
39 assert ConstructorStats.detail_reg_inst() == n_inst
40 assert (
41 capture
42 == """
43 Releasing parent.
44 Releasing child.
45 """
46 )
47
48 p = m.Parent()
49 c = m.Child()
50 assert ConstructorStats.detail_reg_inst() == n_inst + 2
51 m.free_function(p, c)
52 del c
53 assert ConstructorStats.detail_reg_inst() == n_inst + 2
54 del p
55 assert ConstructorStats.detail_reg_inst() == n_inst
56
57 with pytest.raises(RuntimeError) as excinfo:
58 m.invalid_arg_index()
59 assert str(excinfo.value) == "Could not activate keep_alive!"
60
61
63 n_inst = ConstructorStats.detail_reg_inst()
64 with capture:
65 p = m.Parent()
66 assert capture == "Allocating parent."
67 with capture:
68 p.returnChild()
69 assert ConstructorStats.detail_reg_inst() == n_inst + 1
70 assert (
71 capture
72 == """
73 Allocating child.
74 Releasing child.
75 """
76 )
77 with capture:
78 del p
79 assert ConstructorStats.detail_reg_inst() == n_inst
80 assert capture == "Releasing parent."
81
82 with capture:
83 p = m.Parent()
84 assert capture == "Allocating parent."
85 with capture:
86 p.returnChildKeepAlive()
87 assert ConstructorStats.detail_reg_inst() == n_inst + 2
88 assert capture == "Allocating child."
89 with capture:
90 del p
91 assert ConstructorStats.detail_reg_inst() == n_inst
92 assert (
93 capture
94 == """
95 Releasing parent.
96 Releasing child.
97 """
98 )
99
100 p = m.Parent()
101 assert ConstructorStats.detail_reg_inst() == n_inst + 1
102 with capture:
103 m.Parent.staticFunction(p)
104 assert ConstructorStats.detail_reg_inst() == n_inst + 2
105 assert capture == "Allocating child."
106 with capture:
107 del p
108 assert ConstructorStats.detail_reg_inst() == n_inst
109 assert (
110 capture
111 == """
112 Releasing parent.
113 Releasing child.
114 """
115 )
116
117
118# https://foss.heptapod.net/pypy/pypy/-/issues/2447
119@pytest.mark.xfail("env.PYPY", reason="_PyObject_GetDictPtr is unimplemented")
120def test_alive_gc(capture):
121 n_inst = ConstructorStats.detail_reg_inst()
122 p = m.ParentGC()
123 p.addChildKeepAlive(m.Child())
124 assert ConstructorStats.detail_reg_inst() == n_inst + 2
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 (
131 capture
132 == """
133 Releasing parent.
134 Releasing child.
135 """
136 )
137
138
140 class Derived(m.Parent):
141 pass
142
143 n_inst = ConstructorStats.detail_reg_inst()
144 p = Derived()
145 p.addChildKeepAlive(m.Child())
146 assert ConstructorStats.detail_reg_inst() == n_inst + 2
147 lst = [p]
148 lst.append(lst) # creates a circular reference
149 with capture:
150 del p, lst
151 assert ConstructorStats.detail_reg_inst() == n_inst
152 assert (
153 capture
154 == """
155 Releasing parent.
156 Releasing child.
157 """
158 )
159
160
162 class Derived(m.Parent, m.Child):
163 def __init__(self):
164 m.Parent.__init__(self)
165 m.Child.__init__(self)
166
167 n_inst = ConstructorStats.detail_reg_inst()
168 p = Derived()
169 p.addChildKeepAlive(m.Child())
170 # +3 rather than +2 because Derived corresponds to two registered instances
171 assert ConstructorStats.detail_reg_inst() == n_inst + 3
172 lst = [p]
173 lst.append(lst) # creates a circular reference
174 with capture:
175 del p, lst
176 assert ConstructorStats.detail_reg_inst() == n_inst
177 assert (
178 capture
179 == """
180 Releasing parent.
181 Releasing child.
182 Releasing child.
183 """
184 )
185
186
187def test_return_none(capture):
188 n_inst = ConstructorStats.detail_reg_inst()
189 with capture:
190 p = m.Parent()
191 assert capture == "Allocating parent."
192 with capture:
193 p.returnNullChildKeepAliveChild()
194 assert ConstructorStats.detail_reg_inst() == n_inst + 1
195 assert capture == ""
196 with capture:
197 del p
198 assert ConstructorStats.detail_reg_inst() == n_inst
199 assert capture == "Releasing parent."
200
201 with capture:
202 p = m.Parent()
203 assert capture == "Allocating parent."
204 with capture:
205 p.returnNullChildKeepAliveParent()
206 assert ConstructorStats.detail_reg_inst() == n_inst + 1
207 assert capture == ""
208 with capture:
209 del p
210 assert ConstructorStats.detail_reg_inst() == n_inst
211 assert capture == "Releasing parent."
212
213
215 n_inst = ConstructorStats.detail_reg_inst()
216
217 with capture:
218 p = m.Parent(m.Child())
219 assert ConstructorStats.detail_reg_inst() == n_inst + 2
220 assert (
221 capture
222 == """
223 Allocating child.
224 Allocating parent.
225 """
226 )
227 with capture:
228 del p
229 assert ConstructorStats.detail_reg_inst() == n_inst
230 assert (
231 capture
232 == """
233 Releasing parent.
234 Releasing child.
235 """
236 )
237
238
240 assert m.unguarded_call() == "unguarded"
241 assert m.guarded_call() == "guarded"
242
243 assert m.multiple_guards_correct_order() == "guarded & guarded"
244 assert m.multiple_guards_wrong_order() == "unguarded & guarded"
245
246 if hasattr(m, "with_gil"):
247 assert m.with_gil() == "GIL held"
248 assert m.without_gil() == "GIL released"
Definition: pytypes.h:1200
bool hasattr(handle obj, handle name)
Definition: pytypes.h:517
def test_alive_gc(capture)
def test_keep_alive_constructor(capture)
def test_keep_alive_return_value(capture)
def test_alive_gc_derived(capture)
def test_keep_alive_argument(capture)
def test_return_none(capture)
def test_alive_gc_multi_derived(capture)