MCPcopy Create free account
hub / github.com/pybind/pybind11 / test_init_factory_alias

Function test_init_factory_alias

tests/test_factory_constructors.py:157–224  ·  view source on GitHub ↗

Tests py::init_factory() wrapper with value conversions and alias types

()

Source from the content-addressed store, hash-verified

155
156@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC")
157def test_init_factory_alias():
158 """Tests py::init_factory() wrapper with value conversions and alias types"""
159
160 cstats = [m.TestFactory6.get_cstats(), m.TestFactory6.get_alias_cstats()]
161 cstats[0].alive() # force gc
162 n_inst = ConstructorStats.detail_reg_inst()
163
164 a = m.TestFactory6(tag.base, 1)
165 assert a.get() == 1
166 assert not a.has_alias()
167 b = m.TestFactory6(tag.alias, "hi there")
168 assert b.get() == 8
169 assert b.has_alias()
170 c = m.TestFactory6(tag.alias, 3)
171 assert c.get() == 3
172 assert c.has_alias()
173 d = m.TestFactory6(tag.alias, tag.pointer, 4)
174 assert d.get() == 4
175 assert d.has_alias()
176 e = m.TestFactory6(tag.base, tag.pointer, 5)
177 assert e.get() == 5
178 assert not e.has_alias()
179 f = m.TestFactory6(tag.base, tag.alias, tag.pointer, 6)
180 assert f.get() == 6
181 assert f.has_alias()
182
183 assert ConstructorStats.detail_reg_inst() == n_inst + 6
184 assert [i.alive() for i in cstats] == [6, 4]
185
186 del a, b, e
187 assert [i.alive() for i in cstats] == [3, 3]
188 assert ConstructorStats.detail_reg_inst() == n_inst + 3
189 del f, c, d
190 assert [i.alive() for i in cstats] == [0, 0]
191 assert ConstructorStats.detail_reg_inst() == n_inst
192
193 class MyTest(m.TestFactory6):
194 def __init__(self, *args):
195 m.TestFactory6.__init__(self, *args)
196
197 def get(self):
198 return -5 + m.TestFactory6.get(self)
199
200 # Return Class by value, moved into new alias:
201 z = MyTest(tag.base, 123)
202 assert z.get() == 118
203 assert z.has_alias()
204
205 # Return alias by value, moved into new alias:
206 y = MyTest(tag.alias, "why hello!")
207 assert y.get() == 5
208 assert y.has_alias()
209
210 # Return Class by pointer, moved into new alias then original destroyed:
211 x = MyTest(tag.base, tag.pointer, 47)
212 assert x.get() == 42
213 assert x.has_alias()
214

Callers

nothing calls this directly

Calls 7

getMethod · 0.95
has_aliasMethod · 0.95
getMethod · 0.95
MyTestClass · 0.85
aliveMethod · 0.80
TestFactory6Method · 0.80
valuesMethod · 0.80

Tested by

no test coverage detected