test_smart_ptr.py 7.35 KB
Newer Older
1
import pytest
2
from pybind11_tests import smart_ptr as m
Dean Moldovan's avatar
Dean Moldovan committed
3
4
5
6
7
from pybind11_tests import ConstructorStats


def test_smart_ptr(capture):
    # Object1
8
    for i, o in enumerate([m.make_object_1(), m.make_object_2(), m.MyObject1(3)], start=1):
Dean Moldovan's avatar
Dean Moldovan committed
9
10
        assert o.getRefCount() == 1
        with capture:
11
12
13
14
            m.print_object_1(o)
            m.print_object_2(o)
            m.print_object_3(o)
            m.print_object_4(o)
Dean Moldovan's avatar
Dean Moldovan committed
15
16
        assert capture == "MyObject1[{i}]\n".format(i=i) * 4

17
18
    for i, o in enumerate([m.make_myobject1_1(), m.make_myobject1_2(), m.MyObject1(6), 7],
                          start=4):
Dean Moldovan's avatar
Dean Moldovan committed
19
20
21
        print(o)
        with capture:
            if not isinstance(o, int):
22
23
24
25
26
27
28
29
                m.print_object_1(o)
                m.print_object_2(o)
                m.print_object_3(o)
                m.print_object_4(o)
            m.print_myobject1_1(o)
            m.print_myobject1_2(o)
            m.print_myobject1_3(o)
            m.print_myobject1_4(o)
Dean Moldovan's avatar
Dean Moldovan committed
30
31
        assert capture == "MyObject1[{i}]\n".format(i=i) * (4 if isinstance(o, int) else 8)

32
    cstats = ConstructorStats.get(m.MyObject1)
Dean Moldovan's avatar
Dean Moldovan committed
33
34
35
36
37
38
39
40
41
42
    assert cstats.alive() == 0
    expected_values = ['MyObject1[{}]'.format(i) for i in range(1, 7)] + ['MyObject1[7]'] * 4
    assert cstats.values() == expected_values
    assert cstats.default_constructions == 0
    assert cstats.copy_constructions == 0
    # assert cstats.move_constructions >= 0 # Doesn't invoke any
    assert cstats.copy_assignments == 0
    assert cstats.move_assignments == 0

    # Object2
43
    for i, o in zip([8, 6, 7], [m.MyObject2(8), m.make_myobject2_1(), m.make_myobject2_2()]):
Dean Moldovan's avatar
Dean Moldovan committed
44
45
        print(o)
        with capture:
46
47
48
49
            m.print_myobject2_1(o)
            m.print_myobject2_2(o)
            m.print_myobject2_3(o)
            m.print_myobject2_4(o)
Dean Moldovan's avatar
Dean Moldovan committed
50
51
        assert capture == "MyObject2[{i}]\n".format(i=i) * 4

52
    cstats = ConstructorStats.get(m.MyObject2)
Dean Moldovan's avatar
Dean Moldovan committed
53
54
55
56
57
58
59
60
61
62
63
    assert cstats.alive() == 1
    o = None
    assert cstats.alive() == 0
    assert cstats.values() == ['MyObject2[8]', 'MyObject2[6]', 'MyObject2[7]']
    assert cstats.default_constructions == 0
    assert cstats.copy_constructions == 0
    # assert cstats.move_constructions >= 0 # Doesn't invoke any
    assert cstats.copy_assignments == 0
    assert cstats.move_assignments == 0

    # Object3
64
    for i, o in zip([9, 8, 9], [m.MyObject3(9), m.make_myobject3_1(), m.make_myobject3_2()]):
Dean Moldovan's avatar
Dean Moldovan committed
65
66
        print(o)
        with capture:
67
68
69
70
            m.print_myobject3_1(o)
            m.print_myobject3_2(o)
            m.print_myobject3_3(o)
            m.print_myobject3_4(o)
Dean Moldovan's avatar
Dean Moldovan committed
71
72
        assert capture == "MyObject3[{i}]\n".format(i=i) * 4

73
    cstats = ConstructorStats.get(m.MyObject3)
Dean Moldovan's avatar
Dean Moldovan committed
74
75
76
77
78
79
80
81
82
83
    assert cstats.alive() == 1
    o = None
    assert cstats.alive() == 0
    assert cstats.values() == ['MyObject3[9]', 'MyObject3[8]', 'MyObject3[9]']
    assert cstats.default_constructions == 0
    assert cstats.copy_constructions == 0
    # assert cstats.move_constructions >= 0 # Doesn't invoke any
    assert cstats.copy_assignments == 0
    assert cstats.move_assignments == 0

84
85
    # Object
    cstats = ConstructorStats.get(m.Object)
Dean Moldovan's avatar
Dean Moldovan committed
86
87
88
89
90
91
92
93
    assert cstats.alive() == 0
    assert cstats.values() == []
    assert cstats.default_constructions == 10
    assert cstats.copy_constructions == 0
    # assert cstats.move_constructions >= 0 # Doesn't invoke any
    assert cstats.copy_assignments == 0
    assert cstats.move_assignments == 0

