~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to pypy/translator/c/test/test_boehm.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import py
 
2
from pypy.translator.translator import TranslationContext
 
3
from pypy.rpython.lltypesystem.lltype import Void
 
4
from pypy.translator.tool.cbuild import check_boehm_presence
 
5
from pypy.translator.c.genc import CExtModuleBuilder
 
6
from pypy import conftest
 
7
 
 
8
def setup_module(mod):
 
9
    if not check_boehm_presence():
 
10
        py.test.skip("Boehm GC not present")
 
11
 
 
12
class AbstractGCTestClass:
 
13
    gcpolicy = "boehm"
 
14
   
 
15
    # deal with cleanups
 
16
    def setup_method(self, meth):
 
17
        self._cleanups = []
 
18
    def teardown_method(self, meth):
 
19
        while self._cleanups:
 
20
            #print "CLEANUP"
 
21
            self._cleanups.pop()()
 
22
 
 
23
    def getcompiled(self, func, argstypelist = [],
 
24
                    annotatorpolicy=None):
 
25
        from pypy.config.pypyoption import get_pypy_config
 
26
        config = get_pypy_config(translating=True)
 
27
        config.translation.gc = self.gcpolicy
 
28
        config.translation.simplifying = True
 
29
        t = TranslationContext(config=config)
 
30
        self.t = t
 
31
        a = t.buildannotator(policy=annotatorpolicy)
 
32
        a.build_types(func, argstypelist)
 
33
        t.buildrtyper().specialize()
 
34
        t.checkgraphs()
 
35
        def compile():
 
36
            cbuilder = CExtModuleBuilder(t, func, config=config)
 
37
            c_source_filename = cbuilder.generate_source()
 
38
            if conftest.option.view:
 
39
                t.view()
 
40
            cbuilder.compile()
 
41
            mod = cbuilder.isolated_import()
 
42
            self._cleanups.append(cbuilder.cleanup) # schedule cleanup after test
 
43
            return cbuilder.get_entry_point()
 
44
        return compile()
 
45
 
 
46
 
 
47
class TestUsingBoehm(AbstractGCTestClass):
 
48
    gcpolicy = "boehm"
 
49
 
 
50
    def test_malloc_a_lot(self):
 
51
        def malloc_a_lot():
 
52
            i = 0
 
53
            while i < 10:
 
54
                i += 1
 
55
                a = [1] * 10
 
56
                j = 0
 
57
                while j < 20:
 
58
                    j += 1
 
59
                    a.append(j)
 
60
        fn = self.getcompiled(malloc_a_lot)
 
61
        fn()
 
62
 
 
63
    def test__del__(self):
 
64
        from pypy.rpython.lltypesystem.lloperation import llop
 
65
        from pypy.rpython.lltypesystem import lltype
 
66
        class State:
 
67
            pass
 
68
        s = State()
 
69
        class A(object):
 
70
            def __del__(self):
 
71
                s.a_dels += 1
 
72
        class B(A):
 
73
            def __del__(self):
 
74
                s.b_dels += 1
 
75
        class C(A):
 
76
            pass
 
77
        def f():
 
78
            s.a_dels = 0
 
79
            s.b_dels = 0
 
80
            A()
 
81
            B()
 
82
            C()
 
83
            A()
 
84
            B()
 
85
            C()
 
86
            llop.gc__collect(lltype.Void)
 
87
            return s.a_dels * 10 + s.b_dels
 
88
        fn = self.getcompiled(f)
 
89
        # we can't demand that boehm has collected all of the objects,
 
90
        # even with the gc__collect call.  calling the compiled
 
91
        # function twice seems to help, though.
 
92
        res = 0
 
93
        res += fn()
 
94
        res += fn()
 
95
        # if res is still 0, then we haven't tested anything so fail.
 
96
        # it might be the test's fault though.
 
97
        assert 0 < res <= 84
 
98
 
 
99
    def test_weakgcaddress_is_weak(self):
 
100
        from pypy.rpython.lltypesystem.lloperation import llop
 
