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): |
Jason Rhinelander | 353615f | 2017-07-25 00:53:23 -0400 | [diff] [blame] | 119 | def __init__(self): |
| 120 | Parent.__init__(self) |
| 121 | Child.__init__(self) |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 122 | |
| 123 | n_inst = ConstructorStats.detail_reg_inst() |
| 124 | p = Derived() |
| 125 | p.addChildKeepAlive(Child()) |
| 126 | # +3 rather than +2 because Derived corresponds to two registered instances |
| 127 | assert ConstructorStats.detail_reg_inst() == n_inst + 3 |
| 128 | lst = [p] |
| 129 | lst.append(lst) # creates a circular reference |
| 130 | with capture: |
| 131 | del p, lst |
| 132 | assert ConstructorStats.detail_reg_inst() == n_inst |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 133 | assert capture == """ |
| 134 | Releasing parent. |
| 135 | Releasing child. |
Jason Rhinelander | 353615f | 2017-07-25 00:53:23 -0400 | [diff] [blame] | 136 | Releasing child. |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 137 | """ |
| 138 | |
| 139 | |
| 140 | def test_return_none(capture): |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 141 | from pybind11_tests import Parent, ConstructorStats |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 142 | |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 143 | n_inst = ConstructorStats.detail_reg_inst() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 144 | with capture: |
| 145 | p = Parent() |
| 146 | assert capture == "Allocating parent." |
| 147 | with capture: |
| 148 | p.returnNullChildKeepAliveChild() |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 149 | assert ConstructorStats.detail_reg_inst() == n_inst + 1 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 150 | assert capture == "" |
| 151 | with capture: |
| 152 | del p |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 153 | assert ConstructorStats.detail_reg_inst() == n_inst |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 154 | assert capture == "Releasing parent." |
| 155 | |
| 156 | with capture: |
| 157 | p = Parent() |
| 158 | assert capture == "Allocating parent." |
| 159 | with capture: |
| 160 | p.returnNullChildKeepAliveParent() |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 161 | assert ConstructorStats.detail_reg_inst() == n_inst + 1 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 162 | assert capture == "" |
| 163 | with capture: |
| 164 | del p |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 165 | assert ConstructorStats.detail_reg_inst() == n_inst |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 166 | assert capture == "Releasing parent." |
Dean Moldovan | 1ac1903 | 2017-03-16 11:22:26 +0100 | [diff] [blame] | 167 | |
| 168 | |
| 169 | def test_call_guard(): |
| 170 | from pybind11_tests import call_policies |
| 171 | |
| 172 | assert call_policies.unguarded_call() == "unguarded" |
| 173 | assert call_policies.guarded_call() == "guarded" |
| 174 | |
| 175 | assert call_policies.multiple_guards_correct_order() == "guarded & guarded" |
| 176 | assert call_policies.multiple_guards_wrong_order() == "unguarded & guarded" |
| 177 | |
| 178 | if hasattr(call_policies, "with_gil"): |
| 179 | assert call_policies.with_gil() == "GIL held" |
| 180 | assert call_policies.without_gil() == "GIL released" |