94
95
    # ref<>
    cstats = m.cstats_ref()
Dean Moldovan's avatar
Dean Moldovan committed
96
97
98
99
100
101
102
    assert cstats.alive() == 0
    assert cstats.values() == ['from pointer'] * 10
    assert cstats.default_constructions == 30
    assert cstats.copy_constructions == 12
    # assert cstats.move_constructions >= 0 # Doesn't invoke any
    assert cstats.copy_assignments == 30
    assert cstats.move_assignments == 0
103

104

105
def test_smart_ptr_refcounting():
106
    assert m.test_object1_refcounting()
107
108


109
def test_unique_nodelete():
110
    o = m.MyObject4(23)
111
    assert o.value == 23
112
    cstats = ConstructorStats.get(m.MyObject4)
113
114
    assert cstats.alive() == 1
    del o
115
    assert cstats.alive() == 1  # Leak, but that's intentional
116
117


118
def test_large_holder():
119
    o = m.MyObject5(5)
120
    assert o.value == 5
121
    cstats = ConstructorStats.get(m.MyObject5)
122
123
124
125
126
    assert cstats.alive() == 1
    del o
    assert cstats.alive() == 0


127
def test_shared_ptr_and_references():
128
129
    s = m.SharedPtrRef()
    stats = ConstructorStats.get(m.A)
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    assert stats.alive() == 2

    ref = s.ref  # init_holder_helper(holder_ptr=false, owned=false)
    assert stats.alive() == 2
    assert s.set_ref(ref)
    with pytest.raises(RuntimeError) as excinfo:
        assert s.set_holder(ref)
    assert "Unable to cast from non-held to held instance" in str(excinfo.value)

    copy = s.copy  # init_holder_helper(holder_ptr=false, owned=true)
    assert stats.alive() == 3
    assert s.set_ref(copy)
    assert s.set_holder(copy)

    holder_ref = s.holder_ref  # init_holder_helper(holder_ptr=true, owned=false)
    assert stats.alive() == 3
    assert s.set_ref(holder_ref)
    assert s.set_holder(holder_ref)

    holder_copy = s.holder_copy  # init_holder_helper(holder_ptr=true, owned=true)
    assert stats.alive() == 3
    assert s.set_ref(holder_copy)
    assert s.set_holder(holder_copy)

    del ref, copy, holder_ref, holder_copy, s
    assert stats.alive() == 0


def test_shared_ptr_from_this_and_references():
159
160
    s = m.SharedFromThisRef()
    stats = ConstructorStats.get(m.B)
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
    assert stats.alive() == 2

    ref = s.ref  # init_holder_helper(holder_ptr=false, owned=false, bad_wp=false)
    assert stats.alive() == 2
    assert s.set_ref(ref)
    assert s.set_holder(ref)  # std::enable_shared_from_this can create a holder from a reference

    bad_wp = s.bad_wp  # init_holder_helper(holder_ptr=false, owned=false, bad_wp=true)
    assert stats.alive() == 2
    assert s.set_ref(bad_wp)
    with pytest.raises(RuntimeError) as excinfo:
        assert s.set_holder(bad_wp)
    assert "Unable to cast from non-held to held instance" in str(excinfo.value)

    copy = s.copy  # init_holder_helper(holder_ptr=false, owned=true, bad_wp=false)
    assert stats.alive() == 3
    assert s.set_ref(copy)
    assert s.set_holder(copy)

    holder_ref = s.holder_ref  # init_holder_helper(holder_ptr=true, owned=false, bad_wp=false)
    assert stats.alive() == 3
    assert s.set_ref(holder_ref)
    assert s.set_holder(holder_ref)

    holder_copy = s.holder_copy  # init_holder_helper(holder_ptr=true, owned=true, bad_wp=false)
    assert stats.alive() == 3
    assert s.set_ref(holder_copy)
    assert s.set_holder(holder_copy)

    del ref, bad_wp, copy, holder_ref, holder_copy, s
    assert stats.alive() == 0
192

193
194
    z = m.SharedFromThisVirt.get()
    y = m.SharedFromThisVirt.get()
195
196
    assert y is z

197
198

def test_move_only_holder():
199
200
    a = m.TypeWithMoveOnlyHolder.make()
    stats = ConstructorStats.get(m.TypeWithMoveOnlyHolder)
201
202
203
    assert stats.alive() == 1
    del a
    assert stats.alive() == 0
204
205
206


def test_smart_ptr_from_default():
207
    instance = m.HeldByDefaultHolder()
208
    with pytest.raises(RuntimeError) as excinfo:
209
        m.HeldByDefaultHolder.load_shared_ptr(instance)
210
    assert "Unable to load a custom holder type from a default-holder instance" in str(excinfo)
211
212
213
214


def test_shared_ptr_gc():
    """#187: issue involving std::shared_ptr<> return value policy & garbage collection"""
215
    el = m.ElementList()
216
    for i in range(10):
217
        el.add(m.ElementA(i))
218
219
220
    pytest.gc_collect()
    for i, v in enumerate(el.get()):
        assert i == v.value()