Debian Codegraph @

rpython/rlib/test/test_rgc.py

raw symbols SCIP
1 from rpython.rtyper.test.test_llinterp import gengraph, interpret
2 from rpython.rtyper.error import TyperError
3 from rpython.rtyper.lltypesystem import lltype, llmemory, rffi
4 from rpython.rlib import rgc # Force registration of gc.collect
5 import gc
6 import py, sys
7
8 def test_collect():
9 def f():
10 return gc.collect()
11
12 t, typer, graph = gengraph(f, [])
13 ops = list(graph.iterblockops())
14 assert len(ops) == 1
15 op = ops[0][1]
16 assert op.opname == 'gc__collect'
17 assert len(op.args) == 0
18
19 res = interpret(f, [])
20
21 assert res is None
22
23 def test_collect_0():
24 if sys.version_info < (2, 5):
25 py.test.skip("requires Python 2.5 to call gc.collect() with an arg")
26
27 def f():
28 return gc.collect(0)
29
30 t, typer, graph = gengraph(f, [])
31 ops = list(graph.iterblockops())
32 assert len(ops) == 1
33 op = ops[0][1]
34 assert op.opname == 'gc__collect'
35 assert len(op.args) == 1
36 assert op.args[0].value == 0
37
38 res = interpret(f, [])
39
40 assert res is None
41
42 def test_enable_disable():
43 def f():
44 gc.enable()
45 a = gc.isenabled()
46 gc.disable()
47 b = gc.isenabled()
48 return a and not b
49
50 try:
51 t, typer, graph = gengraph(f, [])
52 blockops = list(graph.iterblockops())
53 opnames = [op.opname for block, op in blockops
54 if op.opname.startswith('gc__')]
55 assert opnames == ['gc__enable', 'gc__isenabled',
56 'gc__disable', 'gc__isenabled']
57 res = interpret(f, [])
58 assert res
59 finally:
60 gc.enable()
61
62 def test_collect_step():
63 def f():
64 return rgc.collect_step()
65
66 assert f()
67 t, typer, graph = gengraph(f, [])
68 blockops = list(graph.iterblockops())
69 opnames = [op.opname for block, op in blockops
70 if op.opname.startswith('gc__')]
71 assert opnames == ['gc__collect_step']
72 res = interpret(f, [])
73 assert res
74
75 def test__encode_states():
76 val = rgc._encode_states(42, 43)
77 assert rgc.old_state(val) == 42
78 assert rgc.new_state(val) == 43
79 assert not rgc.is_done(val)
80 #
81 val = rgc.collect_step()
82 assert rgc.is_done(val)
83
84 def test_can_move():
85 T0 = lltype.GcStruct('T')
86 T1 = lltype.GcArray(lltype.Float)
87 def f(i):
88 if i:
89 return rgc.can_move(lltype.malloc(T0))
90 else:
91 return rgc.can_move(lltype.malloc(T1, 1))
92
93 t, typer, graph = gengraph(f, [int])
94 ops = list(graph.iterblockops())
95 res = [op for op in ops if op[1].opname == 'gc_can_move']
96 assert len(res) == 2
97
98 res = interpret(f, [1])
99
100 assert res == True
101
102 def test_ll_arraycopy_1():
103 TYPE = lltype.GcArray(lltype.Signed)
104 a1 = lltype.malloc(TYPE, 10)
105 a2 = lltype.malloc(TYPE, 6)
106 for i in range(10): a1[i] = 100 + i
107 for i in range(6): a2[i] = 200 + i
108 rgc.ll_arraycopy(a1, a2, 4, 2, 3)
109 for i in range(10):
110 assert a1[i] == 100 + i
111 for i in range(6):
112 if 2 <= i < 5:
113 assert a2[i] == a1[i+2]
114 else:
115 assert a2[i] == 200 + i
116
117 def test_ll_arraycopy_2():
118 TYPE = lltype.GcArray(lltype.Void)
119 a1 = lltype.malloc(TYPE, 10)
120 a2 = lltype.malloc(TYPE, 6)
121 rgc.ll_arraycopy(a1, a2, 4, 2, 3)
122 # nothing to assert here, should not crash...
123
124 def test_ll_arraycopy_3():
125 S = lltype.Struct('S') # non-gc
126 TYPE = lltype.GcArray(lltype.Ptr(S))
127 a1 = lltype.malloc(TYPE, 10)
128 a2 = lltype.malloc(TYPE, 6)
129 org1 = [None] * 10
130 org2 = [None] * 6
131 for i in range(10): a1[i] = org1[i] = lltype.malloc(S, immortal=True)
132 for i in range(6): a2[i] = org2[i] = lltype.malloc(S, immortal=True)
133 rgc.ll_arraycopy(a1, a2, 4, 2, 3)
134 for i in range(10):
135 assert a1[i] == org1[i]
136 for i in range(6):
137 if 2 <= i < 5:
138 assert a2[i] == a1[i+2]
139 else:
140 assert a2[i] == org2[i]
141
142 def test_ll_arraycopy_4():
143 S = lltype.GcStruct('S')
144 TYPE = lltype.GcArray(lltype.Ptr(S))
145 a1 = lltype.malloc(TYPE, 10)
146 a2 = lltype.malloc(TYPE, 6)
147 org1 = [None] * 10
148 org2 = [None] * 6
149 for i in range(10): a1[i] = org1[i] = lltype.malloc(S)
150 for i in range(6): a2[i] = org2[i] = lltype.malloc(S)
151 rgc.ll_arraycopy(a1, a2, 4, 2, 3)
152 for i in range(10):
153 assert a1[i] == org1[i]
154 for i in range(6):
155 if 2 <= i < 5:
156 assert a2[i] == a1[i+2]
157 else:
158 assert a2[i] == org2[i]
159
160 def test_ll_arraycopy_5(monkeypatch):
161 S = lltype.GcStruct('S')
162 TYPE = lltype.GcArray(lltype.Ptr(S))
163 def f():
164 a1 = lltype.malloc(TYPE, 10)
165 a2 = lltype.malloc(TYPE, 6)
166 rgc.ll_arraycopy(a2, a1, 0, 1, 5)
167
168 CHK = lltype.Struct('CHK', ('called', lltype.Bool))
169 check = lltype.malloc(CHK, immortal=True)
170
171 def raw_memcopy(*args):
172 check.called = True
173
174 monkeypatch.setattr(llmemory, "raw_memcopy", raw_memcopy)
175
176 interpret(f, [])
177
178 assert check.called
179
180 def test_ll_arraycopy_array_of_structs():
181 TP = lltype.GcArray(lltype.Struct('x', ('x', lltype.Signed),
182 ('y', lltype.Signed)))
183 def f():
184 a1 = lltype.malloc(TP, 3)
185 a2 = lltype.malloc(TP, 3)
186 for i in range(3):
187 a1[i].x = 2 * i
188 a1[i].y = 2 * i + 1
189 rgc.ll_arraycopy(a1, a2, 0, 0, 3)
190 for i in range(3):
191 assert a2[i].x == 2 * i
192 assert a2[i].y == 2 * i + 1
193
194
195 interpret(f, [])
196 a1 = lltype.malloc(TP, 3)
197 a2 = lltype.malloc(TP, 3)
198 a1[1].x = 3
199 a1[1].y = 15
200 rgc.copy_struct_item(a1, a2, 1, 2)
201 assert a2[2].x == 3
202 assert a2[2].y == 15
203
204 def test_ll_arraymove_1():
205 TYPE = lltype.GcArray(lltype.Signed)
206 a1 = lltype.malloc(TYPE, 10)
207 org1 = [None] * 10
208 for i in range(10): a1[i] = org1[i] = 1000 + i
209 rgc.ll_arraymove(a1, 2, 4, 3)
210 for i in range(10):
211 if 4 <= i < 7:
212 expected = org1[i - 2]
213 else:
214 expected = org1[i]
215 assert a1[i] == expected
216
217 def test_ll_arraymove_2():
218 TYPE = lltype.GcArray(lltype.Signed)
219 a1 = lltype.malloc(TYPE, 10)
220 org1 = [None] * 10
221 for i in range(10): a1[i] = org1[i] = 1000 + i
222 rgc.ll_arraymove(a1, 4, 2, 3)
223 for i in range(10):
224 if 2 <= i < 5:
225 expected = org1[i + 2]
226 else:
227 expected = org1[i]
228 assert a1[i] == expected
229
230 def test_ll_arraymove_gc():
231 S = lltype.GcStruct('S')
232 TYPE = lltype.GcArray(lltype.Ptr(S))
233 a1 = lltype.malloc(TYPE, 10)
234 org1 = [None] * 10
235 for i in range(10): a1[i] = org1[i] = lltype.malloc(S)
236 rgc.ll_arraymove(a1, 2, 4, 3)
237 for i in range(10):
238 if 4 <= i < 7:
239 expected = org1[i - 2]
240 else:
241 expected = org1[i]
242 assert a1[i] == expected
243
244 def test_ll_arraymove_slowpath(monkeypatch):
245 monkeypatch.setattr(rgc, "must_split_gc_address_space", lambda: True)
246 test_ll_arraymove_1()
247 test_ll_arraymove_2()
248
249 def test_ll_arrayclear():
250 TYPE = lltype.GcArray(lltype.Signed)
251 a1 = lltype.malloc(TYPE, 10)
252 for i in range(10):
253 a1[i] = 100 + i
254 rgc.ll_arrayclear(a1)
255 assert len(a1) == 10
256 for i in range(10):
257 assert a1[i] == 0
258
259 def test__contains_gcptr():
260 assert not rgc._contains_gcptr(lltype.Signed)
261 assert not rgc._contains_gcptr(
262 lltype.Struct('x', ('x', lltype.Signed)))
263 assert rgc._contains_gcptr(
264 lltype.Struct('x', ('x', lltype.Signed),
265 ('y', lltype.Ptr(lltype.GcArray(lltype.Signed)))))
266 assert rgc._contains_gcptr(
267 lltype.Struct('x', ('x', lltype.Signed),
268 ('y', llmemory.GCREF)))
269 assert rgc._contains_gcptr(lltype.Ptr(lltype.GcStruct('x')))
270 assert not rgc._contains_gcptr(lltype.Ptr(lltype.Struct('x')))
271 GCPTR = lltype.Ptr(lltype.GcStruct('x'))
272 assert rgc._contains_gcptr(
273 lltype.Struct('FOO', ('s', lltype.Struct('BAR', ('y', GCPTR)))))
274
275 def test_ll_arraycopy_small():
276 TYPE = lltype.GcArray(lltype.Signed)
277 for length in range(5):
278 a1 = lltype.malloc(TYPE, 10)
279 a2 = lltype.malloc(TYPE, 6)
280 org1 = range(20, 30)
281 org2 = range(50, 56)
282 for i in range(len(a1)): a1[i] = org1[i]
283 for i in range(len(a2)): a2[i] = org2[i]
284 rgc.ll_arraycopy(a1, a2, 4, 2, length)
285 for i in range(10):
286 assert a1[i] == org1[i]
287 for i in range(6):
288 if 2 <= i < 2 + length:
289 assert a2[i] == a1[i+2]
290 else:
291 assert a2[i] == org2[i]
292
293
294 def test_ll_shrink_array_1():
295 py.test.skip("implement ll_shrink_array for GcStructs or GcArrays that "
296 "don't have the shape of STR or UNICODE")
297
298 def test_ll_shrink_array_2():
299 S = lltype.GcStruct('S', ('x', lltype.Signed),
300 ('vars', lltype.Array(lltype.Signed)))
301 s1 = lltype.malloc(S, 5)
302 s1.x = 1234
303 for i in range(5):
304 s1.vars[i] = 50 + i
305 s2 = rgc.ll_shrink_array(s1, 3)
306 assert lltype.typeOf(s2) == lltype.Ptr(S)
307 assert s2.x == 1234
308 assert len(s2.vars) == 3
309 for i in range(3):
310 assert s2.vars[i] == 50 + i
311
312 def test_get_referents():
313 class X(object):
314 __slots__ = ['stuff']
315 x1 = X()
316 x1.stuff = X()
317 x2 = X()
318 lst = rgc.get_rpy_referents(rgc.cast_instance_to_gcref(x1))
319 lst2 = [rgc.try_cast_gcref_to_instance(X, x) for x in lst]
320 assert x1.stuff in lst2
321 assert x2 not in lst2
322
323 @py.test.mark.skipif(
324 "__pypy__" in sys.builtin_module_names,
325 reason="rgc.get_rpy_memory_usage does not work on pypy",
326 )
327 def test_get_memory_usage():
328 class X(object):
329 pass
330 x1 = X()
331 n = rgc.get_rpy_memory_usage(rgc.cast_instance_to_gcref(x1))
332 assert n >= 8 and n <= 64
333
334 def test_register_custom_trace_hook():
335 TP = lltype.GcStruct('X')
336
337 def trace_func():
338 xxx # should not be annotated here
339 lambda_trace_func = lambda: trace_func
340
341 def f():
342 rgc.register_custom_trace_hook(TP, lambda_trace_func)
343
344 t, typer, graph = gengraph(f, [])
345
346 assert typer.custom_trace_funcs == [(TP, trace_func)]
347
348 def test_nonmoving_raw_ptr_for_resizable_list():
349 def f(n):
350 lst = ['a', 'b', 'c']
351 lst = rgc.resizable_list_supporting_raw_ptr(lst)
352 lst.append(chr(n))
353 assert lst[3] == chr(n)
354 assert lst[-1] == chr(n)
355 #
356 ptr = rgc.nonmoving_raw_ptr_for_resizable_list(lst)
357 assert lst[:] == ['a', 'b', 'c', chr(n)]
358 assert lltype.typeOf(ptr) == rffi.CCHARP
359 assert [ptr[i] for i in range(4)] == ['a', 'b', 'c', chr(n)]
360 #
361 lst[-3] = 'X'
362 assert ptr[1] == 'X'
363 ptr[2] = 'Y'
364 assert lst[-2] == 'Y'
365 #
366 addr = rffi.cast(lltype.Signed, ptr)
367 ptr = rffi.cast(rffi.CCHARP, addr)
368 rgc.collect() # should not move lst.items
369 lst[-4] = 'g'
370 assert ptr[0] == 'g'
371 ptr[3] = 'H'
372 assert lst[-1] == 'H'
373 return lst
374 #
375 # direct untranslated run
376 lst = f(35)
377 assert isinstance(lst, rgc._ResizableListSupportingRawPtr)
378 #
379 # llinterp run
380 interpret(f, [35])
381 #
382 # compilation with the GC transformer
383 import subprocess
384 from rpython.translator.interactive import Translation
385 #
386 def main(argv):
387 f(len(argv))
388 print "OK!"
389 return 0
390 #
391 t = Translation(main, gc="incminimark")
392 t.disable(['backendopt'])
393 t.set_backend_extra_options(c_debug_defines=True)
394 exename = t.compile()
395 data = subprocess.check_output([str(exename), '.', '.', '.'])
396 assert data.strip().endswith('OK!')
397
398
399 def test_nonmoving_raw_ptr_for_resizable_list_getslice():
400 def f(n):
401 lst = ['a', 'b', 'c', 'd', 'e']
402 lst = rgc.resizable_list_supporting_raw_ptr(lst)
403 lst = lst[:3]
404 lst.append(chr(n))
405 assert lst[3] == chr(n)
406 assert lst[-1] == chr(n)
407 #
408 ptr = rgc.nonmoving_raw_ptr_for_resizable_list(lst)
409 assert lst[:] == ['a', 'b', 'c', chr(n)]
410 assert lltype.typeOf(ptr) == rffi.CCHARP
411 assert [ptr[i] for i in range(4)] == ['a', 'b', 'c', chr(n)]
412 return lst
413 #
414 # direct untranslated run
415 lst = f(35)
416 assert isinstance(lst, rgc._ResizableListSupportingRawPtr)
417 #
418 # llinterp run
419 interpret(f, [35])
420
421
422 def test_ll_for_resizable_list():
423 def f(n):
424 lst = ['a', 'b', 'c']
425 lst = rgc.resizable_list_supporting_raw_ptr(lst)
426 lst.append(chr(n))
427 assert lst[3] == chr(n)
428 assert lst[-1] == chr(n)
429 #
430 ll_list = rgc.ll_for_resizable_list(lst)
431 assert lst[:] == ['a', 'b', 'c', chr(n)]
432 assert ll_list.length == 4
433 assert [ll_list.items[i] for i in range(4)] == ['a', 'b', 'c', chr(n)]
434 #
435 lst[-3] = 'X'
436 assert ll_list.items[1] == 'X'
437 ll_list.items[2] = 'Y'
438 assert lst[-2] == 'Y'
439 #
440 return lst
441 #
442 # direct untranslated run
443 lst = f(35)
444 assert isinstance(lst, rgc._ResizableListSupportingRawPtr)
445 #
446 # llinterp run
447 interpret(f, [35])
448 #
449 # compilation with the GC transformer
450 import subprocess
451 from rpython.translator.interactive import Translation
452 #
453 def main(argv):
454 f(len(argv))
455 print "OK!"
456 return 0
457 #
458 t = Translation(main, gc="incminimark")
459 t.disable(['backendopt'])
460 t.set_backend_extra_options(c_debug_defines=True)
461 exename = t.compile()
462 data = subprocess.check_output([str(exename), '.', '.', '.'])
463 assert data.strip().endswith('OK!')
464
465
466 def test_ListSupportingRawPtr_direct():
467 lst = ['a', 'b', 'c']
468 lst = rgc.resizable_list_supporting_raw_ptr(lst)
469
470 def check_nonresizing():
471 assert lst[1] == lst[-2] == 'b'
472 lst[1] = 'X'
473 assert lst[1] == 'X'
474 lst[-1] = 'Y'
475 assert lst[1:3] == ['X', 'Y']
476 assert lst[-2:9] == ['X', 'Y']
477 lst[1:2] = 'B'
478 assert lst[:] == ['a', 'B', 'Y']
479 assert list(iter(lst)) == ['a', 'B', 'Y']
480 assert list(reversed(lst)) == ['Y', 'B', 'a']
481 assert 'B' in lst
482 assert 'b' not in lst
483 assert p[0] == 'a'
484 assert p[1] == 'B'
485 assert p[2] == 'Y'
486 assert lst + ['*'] == ['a', 'B', 'Y', '*']
487 assert ['*'] + lst == ['*', 'a', 'B', 'Y']
488 assert lst + lst == ['a', 'B', 'Y', 'a', 'B', 'Y']
489 base = ['8']
490 base += lst
491 assert base == ['8', 'a', 'B', 'Y']
492 assert lst == ['a', 'B', 'Y']
493 assert ['a', 'B', 'Y'] == lst
494 assert ['a', 'B', 'Z'] != lst
495 assert ['a', 'B', 'Z'] > lst
496 assert ['a', 'B', 'Z'] >= lst
497 assert lst * 2 == ['a', 'B', 'Y', 'a', 'B', 'Y']
498 assert 2 * lst == ['a', 'B', 'Y', 'a', 'B', 'Y']
499 assert lst.count('B') == 1
500 assert lst.index('Y') == 2
501 lst.reverse()
502 assert lst == ['Y', 'B', 'a']
503 lst.sort()
504 assert lst == ['B', 'Y', 'a']
505 lst.sort(reverse=True)
506 assert lst == ['a', 'Y', 'B']
507 lst[1] = 'b'
508 lst[2] = 'c'
509 assert list(lst) == ['a', 'b', 'c']
510
511 p = lst
512 check_nonresizing()
513 assert lst._ll_list is None
514 p = lst._nonmoving_raw_ptr_for_resizable_list()
515 ll_list = rgc.ll_for_resizable_list(lst)
516 assert ll_list is lst._ll_list
517 check_nonresizing()
518 assert lst._ll_list == ll_list
519 assert p[0] == ll_list.items[0] == 'a'
520 assert p[1] == ll_list.items[1] == 'b'
521 assert p[2] == ll_list.items[2] == 'c'
522
523 def do_resizing_operation():
524 del lst[1]
525 yield ['a', 'c']
526
527 lst[:2] = ['X']
528 yield ['X', 'c']
529
530 del lst[:2]
531 yield ['c']
532
533 x = lst
534 x += ['t']
535 yield ['a', 'b', 'c', 't']
536
537 x = lst
538 x *= 3
539 yield ['a', 'b', 'c'] * 3
540
541 lst.append('f')
542 yield ['a', 'b', 'c', 'f']
543
544 lst.extend('fg')
545 yield ['a', 'b', 'c', 'f', 'g']
546
547 lst.insert(1, 'k')
548 yield ['a', 'k', 'b', 'c']
549
550 n = lst.pop(1)
551 assert n == 'b'
552 yield ['a', 'c']
553
554 lst.remove('c')
555 yield ['a', 'b']
556
557 assert lst == ['a', 'b', 'c']
558 for expect in do_resizing_operation():
559 assert lst == expect
560 assert lst._ll_list is None
561 lst = ['a', 'b', 'c']
562 lst = rgc.resizable_list_supporting_raw_ptr(lst)
563 lst._nonmoving_raw_ptr_for_resizable_list()
564
565 # ____________________________________________________________
566
567
568 class T_Root(object):
569 pass
570
571 class T_Int(T_Root):
572 def __init__(self, x):
573 self.x = x
574
575 class SimpleFQ(rgc.FinalizerQueue):
576 Class = T_Root
577 _triggered = 0
578 def finalizer_trigger(self):
579 self._triggered += 1
580
581 class TestFinalizerQueue:
582
583 def test_simple(self):
584 fq = SimpleFQ()
585 assert fq.next_dead() is None
586 assert fq._triggered == 0
587 w = T_Int(67)
588 fq.register_finalizer(w)
589 #
590 gc.collect()
591 assert fq._triggered == 0
592 assert fq.next_dead() is None
593 #
594 del w
595 gc.collect()
596 assert fq._triggered == 1
597 n = fq.next_dead()
598 assert type(n) is T_Int and n.x == 67
599 #
600 gc.collect()
601 assert fq._triggered == 1
602 assert fq.next_dead() is None
603
604 def test_del_1(self):
605 deleted = {}
606 class T_Del(T_Int):
607 def __del__(self):
608 deleted[self.x] = deleted.get(self.x, 0) + 1
609
610 fq = SimpleFQ()
611 fq.register_finalizer(T_Del(42))
612 gc.collect(); gc.collect()
613 assert deleted == {}
614 assert fq._triggered == 1
615 n = fq.next_dead()
616 assert type(n) is T_Del and n.x == 42
617 assert deleted == {}
618 del n
619 gc.collect()
620 assert fq.next_dead() is None
621 assert deleted == {42: 1}
622 assert fq._triggered == 1
623
624 def test_del_2(self):
625 deleted = {}
626 class T_Del1(T_Int):
627 def __del__(self):
628 deleted[1, self.x] = deleted.get((1, self.x), 0) + 1
629 class T_Del2(T_Del1):
630 def __del__(self):
631 deleted[2, self.x] = deleted.get((2, self.x), 0) + 1
632 T_Del1.__del__(self)
633
634 fq = SimpleFQ()
635 w = T_Del2(42)
636 fq.register_finalizer(w)
637 del w
638 fq.register_finalizer(T_Del1(21))
639 gc.collect(); gc.collect()
640 assert deleted == {}
641 assert fq._triggered == 2
642 a = fq.next_dead()
643 b = fq.next_dead()
644 if a.x == 21:
645 a, b = b, a
646 assert type(a) is T_Del2 and a.x == 42
647 assert type(b) is T_Del1 and b.x == 21
648 assert deleted == {}
649 del a, b
650 gc.collect()
651 assert fq.next_dead() is None
652 assert deleted == {(1, 42): 1, (2, 42): 1, (1, 21): 1}
653 assert fq._triggered == 2
654
655 def test_del_3(self):
656 deleted = {}
657 class T_Del1(T_Int):
658 def __del__(self):
659 deleted[1, self.x] = deleted.get((1, self.x), 0) + 1
660 class T_Del2(T_Del1):
661 pass
662
663 fq = SimpleFQ()
664 fq.register_finalizer(T_Del2(42))
665 gc.collect(); gc.collect()
666 assert deleted == {}
667 assert fq._triggered == 1
668 a = fq.next_dead()
669 assert type(a) is T_Del2 and a.x == 42
670 assert deleted == {}
671 del a
672 gc.collect()
673 assert fq.next_dead() is None
674 assert deleted == {(1, 42): 1}
675 assert fq._triggered == 1
676
677 def test_finalizer_trigger_calls_too_much(self):
678 external_func = rffi.llexternal("foo", [], lltype.Void)
679 # ^^^ with release_gil=True
680 class X(object):
681 pass
682 class FQ(rgc.FinalizerQueue):
683 Class = X
684 def finalizer_trigger(self):
685 external_func()
686 fq = FQ()
687 def f():
688 x = X()
689 fq.register_finalizer(x)
690
691 e = py.test.raises(TyperError, gengraph, f, [])
692 assert str(e.value).startswith('the RPython-level __del__() method in')
693
694 def test_translated_boehm(self):
695 self._test_translated(use_gc="boehm", llcase=False)
696
697 def test_translated_boehm_ll(self):
698 self._test_translated(use_gc="boehm", llcase=True)
699
700 def test_translated_incminimark(self):
701 self._test_translated(use_gc="incminimark", llcase=False)
702
703 def test_translated_incminimark_ll(self):
704 self._test_translated(use_gc="incminimark", llcase=True)
705
706 def _test_translated(self, use_gc, llcase):
707 import subprocess
708 from rpython.rlib import objectmodel
709 from rpython.translator.interactive import Translation
710 #
711 class Seen:
712 count = 0
713 class MySimpleFQ(rgc.FinalizerQueue):
714 if not llcase:
715 Class = T_Root
716 else:
717 Class = None
718 def finalizer_trigger(self):
719 seen.count += 1
720 seen = Seen()
721 fq = MySimpleFQ()
722 if not llcase:
723 EMPTY = None
724 llbuilder = T_Int
725 else:
726 from rpython.rtyper.annlowlevel import llstr
727 EMPTY = lltype.nullptr(llmemory.GCREF.TO)
728 def llbuilder(n):
729 return lltype.cast_opaque_ptr(llmemory.GCREF, llstr(str(n)))
730
731 def subfunc():
732 w0 = llbuilder(40); fq.register_finalizer(w0)
733 w1 = llbuilder(41); fq.register_finalizer(w1)
734 w2 = llbuilder(42); fq.register_finalizer(w2)
735 w3 = llbuilder(43); fq.register_finalizer(w3)
736 w4 = llbuilder(44); fq.register_finalizer(w4)
737 w5 = llbuilder(45); fq.register_finalizer(w5)
738 w6 = llbuilder(46); fq.register_finalizer(w6)
739 w7 = llbuilder(47); fq.register_finalizer(w7)
740 w8 = llbuilder(48); fq.register_finalizer(w8)
741 w9 = llbuilder(49); fq.register_finalizer(w9)
742 gc.collect()
743 assert seen.count == 0
744 assert fq.next_dead() is EMPTY
745 objectmodel.keepalive_until_here(w0)
746 objectmodel.keepalive_until_here(w1)
747 objectmodel.keepalive_until_here(w2)
748 objectmodel.keepalive_until_here(w3)
749 objectmodel.keepalive_until_here(w4)
750 objectmodel.keepalive_until_here(w5)
751 objectmodel.keepalive_until_here(w6)
752 objectmodel.keepalive_until_here(w7)
753 objectmodel.keepalive_until_here(w8)
754 objectmodel.keepalive_until_here(w9)
755
756 def main(argv):
757 assert fq.next_dead() is EMPTY
758 subfunc()
759 gc.collect(); gc.collect(); gc.collect()
760 assert seen.count > 0
761 n = fq.next_dead()
762 while True:
763 if not llcase:
764 assert type(n) is T_Int and 40 <= n.x <= 49
765 else:
766 from rpython.rtyper.lltypesystem.rstr import STR
767 assert lltype.typeOf(n) is llmemory.GCREF
768 p = lltype.cast_opaque_ptr(lltype.Ptr(STR), n)
769 assert len(p.chars) == 2
770 assert p.chars[0] == "4"
771 assert "0" <= p.chars[1] <= "9"
772 n = fq.next_dead()
773 if n is EMPTY:
774 break
775 print "OK!"
776 return 0
777 #
778 t = Translation(main, gc=use_gc)
779 t.disable(['backendopt'])
780 t.set_backend_extra_options(c_debug_defines=True)
781 exename = t.compile()
782 data = subprocess.check_output([str(exename), '.', '.', '.'])
783 assert data.strip().endswith('OK!')