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