Debian Codegraph @

rpython/rtyper/test/test_rweakref.py

raw symbols SCIP
1 import py, weakref
2 from rpython.rlib import rgc
3 from rpython.rtyper.lltypesystem import lltype, llmemory
4 from rpython.rtyper.test.tool import BaseRtypingTest
5
6 class TestRweakref(BaseRtypingTest):
7
8 def test_weakref_simple(self):
9 class A:
10 pass
11 class B(A):
12 pass
13 class C(A):
14 pass
15
16 def f(n):
17 if n:
18 x = B()
19 x.hello = 42
20 r = weakref.ref(x)
21 else:
22 x = C()
23 x.hello = 64
24 r = weakref.ref(x)
25 return r().hello, x # returns 'x' too, to keep it alive
26 res = self.interpret(f, [1])
27 assert res.item0 == 42
28 res = self.interpret(f, [0])
29 assert res.item0 == 64
30
31 def test_prebuilt_weakref(self):
32 class A:
33 pass
34 a1 = A()
35 a1.hello = 5
36 w1 = weakref.ref(a1)
37 a2 = A()
38 a2.hello = 8
39 w2 = weakref.ref(a2)
40
41 def f(n):
42 if n:
43 r = w1
44 else:
45 r = w2
46 return r().hello
47 res = self.interpret(f, [1])
48 assert res == 5
49 res = self.interpret(f, [0])
50 assert res == 8
51
52 def test_prebuilt_dead_weakref(self):
53 class A:
54 pass
55 a1 = A()
56 w1 = weakref.ref(a1)
57 a2 = A()
58 w2 = weakref.ref(a2)
59
60 del a1
61 rgc.collect()
62 assert w1() is None
63
64 def f(n):
65 if n:
66 r = w1
67 else:
68 r = w2
69 return r() is not None
70 res = self.interpret(f, [1])
71 assert res == False
72 res = self.interpret(f, [0])
73 assert res == True
74
75 def test_multiple_prebuilt_dead_weakrefs(self):
76 class A:
77 pass
78 a1 = A()
79 w1 = weakref.ref(a1)
80 a2 = A()
81 w2 = weakref.ref(a2)
82 a3 = A()
83 w3 = weakref.ref(a3)
84 a4 = A()
85 w4 = weakref.ref(a4)
86
87 del a1, a3
88 rgc.collect()
89 assert w1() is None
90 assert w3() is None
91
92 def f(n):
93 if n > 0:
94 if n > 5:
95 r = w1
96 else:
97 r = w3
98 assert r() is None
99 else:
100 if n < -5:
101 r = w2
102 else:
103 r = w4
104 assert r() is not None
105 return r() is not None
106 res = self.interpret(f, [1])
107 assert res == False
108 res = self.interpret(f, [0])
109 assert res == True
110 res = self.interpret(f, [100])
111 assert res == False
112 res = self.interpret(f, [-100])
113 assert res == True
114
115 def test_pbc_null_weakref(self):
116 class A:
117 pass
118 a1 = A()
119 mylist = [weakref.ref(a1), None]
120 def fn(i):
121 item = mylist[i]
122 return item is None
123 assert self.interpret(fn, [0]) is False
124 assert self.interpret(fn, [1]) is True
125
126 def test_ll_weakref(self):
127 S = lltype.GcStruct('S', ('x',lltype.Signed))
128 def g():
129 s = lltype.malloc(S)
130 w = llmemory.weakref_create(s)
131 assert llmemory.weakref_deref(lltype.Ptr(S), w) == s
132 assert llmemory.weakref_deref(lltype.Ptr(S), w) == s
133 return w # 's' is forgotten here
134 def f():
135 w = g()
136 rgc.collect()
137 return llmemory.weakref_deref(lltype.Ptr(S), w)
138
139 res = self.interpret(f, [])
140 assert res == lltype.nullptr(S)
141
142
143 class TestRWeakrefDisabled(BaseRtypingTest):
144 def test_no_real_weakref(self):
145 class A:
146 pass
147 a1 = A()
148 mylist = [weakref.ref(a1), None]
149 def g():
150 a2 = A()
151 return weakref.ref(a2)
152 def fn(i):
153 w = g()
154 rgc.collect()
155 assert w() is not None
156 return mylist[i] is None
157
158 assert self.interpret(fn, [0], rweakref=False) is False
159 assert self.interpret(fn, [1], rweakref=False) is True