Debian Codegraph @

rpython/translator/backendopt/test/test_finalizer.py

raw symbols SCIP
1
2 import py
3 from rpython.translator.backendopt.finalizer import FinalizerAnalyzer,\
4 FinalizerError
5 from rpython.translator.translator import TranslationContext, graphof
6 from rpython.translator.backendopt.all import backend_optimizations
7 from rpython.translator.unsimplify import varoftype
8 from rpython.rtyper.lltypesystem import lltype, rffi
9 from rpython.conftest import option
10 from rpython.rlib import rgc
11
12
13 class TestFinalizerAnalyzer(object):
14 """ Below are typical destructors that we encounter in pypy
15 """
16
17 def analyze(self, func, sig, func_to_analyze=None, backendopt=False):
18 if func_to_analyze is None:
19 func_to_analyze = func
20 t = TranslationContext()
21 t.buildannotator().build_types(func, sig)
22 t.buildrtyper().specialize()
23 if backendopt:
24 backend_optimizations(t)
25 if option.view:
26 t.view()
27 a = FinalizerAnalyzer(t)
28 fgraph = graphof(t, func_to_analyze)
29 result = a.analyze_light_finalizer(fgraph)
30 return result
31
32 def test_nothing(self):
33 def f():
34 pass
35 r = self.analyze(f, [])
36 assert not r
37
38 def test_malloc(self):
39 S = lltype.GcStruct('S')
40
41 def f():
42 return lltype.malloc(S)
43
44 r = self.analyze(f, [])
45 assert r
46
47 def test_raw_free_getfield(self):
48 S = lltype.Struct('S')
49
50 class A(object):
51 def __init__(self):
52 self.x = lltype.malloc(S, flavor='raw')
53
54 def __del__(self):
55 if self.x:
56 lltype.free(self.x, flavor='raw')
57 self.x = lltype.nullptr(S)
58
59 def f():
60 return A()
61
62 r = self.analyze(f, [], A.__del__.im_func)
63 assert not r
64
65 def test_c_call(self):
66 C = rffi.CArray(lltype.Signed)
67 c = rffi.llexternal('x', [lltype.Ptr(C)], lltype.Signed)
68
69 def g():
70 p = lltype.malloc(C, 3, flavor='raw')
71 f(p)
72
73 def f(p):
74 c(rffi.ptradd(p, 0))
75 lltype.free(p, flavor='raw')
76
77 r = self.analyze(g, [], f, backendopt=True)
78 assert r
79
80 def test_c_call_without_release_gil(self):
81 C = rffi.CArray(lltype.Signed)
82 c = rffi.llexternal('x', [lltype.Ptr(C)], lltype.Signed,
83 releasegil=False)
84
85 def g():
86 p = lltype.malloc(C, 3, flavor='raw')
87 f(p)
88
89 def f(p):
90 c(rffi.ptradd(p, 0))
91 lltype.free(p, flavor='raw')
92
93 r = self.analyze(g, [], f, backendopt=True)
94 assert not r
95
96 def test_chain(self):
97 class B(object):
98 def __init__(self):
99 self.counter = 1
100
101 class A(object):
102 def __init__(self):
103 self.x = B()
104
105 def __del__(self):
106 self.x.counter += 1
107
108 def f():
109 A()
110
111 r = self.analyze(f, [], A.__del__.im_func)
112 assert r
113
114 def test_must_be_light_finalizer_decorator(self):
115 S = lltype.GcStruct('S')
116
117 @rgc.must_be_light_finalizer
118 def f():
119 lltype.malloc(S)
120 @rgc.must_be_light_finalizer
121 def g():
122 pass
123 self.analyze(g, []) # did not explode
124 py.test.raises(FinalizerError, self.analyze, f, [])
125
126
127 def test_various_ops():
128 from rpython.flowspace.model import SpaceOperation, Constant
129
130 X = lltype.Ptr(lltype.GcStruct('X'))
131 Z = lltype.Ptr(lltype.Struct('Z'))
132 S = lltype.GcStruct('S', ('x', lltype.Signed),
133 ('y', X),
134 ('z', Z))
135 v1 = varoftype(lltype.Bool)
136 v2 = varoftype(lltype.Signed)
137 f = FinalizerAnalyzer(None)
138 r = f.analyze(SpaceOperation('cast_int_to_bool', [v2],
139 v1))
140 assert not r
141 v1 = varoftype(lltype.Ptr(S))
142 v2 = varoftype(lltype.Signed)
143 v3 = varoftype(X)
144 v4 = varoftype(Z)
145 assert not f.analyze(SpaceOperation('bare_setfield', [v1, Constant('x'),
146 v2], None))
147 assert f.analyze(SpaceOperation('bare_setfield', [v1, Constant('y'),
148 v3], None))
149 assert not f.analyze(SpaceOperation('bare_setfield', [v1, Constant('z'),
150 v4], None))
151