blob: 6532757f9c30837034c5f8aba898203a7c2d8ce4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
# mode: run
from __future__ import print_function
cimport cython
import gc
cdef class nontrivial_del:
def __init__(self):
print("init")
def __del__(self):
print("del")
def test_del():
"""
>>> test_del()
start
init
del
finish
"""
print("start")
d = nontrivial_del()
d = None
gc.collect()
print("finish")
cdef class del_and_dealloc:
def __init__(self):
print("init")
def __del__(self):
print("del")
def __dealloc__(self):
print("dealloc")
def test_del_and_dealloc():
"""
>>> test_del_and_dealloc()
start
init
del
dealloc
finish
"""
print("start")
d = del_and_dealloc()
d = None
gc.collect()
print("finish")
@cython.final
cdef class FinalClass:
def __init__(self):
print("init")
def __del__(self):
print("del")
def test_final_class():
"""
>>> test_final_class()
start
init
del
finish
"""
print("start")
d = FinalClass()
d = None
gc.collect()
print("finish")
@cython.final
cdef class FinalInherits(nontrivial_del):
def __init__(self):
super().__init__()
print("FinalInherits init")
# no __del__ but nontrivial_del should still be called
def __dealloc__(self):
pass # define __dealloc__ so as not to fall back on base __dealloc__
def test_final_inherited():
"""
>>> test_final_inherited()
start
init
FinalInherits init
del
finish
"""
print("start")
d = FinalInherits()
d = None
gc.collect()
print("finish")
cdef class DummyBase:
pass
class RegularClass:
__slots__ = ()
def __del__(self):
print("del")
@cython.final
cdef class FinalMultipleInheritance(DummyBase, RegularClass):
def __init__(self):
super().__init__()
print("init")
def __dealloc__(self):
pass
def test_final_multiple_inheritance():
"""
>>> test_final_multiple_inheritance()
start
init
del
finish
"""
print("start")
d = FinalMultipleInheritance()
d = None
gc.collect()
print("finish")
cdef class del_with_exception:
def __init__(self):
print("init")
def __del__(self):
print("del")
raise Exception("Error")
def test_del_with_exception():
"""
>>> test_del_with_exception()
start
init
del
finish
"""
print("start")
d = nontrivial_del()
d = None
gc.collect()
print("finish")
def test_nontrivial_del_with_exception():
"""
>>> test_nontrivial_del_with_exception()
start
init
del
end
"""
print("start")
def inner():
c = nontrivial_del()
raise RuntimeError()
try:
inner()
except RuntimeError:
pass
print("end")
cdef class parent:
def __del__(self):
print("del parent")
class child(parent):
def __del__(self):
print("del child")
def test_del_inheritance():
"""
>>> test_del_inheritance()
start
del child
finish
"""
print("start")
c = child()
c = None
gc.collect()
print("finish")
cdef class cy_parent:
def __del__(self):
print("del cy_parent")
def __dealloc__(self):
print("dealloc cy_parent")
class py_parent:
def __del__(self):
print("del py_parent")
class multi_child(cy_parent, py_parent):
def __del__(self):
print("del child")
def test_multiple_inheritance():
"""
>>> test_multiple_inheritance()
start
del child
dealloc cy_parent
finish
"""
print("start")
c = multi_child()
c = None
gc.collect()
print("finish")
cdef class zombie_object:
def __del__(self):
global global_zombie_object
print("del")
global_zombie_object = self
def __dealloc__(self):
print("dealloc")
def test_zombie_object():
"""
>>> test_zombie_object()
start
del
del global
del
finish
"""
global global_zombie_object
print("start")
i = zombie_object()
i = None
print("del global")
del global_zombie_object
gc.collect()
print("finish")
# Same as above, but the member
# makes the class GC, so it
# is deallocated
cdef class gc_zombie_object:
cdef object x
def __del__(self):
global global_gc_zombie_object
print("del")
global_gc_zombie_object = self
def __dealloc__(self):
print("dealloc")
def test_gc_zombie_object():
"""
>>> test_gc_zombie_object()
start
del
del global
dealloc
finish
"""
global global_gc_zombie_object
print("start")
i = gc_zombie_object()
i = None
print("del global")
del global_gc_zombie_object
gc.collect()
print("finish")
cdef class cdef_parent:
pass
cdef class cdef_child(cdef_parent):
def __del__(self):
print("del")
def __dealloc__(self):
print("dealloc")
def test_cdef_parent_object():
"""
>>> test_cdef_parent_object()
start
del
dealloc
finish
"""
print("start")
i = cdef_child()
i = None
gc.collect()
print("finish")
cdef class cdef_nontrivial_parent:
def __del__(self):
print("del parent")
def __dealloc__(self):
print("dealloc parent")
cdef class cdef_nontrivial_child(cdef_nontrivial_parent):
def __del__(self):
print("del child")
def __dealloc__(self):
print("dealloc child")
def test_cdef_nontrivial_parent_object():
"""
>>> test_cdef_nontrivial_parent_object()
start
del child
dealloc child
dealloc parent
finish
"""
print("start")
i = cdef_nontrivial_child()
i = None
gc.collect()
print("finish")
class python_child(cdef_nontrivial_parent):
def __del__(self):
print("del python child")
super().__del__()
def test_python_child_object():
"""
>>> test_python_child_object()
Traceback (most recent call last):
...
RuntimeError: End function
"""
def func(tp):
inst = tp()
raise RuntimeError("End function")
func(python_child)
def test_python_child_fancy_inherit():
"""
>>> test_python_child_fancy_inherit()
Traceback (most recent call last):
...
RuntimeError: End function
"""
# inherit using "true python" rather than Cython
globs = { 'cdef_nontrivial_parent': cdef_nontrivial_parent }
exec("""
class derived_python_child(cdef_nontrivial_parent):
pass
""", globs)
derived_python_child = globs['derived_python_child']
def func(tp):
inst = tp()
raise RuntimeError("End function")
func(derived_python_child)
|