blob: de4ec9690e860b457777136325fb2d2ecc2d8b82 [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):
119 pass
120
121 n_inst = ConstructorStats.detail_reg_inst()
122 p = Derived()
123 p.addChildKeepAlive(Child())
124 # +3 rather than +2 because Derived corresponds to two registered instances
125 assert ConstructorStats.detail_reg_inst() == n_inst + 3
126 lst = [p]
127 lst.append(lst) # creates a circular reference
128 with capture:
129 del p, lst
130 assert ConstructorStats.detail_reg_inst() == n_inst
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200131 assert capture == """
132 Releasing parent.
133 Releasing child.
134 """
135
136
137def test_return_none(capture):
Bruce Merry9d698f72017-06-24 14:58:42 +0200138 from pybind11_tests import Parent, ConstructorStats
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200139
Bruce Merry9d698f72017-06-24 14:58:42 +0200140 n_inst = ConstructorStats.detail_reg_inst()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200141 with capture:
142 p = Parent()
143 assert capture == "Allocating parent."
144 with capture:
145 p.returnNullChildKeepAliveChild()
Bruce Merry9d698f72017-06-24 14:58:42 +0200146 assert ConstructorStats.detail_reg_inst() == n_inst + 1
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200147 assert capture == ""
148 with capture:
149 del p
Bruce Merry9d698f72017-06-24 14:58:42 +0200150 assert ConstructorStats.detail_reg_inst() == n_inst
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200151 assert capture == "Releasing parent."
152
153 with capture:
154 p = Parent()
155 assert capture == "Allocating parent."
156 with capture:
157 p.returnNullChildKeepAliveParent()
Bruce Merry9d698f72017-06-24 14:58:42 +0200158 assert ConstructorStats.detail_reg_inst() == n_inst + 1
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200159 assert capture == ""
160 with capture:
161 del p
Bruce Merry9d698f72017-06-24 14:58:42 +0200162 assert ConstructorStats.detail_reg_inst() == n_inst
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200163 assert capture == "Releasing parent."
Dean Moldovan1ac19032017-03-16 11:22:26 +0100164
165
166def test_call_guard():
167 from pybind11_tests import call_policies
168
169 assert call_policies.unguarded_call() == "unguarded"
170 assert call_policies.guarded_call() == "guarded"
171
172 assert call_policies.multiple_guards_correct_order() == "guarded & guarded"
173 assert call_policies.multiple_guards_wrong_order() == "unguarded & guarded"
174
175 if hasattr(call_policies, "with_gil"):
176 assert call_policies.with_gil() == "GIL held"
177 assert call_policies.without_gil() == "GIL released"