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