test_smart_ptr.py revision 12391:ceeca8b41e4b
112855Sgabeblack@google.comimport pytest
212855Sgabeblack@google.comfrom pybind11_tests import smart_ptr as m
312855Sgabeblack@google.comfrom pybind11_tests import ConstructorStats
412855Sgabeblack@google.com
512855Sgabeblack@google.com
612855Sgabeblack@google.comdef test_smart_ptr(capture):
712855Sgabeblack@google.com    # Object1
812855Sgabeblack@google.com    for i, o in enumerate([m.make_object_1(), m.make_object_2(), m.MyObject1(3)], start=1):
912855Sgabeblack@google.com        assert o.getRefCount() == 1
1012855Sgabeblack@google.com        with capture:
1112855Sgabeblack@google.com            m.print_object_1(o)
1212855Sgabeblack@google.com            m.print_object_2(o)
1312855Sgabeblack@google.com            m.print_object_3(o)
1412855Sgabeblack@google.com            m.print_object_4(o)
1512855Sgabeblack@google.com        assert capture == "MyObject1[{i}]\n".format(i=i) * 4
1612855Sgabeblack@google.com
1712855Sgabeblack@google.com    for i, o in enumerate([m.make_myobject1_1(), m.make_myobject1_2(), m.MyObject1(6), 7],
1812855Sgabeblack@google.com                          start=4):
1912855Sgabeblack@google.com        print(o)
2012855Sgabeblack@google.com        with capture:
2112855Sgabeblack@google.com            if not isinstance(o, int):
2212855Sgabeblack@google.com                m.print_object_1(o)
2312855Sgabeblack@google.com                m.print_object_2(o)
2412855Sgabeblack@google.com                m.print_object_3(o)
2512855Sgabeblack@google.com                m.print_object_4(o)
2612855Sgabeblack@google.com            m.print_myobject1_1(o)
2712855Sgabeblack@google.com            m.print_myobject1_2(o)
2812855Sgabeblack@google.com            m.print_myobject1_3(o)
2912855Sgabeblack@google.com            m.print_myobject1_4(o)
3012855Sgabeblack@google.com        assert capture == "MyObject1[{i}]\n".format(i=i) * (4 if isinstance(o, int) else 8)
3112855Sgabeblack@google.com
3212855Sgabeblack@google.com    cstats = ConstructorStats.get(m.MyObject1)
3312855Sgabeblack@google.com    assert cstats.alive() == 0
3412855Sgabeblack@google.com    expected_values = ['MyObject1[{}]'.format(i) for i in range(1, 7)] + ['MyObject1[7]'] * 4
3512855Sgabeblack@google.com    assert cstats.values() == expected_values
3612855Sgabeblack@google.com    assert cstats.default_constructions == 0
3712855Sgabeblack@google.com    assert cstats.copy_constructions == 0
3812855Sgabeblack@google.com    # assert cstats.move_constructions >= 0 # Doesn't invoke any
3912855Sgabeblack@google.com    assert cstats.copy_assignments == 0
4012855Sgabeblack@google.com    assert cstats.move_assignments == 0
4112855Sgabeblack@google.com
4212855Sgabeblack@google.com    # Object2
4312855Sgabeblack@google.com    for i, o in zip([8, 6, 7], [m.MyObject2(8), m.make_myobject2_1(), m.make_myobject2_2()]):
4412855Sgabeblack@google.com        print(o)
4512855Sgabeblack@google.com        with capture:
4612855Sgabeblack@google.com            m.print_myobject2_1(o)
4712855Sgabeblack@google.com            m.print_myobject2_2(o)
4812855Sgabeblack@google.com            m.print_myobject2_3(o)
4912855Sgabeblack@google.com            m.print_myobject2_4(o)
5012855Sgabeblack@google.com        assert capture == "MyObject2[{i}]\n".format(i=i) * 4
5112855Sgabeblack@google.com
5212855Sgabeblack@google.com    cstats = ConstructorStats.get(m.MyObject2)
5312855Sgabeblack@google.com    assert cstats.alive() == 1
5412855Sgabeblack@google.com    o = None
5512855Sgabeblack@google.com    assert cstats.alive() == 0
5612855Sgabeblack@google.com    assert cstats.values() == ['MyObject2[8]', 'MyObject2[6]', 'MyObject2[7]']
5712855Sgabeblack@google.com    assert cstats.default_constructions == 0
5812855Sgabeblack@google.com    assert cstats.copy_constructions == 0
5912855Sgabeblack@google.com    # assert cstats.move_constructions >= 0 # Doesn't invoke any
6012855Sgabeblack@google.com    assert cstats.copy_assignments == 0
6112855Sgabeblack@google.com    assert cstats.move_assignments == 0
6212855Sgabeblack@google.com
6312855Sgabeblack@google.com    # Object3
6412855Sgabeblack@google.com    for i, o in zip([9, 8, 9], [m.MyObject3(9), m.make_myobject3_1(), m.make_myobject3_2()]):
6512855Sgabeblack@google.com        print(o)
6612855Sgabeblack@google.com        with capture:
6712855Sgabeblack@google.com            m.print_myobject3_1(o)
6812855Sgabeblack@google.com            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_large_holder():
119    o = m.MyObject5(5)
120    assert o.value == 5
121    cstats = ConstructorStats.get(m.MyObject5)
122    assert cstats.alive() == 1
123    del o
124    assert cstats.alive() == 0
125
126
127def test_shared_ptr_and_references():
128    s = m.SharedPtrRef()
129    stats = ConstructorStats.get(m.A)
130    assert stats.alive() == 2
131
132    ref = s.ref  # init_holder_helper(holder_ptr=false, owned=false)
133    assert stats.alive() == 2
134    assert s.set_ref(ref)
135    with pytest.raises(RuntimeError) as excinfo:
136        assert s.set_holder(ref)
137    assert "Unable to cast from non-held to held instance" in str(excinfo.value)
138
139    copy = s.copy  # init_holder_helper(holder_ptr=false, owned=true)
140    assert stats.alive() == 3
141    assert s.set_ref(copy)
142    assert s.set_holder(copy)
143
144    holder_ref = s.holder_ref  # init_holder_helper(holder_ptr=true, owned=false)
145    assert stats.alive() == 3
146    assert s.set_ref(holder_ref)
147    assert s.set_holder(holder_ref)
148
149    holder_copy = s.holder_copy  # init_holder_helper(holder_ptr=true, owned=true)
150    assert stats.alive() == 3
151    assert s.set_ref(holder_copy)
152    assert s.set_holder(holder_copy)
153
154    del ref, copy, holder_ref, holder_copy, s
155    assert stats.alive() == 0
156
157
158def test_shared_ptr_from_this_and_references():
159    s = m.SharedFromThisRef()
160    stats = ConstructorStats.get(m.B)
161    assert stats.alive() == 2
162
163    ref = s.ref  # init_holder_helper(holder_ptr=false, owned=false, bad_wp=false)
164    assert stats.alive() == 2
165    assert s.set_ref(ref)
166    assert s.set_holder(ref)  # std::enable_shared_from_this can create a holder from a reference
167
168    bad_wp = s.bad_wp  # init_holder_helper(holder_ptr=false, owned=false, bad_wp=true)
169    assert stats.alive() == 2
170    assert s.set_ref(bad_wp)
171    with pytest.raises(RuntimeError) as excinfo:
172        assert s.set_holder(bad_wp)
173    assert "Unable to cast from non-held to held instance" in str(excinfo.value)
174
175    copy = s.copy  # init_holder_helper(holder_ptr=false, owned=true, bad_wp=false)
176    assert stats.alive() == 3
177    assert s.set_ref(copy)
178    assert s.set_holder(copy)
179
180    holder_ref = s.holder_ref  # init_holder_helper(holder_ptr=true, owned=false, bad_wp=false)
181    assert stats.alive() == 3
182    assert s.set_ref(holder_ref)
183    assert s.set_holder(holder_ref)
184
185    holder_copy = s.holder_copy  # init_holder_helper(holder_ptr=true, owned=true, bad_wp=false)
186    assert stats.alive() == 3
187    assert s.set_ref(holder_copy)
188    assert s.set_holder(holder_copy)
189
190    del ref, bad_wp, copy, holder_ref, holder_copy, s
191    assert stats.alive() == 0
192
193    z = m.SharedFromThisVirt.get()
194    y = m.SharedFromThisVirt.get()
195    assert y is z
196
197
198def test_move_only_holder():
199    a = m.TypeWithMoveOnlyHolder.make()
200    stats = ConstructorStats.get(m.TypeWithMoveOnlyHolder)
201    assert stats.alive() == 1
202    del a
203    assert stats.alive() == 0
204
205
206def 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
213def test_shared_ptr_gc():
214    """#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    pytest.gc_collect()
219    for i, v in enumerate(el.get()):
220        assert i == v.value()
221