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

Function test_init_factory_casting

tests/test_factory_constructors.py:108–153  ·  view source on GitHub ↗

Tests py::init_factory() wrapper with various upcasting and downcasting returns

()

Source from the content-addressed store, hash-verified

106
107@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC")
108def test_init_factory_casting():
109 """Tests py::init_factory() wrapper with various upcasting and downcasting returns"""
110
111 cstats = [
112 ConstructorStats.get(c)
113 for c in [m.TestFactory3, m.TestFactory4, m.TestFactory5]
114 ]
115 cstats[0].alive() # force gc
116 n_inst = ConstructorStats.detail_reg_inst()
117
118 # Construction from derived references:
119 a = m.TestFactory3(tag.pointer, tag.TF4, 4)
120 assert a.value == "4"
121 b = m.TestFactory3(tag.shared_ptr, tag.TF4, 5)
122 assert b.value == "5"
123 c = m.TestFactory3(tag.pointer, tag.TF5, 6)
124 assert c.value == "6"
125 d = m.TestFactory3(tag.shared_ptr, tag.TF5, 7)
126 assert d.value == "7"
127
128 assert ConstructorStats.detail_reg_inst() == n_inst + 4
129
130 # Shared a lambda with TF3:
131 e = m.TestFactory4(tag.pointer, tag.TF4, 8)
132 assert e.value == "8"
133
134 assert ConstructorStats.detail_reg_inst() == n_inst + 5
135 assert [i.alive() for i in cstats] == [5, 3, 2]
136
137 del a
138 assert [i.alive() for i in cstats] == [4, 2, 2]
139 assert ConstructorStats.detail_reg_inst() == n_inst + 4
140
141 del b, c, e
142 assert [i.alive() for i in cstats] == [1, 0, 1]
143 assert ConstructorStats.detail_reg_inst() == n_inst + 1
144
145 del d
146 assert [i.alive() for i in cstats] == [0, 0, 0]
147 assert ConstructorStats.detail_reg_inst() == n_inst
148
149 assert [i.values() for i in cstats] == [
150 ["4", "5", "6", "7", "8"],
151 ["4", "5", "8"],
152 ["6", "7"],
153 ]
154
155
156@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC")

Callers

nothing calls this directly

Calls 5

aliveMethod · 0.80
TestFactory3Method · 0.80
TestFactory4Method · 0.80
valuesMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected