|
2 | 2 |
|
3 | 3 |
|
4 | 4 | def test_keep_alive_argument(capture):
|
5 |
| - from pybind11_tests import Parent, Child |
| 5 | + from pybind11_tests import Parent, Child, ConstructorStats |
6 | 6 |
|
| 7 | + n_inst = ConstructorStats.detail_reg_inst() |
7 | 8 | with capture:
|
8 | 9 | p = Parent()
|
9 | 10 | assert capture == "Allocating parent."
|
10 | 11 | with capture:
|
11 | 12 | p.addChild(Child())
|
12 |
| - pytest.gc_collect() |
| 13 | + assert ConstructorStats.detail_reg_inst() == n_inst + 1 |
13 | 14 | assert capture == """
|
14 | 15 | Allocating child.
|
15 | 16 | Releasing child.
|
16 | 17 | """
|
17 | 18 | with capture:
|
18 | 19 | del p
|
19 |
| - pytest.gc_collect() |
| 20 | + assert ConstructorStats.detail_reg_inst() == n_inst |
20 | 21 | assert capture == "Releasing parent."
|
21 | 22 |
|
22 | 23 | with capture:
|
23 | 24 | p = Parent()
|
24 | 25 | assert capture == "Allocating parent."
|
25 | 26 | with capture:
|
26 | 27 | p.addChildKeepAlive(Child())
|
27 |
| - pytest.gc_collect() |
| 28 | + assert ConstructorStats.detail_reg_inst() == n_inst + 2 |
28 | 29 | assert capture == "Allocating child."
|
29 | 30 | with capture:
|
30 | 31 | del p
|
31 |
| - pytest.gc_collect() |
| 32 | + assert ConstructorStats.detail_reg_inst() == n_inst |
32 | 33 | assert capture == """
|
33 | 34 | Releasing parent.
|
34 | 35 | Releasing child.
|
35 | 36 | """
|
36 | 37 |
|
37 | 38 |
|
38 | 39 | def test_keep_alive_return_value(capture):
|
39 |
| - from pybind11_tests import Parent |
| 40 | + from pybind11_tests import Parent, ConstructorStats |
40 | 41 |
|
| 42 | + n_inst = ConstructorStats.detail_reg_inst() |
41 | 43 | with capture:
|
42 | 44 | p = Parent()
|
43 | 45 | assert capture == "Allocating parent."
|
44 | 46 | with capture:
|
45 | 47 | p.returnChild()
|
46 |
| - pytest.gc_collect() |
| 48 | + assert ConstructorStats.detail_reg_inst() == n_inst + 1 |
47 | 49 | assert capture == """
|
48 | 50 | Allocating child.
|
49 | 51 | Releasing child.
|
50 | 52 | """
|
51 | 53 | with capture:
|
52 | 54 | del p
|
53 |
| - pytest.gc_collect() |
| 55 | + assert ConstructorStats.detail_reg_inst() == n_inst |
54 | 56 | assert capture == "Releasing parent."
|
55 | 57 |
|
56 | 58 | with capture:
|
57 | 59 | p = Parent()
|
58 | 60 | assert capture == "Allocating parent."
|
59 | 61 | with capture:
|
60 | 62 | p.returnChildKeepAlive()
|
61 |
| - pytest.gc_collect() |
| 63 | + assert ConstructorStats.detail_reg_inst() == n_inst + 2 |
62 | 64 | assert capture == "Allocating child."
|
63 | 65 | with capture:
|
64 | 66 | del p
|
65 |
| - pytest.gc_collect() |
| 67 | + assert ConstructorStats.detail_reg_inst() == n_inst |
| 68 | + assert capture == """ |
| 69 | + Releasing parent. |
| 70 | + Releasing child. |
| 71 | + """ |
| 72 | + |
| 73 | + |
| 74 | +# https://bitbucket.org/pypy/pypy/issues/2447 |
| 75 | +@pytest.unsupported_on_pypy |
| 76 | +def test_alive_gc(capture): |
| 77 | + from pybind11_tests import ParentGC, Child, ConstructorStats |
| 78 | + |
| 79 | + n_inst = ConstructorStats.detail_reg_inst() |
| 80 | + p = ParentGC() |
| 81 | + p.addChildKeepAlive(Child()) |
| 82 | + assert ConstructorStats.detail_reg_inst() == n_inst + 2 |
| 83 | + lst = [p] |
| 84 | + lst.append(lst) # creates a circular reference |
| 85 | + with capture: |
| 86 | + del p, lst |
| 87 | + assert ConstructorStats.detail_reg_inst() == n_inst |
| 88 | + assert capture == """ |
| 89 | + Releasing parent. |
| 90 | + Releasing child. |
| 91 | + """ |
| 92 | + |
| 93 | + |
| 94 | +def test_alive_gc_derived(capture): |
| 95 | + from pybind11_tests import Parent, Child, ConstructorStats |
| 96 | + |
| 97 | + class Derived(Parent): |
| 98 | + pass |
| 99 | + |
| 100 | + n_inst = ConstructorStats.detail_reg_inst() |
| 101 | + p = Derived() |
| 102 | + p.addChildKeepAlive(Child()) |
| 103 | + assert ConstructorStats.detail_reg_inst() == n_inst + 2 |
| 104 | + lst = [p] |
| 105 | + lst.append(lst) # creates a circular reference |
| 106 | + with capture: |
| 107 | + del p, lst |
| 108 | + assert ConstructorStats.detail_reg_inst() == n_inst |
| 109 | + assert capture == """ |
| 110 | + Releasing parent. |
| 111 | + Releasing child. |
| 112 | + """ |
| 113 | + |
| 114 | + |
| 115 | +def test_alive_gc_multi_derived(capture): |
| 116 | + from pybind11_tests import Parent, Child, ConstructorStats |
| 117 | + |
| 118 | + class Derived(Parent, Child): |
| 119 | + pass |
| 120 | + |
| 121 | + n_inst = ConstructorStats.detail_reg_inst() |
| 122 | + p = Derived() |
| 123 | + p.addChildKeepAlive(Child()) |
| 124 | + # +3 rather than +2 because Derived corresponds to two registered instances |
| 125 | + assert ConstructorStats.detail_reg_inst() == n_inst + 3 |
| 126 | + lst = [p] |
| 127 | + lst.append(lst) # creates a circular reference |
| 128 | + with capture: |
| 129 | + del p, lst |
| 130 | + assert ConstructorStats.detail_reg_inst() == n_inst |
66 | 131 | assert capture == """
|
67 | 132 | Releasing parent.
|
68 | 133 | Releasing child.
|
69 | 134 | """
|
70 | 135 |
|
71 | 136 |
|
72 | 137 | def test_return_none(capture):
|
73 |
| - from pybind11_tests import Parent |
| 138 | + from pybind11_tests import Parent, ConstructorStats |
74 | 139 |
|
| 140 | + n_inst = ConstructorStats.detail_reg_inst() |
75 | 141 | with capture:
|
76 | 142 | p = Parent()
|
77 | 143 | assert capture == "Allocating parent."
|
78 | 144 | with capture:
|
79 | 145 | p.returnNullChildKeepAliveChild()
|
80 |
| - pytest.gc_collect() |
| 146 | + assert ConstructorStats.detail_reg_inst() == n_inst + 1 |
81 | 147 | assert capture == ""
|
82 | 148 | with capture:
|
83 | 149 | del p
|
84 |
| - pytest.gc_collect() |
| 150 | + assert ConstructorStats.detail_reg_inst() == n_inst |
85 | 151 | assert capture == "Releasing parent."
|
86 | 152 |
|
87 | 153 | with capture:
|
88 | 154 | p = Parent()
|
89 | 155 | assert capture == "Allocating parent."
|
90 | 156 | with capture:
|
91 | 157 | p.returnNullChildKeepAliveParent()
|
92 |
| - pytest.gc_collect() |
| 158 | + assert ConstructorStats.detail_reg_inst() == n_inst + 1 |
93 | 159 | assert capture == ""
|
94 | 160 | with capture:
|
95 | 161 | del p
|
96 |
| - pytest.gc_collect() |
| 162 | + assert ConstructorStats.detail_reg_inst() == n_inst |
97 | 163 | assert capture == "Releasing parent."
|
98 | 164 |
|
99 | 165 |
|
|
0 commit comments