Debian Codegraph @

rpython/memory/test/test_hybrid_gc.py

raw symbols SCIP
1 import py
2
3 from rpython.rtyper.lltypesystem import lltype
4 from rpython.rtyper.lltypesystem.lloperation import llop
5
6 from rpython.memory.test import test_generational_gc
7
8
9 class TestHybridGC(test_generational_gc.TestGenerationalGC):
10 from rpython.memory.gc.hybrid import HybridGC as GCClass
11 GC_CAN_SHRINK_BIG_ARRAY = False
12
13 def test_ref_from_rawmalloced_to_regular(self):
14 import gc
15 def concat(j):
16 lst = []
17 for i in range(j):
18 lst.append(str(i))
19 gc.collect()
20 return len("".join(lst))
21 res = self.interpret(concat, [100])
22 assert res == concat(100)
23
24 def test_longliving_weakref(self):
25 # test for the case where a weakref points to a very old object
26 # that was made non-movable after several collections
27 import gc, weakref
28 class A:
29 pass
30 def step1(x):
31 a = A()
32 a.x = 42
33 ref = weakref.ref(a)
34 i = 0
35 while i < x:
36 gc.collect()
37 i += 1
38 assert ref() is a
39 assert ref().x == 42
40 return ref
41 def step2(ref):
42 gc.collect() # 'a' is freed here
43 assert ref() is None
44 def f(x):
45 ref = step1(x)
46 step2(ref)
47 self.interpret(f, [10])
48
49 def test_longliving_object_with_finalizer(self):
50 class B(object):
51 pass
52 b = B()
53 b.nextid = 0
54 b.num_deleted = 0
55 class A(object):
56 def __init__(self):
57 self.id = b.nextid
58 b.nextid += 1
59 def __del__(self):
60 b.num_deleted += 1
61 def f(x):
62 a = A()
63 i = 0
64 while i < x:
65 i += 1
66 a = A()
67 llop.gc__collect(lltype.Void)
68 llop.gc__collect(lltype.Void)
69 llop.gc__collect(lltype.Void)
70 return b.num_deleted
71 res = self.interpret(f, [15])
72 assert res == 16