blob: 1cb11c222ffb7c9334e3ca291c3bc038eb2a072a [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):
5 from pybind11_tests import Parent, Child
6
7 with capture:
8 p = Parent()
9 assert capture == "Allocating parent."
10 with capture:
11 p.addChild(Child())
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +010012 pytest.gc_collect()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020013 assert capture == """
14 Allocating child.
15 Releasing child.
16 """
17 with capture:
18 del p
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +010019 pytest.gc_collect()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020020 assert capture == "Releasing parent."
21
22 with capture:
23 p = Parent()
24 assert capture == "Allocating parent."
25 with capture:
26 p.addChildKeepAlive(Child())
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +010027 pytest.gc_collect()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020028 assert capture == "Allocating child."
29 with capture:
30 del p
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +010031 pytest.gc_collect()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020032 assert capture == """
33 Releasing parent.
34 Releasing child.
35 """
36
37
38def test_keep_alive_return_value(capture):
39 from pybind11_tests import Parent
40
41 with capture:
42 p = Parent()
43 assert capture == "Allocating parent."
44 with capture:
45 p.returnChild()
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +010046 pytest.gc_collect()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020047 assert capture == """
48 Allocating child.
49 Releasing child.
50 """
51 with capture:
52 del p
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +010053 pytest.gc_collect()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020054 assert capture == "Releasing parent."
55
56 with capture:
57 p = Parent()
58 assert capture == "Allocating parent."
59 with capture:
60 p.returnChildKeepAlive()
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +010061 pytest.gc_collect()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020062 assert capture == "Allocating child."
63 with capture:
64 del p
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +010065 pytest.gc_collect()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020066 assert capture == """
67 Releasing parent.
68 Releasing child.
69 """
70
71
72def test_return_none(capture):
73 from pybind11_tests import Parent
74
75 with capture:
76 p = Parent()
77 assert capture == "Allocating parent."
78 with capture:
79 p.returnNullChildKeepAliveChild()
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +010080 pytest.gc_collect()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020081 assert capture == ""
82 with capture:
83 del p
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +010084 pytest.gc_collect()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020085 assert capture == "Releasing parent."
86
87 with capture:
88 p = Parent()
89 assert capture == "Allocating parent."
90 with capture:
91 p.returnNullChildKeepAliveParent()
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +010092 pytest.gc_collect()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020093 assert capture == ""
94 with capture:
95 del p
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +010096 pytest.gc_collect()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020097 assert capture == "Releasing parent."
Dean Moldovan1ac19032017-03-16 11:22:26 +010098
99
100def test_call_guard():
101 from pybind11_tests import call_policies
102
103 assert call_policies.unguarded_call() == "unguarded"
104 assert call_policies.guarded_call() == "guarded"
105
106 assert call_policies.multiple_guards_correct_order() == "guarded & guarded"
107 assert call_policies.multiple_guards_wrong_order() == "unguarded & guarded"
108
109 if hasattr(call_policies, "with_gil"):
110 assert call_policies.with_gil() == "GIL held"
111 assert call_policies.without_gil() == "GIL released"