Wenzel Jakob | 1d1f81b | 2016-12-16 15:00:46 +0100 | [diff] [blame] | 1 | import pytest |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 2 | |
| 3 | |
| 4 | def test_keep_alive_argument(capture): |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 5 | from pybind11_tests import Parent, Child, ConstructorStats |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 6 | |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 7 | n_inst = ConstructorStats.detail_reg_inst() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 8 | with capture: |
| 9 | p = Parent() |
| 10 | assert capture == "Allocating parent." |
| 11 | with capture: |
| 12 | p.addChild(Child()) |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 13 | assert ConstructorStats.detail_reg_inst() == n_inst + 1 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 14 | assert capture == """ |
| 15 | Allocating child. |
| 16 | Releasing child. |
| 17 | """ |
| 18 | with capture: |
| 19 | del p |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 20 | assert ConstructorStats.detail_reg_inst() == n_inst |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 21 | assert capture == "Releasing parent." |
| 22 | |
| 23 | with capture: |
| 24 | p = Parent() |
| 25 | assert capture == "Allocating parent." |
| 26 | with capture: |
| 27 | p.addChildKeepAlive(Child()) |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 28 | assert ConstructorStats.detail_reg_inst() == n_inst + 2 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 29 | assert capture == "Allocating child." |
| 30 | with capture: |
| 31 | del p |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 32 | assert ConstructorStats.detail_reg_inst() == n_inst |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 33 | assert capture == """ |
| 34 | Releasing parent. |
| 35 | Releasing child. |
| 36 | """ |
| 37 | |
| 38 | |
| 39 | def test_keep_alive_return_value(capture): |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 40 | from pybind11_tests import Parent, ConstructorStats |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 41 | |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 42 | n_inst = ConstructorStats.detail_reg_inst() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 43 | with capture: |
| 44 | p = Parent() |
| 45 | assert capture == "Allocating parent." |
| 46 | with capture: |
| 47 | p.returnChild() |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 48 | assert ConstructorStats.detail_reg_inst() == n_inst + 1 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 49 | assert capture == """ |
| 50 | Allocating child. |
| 51 | Releasing child. |
| 52 | """ |
| 53 | with capture: |
| 54 | del p |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 55 | assert ConstructorStats.detail_reg_inst() == n_inst |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 56 | assert capture == "Releasing parent." |
| 57 | |
| 58 | with capture: |
| 59 | p = Parent() |
| 60 | assert capture == "Allocating parent." |
| 61 | with capture: |
| 62 | p.returnChildKeepAlive() |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 63 | assert ConstructorStats.detail_reg_inst() == n_inst + 2 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 64 | assert capture == "Allocating child." |
| 65 | with capture: |
| 66 | del p |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 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 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 131 | assert capture == """ |
| 132 | Releasing parent. |
| 133 | Releasing child. |
| 134 | """ |
| 135 | |
| 136 | |
| 137 | def test_return_none(capture): |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 138 | from pybind11_tests import Parent, ConstructorStats |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 139 | |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 140 | n_inst = ConstructorStats.detail_reg_inst() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 141 | with capture: |
| 142 | p = Parent() |
| 143 | assert capture == "Allocating parent." |
| 144 | with capture: |
| 145 | p.returnNullChildKeepAliveChild() |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 146 | assert ConstructorStats.detail_reg_inst() == n_inst + 1 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 147 | assert capture == "" |
| 148 | with capture: |
| 149 | del p |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 150 | assert ConstructorStats.detail_reg_inst() == n_inst |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 151 | assert capture == "Releasing parent." |
| 152 | |
| 153 | with capture: |
| 154 | p = Parent() |
| 155 | assert capture == "Allocating parent." |
| 156 | with capture: |
| 157 | p.returnNullChildKeepAliveParent() |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 158 | assert ConstructorStats.detail_reg_inst() == n_inst + 1 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 159 | assert capture == "" |
| 160 | with capture: |
| 161 | del p |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 162 | assert ConstructorStats.detail_reg_inst() == n_inst |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 163 | assert capture == "Releasing parent." |
Dean Moldovan | 1ac1903 | 2017-03-16 11:22:26 +0100 | [diff] [blame] | 164 | |
| 165 | |
| 166 | def test_call_guard(): |
| 167 | from pybind11_tests import call_policies |
| 168 | |
| 169 | assert call_policies.unguarded_call() == "unguarded" |
| 170 | assert call_policies.guarded_call() == "guarded" |
| 171 | |
| 172 | assert call_policies.multiple_guards_correct_order() == "guarded & guarded" |
| 173 | assert call_policies.multiple_guards_wrong_order() == "unguarded & guarded" |
| 174 | |
| 175 | if hasattr(call_policies, "with_gil"): |
| 176 | assert call_policies.with_gil() == "GIL held" |
| 177 | assert call_policies.without_gil() == "GIL released" |