1import pytest
2from pybind11_tests import smart_ptr as m
3from pybind11_tests import ConstructorStats
4
5
6def test_smart_ptr(capture):
7    # Object1
8    for i, o in enumerate([m.make_object_1(), m.make_object_2(), m.MyObject1(3)], start=1):
9        assert o.getRefCount() == 1
10        with capture:
11            m.print_object_1(o)
12            m.print_object_2(o)
13            m.print_object_3(o)
14            m.print_object_4(o)
15        assert capture == "MyObject1[{i}]\n".format(i=i) * 4
16
17    for i, o in enumerate([m.make_myobject1_1(), m.make_myobject1_2(), m.MyObject1(6), 7],
18                          start=4):
19        print(o)
20        with capture:
21            if not isinstance(o, int):
22                m.print_object_1(o)
23                m.print_object_2(o)
24                m.print_object_3(o)
25                m.print_object_4(o)
26            m.print_myobject1_1(o)
27            m.print_myobject1_2(o)
28            m.print_myobject1_3(o)
29            m.print_myobject1_4(o)
30        assert capture == "MyObject1[{i}]\n".format(i=i) * (4 if isinstance(o, int) else 8)
31
32    cstats = ConstructorStats.get(m.MyObject1)
33    assert cstats.alive() == 0
34    expected_values = ['MyObject1[{}]'.format(i) for i in range(1, 7)] + ['MyObject1[7]'] * 4
35    assert cstats.values() == expected_values
36    assert cstats.default_constructions == 0
37    assert cstats.copy_constructions == 0
38    # assert cstats.move_constructions >= 0 # Doesn't invoke any
39    assert cstats.copy_assignments == 0
40    assert cstats.move_assignments == 0
41
42    # Object2
43    for i, o in zip([8, 6, 7], [m.MyObject2(8), m.make_myobject2_1(), m.make_myobject2_2()]):
44        print(o)
45        with capture:
46            m.print_myobject2_1(o)
47            m.print_myobject2_2(o)
48            m.print_myobject2_3(o)
49            m.print_myobject2_4(o)
50        assert capture == "MyObject2[{i}]\n".format(i=i) * 4
51
52    cstats = ConstructorStats.get(m.MyObject2)
53    assert cstats.alive() == 1
54    o = None
55    assert cstats.alive() == 0
56    assert cstats.values() == ['MyObject2[8]', 'MyObject2[6]', 'MyObject2[7]']
57    assert cstats.default_constructions == 0
58    assert cstats.copy_constructions == 0
59    # assert cstats.move_constructions >= 0 # Doesn't invoke any
60    assert cstats.copy_assignments == 0
61    assert cstats.move_assignments == 0
62
63    # Object3
64    for i, o in zip([9, 8, 9], [m.MyObject3(9), m.make_myobject3_1(), m.make_myobject3_2()]):
65        print(o)
66        with capture:
67            m.print_myobject3_1(o)
68            m.print_myobject3_2(o)
69            m.print_myobject3_3(o)
70            m.print_myobject3_4(o)
71        assert capture == "MyObject3[{i}]\n".format(i=i) * 4
72
73    cstats = ConstructorStats.get(m.MyObject3)
74    assert cstats.alive() == 1
75    o = None
76    assert cstats.alive() == 0
77    assert cstats.values() == ['MyObject3[9]', 'MyObject3[8]', 'MyObject3[9]']
78    assert cstats.default_constructions == 0
79    assert cstats.copy_constructions == 0
80    # assert cstats.move_constructions >= 0 # Doesn't invoke any
81    assert cstats.copy_assignments == 0
82    assert cstats.move_assignments == 0
83
84    # Object
85    cstats = ConstructorStats.get(m.Object)
86    assert cstats.alive() == 0
87    assert cstats.values() == []
88    assert cstats.default_constructions == 10
89    assert cstats.copy_constructions == 0
90    # assert cstats.move_constructions >= 0 # Doesn't invoke any
91    assert cstats.copy_assignments == 0
92    assert cstats.move_assignments == 0
93
94    # ref<>
95    cstats = m.cstats_ref()
96    assert cstats.alive() == 0
97    assert cstats.values() == ['from pointer'] * 10
98    assert cstats.default_constructions == 30
99    assert cstats.copy_constructions == 12
100    # assert cstats.move_constructions >= 0 # Doesn't invoke any
101    assert cstats.copy_assignments == 30
102    assert cstats.move_assignments == 0
103
104
105def test_smart_ptr_refcounting():
106    assert m.test_object1_refcounting()
107
108
109def test_unique_nodelete():
110    o = m.MyObject4(23)
111    assert o.value == 23
112    cstats = ConstructorStats.get(m.MyObject4)
113    assert cstats.alive() == 1
114    del o
115    assert cstats.alive() == 1  # Leak, but that's intentional
116
117
118def test_unique_nodelete4a():
119    o = m.MyObject4a(23)
120    assert o.value == 23
121    cstats = ConstructorStats.get(m.MyObject4a)
122    assert cstats.alive() == 1
123    del o
124    assert cstats.alive() == 1  # Leak, but that's intentional
125
126
127def test_unique_deleter():
128    o = m.MyObject4b(23)
129    assert o.value == 23
130    cstats4a = ConstructorStats.get(m.MyObject4a)
131    assert cstats4a.alive() == 2  # Two because of previous test
132    cstats4b = ConstructorStats.get(m.MyObject4b)
133    assert cstats4b.alive() == 1
134    del o
135    assert cstats4a.alive() == 1  # Should now only be one leftover from previous test
136    assert cstats4b.alive() == 0  # Should be deleted
137
138
139def test_large_holder():
140    o = m.MyObject5(5)
141    assert o.value == 5
142    cstats = ConstructorStats.get(m.MyObject5)
143    assert cstats.alive() == 1
144    del o
145    assert cstats.alive() == 0
146
147
148def test_shared_ptr_and_references():
149    s = m.SharedPtrRef()
150    stats = ConstructorStats.get(m.A)
151    assert stats.alive() == 2
152
153    ref = s.ref  # init_holder_helper(holder_ptr=false, owned=false)
154    assert stats.alive() == 2
155    assert s.set_ref(ref)
156    with pytest.raises(RuntimeError) as excinfo:
157        assert s.set_holder(ref)
158    assert "Unable to cast from non-held to held instance" in str(excinfo.value)
159
160    copy = s.copy  # init_holder_helper(holder_ptr=false, owned=true)
161    assert stats.alive() == 3
162    assert s.set_ref(copy)
163    assert s.set_holder(copy)
164
165    holder_ref = s.holder_ref  # init_holder_helper(holder_ptr=true, owned=false)
166    assert stats.alive() == 3
167    assert s.set_ref(holder_ref)
168    assert s.set_holder(holder_ref)
169
170    holder_copy = s.holder_copy  # init_holder_helper(holder_ptr=true, owned=true)
171    assert stats.alive() == 3
172    assert s.set_ref(holder_copy)
173    assert s.set_holder(holder_copy)
174
175    del ref, copy, holder_ref, holder_copy, s
176    assert stats.alive() == 0
177
178
179def test_shared_ptr_from_this_and_references():
180    s = m.SharedFromThisRef()
181    stats = ConstructorStats.get(m.B)
182    assert stats.alive() == 2
183
184    ref = s.ref  # init_holder_helper(holder_ptr=false, owned=false, bad_wp=false)
185    assert stats.alive() == 2
186    assert s.set_ref(ref)
187    assert s.set_holder(ref)  # std::enable_shared_from_this can create a holder from a reference
188
189    bad_wp = s.bad_wp  # init_holder_helper(holder_ptr=false, owned=false, bad_wp=true)
190    assert stats.alive() == 2
191    assert s.set_ref(bad_wp)
192    with pytest.raises(RuntimeError) as excinfo:
193        assert s.set_holder(bad_wp)
194    assert "Unable to cast from non-held to held instance" in str(excinfo.value)
195
196    copy = s.copy  # init_holder_helper(holder_ptr=false, owned=true, bad_wp=false)
197    assert stats.alive() == 3
198    assert s.set_ref(copy)
199    assert s.set_holder(copy)
200
201    holder_ref = s.holder_ref  # init_holder_helper(holder_ptr=true, owned=false, bad_wp=false)
202    assert stats.alive() == 3
203    assert s.set_ref(holder_ref)
204    assert s.set_holder(holder_ref)
205
206    holder_copy = s.holder_copy  # init_holder_helper(holder_ptr=true, owned=true, bad_wp=false)
207    assert stats.alive() == 3
208    assert s.set_ref(holder_copy)
209    assert s.set_holder(holder_copy)
210
211    del ref, bad_wp, copy, holder_ref, holder_copy, s
212    assert stats.alive() == 0
213
214    z = m.SharedFromThisVirt.get()
215    y = m.SharedFromThisVirt.get()
216    assert y is z
217
218
219def test_move_only_holder():
220    a = m.TypeWithMoveOnlyHolder.make()
221    stats = ConstructorStats.get(m.TypeWithMoveOnlyHolder)
222    assert stats.alive() == 1
223    del a
224    assert stats.alive() == 0
225
226
227def test_holder_with_addressof_operator():
228    # this test must not throw exception from c++
229    a = m.TypeForHolderWithAddressOf.make()
230    a.print_object_1()
231    a.print_object_2()
232    a.print_object_3()
233    a.print_object_4()
234
235    stats = ConstructorStats.get(m.TypeForHolderWithAddressOf)
236    assert stats.alive() == 1
237
238    np = m.TypeForHolderWithAddressOf.make()
239    assert stats.alive() == 2
240    del a
241    assert stats.alive() == 1
242    del np
243    assert stats.alive() == 0
244
245    b = m.TypeForHolderWithAddressOf.make()
246    c = b
247    assert b.get() is c.get()
248    assert stats.alive() == 1
249
250    del b
251    assert stats.alive() == 1
252
253    del c
254    assert stats.alive() == 0
255
256
257def test_move_only_holder_with_addressof_operator():
258    a = m.TypeForMoveOnlyHolderWithAddressOf.make()
259    a.print_object()
260
261    stats = ConstructorStats.get(m.TypeForMoveOnlyHolderWithAddressOf)
262    assert stats.alive() == 1
263
264    a.value = 42
265    assert a.value == 42
266
267    del a
268    assert stats.alive() == 0
269
270
271def test_smart_ptr_from_default():
272    instance = m.HeldByDefaultHolder()
273    with pytest.raises(RuntimeError) as excinfo:
274        m.HeldByDefaultHolder.load_shared_ptr(instance)
275    assert "Unable to load a custom holder type from a " \
276           "default-holder instance" in str(excinfo.value)
277
278
279def test_shared_ptr_gc():
280    """#187: issue involving std::shared_ptr<> return value policy & garbage collection"""
281    el = m.ElementList()
282    for i in range(10):
283        el.add(m.ElementA(i))
284    pytest.gc_collect()
285    for i, v in enumerate(el.get()):
286        assert i == v.value()
287