2
2
3
3
4
4
def test_keep_alive_argument (capture ):
5
- from pybind11_tests import Parent , Child
5
+ from pybind11_tests import Parent , Child , ConstructorStats
6
6
7
+ n_inst = ConstructorStats .detail_reg_inst ()
7
8
with capture :
8
9
p = Parent ()
9
10
assert capture == "Allocating parent."
10
11
with capture :
11
12
p .addChild (Child ())
12
- pytest . gc_collect ()
13
+ assert ConstructorStats . detail_reg_inst () == n_inst + 1
13
14
assert capture == """
14
15
Allocating child.
15
16
Releasing child.
16
17
"""
17
18
with capture :
18
19
del p
19
- pytest . gc_collect ()
20
+ assert ConstructorStats . detail_reg_inst () == n_inst
20
21
assert capture == "Releasing parent."
21
22
22
23
with capture :
23
24
p = Parent ()
24
25
assert capture == "Allocating parent."
25
26
with capture :
26
27
p .addChildKeepAlive (Child ())
27
- pytest . gc_collect ()
28
+ assert ConstructorStats . detail_reg_inst () == n_inst + 2
28
29
assert capture == "Allocating child."
29
30
with capture :
30
31
del p
31
- pytest . gc_collect ()
32
+ assert ConstructorStats . detail_reg_inst () == n_inst
32
33
assert capture == """
33
34
Releasing parent.
34
35
Releasing child.
35
36
"""
36
37
37
38
38
39
def test_keep_alive_return_value (capture ):
39
- from pybind11_tests import Parent
40
+ from pybind11_tests import Parent , ConstructorStats
40
41
42
+ n_inst = ConstructorStats .detail_reg_inst ()
41
43
with capture :
42
44
p = Parent ()
43
45
assert capture == "Allocating parent."
44
46
with capture :
45
47
p .returnChild ()
46
- pytest . gc_collect ()
48
+ assert ConstructorStats . detail_reg_inst () == n_inst + 1
47
49
assert capture == """
48
50
Allocating child.
49
51
Releasing child.
50
52
"""
51
53
with capture :
52
54
del p
53
- pytest . gc_collect ()
55
+ assert ConstructorStats . detail_reg_inst () == n_inst
54
56
assert capture == "Releasing parent."
55
57
56
58
with capture :
57
59
p = Parent ()
58
60
assert capture == "Allocating parent."
59
61
with capture :
60
62
p .returnChildKeepAlive ()
61
- pytest . gc_collect ()
63
+ assert ConstructorStats . detail_reg_inst () == n_inst + 2
62
64
assert capture == "Allocating child."
63
65
with capture :
64
66
del p
65
- pytest . gc_collect ()
67
+ assert ConstructorStats . detail_reg_inst () == n_inst
66
68
assert capture == """
67
69
Releasing parent.
68
70
Releasing child.
@@ -72,66 +74,92 @@ def test_keep_alive_return_value(capture):
72
74
# https://bitbucket.org/pypy/pypy/issues/2447
73
75
@pytest .unsupported_on_pypy
74
76
def test_alive_gc (capture ):
75
- from pybind11_tests import ParentGC , Child
77
+ from pybind11_tests import ParentGC , Child , ConstructorStats
76
78
79
+ n_inst = ConstructorStats .detail_reg_inst ()
77
80
p = ParentGC ()
78
81
p .addChildKeepAlive (Child ())
82
+ assert ConstructorStats .detail_reg_inst () == n_inst + 2
79
83
lst = [p ]
80
84
lst .append (lst ) # creates a circular reference
81
85
with capture :
82
86
del p , lst
83
- pytest . gc_collect ()
87
+ assert ConstructorStats . detail_reg_inst () == n_inst
84
88
assert capture == """
85
89
Releasing parent.
86
90
Releasing child.
87
91
"""
88
92
89
93
90
94
def test_alive_gc_derived (capture ):
91
- from pybind11_tests import Parent , Child
95
+ from pybind11_tests import Parent , Child , ConstructorStats
92
96
93
97
class Derived (Parent ):
94
98
pass
95
99
100
+ n_inst = ConstructorStats .detail_reg_inst ()
96
101
p = Derived ()
97
102
p .addChildKeepAlive (Child ())
103
+ assert ConstructorStats .detail_reg_inst () == n_inst + 2
98
104
lst = [p ]
99
105
lst .append (lst ) # creates a circular reference
100
106
with capture :
101
107
del p , lst
102
- pytest .gc_collect ()
103
- pytest .gc_collect () # Needed to make PyPy free the child
108
+ assert ConstructorStats .detail_reg_inst () == n_inst
109
+ assert capture == """
110
+ Releasing parent.
111
+ Releasing child.
112
+ """
113
+
114
+
115
+ def 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
104
131
assert capture == """
105
132
Releasing parent.
106
133
Releasing child.
107
134
"""
108
135
109
136
110
137
def test_return_none (capture ):
111
- from pybind11_tests import Parent
138
+ from pybind11_tests import Parent , ConstructorStats
112
139
140
+ n_inst = ConstructorStats .detail_reg_inst ()
113
141
with capture :
114
142
p = Parent ()
115
143
assert capture == "Allocating parent."
116
144
with capture :
117
145
p .returnNullChildKeepAliveChild ()
118
- pytest . gc_collect ()
146
+ assert ConstructorStats . detail_reg_inst () == n_inst + 1
119
147
assert capture == ""
120
148
with capture :
121
149
del p
122
- pytest . gc_collect ()
150
+ assert ConstructorStats . detail_reg_inst () == n_inst
123
151
assert capture == "Releasing parent."
124
152
125
153
with capture :
126
154
p = Parent ()
127
155
assert capture == "Allocating parent."
128
156
with capture :
129
157
p .returnNullChildKeepAliveParent ()
130
- pytest . gc_collect ()
158
+ assert ConstructorStats . detail_reg_inst () == n_inst + 1
131
159
assert capture == ""
132
160
with capture :
133
161
del p
134
- pytest . gc_collect ()
162
+ assert ConstructorStats . detail_reg_inst () == n_inst
135
163
assert capture == "Releasing parent."
136
164
137
165
0 commit comments