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
9
if not check_boehm_presence():
10
py.test.skip("Boehm GC not present")
12
class AbstractGCTestClass:
16
def setup_method(self, meth):
18
def teardown_method(self, meth):
21
self._cleanups.pop()()
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)
31
a = t.buildannotator(policy=annotatorpolicy)
32
a.build_types(func, argstypelist)
33
t.buildrtyper().specialize()
36
cbuilder = CExtModuleBuilder(t, func, config=config)
37
c_source_filename = cbuilder.generate_source()
38
if conftest.option.view:
41
mod = cbuilder.isolated_import()
42
self._cleanups.append(cbuilder.cleanup) # schedule cleanup after test
43
return cbuilder.get_entry_point()
47
class TestUsingBoehm(AbstractGCTestClass):
50
def test_malloc_a_lot(self):
60
fn = self.getcompiled(malloc_a_lot)
63
def test__del__(self):
64
from pypy.rpython.lltypesystem.lloperation import llop
65
from pypy.rpython.lltypesystem import lltype
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.
95
# if res is still 0, then we haven't tested anything so fail.
96
# it might be the test's fault though.
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
113
# this should not keep a alive
114
s.a = cast_object_to_weakgcaddress(a)
116
llop.gc__collect(lltype.Void)
117
llop.gc__collect(lltype.Void)
118
llop.gc__collect(lltype.Void)
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.
131
def test_del_raises(self):
132
from pypy.rpython.lltypesystem.lloperation import llop
133
from pypy.rpython.lltypesystem import lltype
149
llop.gc__collect(lltype.Void)
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.
158
# if res is still 0, then we haven't tested anything so fail.
159
# it might be the test's fault though.
162
def test_memory_error_varsize(self):
163
from pypy.rpython.lltypesystem import lltype
165
A = lltype.GcArray(lltype.Char)
167
return lltype.malloc(A, n)
175
return len(y) # allocation may work on 64 bits machines
176
fn = self.getcompiled(f)
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):
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
203
assert abs(before - after) < 5
206
class TestUsingExactBoehm(TestUsingBoehm):
207
gcpolicy = "exact_boehm"