1 from __future__
import annotations
5 m = pytest.importorskip(
"pybind11_tests.smart_ptr")
6 from pybind11_tests
import ConstructorStats
11 for i, o
in enumerate(
12 [m.make_object_1(), m.make_object_2(), m.MyObject1(3)], start=1
14 assert o.getRefCount() == 1
20 assert capture == f
"MyObject1[{i}]\n" * 4
22 for i, o
in enumerate(
23 [m.make_myobject1_1(), m.make_myobject1_2(), m.MyObject1(6), 7], start=4
32 m.print_myobject1_1(o)
33 m.print_myobject1_2(o)
34 m.print_myobject1_3(o)
35 m.print_myobject1_4(o)
38 assert capture == f
"MyObject1[{i}]\n" * times
41 assert cstats.alive() == 0
42 expected_values = [f
"MyObject1[{i}]" for i
in range(1, 7)] + [
"MyObject1[7]"] * 4
43 assert cstats.values() == expected_values
44 assert cstats.default_constructions == 0
45 assert cstats.copy_constructions == 0
47 assert cstats.copy_assignments == 0
48 assert cstats.move_assignments == 0
52 [8, 6, 7], [m.MyObject2(8), m.make_myobject2_1(), m.make_myobject2_2()]
56 m.print_myobject2_1(o)
57 m.print_myobject2_2(o)
58 m.print_myobject2_3(o)
59 m.print_myobject2_4(o)
60 assert capture == f
"MyObject2[{i}]\n" * 4
63 assert cstats.alive() == 1
65 assert cstats.alive() == 0
66 assert cstats.values() == [
"MyObject2[8]",
"MyObject2[6]",
"MyObject2[7]"]
67 assert cstats.default_constructions == 0
68 assert cstats.copy_constructions == 0
70 assert cstats.copy_assignments == 0
71 assert cstats.move_assignments == 0
75 [9, 8, 9], [m.MyObject3(9), m.make_myobject3_1(), m.make_myobject3_2()]
79 m.print_myobject3_1(o)
80 m.print_myobject3_2(o)
81 m.print_myobject3_3(o)
82 m.print_myobject3_4(o)
83 assert capture == f
"MyObject3[{i}]\n" * 4
86 assert cstats.alive() == 1
88 assert cstats.alive() == 0
89 assert cstats.values() == [
"MyObject3[9]",
"MyObject3[8]",
"MyObject3[9]"]
90 assert cstats.default_constructions == 0
91 assert cstats.copy_constructions == 0
93 assert cstats.copy_assignments == 0
94 assert cstats.move_assignments == 0
98 assert cstats.alive() == 0
99 assert cstats.values() == []
100 assert cstats.default_constructions == 10
101 assert cstats.copy_constructions == 0
103 assert cstats.copy_assignments == 0
104 assert cstats.move_assignments == 0
107 cstats = m.cstats_ref()
108 assert cstats.alive() == 0
109 assert cstats.values() == [
"from pointer"] * 10
110 assert cstats.default_constructions == 30
111 assert cstats.copy_constructions == 12
113 assert cstats.copy_assignments == 30
114 assert cstats.move_assignments == 0
118 assert m.test_object1_refcounting()
125 assert cstats.alive() == 1
127 assert cstats.alive() == 1
128 m.MyObject4.cleanup_all_instances()
129 assert cstats.alive() == 0
136 assert cstats.alive() == 1
138 assert cstats.alive() == 1
139 m.MyObject4a.cleanup_all_instances()
140 assert cstats.alive() == 0
148 assert cstats4a.alive() == 2
150 assert cstats4b.alive() == 1
152 assert cstats4a.alive() == 1
153 assert cstats4b.alive() == 0
154 m.MyObject4a.cleanup_all_instances()
155 assert cstats4a.alive() == 0
156 assert cstats4b.alive() == 0
163 assert cstats.alive() == 1
165 assert cstats.alive() == 0
171 assert stats.alive() == 2
174 assert stats.alive() == 2
175 assert s.set_ref(ref)
176 with pytest.raises(RuntimeError)
as excinfo:
177 assert s.set_holder(ref)
178 assert "Unable to cast from non-held to held instance" in str(excinfo.value)
181 assert stats.alive() == 3
182 assert s.set_ref(copy)
183 assert s.set_holder(copy)
185 holder_ref = s.holder_ref
186 assert stats.alive() == 3
187 assert s.set_ref(holder_ref)
188 assert s.set_holder(holder_ref)
190 holder_copy = s.holder_copy
191 assert stats.alive() == 3
192 assert s.set_ref(holder_copy)
193 assert s.set_holder(holder_copy)
195 del ref, copy, holder_ref, holder_copy, s
196 assert stats.alive() == 0
200 s = m.SharedFromThisRef()
202 assert stats.alive() == 2
205 assert stats.alive() == 2
206 assert s.set_ref(ref)
212 assert stats.alive() == 2
213 assert s.set_ref(bad_wp)
214 with pytest.raises(RuntimeError)
as excinfo:
215 assert s.set_holder(bad_wp)
216 assert "Unable to cast from non-held to held instance" in str(excinfo.value)
219 assert stats.alive() == 3
220 assert s.set_ref(copy)
221 assert s.set_holder(copy)
226 assert stats.alive() == 3
227 assert s.set_ref(holder_ref)
228 assert s.set_holder(holder_ref)
233 assert stats.alive() == 3
234 assert s.set_ref(holder_copy)
235 assert s.set_holder(holder_copy)
237 del ref, bad_wp, copy, holder_ref, holder_copy, s
238 assert stats.alive() == 0
240 z = m.SharedFromThisVirt.get()
241 y = m.SharedFromThisVirt.get()
246 a = m.TypeWithMoveOnlyHolder.make()
247 b = m.TypeWithMoveOnlyHolder.make_as_object()
249 assert stats.alive() == 2
251 assert stats.alive() == 1
253 assert stats.alive() == 0
258 a = m.TypeForHolderWithAddressOf.make()
265 assert stats.alive() == 1
267 np = m.TypeForHolderWithAddressOf.make()
268 assert stats.alive() == 2
270 assert stats.alive() == 1
272 assert stats.alive() == 0
274 b = m.TypeForHolderWithAddressOf.make()
276 assert b.get()
is c.get()
277 assert stats.alive() == 1
280 assert stats.alive() == 1
283 assert stats.alive() == 0
287 a = m.TypeForMoveOnlyHolderWithAddressOf.make()
291 assert stats.alive() == 1
297 assert stats.alive() == 0
301 instance = m.HeldByDefaultHolder()
302 with pytest.raises(RuntimeError)
as excinfo:
303 m.HeldByDefaultHolder.load_shared_ptr(instance)
305 "Unable to load a custom holder type from a "
306 "default-holder instance" in str(excinfo.value)
311 """#187: issue involving std::shared_ptr<> return value policy & garbage collection"""
314 el.add(m.ElementA(i))
316 for i, v
in enumerate(el.get()):
317 assert i == v.value()