101
        from pypy.rpython.lltypesystem import lltype
 
102
        from pypy.rlib.objectmodel import cast_object_to_weakgcaddress
 
103
        class State:
 
104
            pass
 
105
        s = State()
 
106
        class A(object):
 
107
            def __del__(self):
 
108
                s.a_dels += 1
 
109
        def f(i):
 
110
            if i:
 
111
                s.a_dels = 0
 
112
                a = A()
 
113
                # this should not keep a alive
 
114
                s.a = cast_object_to_weakgcaddress(a)
 
115
                a = None
 
116
            llop.gc__collect(lltype.Void)
 
117
            llop.gc__collect(lltype.Void)
 
118
            llop.gc__collect(lltype.Void)
 
119
            return s.a_dels
 
120
        fn = self.getcompiled(f, [int])
 
121
        # we can't demand that boehm has collected all of the objects,
 
122
        # even with the gc__collect call.  calling the compiled
 
123
        # function twice seems to help, though.
 
124
        fn(1)
 
125
        fn(0)
 
126
        fn(0)
 
127
        res = fn(0)
 
128
        print res
 
129
        assert res == 1
 
130
 
 
131
    def test_del_raises(self):
 
132
        from pypy.rpython.lltypesystem.lloperation import llop
 
133
        from pypy.rpython.lltypesystem import lltype
 
134
        import os
 
135
        class A(object):
 
136
            def __del__(self):
 
137
                s.dels += 1
 
138
                raise Exception
 
139
        class State:
 
140
            pass
 
141
        s = State()
 
142
        s.dels = 0
 
143
        def g():
 
144
            a = A()
 
145
        def f():
 
146
            s.dels = 0
 
147
            for i in range(10):
 
148
                g()
 
149
            llop.gc__collect(lltype.Void)
 
150
            return s.dels
 
151
        fn = self.getcompiled(f)
 
152
        # we can't demand that boehm has collected all of the objects,
 
153
        # even with the gc__collect call.  calling the compiled
 
154
        # function twice seems to help, though.
 
155
        res = 0
 
156
        res += fn()
 
157
        res += fn()
 
158
        # if res is still 0, then we haven't tested anything so fail.
 
159
        # it might be the test's fault though.
 
160
        assert res > 0
 
161
 
 
162
    def test_memory_error_varsize(self):
 
163
        from pypy.rpython.lltypesystem import lltype
 
164
        N = int(2**31-1)
 
165
        A = lltype.GcArray(lltype.Char)
 
166
        def alloc(n):
 
167
            return lltype.malloc(A, n)
 
168
        def f():
 
169
            try:
 
170
                x = alloc(N)
 
171
            except MemoryError:
 
172
                y = alloc(10)
 
173
                return len(y)
 
174
            y = alloc(10)
 
175
            return len(y) # allocation may work on 64 bits machines
 
176
        fn = self.getcompiled(f)
 
177
        res = fn()
 
178
        assert res == 10
 
179
        
 
180
    # this test shows if we have a problem with refcounting PyObject
 
181
    def test_refcount_pyobj(self):
 
182
        from pypy.rpython.lltypesystem.lloperation import llop
 
183
        def prob_with_pyobj(b):
 
184
            return 3, b
 
185
        def collect():
 
186
            llop.gc__collect(Void)
 
187
        f = self.getcompiled(prob_with_pyobj, [object])
 
188
        c = self.getcompiled(collect, [])
 
189
        from sys import getrefcount as g
 
190
        obj = None
 
191
        before = g(obj)
 
192
        f(obj)
 
193
        f(obj)
 
194
        f(obj)
 
195
        f(obj)
 
196
        f(obj)
 
197
        c()
 
198
        c()
 
199
        c()
 
200
        c()
 
201
        c()
 
202
        after = g(obj)
 
203
        assert abs(before - after) < 5
 
204
 
 
205
 
 
206
class TestUsingExactBoehm(TestUsingBoehm):
 
207
    gcpolicy = "exact_boehm"
 
208
 
 
209