4 m = pytest.importorskip(
"pybind11_tests.smart_ptr")
5 from pybind11_tests
import ConstructorStats
10 for i, o
in enumerate([m.make_object_1(), m.make_object_2(), m.MyObject1(3)], start=1):
11 assert o.getRefCount() == 1
17 assert capture ==
"MyObject1[{i}]\n".format(i=i) * 4
19 for i, o
in enumerate([m.make_myobject1_1(), m.make_myobject1_2(), m.MyObject1(6), 7],
28 m.print_myobject1_1(o)
29 m.print_myobject1_2(o)
30 m.print_myobject1_3(o)
31 m.print_myobject1_4(o)
32 assert capture ==
"MyObject1[{i}]\n".format(i=i) * (4
if isinstance(o, int)
else 8)
35 assert cstats.alive() == 0
36 expected_values = [
'MyObject1[{}]'.format(i)
for i
in range(1, 7)] + [
'MyObject1[7]'] * 4
37 assert cstats.values() == expected_values
38 assert cstats.default_constructions == 0
39 assert cstats.copy_constructions == 0
41 assert cstats.copy_assignments == 0
42 assert cstats.move_assignments == 0
45 for i, o
in zip([8, 6, 7], [m.MyObject2(8), m.make_myobject2_1(), m.make_myobject2_2()]):
48 m.print_myobject2_1(o)
49 m.print_myobject2_2(o)
50 m.print_myobject2_3(o)
51 m.print_myobject2_4(o)
52 assert capture ==
"MyObject2[{i}]\n".format(i=i) * 4
55 assert cstats.alive() == 1
57 assert cstats.alive() == 0
58 assert cstats.values() == [
'MyObject2[8]',
'MyObject2[6]',
'MyObject2[7]']
59 assert cstats.default_constructions == 0
60 assert cstats.copy_constructions == 0
62 assert cstats.copy_assignments == 0
63 assert cstats.move_assignments == 0
66 for i, o
in zip([9, 8, 9], [m.MyObject3(9), m.make_myobject3_1(), m.make_myobject3_2()]):
69 m.print_myobject3_1(o)
70 m.print_myobject3_2(o)
71 m.print_myobject3_3(o)
72 m.print_myobject3_4(o)
73 assert capture ==
"MyObject3[{i}]\n".format(i=i) * 4
76 assert cstats.alive() == 1
78 assert cstats.alive() == 0
79 assert cstats.values() == [
'MyObject3[9]',
'MyObject3[8]',
'MyObject3[9]']
80 assert cstats.default_constructions == 0
81 assert cstats.copy_constructions == 0
83 assert cstats.copy_assignments == 0
84 assert cstats.move_assignments == 0
88 assert cstats.alive() == 0
89 assert cstats.values() == []
90 assert cstats.default_constructions == 10
91 assert cstats.copy_constructions == 0
93 assert cstats.copy_assignments == 0
94 assert cstats.move_assignments == 0
97 cstats = m.cstats_ref()
98 assert cstats.alive() == 0
99 assert cstats.values() == [
'from pointer'] * 10
100 assert cstats.default_constructions == 30
101 assert cstats.copy_constructions == 12
103 assert cstats.copy_assignments == 30
104 assert cstats.move_assignments == 0
108 assert m.test_object1_refcounting()
115 assert cstats.alive() == 1
117 assert cstats.alive() == 1
124 assert cstats.alive() == 1
126 assert cstats.alive() == 1
133 assert cstats4a.alive() == 2
135 assert cstats4b.alive() == 1
137 assert cstats4a.alive() == 1
138 assert cstats4b.alive() == 0
145 assert cstats.alive() == 1
147 assert cstats.alive() == 0
153 assert stats.alive() == 2
156 assert stats.alive() == 2
157 assert s.set_ref(ref)
158 with pytest.raises(RuntimeError)
as excinfo:
159 assert s.set_holder(ref)
160 assert "Unable to cast from non-held to held instance" in str(excinfo.value)
163 assert stats.alive() == 3
164 assert s.set_ref(copy)
165 assert s.set_holder(copy)
167 holder_ref = s.holder_ref
168 assert stats.alive() == 3
169 assert s.set_ref(holder_ref)
170 assert s.set_holder(holder_ref)
172 holder_copy = s.holder_copy
173 assert stats.alive() == 3
174 assert s.set_ref(holder_copy)
175 assert s.set_holder(holder_copy)
177 del ref, copy, holder_ref, holder_copy, s
178 assert stats.alive() == 0
182 s = m.SharedFromThisRef()
184 assert stats.alive() == 2
187 assert stats.alive() == 2
188 assert s.set_ref(ref)
189 assert s.set_holder(ref)
192 assert stats.alive() == 2
193 assert s.set_ref(bad_wp)
194 with pytest.raises(RuntimeError)
as excinfo:
195 assert s.set_holder(bad_wp)
196 assert "Unable to cast from non-held to held instance" in str(excinfo.value)
199 assert stats.alive() == 3
200 assert s.set_ref(copy)
201 assert s.set_holder(copy)
203 holder_ref = s.holder_ref
204 assert stats.alive() == 3
205 assert s.set_ref(holder_ref)
206 assert s.set_holder(holder_ref)
208 holder_copy = s.holder_copy
209 assert stats.alive() == 3
210 assert s.set_ref(holder_copy)
211 assert s.set_holder(holder_copy)
213 del ref, bad_wp, copy, holder_ref, holder_copy, s
214 assert stats.alive() == 0
216 z = m.SharedFromThisVirt.get()
217 y = m.SharedFromThisVirt.get()
222 a = m.TypeWithMoveOnlyHolder.make()
223 b = m.TypeWithMoveOnlyHolder.make_as_object()
225 assert stats.alive() == 2
227 assert stats.alive() == 1
229 assert stats.alive() == 0
234 a = m.TypeForHolderWithAddressOf.make()
241 assert stats.alive() == 1
243 np = m.TypeForHolderWithAddressOf.make()
244 assert stats.alive() == 2
246 assert stats.alive() == 1
248 assert stats.alive() == 0
250 b = m.TypeForHolderWithAddressOf.make()
252 assert b.get()
is c.get()
253 assert stats.alive() == 1
256 assert stats.alive() == 1
259 assert stats.alive() == 0
263 a = m.TypeForMoveOnlyHolderWithAddressOf.make()
267 assert stats.alive() == 1
273 assert stats.alive() == 0
277 instance = m.HeldByDefaultHolder()
278 with pytest.raises(RuntimeError)
as excinfo:
279 m.HeldByDefaultHolder.load_shared_ptr(instance)
280 assert "Unable to load a custom holder type from a " \
281 "default-holder instance" in str(excinfo.value)
285 """#187: issue involving std::shared_ptr<> return value policy & garbage collection""" 288 el.add(m.ElementA(i))
290 for i, v
in enumerate(el.get()):
291 assert i == v.value()
void print(const Matrix &A, const string &s, ostream &stream)
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()
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()
def test_unique_deleter()
def test_unique_nodelete()