| 1 | from rpython.memory.gctransform.boehm import BoehmGCTransformer |
| 2 | from rpython.memory.gctransform.test.test_transform import rtype_and_transform, getops |
| 3 | from rpython.memory.gctransform.test.test_refcounting import make_deallocator |
| 4 | from rpython.rtyper.lltypesystem import lltype |
| 5 | from rpython.translator.translator import graphof |
| 6 | from rpython.translator.c.gc import BoehmGcPolicy |
| 7 | from rpython.memory.gctransform.test.test_transform import LLInterpedTranformerTests |
| 8 | |
| 9 | |
| 10 | class TestLLInterpedBoehm(LLInterpedTranformerTests): |
| 11 | gcpolicy = BoehmGcPolicy |
| 12 | |
| 13 | def make_boehm_finalizer(TYPE): |
| 14 | return make_deallocator(TYPE, attr="finalizer_funcptr_for_type", |
| 15 | cls=BoehmGCTransformer) |
| 16 | |
| 17 | def test_boehm_simple(): |
| 18 | class C: |
| 19 | pass |
| 20 | def f(): |
| 21 | c = C() |
| 22 | c.x = 1 |
| 23 | return c.x |
| 24 | t, transformer = rtype_and_transform( |
| 25 | f, [], BoehmGCTransformer, check=False) |
| 26 | ops = getops(graphof(t, f)) |
| 27 | assert len(ops.get('direct_call', [])) <= 1 |
| 28 | gcs = [k for k in ops if k.startswith('gc')] |
| 29 | assert len(gcs) == 0 |
| 30 | |
| 31 | def test_boehm_finalizer_simple(): |
| 32 | S = lltype.GcStruct("S", ('x', lltype.Signed)) |
| 33 | f, t = make_boehm_finalizer(S) |
| 34 | assert f is None |
| 35 | |
| 36 | def test_boehm_finalizer___del__(): |
| 37 | S = lltype.GcStruct("S", ('x', lltype.Signed), rtti=True) |
| 38 | def f(s): |
| 39 | s.x = 1 |
| 40 | def type_info_S(p): |
| 41 | return lltype.getRuntimeTypeInfo(S) |
| 42 | qp = lltype.functionptr(lltype.FuncType([lltype.Ptr(S)], |
| 43 | lltype.Ptr(lltype.RuntimeTypeInfo)), |
| 44 | "type_info_S", |
| 45 | _callable=type_info_S) |
| 46 | dp = lltype.functionptr(lltype.FuncType([lltype.Ptr(S)], |
| 47 | lltype.Void), |
| 48 | "destructor_funcptr", |
| 49 | _callable=f) |
| 50 | lltype.attachRuntimeTypeInfo(S, qp, destrptr=dp) |
| 51 | f, t = make_boehm_finalizer(S) |
| 52 | assert f is not None |