1 from __future__
import annotations
6 from pybind11_tests
import ConstructorStats
7 from pybind11_tests
import call_policies
as m
10 @pytest.mark.xfail(
"env.PYPY", reason=
"sometimes comes out 1 off on PyPy", strict=
False)
12 n_inst = ConstructorStats.detail_reg_inst()
15 assert capture ==
"Allocating parent."
18 assert ConstructorStats.detail_reg_inst() == n_inst + 1
28 assert ConstructorStats.detail_reg_inst() == n_inst
29 assert capture ==
"Releasing parent."
33 assert capture ==
"Allocating parent."
35 p.addChildKeepAlive(m.Child())
36 assert ConstructorStats.detail_reg_inst() == n_inst + 2
37 assert capture ==
"Allocating child."
40 assert ConstructorStats.detail_reg_inst() == n_inst
51 assert ConstructorStats.detail_reg_inst() == n_inst + 2
54 assert ConstructorStats.detail_reg_inst() == n_inst + 2
56 assert ConstructorStats.detail_reg_inst() == n_inst
58 with pytest.raises(RuntimeError)
as excinfo:
60 assert str(excinfo.value) ==
"Could not activate keep_alive!"
64 n_inst = ConstructorStats.detail_reg_inst()
67 assert capture ==
"Allocating parent."
70 assert ConstructorStats.detail_reg_inst() == n_inst + 1
80 assert ConstructorStats.detail_reg_inst() == n_inst
81 assert capture ==
"Releasing parent."
85 assert capture ==
"Allocating parent."
87 p.returnChildKeepAlive()
88 assert ConstructorStats.detail_reg_inst() == n_inst + 2
89 assert capture ==
"Allocating child."
92 assert ConstructorStats.detail_reg_inst() == n_inst
102 assert ConstructorStats.detail_reg_inst() == n_inst + 1
104 m.Parent.staticFunction(p)
105 assert ConstructorStats.detail_reg_inst() == n_inst + 2
106 assert capture ==
"Allocating child."
109 assert ConstructorStats.detail_reg_inst() == n_inst
120 @pytest.mark.xfail(
"env.PYPY", reason=
"_PyObject_GetDictPtr is unimplemented")
122 n_inst = ConstructorStats.detail_reg_inst()
124 p.addChildKeepAlive(m.Child())
125 assert ConstructorStats.detail_reg_inst() == n_inst + 2
130 assert ConstructorStats.detail_reg_inst() == n_inst
141 class Derived(m.Parent):
144 n_inst = ConstructorStats.detail_reg_inst()
146 p.addChildKeepAlive(m.Child())
147 assert ConstructorStats.detail_reg_inst() == n_inst + 2
152 assert ConstructorStats.detail_reg_inst() == n_inst
163 class Derived(m.Parent, m.Child):
165 m.Parent.__init__(self)
166 m.Child.__init__(self)
168 n_inst = ConstructorStats.detail_reg_inst()
170 p.addChildKeepAlive(m.Child())
172 assert ConstructorStats.detail_reg_inst() == n_inst + 3
177 assert ConstructorStats.detail_reg_inst() == n_inst
189 n_inst = ConstructorStats.detail_reg_inst()
192 assert capture ==
"Allocating parent."
194 p.returnNullChildKeepAliveChild()
195 assert ConstructorStats.detail_reg_inst() == n_inst + 1
199 assert ConstructorStats.detail_reg_inst() == n_inst
200 assert capture ==
"Releasing parent."
204 assert capture ==
"Allocating parent."
206 p.returnNullChildKeepAliveParent()
207 assert ConstructorStats.detail_reg_inst() == n_inst + 1
211 assert ConstructorStats.detail_reg_inst() == n_inst
212 assert capture ==
"Releasing parent."
216 n_inst = ConstructorStats.detail_reg_inst()
219 p = m.Parent(m.Child())
220 assert ConstructorStats.detail_reg_inst() == n_inst + 2
230 assert ConstructorStats.detail_reg_inst() == n_inst
241 assert m.unguarded_call() ==
"unguarded"
242 assert m.guarded_call() ==
"guarded"
244 assert m.multiple_guards_correct_order() ==
"guarded & guarded"
245 assert m.multiple_guards_wrong_order() ==
"unguarded & guarded"
248 assert m.with_gil() ==
"GIL held"
249 assert m.without_gil() ==
"GIL released"