test_smart_ptr.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 import pytest
3 
4 m = pytest.importorskip("pybind11_tests.smart_ptr")
5 from pybind11_tests import ConstructorStats # noqa: E402
6 
7 
8 def test_smart_ptr(capture):
9  # Object1
10  for i, o in enumerate([m.make_object_1(), m.make_object_2(), m.MyObject1(3)], start=1):
11  assert o.getRefCount() == 1
12  with capture:
13  m.print_object_1(o)
14  m.print_object_2(o)
15  m.print_object_3(o)
16  m.print_object_4(o)
17  assert capture == "MyObject1[{i}]\n".format(i=i) * 4
18 
19  for i, o in enumerate([m.make_myobject1_1(), m.make_myobject1_2(), m.MyObject1(6), 7],
20  start=4):
21  print(o)
22  with capture:
23  if not isinstance(o, int):
24  m.print_object_1(o)
25  m.print_object_2(o)
26  m.print_object_3(o)
27  m.print_object_4(o)
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)
33 
34  cstats = ConstructorStats.get(m.MyObject1)
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
40  # assert cstats.move_constructions >= 0 # Doesn't invoke any
41  assert cstats.copy_assignments == 0
42  assert cstats.move_assignments == 0
43 
44  # Object2
45  for i, o in zip([8, 6, 7], [m.MyObject2(8), m.make_myobject2_1(), m.make_myobject2_2()]):
46  print(o)
47  with capture:
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
53 
54  cstats = ConstructorStats.get(m.MyObject2)
55  assert cstats.alive() == 1
56  o = None
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
61  # assert cstats.move_constructions >= 0 # Doesn't invoke any
62  assert cstats.copy_assignments == 0
63  assert cstats.move_assignments == 0
64 
65  # Object3
66  for i, o in zip([9, 8, 9], [m.MyObject3(9), m.make_myobject3_1(), m.make_myobject3_2()]):
67  print(o)
68  with capture:
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
74 
75  cstats = ConstructorStats.get(m.MyObject3)
76  assert cstats.alive() == 1
77  o = None
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
82  # assert cstats.move_constructions >= 0 # Doesn't invoke any
83  assert cstats.copy_assignments == 0
84  assert cstats.move_assignments == 0
85 
86  # Object
87  cstats = ConstructorStats.get(m.Object)
88  assert cstats.alive() == 0
89  assert cstats.values() == []
90  assert cstats.default_constructions == 10
91  assert cstats.copy_constructions == 0
92  # assert cstats.move_constructions >= 0 # Doesn't invoke any
93  assert cstats.copy_assignments == 0
94  assert cstats.move_assignments == 0
95 
96  # ref<>
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
102  # assert cstats.move_constructions >= 0 # Doesn't invoke any
103  assert cstats.copy_assignments == 30
104  assert cstats.move_assignments == 0
105 
106 
108  assert m.test_object1_refcounting()
109 
110 
112  o = m.MyObject4(23)
113  assert o.value == 23
114  cstats = ConstructorStats.get(m.MyObject4)
115  assert cstats.alive() == 1
116  del o
117  assert cstats.alive() == 1 # Leak, but that's intentional
118 
119 
121  o = m.MyObject4a(23)
122  assert o.value == 23
123  cstats = ConstructorStats.get(m.MyObject4a)
124  assert cstats.alive() == 1
125  del o
126  assert cstats.alive() == 1 # Leak, but that's intentional
127 
128 
130  o = m.MyObject4b(23)
131  assert o.value == 23
132  cstats4a = ConstructorStats.get(m.MyObject4a)
133  assert cstats4a.alive() == 2 # Two because of previous test
134  cstats4b = ConstructorStats.get(m.MyObject4b)
135  assert cstats4b.alive() == 1
136  del o
137  assert cstats4a.alive() == 1 # Should now only be one leftover from previous test
138  assert cstats4b.alive() == 0 # Should be deleted
139 
140 
142  o = m.MyObject5(5)
143  assert o.value == 5
144  cstats = ConstructorStats.get(m.MyObject5)
145  assert cstats.alive() == 1
146  del o
147  assert cstats.alive() == 0
148 
149 
151  s = m.SharedPtrRef()
152  stats = ConstructorStats.get(m.A)
153  assert stats.alive() == 2
154 
155  ref = s.ref # init_holder_helper(holder_ptr=false, owned=false)
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)
161 
162  copy = s.copy # init_holder_helper(holder_ptr=false, owned=true)
163  assert stats.alive() == 3
164  assert s.set_ref(copy)
165  assert s.set_holder(copy)
166 
167  holder_ref = s.holder_ref # init_holder_helper(holder_ptr=true, owned=false)
168  assert stats.alive() == 3
169  assert s.set_ref(holder_ref)
170  assert s.set_holder(holder_ref)
171 
172  holder_copy = s.holder_copy # init_holder_helper(holder_ptr=true, owned=true)
173  assert stats.alive() == 3
174  assert s.set_ref(holder_copy)
175  assert s.set_holder(holder_copy)
176 
177  del ref, copy, holder_ref, holder_copy, s
178  assert stats.alive() == 0
179 
180 
182  s = m.SharedFromThisRef()
183  stats = ConstructorStats.get(m.B)
184  assert stats.alive() == 2
185 
186  ref = s.ref # init_holder_helper(holder_ptr=false, owned=false, bad_wp=false)
187  assert stats.alive() == 2
188  assert s.set_ref(ref)
189  assert s.set_holder(ref) # std::enable_shared_from_this can create a holder from a reference
190 
191  bad_wp = s.bad_wp # init_holder_helper(holder_ptr=false, owned=false, bad_wp=true)
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)
197 
198  copy = s.copy # init_holder_helper(holder_ptr=false, owned=true, bad_wp=false)
199  assert stats.alive() == 3
200  assert s.set_ref(copy)
201  assert s.set_holder(copy)
202 
203  holder_ref = s.holder_ref # init_holder_helper(holder_ptr=true, owned=false, bad_wp=false)
204  assert stats.alive() == 3
205  assert s.set_ref(holder_ref)
206  assert s.set_holder(holder_ref)
207 
208  holder_copy = s.holder_copy # init_holder_helper(holder_ptr=true, owned=true, bad_wp=false)
209  assert stats.alive() == 3
210  assert s.set_ref(holder_copy)
211  assert s.set_holder(holder_copy)
212 
213  del ref, bad_wp, copy, holder_ref, holder_copy, s
214  assert stats.alive() == 0
215 
216  z = m.SharedFromThisVirt.get()
217  y = m.SharedFromThisVirt.get()
218  assert y is z
219 
220 
222  a = m.TypeWithMoveOnlyHolder.make()
223  b = m.TypeWithMoveOnlyHolder.make_as_object()
224  stats = ConstructorStats.get(m.TypeWithMoveOnlyHolder)
225  assert stats.alive() == 2
226  del b
227  assert stats.alive() == 1
228  del a
229  assert stats.alive() == 0
230 
231 
233  # this test must not throw exception from c++
234  a = m.TypeForHolderWithAddressOf.make()
235  a.print_object_1()
236  a.print_object_2()
237  a.print_object_3()
238  a.print_object_4()
239 
240  stats = ConstructorStats.get(m.TypeForHolderWithAddressOf)
241  assert stats.alive() == 1
242 
243  np = m.TypeForHolderWithAddressOf.make()
244  assert stats.alive() == 2
245  del a
246  assert stats.alive() == 1
247  del np
248  assert stats.alive() == 0
249 
250  b = m.TypeForHolderWithAddressOf.make()
251  c = b
252  assert b.get() is c.get()
253  assert stats.alive() == 1
254 
255  del b
256  assert stats.alive() == 1
257 
258  del c
259  assert stats.alive() == 0
260 
261 
263  a = m.TypeForMoveOnlyHolderWithAddressOf.make()
264  a.print_object()
265 
266  stats = ConstructorStats.get(m.TypeForMoveOnlyHolderWithAddressOf)
267  assert stats.alive() == 1
268 
269  a.value = 42
270  assert a.value == 42
271 
272  del a
273  assert stats.alive() == 0
274 
275 
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)
282 
283 
285  """#187: issue involving std::shared_ptr<> return value policy & garbage collection"""
286  el = m.ElementList()
287  for i in range(10):
288  el.add(m.ElementA(i))
289  pytest.gc_collect()
290  for i, v in enumerate(el.get()):
291  assert i == v.value()
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
def test_shared_ptr_and_references()
def test_smart_ptr_refcounting()
def test_smart_ptr(capture)
def test_shared_ptr_gc()
def test_smart_ptr_from_default()
def test_unique_nodelete4a()
bool isinstance(handle obj)
Definition: pytypes.h:384
def test_holder_with_addressof_operator()
def test_move_only_holder_with_addressof_operator()
static ConstructorStats & get(std::type_index type)
Definition: pytypes.h:928
def test_move_only_holder()
def test_shared_ptr_from_this_and_references()
def test_unique_deleter()
def test_unique_nodelete()


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:04