Wenzel Jakob | 1d1f81b | 2016-12-16 15:00:46 +0100 | [diff] [blame] | 1 | import pytest |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 2 | from pybind11_tests import call_policies as m |
| 3 | from pybind11_tests import ConstructorStats |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 4 | |
| 5 | |
| 6 | def test_keep_alive_argument(capture): |
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: |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 9 | p = m.Parent() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 10 | assert capture == "Allocating parent." |
| 11 | with capture: |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 12 | p.addChild(m.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: |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 24 | p = m.Parent() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 25 | assert capture == "Allocating parent." |
| 26 | with capture: |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 27 | p.addChildKeepAlive(m.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 | n_inst = ConstructorStats.detail_reg_inst() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 41 | with capture: |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 42 | p = m.Parent() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 43 | assert capture == "Allocating parent." |
| 44 | with capture: |
| 45 | p.returnChild() |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 46 | assert ConstructorStats.detail_reg_inst() == n_inst + 1 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 47 | assert capture == """ |
| 48 | Allocating child. |
| 49 | Releasing child. |
| 50 | """ |
| 51 | with capture: |
| 52 | del p |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 53 | assert ConstructorStats.detail_reg_inst() == n_inst |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 54 | assert capture == "Releasing parent." |
| 55 | |
| 56 | with capture: |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 57 | p = m.Parent() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 58 | assert capture == "Allocating parent." |
| 59 | with capture: |
| 60 | p.returnChildKeepAlive() |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 61 | assert ConstructorStats.detail_reg_inst() == n_inst + 2 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 62 | assert capture == "Allocating child." |
| 63 | with capture: |
| 64 | del p |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 65 | assert ConstructorStats.detail_reg_inst() == n_inst |
| 66 | assert capture == """ |
| 67 | Releasing parent. |
| 68 | Releasing child. |
| 69 | """ |
| 70 | |
| 71 | |
| 72 | # https://bitbucket.org/pypy/pypy/issues/2447 |
| 73 | @pytest.unsupported_on_pypy |
| 74 | def test_alive_gc(capture): |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 75 | n_inst = ConstructorStats.detail_reg_inst() |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 76 | p = m.ParentGC() |
| 77 | p.addChildKeepAlive(m.Child()) |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 78 | assert ConstructorStats.detail_reg_inst() == n_inst + 2 |
| 79 | lst = [p] |
| 80 | lst.append(lst) # creates a circular reference |
| 81 | with capture: |
| 82 | del p, lst |
| 83 | assert ConstructorStats.detail_reg_inst() == n_inst |
| 84 | assert capture == """ |
| 85 | Releasing parent. |
| 86 | Releasing child. |
| 87 | """ |
| 88 | |
| 89 | |
| 90 | def test_alive_gc_derived(capture): |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 91 | class Derived(m.Parent): |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 92 | pass |
| 93 | |
| 94 | n_inst = ConstructorStats.detail_reg_inst() |
| 95 | p = Derived() |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 96 | p.addChildKeepAlive(m.Child()) |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 97 | assert ConstructorStats.detail_reg_inst() == n_inst + 2 |
| 98 | lst = [p] |
| 99 | lst.append(lst) # creates a circular reference |
| 100 | with capture: |
| 101 | del p, lst |
| 102 | assert ConstructorStats.detail_reg_inst() == n_inst |
| 103 | assert capture == """ |
| 104 | Releasing parent. |
| 105 | Releasing child. |
| 106 | """ |
| 107 | |
| 108 | |
| 109 | def test_alive_gc_multi_derived(capture): |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 110 | class Derived(m.Parent, m.Child): |
Jason Rhinelander | 353615f | 2017-07-25 00:53:23 -0400 | [diff] [blame] | 111 | def __init__(self): |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 112 | m.Parent.__init__(self) |
| 113 | m.Child.__init__(self) |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 114 | |
| 115 | n_inst = ConstructorStats.detail_reg_inst() |
| 116 | p = Derived() |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 117 | p.addChildKeepAlive(m.Child()) |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 118 | # +3 rather than +2 because Derived corresponds to two registered instances |
| 119 | assert ConstructorStats.detail_reg_inst() == n_inst + 3 |
| 120 | lst = [p] |
| 121 | lst.append(lst) # creates a circular reference |
| 122 | with capture: |
| 123 | del p, lst |
| 124 | assert ConstructorStats.detail_reg_inst() == n_inst |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 125 | assert capture == """ |
| 126 | Releasing parent. |
| 127 | Releasing child. |
Jason Rhinelander | 353615f | 2017-07-25 00:53:23 -0400 | [diff] [blame] | 128 | Releasing child. |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 129 | """ |
| 130 | |
| 131 | |
| 132 | def test_return_none(capture): |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 133 | n_inst = ConstructorStats.detail_reg_inst() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 134 | with capture: |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 135 | p = m.Parent() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 136 | assert capture == "Allocating parent." |
| 137 | with capture: |
| 138 | p.returnNullChildKeepAliveChild() |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 139 | assert ConstructorStats.detail_reg_inst() == n_inst + 1 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 140 | assert capture == "" |
| 141 | with capture: |
| 142 | del p |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 143 | assert ConstructorStats.detail_reg_inst() == n_inst |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 144 | assert capture == "Releasing parent." |
| 145 | |
| 146 | with capture: |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 147 | p = m.Parent() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 148 | assert capture == "Allocating parent." |
| 149 | with capture: |
| 150 | p.returnNullChildKeepAliveParent() |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 151 | assert ConstructorStats.detail_reg_inst() == n_inst + 1 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 152 | assert capture == "" |
| 153 | with capture: |
| 154 | del p |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 155 | assert ConstructorStats.detail_reg_inst() == n_inst |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 156 | assert capture == "Releasing parent." |
Dean Moldovan | 1ac1903 | 2017-03-16 11:22:26 +0100 | [diff] [blame] | 157 | |
| 158 | |
| 159 | def test_call_guard(): |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 160 | assert m.unguarded_call() == "unguarded" |
| 161 | assert m.guarded_call() == "guarded" |
Dean Moldovan | 1ac1903 | 2017-03-16 11:22:26 +0100 | [diff] [blame] | 162 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 163 | assert m.multiple_guards_correct_order() == "guarded & guarded" |
| 164 | assert m.multiple_guards_wrong_order() == "unguarded & guarded" |
Dean Moldovan | 1ac1903 | 2017-03-16 11:22:26 +0100 | [diff] [blame] | 165 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 166 | if hasattr(m, "with_gil"): |
| 167 | assert m.with_gil() == "GIL held" |
| 168 | assert m.without_gil() == "GIL released" |