blob: 42d0ffd7b7d160afbe6bc55be70563b9356ff38f [file] [log] [blame]
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001import pytest
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02002
3
4def test_keep_alive_argument(capture):
Bruce Merry9d698f72017-06-24 14:58:42 +02005 from pybind11_tests import Parent, Child, ConstructorStats
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02006
Bruce Merry9d698f72017-06-24 14:58:42 +02007 n_inst = ConstructorStats.detail_reg_inst()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02008 with capture:
9 p = Parent()
10 assert capture == "Allocating parent."
11 with capture:
12 p.addChild(Child())
Bruce Merry9d698f72017-06-24 14:58:42 +020013 assert ConstructorStats.detail_reg_inst() == n_inst + 1
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020014 assert capture == """
15 Allocating child.
16 Releasing child.
17 """
18 with capture:
19 del p
Bruce Merry9d698f72017-06-24 14:58:42 +020020 assert ConstructorStats.detail_reg_inst() == n_inst
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020021 assert capture == "Releasing parent."
22
23 with capture:
24 p = Parent()
25 assert capture == "Allocating parent."
26 with capture:
27 p.addChildKeepAlive(Child())
Bruce Merry9d698f72017-06-24 14:58:42 +020028 assert ConstructorStats.detail_reg_inst() == n_inst + 2
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020029 assert capture == "Allocating child."
30 with capture:
31 del p
Bruce Merry9d698f72017-06-24 14:58:42 +020032 assert ConstructorStats.detail_reg_inst() == n_inst
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020033 assert capture == """
34 Releasing parent.
35 Releasing child.
36 """
37
38
39def test_keep_alive_return_value(capture):
Bruce Merry9d698f72017-06-24 14:58:42 +020040 from pybind11_tests import Parent, ConstructorStats
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020041
Bruce Merry9d698f72017-06-24 14:58:42 +020042 n_inst = ConstructorStats.detail_reg_inst()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020043 with capture:
44 p = Parent()
45 assert capture == "Allocating parent."
46 with capture:
47 p.returnChild()
Bruce Merry9d698f72017-06-24 14:58:42 +020048 assert ConstructorStats.detail_reg_inst() == n_inst + 1
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020049 assert capture == """
50 Allocating child.
51 Releasing child.
52 """
53 with capture:
54 del p
Bruce Merry9d698f72017-06-24 14:58:42 +020055 assert ConstructorStats.detail_reg_inst() == n_inst
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020056 assert capture == "Releasing parent."
57
58 with capture:
59 p = Parent()
60 assert capture == "Allocating parent."
61 with capture:
62 p.returnChildKeepAlive()
Bruce Merry9d698f72017-06-24 14:58:42 +020063 assert ConstructorStats.detail_reg_inst() == n_inst + 2
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020064 assert capture == "Allocating child."
65 with capture:
66 del p
Bruce Merry9d698f72017-06-24 14:58:42 +020067 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
76def 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
94def 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
115def test_alive_gc_multi_derived(capture):
116 from pybind11_tests import Parent, Child, ConstructorStats
117
118 class Derived(Parent, Child):
Jason Rhinelander353615f2017-07-25 00:53:23 -0400119 def __init__(self):
120 Parent.__init__(self)
121 Child.__init__(self)
Bruce Merry9d698f72017-06-24 14:58:42 +0200122
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 Moldovana0c1ccf2016-08-12 13:50:00 +0200133 assert capture == """
134 Releasing parent.
135 Releasing child.
Jason Rhinelander353615f2017-07-25 00:53:23 -0400136 Releasing child.
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200137 """
138
139
140def test_return_none(capture):
Bruce Merry9d698f72017-06-24 14:58:42 +0200141 from pybind11_tests import Parent, ConstructorStats
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200142
Bruce Merry9d698f72017-06-24 14:58:42 +0200143 n_inst = ConstructorStats.detail_reg_inst()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200144 with capture:
145 p = Parent()
146 assert capture == "Allocating parent."
147 with capture:
148 p.returnNullChildKeepAliveChild()
Bruce Merry9d698f72017-06-24 14:58:42 +0200149 assert ConstructorStats.detail_reg_inst() == n_inst + 1
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200150 assert capture == ""
151 with capture:
152 del p
Bruce Merry9d698f72017-06-24 14:58:42 +0200153 assert ConstructorStats.detail_reg_inst() == n_inst
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200154 assert capture == "Releasing parent."
155
156 with capture:
157 p = Parent()
158 assert capture == "Allocating parent."
159 with capture:
160 p.returnNullChildKeepAliveParent()
Bruce Merry9d698f72017-06-24 14:58:42 +0200161 assert ConstructorStats.detail_reg_inst() == n_inst + 1
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200162 assert capture == ""
163 with capture:
164 del p
Bruce Merry9d698f72017-06-24 14:58:42 +0200165 assert ConstructorStats.detail_reg_inst() == n_inst
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200166 assert capture == "Releasing parent."
Dean Moldovan1ac19032017-03-16 11:22:26 +0100167
168
169def 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"