Tests py::init_factory() wrapper around various ways of returning the object
()
| 12 | |
| 13 | @pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") |
| 14 | def test_init_factory_basic(): |
| 15 | """Tests py::init_factory() wrapper around various ways of returning the object""" |
| 16 | |
| 17 | cstats = [ |
| 18 | ConstructorStats.get(c) |
| 19 | for c in [m.TestFactory1, m.TestFactory2, m.TestFactory3] |
| 20 | ] |
| 21 | cstats[0].alive() # force gc |
| 22 | n_inst = ConstructorStats.detail_reg_inst() |
| 23 | |
| 24 | x1 = m.TestFactory1(tag.unique_ptr, 3) |
| 25 | assert x1.value == "3" |
| 26 | y1 = m.TestFactory1(tag.pointer) |
| 27 | assert y1.value == "(empty)" |
| 28 | z1 = m.TestFactory1("hi!") |
| 29 | assert z1.value == "hi!" |
| 30 | |
| 31 | assert ConstructorStats.detail_reg_inst() == n_inst + 3 |
| 32 | |
| 33 | x2 = m.TestFactory2(tag.move) |
| 34 | assert x2.value == "(empty2)" |
| 35 | y2 = m.TestFactory2(tag.pointer, 7) |
| 36 | assert y2.value == "7" |
| 37 | z2 = m.TestFactory2(tag.unique_ptr, "hi again") |
| 38 | assert z2.value == "hi again" |
| 39 | |
| 40 | assert ConstructorStats.detail_reg_inst() == n_inst + 6 |
| 41 | |
| 42 | x3 = m.TestFactory3(tag.shared_ptr) |
| 43 | assert x3.value == "(empty3)" |
| 44 | y3 = m.TestFactory3(tag.pointer, 42) |
| 45 | assert y3.value == "42" |
| 46 | z3 = m.TestFactory3("bye") |
| 47 | assert z3.value == "bye" |
| 48 | |
| 49 | for null_ptr_kind in [tag.null_ptr, tag.null_unique_ptr, tag.null_shared_ptr]: |
| 50 | with pytest.raises(TypeError) as excinfo: |
| 51 | m.TestFactory3(null_ptr_kind) |
| 52 | assert ( |
| 53 | str(excinfo.value) == "pybind11::init(): factory function returned nullptr" |
| 54 | ) |
| 55 | |
| 56 | assert [i.alive() for i in cstats] == [3, 3, 3] |
| 57 | assert ConstructorStats.detail_reg_inst() == n_inst + 9 |
| 58 | |
| 59 | del x1, y2, y3, z3 |
| 60 | assert [i.alive() for i in cstats] == [2, 2, 1] |
| 61 | assert ConstructorStats.detail_reg_inst() == n_inst + 5 |
| 62 | del x2, x3, y1, z1, z2 |
| 63 | assert [i.alive() for i in cstats] == [0, 0, 0] |
| 64 | assert ConstructorStats.detail_reg_inst() == n_inst |
| 65 | |
| 66 | assert [i.values() for i in cstats] == [ |
| 67 | ["3", "hi!"], |
| 68 | ["7", "hi again"], |
| 69 | ["42", "bye"], |
| 70 | ] |
| 71 | assert [i.default_constructions for i in cstats] == [1, 1, 1] |
nothing calls this directly
no test coverage detected