Debian Codegraph @

rpython/rtyper/lltypesystem/test/test_ztranslated.py

raw symbols SCIP
1 import gc
2 from rpython.translator.c.test.test_genc import compile
3 from rpython.rtyper.lltypesystem import rffi
4 from rpython.rtyper.lltypesystem import lltype
5 from rpython.rtyper.lltypesystem.lloperation import llop
6 from rpython.rlib import rgc
7
8 def debug_assert(boolresult, msg):
9 if not boolresult:
10 llop.debug_print(lltype.Void, "\n\nassert failed: %s\n\n" % msg)
11 assert boolresult
12
13 def use_str():
14 mystr = b'abc'
15 #debug_assert(rgc.can_move(mystr), "short string cannot move... why?")
16 ptr = rffi.get_raw_address_of_string(mystr)
17 ptr2 = rffi.get_raw_address_of_string(mystr)
18 debug_assert(ptr == ptr2, "ptr != ptr2")
19 debug_assert(ptr[0] == b'a', "notnurseryadr[0] == b'a' is is %s" % ptr[0])
20 ptr[0] = b'x' # oh no no, in real programs nobody is allowed to modify that
21 debug_assert(mystr[0] == b'a', "mystr[0] != b'a'")
22 debug_assert(ptr[0] == b'x', "notnurseryadr[0] == b'x'")
23 gc.collect()
24 nptr = rffi.get_raw_address_of_string(mystr)
25 debug_assert(nptr == ptr, "second call to mystr must return the same ptr")
26 debug_assert(ptr[0] == b'x', "failure a")
27 debug_assert(nptr[0] == b'x', "failure b")
28 mystr = None
29
30 def long_str(lstr):
31 ptr = rffi.get_raw_address_of_string(lstr)
32 for i,c in enumerate(lstr):
33 debug_assert(ptr[i] == c, "failure c")
34 gc.collect()
35 ptr2 = rffi.get_raw_address_of_string(lstr)
36 debug_assert(ptr == ptr2, "ptr != ptr2!!!")
37 return ptr
38
39 def main(argv=[]):
40 try:
41 use_str()
42 except ValueError:
43 return 42
44 gc.collect()
45 mystr = b"12341234aa"*4096*10
46 #debug_assert(not rgc.can_move(mystr), "long string can move... why?")
47 p1 = long_str(mystr)
48 gc.collect()
49 copystr = mystr[:]
50 copystr += 'a'
51 p2 = long_str(copystr)
52 debug_assert(p1 != p2, "p1 == p2")
53 return 0
54
55 # ____________________________________________________________
56
57 def target(driver, args):
58 return main
59
60 def test_compiled_incminimark():
61 fn = compile(main, [], gcpolicy="incminimark")
62 res = fn()
63 assert res == 0
64
65 def test_compiled_semispace():
66 fn = compile(main, [], gcpolicy="semispace")
67 res = fn()
68 # get_raw_address_of_string() never raise ValueError any more
69 assert res == 0
70
71 def test_compiled_boehm():
72 fn = compile(main, [], gcpolicy="boehm")
73 res = fn()
74 assert res == 0