3 m = pytest.importorskip(
"pybind11_tests.smart_ptr")
4 from pybind11_tests
import ConstructorStats
10 [m.make_object_1(), m.make_object_2(), m.MyObject1(3)], start=1
12 assert o.getRefCount() == 1
18 assert capture == f
"MyObject1[{i}]\n" * 4
20 for i, o
in enumerate(
21 [m.make_myobject1_1(), m.make_myobject1_2(), m.MyObject1(6), 7], start=4
30 m.print_myobject1_1(o)
31 m.print_myobject1_2(o)
32 m.print_myobject1_3(o)
33 m.print_myobject1_4(o)
36 assert capture == f
"MyObject1[{i}]\n" * times
39 assert cstats.alive() == 0
40 expected_values = [f
"MyObject1[{i}]" for i
in range(1, 7)] + [
"MyObject1[7]"] * 4
41 assert cstats.values() == expected_values
42 assert cstats.default_constructions == 0
43 assert cstats.copy_constructions == 0
45 assert cstats.copy_assignments == 0
46 assert cstats.move_assignments == 0
50 [8, 6, 7], [m.MyObject2(8), m.make_myobject2_1(), m.make_myobject2_2()]
54 m.print_myobject2_1(o)
55 m.print_myobject2_2(o)
56 m.print_myobject2_3(o)
57 m.print_myobject2_4(o)
58 assert capture == f
"MyObject2[{i}]\n" * 4
61 assert cstats.alive() == 1
63 assert cstats.alive() == 0
64 assert cstats.values() == [
"MyObject2[8]",
"MyObject2[6]",
"MyObject2[7]"]
65 assert cstats.default_constructions == 0
66 assert cstats.copy_constructions == 0
68 assert cstats.copy_assignments == 0
69 assert cstats.move_assignments == 0
73 [9, 8, 9], [m.MyObject3(9), m.make_myobject3_1(), m.make_myobject3_2()]
77 m.print_myobject3_1(o)
78 m.print_myobject3_2(o)
79 m.print_myobject3_3(o)
80 m.print_myobject3_4(o)
81 assert capture == f
"MyObject3[{i}]\n" * 4
84 assert cstats.alive() == 1
86 assert cstats.alive() == 0
87 assert cstats.values() == [
"MyObject3[9]",
"MyObject3[8]",
"MyObject3[9]"]
88 assert cstats.default_constructions == 0
89 assert cstats.copy_constructions == 0
91 assert cstats.copy_assignments == 0
92 assert cstats.move_assignments == 0
96 assert cstats.alive() == 0
97 assert cstats.values() == []
98 assert cstats.default_constructions == 10
99 assert cstats.copy_constructions == 0
101 assert cstats.copy_assignments == 0
102 assert cstats.move_assignments == 0
105 cstats = m.cstats_ref()
106 assert cstats.alive() == 0
107 assert cstats.values() == [
"from pointer"] * 10
108 assert cstats.default_constructions == 30
109 assert cstats.copy_constructions == 12
111 assert cstats.copy_assignments == 30
112 assert cstats.move_assignments == 0
116 assert m.test_object1_refcounting()
123 assert cstats.alive() == 1
125 assert cstats.alive() == 1
126 m.MyObject4.cleanup_all_instances()
127 assert cstats.alive() == 0
134 assert cstats.alive() == 1
136 assert cstats.alive() == 1
137 m.MyObject4a.cleanup_all_instances()
138 assert cstats.alive() == 0
146 assert cstats4a.alive() == 2
148 assert cstats4b.alive() == 1
150 assert cstats4a.alive() == 1
151 assert cstats4b.alive() == 0
152 m.MyObject4a.cleanup_all_instances()
153 assert cstats4a.alive() == 0
154 assert cstats4b.alive() == 0
161 assert cstats.alive() == 1
163 assert cstats.alive() == 0
169 assert stats.alive() == 2
172 assert stats.alive() == 2
173 assert s.set_ref(ref)
174 with pytest.raises(RuntimeError)
as excinfo:
175 assert s.set_holder(ref)
176 assert "Unable to cast from non-held to held instance" in str(excinfo.value)
179 assert stats.alive() == 3
180 assert s.set_ref(copy)
181 assert s.set_holder(copy)
183 holder_ref = s.holder_ref
184 assert stats.alive() == 3
185 assert s.set_ref(holder_ref)
186 assert s.set_holder(holder_ref)
188 holder_copy = s.holder_copy
189 assert stats.alive() == 3
190 assert s.set_ref(holder_copy)
191 assert s.set_holder(holder_copy)
193 del ref, copy, holder_ref, holder_copy, s
194 assert stats.alive() == 0
198 s = m.SharedFromThisRef()
200 assert stats.alive() == 2
203 assert stats.alive() == 2
204 assert s.set_ref(ref)
210 assert stats.alive() == 2
211 assert s.set_ref(bad_wp)
212 with pytest.raises(RuntimeError)
as excinfo:
213 assert s.set_holder(bad_wp)
214 assert "Unable to cast from non-held to held instance" in str(excinfo.value)
217 assert stats.alive() == 3
218 assert s.set_ref(copy)
219 assert s.set_holder(copy)
224 assert stats.alive() == 3
225 assert s.set_ref(holder_ref)
226 assert s.set_holder(holder_ref)
231 assert stats.alive() == 3
232 assert s.set_ref(holder_copy)
233 assert s.set_holder(holder_copy)
235 del ref, bad_wp, copy, holder_ref, holder_copy, s
236 assert stats.alive() == 0
238 z = m.SharedFromThisVirt.get()
239 y = m.SharedFromThisVirt.get()
244 a = m.TypeWithMoveOnlyHolder.make()
245 b = m.TypeWithMoveOnlyHolder.make_as_object()
247 assert stats.alive() == 2
249 assert stats.alive() == 1
251 assert stats.alive() == 0
256 a = m.TypeForHolderWithAddressOf.make()
263 assert stats.alive() == 1
265 np = m.TypeForHolderWithAddressOf.make()
266 assert stats.alive() == 2
268 assert stats.alive() == 1
270 assert stats.alive() == 0
272 b = m.TypeForHolderWithAddressOf.make()
274 assert b.get()
is c.get()
275 assert stats.alive() == 1
278 assert stats.alive() == 1
281 assert stats.alive() == 0
285 a = m.TypeForMoveOnlyHolderWithAddressOf.make()
289 assert stats.alive() == 1
295 assert stats.alive() == 0
299 instance = m.HeldByDefaultHolder()
300 with pytest.raises(RuntimeError)
as excinfo:
301 m.HeldByDefaultHolder.load_shared_ptr(instance)
303 "Unable to load a custom holder type from a " 304 "default-holder instance" in str(excinfo.value)
309 """#187: issue involving std::shared_ptr<> return value policy & garbage collection""" 312 el.add(m.ElementA(i))
314 for i, v
in enumerate(el.get()):
315 assert i == v.value()
def test_shared_ptr_and_references()
def test_smart_ptr_refcounting()
def test_smart_ptr(capture)
def test_smart_ptr_from_default()
def test_unique_nodelete4a()
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
bool isinstance(handle obj)
def test_holder_with_addressof_operator()
def test_move_only_holder_with_addressof_operator()
static ConstructorStats & get(std::type_index type)
def test_move_only_holder()
def test_shared_ptr_from_this_and_references()
Double_ range(const Point2_ &p, const Point2_ &q)
def test_unique_deleter()
def test_unique_nodelete()