| 10 | @pytest.mark.xfail("env.PYPY", reason="sometimes comes out 1 off on PyPy", strict=False) |
| 11 | @pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") |
| 12 | def test_keep_alive_argument(capture): |
| 13 | n_inst = ConstructorStats.detail_reg_inst() |
| 14 | with capture: |
| 15 | p = m.Parent() |
| 16 | assert capture == "Allocating parent." |
| 17 | with capture: |
| 18 | p.addChild(m.Child()) |
| 19 | assert ConstructorStats.detail_reg_inst() == n_inst + 1 |
| 20 | assert ( |
| 21 | capture |
| 22 | == """ |
| 23 | Allocating child. |
| 24 | Releasing child. |
| 25 | """ |
| 26 | ) |
| 27 | with capture: |
| 28 | del p |
| 29 | assert ConstructorStats.detail_reg_inst() == n_inst |
| 30 | assert capture == "Releasing parent." |
| 31 | |
| 32 | with capture: |
| 33 | p = m.Parent() |
| 34 | assert capture == "Allocating parent." |
| 35 | with capture: |
| 36 | p.addChildKeepAlive(m.Child()) |
| 37 | assert ConstructorStats.detail_reg_inst() == n_inst + 2 |
| 38 | assert capture == "Allocating child." |
| 39 | with capture: |
| 40 | del p |
| 41 | assert ConstructorStats.detail_reg_inst() == n_inst |
| 42 | assert ( |
| 43 | capture |
| 44 | == """ |
| 45 | Releasing parent. |
| 46 | Releasing child. |
| 47 | """ |
| 48 | ) |
| 49 | |
| 50 | p = m.Parent() |
| 51 | c = m.Child() |
| 52 | assert ConstructorStats.detail_reg_inst() == n_inst + 2 |
| 53 | m.free_function(p, c) |
| 54 | del c |
| 55 | assert ConstructorStats.detail_reg_inst() == n_inst + 2 |
| 56 | del p |
| 57 | assert ConstructorStats.detail_reg_inst() == n_inst |
| 58 | |
| 59 | with pytest.raises(RuntimeError) as excinfo: |
| 60 | m.invalid_arg_index() |
| 61 | assert str(excinfo.value) == "Could not activate keep_alive!" |
| 62 | |
| 63 | |
| 64 | @pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") |