`A` only initializes its trampoline class when we inherit from it If we just create and use an A instance directly, the trampoline initialization is bypassed and we only initialize an A() instead (for performance reasons).
(capture)
| 96 | |
| 97 | @pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") |
| 98 | def test_alias_delay_initialization1(capture): |
| 99 | """`A` only initializes its trampoline class when we inherit from it |
| 100 | |
| 101 | If we just create and use an A instance directly, the trampoline initialization is |
| 102 | bypassed and we only initialize an A() instead (for performance reasons). |
| 103 | """ |
| 104 | |
| 105 | class B(m.A): |
| 106 | def __init__(self): |
| 107 | super().__init__() |
| 108 | |
| 109 | def f(self): |
| 110 | print("In python f()") |
| 111 | |
| 112 | # C++ version |
| 113 | with capture: |
| 114 | a = m.A() |
| 115 | m.call_f(a) |
| 116 | del a |
| 117 | pytest.gc_collect() |
| 118 | assert capture == "A.f()" |
| 119 | |
| 120 | # Python version |
| 121 | with capture: |
| 122 | b = B() |
| 123 | m.call_f(b) |
| 124 | del b |
| 125 | pytest.gc_collect() |
| 126 | assert ( |
| 127 | capture |
| 128 | == """ |
| 129 | PyA.PyA() |
| 130 | PyA.f() |
| 131 | In python f() |
| 132 | PyA.~PyA() |
| 133 | """ |
| 134 | ) |
| 135 | |
| 136 | |
| 137 | @pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") |