PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/rpython/translator/c/test/test_refcount.py

https://bitbucket.org/pypy/pypy/
Python | 158 lines | 143 code | 14 blank | 1 comment | 9 complexity | af0d25f62d212c660df17fa210ed80de MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. import py
  2. from rpython.rtyper.lltypesystem import lltype
  3. from rpython.translator.translator import TranslationContext
  4. from rpython.translator.c.test.test_genc import compile
  5. class TestRefcount(object):
  6. def compile_func(self, func, args):
  7. return compile(func, args, gcpolicy='ref')
  8. def test_something(self):
  9. def f():
  10. return 1
  11. fn = self.compile_func(f, [])
  12. assert fn() == 1
  13. def test_something_more(self):
  14. S = lltype.GcStruct("S", ('x', lltype.Signed))
  15. def f(x):
  16. s = lltype.malloc(S)
  17. s.x = x
  18. return s.x
  19. fn = self.compile_func(f, [int])
  20. assert fn(1) == 1
  21. def test_call_function(self):
  22. class C:
  23. pass
  24. def f():
  25. c = C()
  26. c.x = 1
  27. return c
  28. def g():
  29. return f().x
  30. fn = self.compile_func(g, [])
  31. assert fn() == 1
  32. def test_multiple_exits(self):
  33. S = lltype.GcStruct("S", ('x', lltype.Signed))
  34. T = lltype.GcStruct("T", ('y', lltype.Signed))
  35. def f(n):
  36. c = lltype.malloc(S)
  37. d = lltype.malloc(T)
  38. d.y = 1
  39. e = lltype.malloc(T)
  40. e.y = 2
  41. if n:
  42. x = d
  43. else:
  44. x = e
  45. return x.y
  46. fn = self.compile_func(f, [int])
  47. assert fn(1) == 1
  48. assert fn(0) == 2
  49. def test_cleanup_vars_on_call(self):
  50. S = lltype.GcStruct("S", ('x', lltype.Signed))
  51. def f():
  52. return lltype.malloc(S)
  53. def g():
  54. s1 = f()
  55. s1.x = 42
  56. s2 = f()
  57. s3 = f()
  58. return s1.x
  59. fn = self.compile_func(g, [])
  60. assert fn() == 42
  61. def test_multiply_passed_var(self):
  62. S = lltype.GcStruct("S", ('x', lltype.Signed))
  63. def f(x):
  64. if x:
  65. a = lltype.malloc(S)
  66. a.x = 1
  67. b = a
  68. else:
  69. a = lltype.malloc(S)
  70. a.x = 1
  71. b = lltype.malloc(S)
  72. b.x = 2
  73. return a.x + b.x
  74. fn = self.compile_func(f, [int])
  75. fn(1) == 2
  76. fn(0) == 3
  77. def test_write_barrier(self):
  78. S = lltype.GcStruct("S", ('x', lltype.Signed))
  79. T = lltype.GcStruct("T", ('s', lltype.Ptr(S)))
  80. def f(x):
  81. s = lltype.malloc(S)
  82. s.x = 0
  83. s1 = lltype.malloc(S)
  84. s1.x = 1
  85. s2 = lltype.malloc(S)
  86. s2.x = 2
  87. t = lltype.malloc(T)
  88. t.s = s
  89. if x:
  90. t.s = s1
  91. else:
  92. t.s = s2
  93. return t.s.x + s.x + s1.x + s2.x
  94. fn = self.compile_func(f, [int])
  95. assert fn(1) == 4
  96. assert fn(0) == 5
  97. def test_del_catches(self):
  98. import os
  99. def g():
  100. pass
  101. class A(object):
  102. def __del__(self):
  103. try:
  104. g()
  105. except:
  106. pass #os.write(1, "hallo")
  107. def f1(i):
  108. if i:
  109. raise TypeError
  110. def f(i):
  111. a = A()
  112. f1(i)
  113. a.b = 1
  114. return a.b
  115. fn = self.compile_func(f, [int])
  116. assert fn(0) == 1
  117. fn(1, expected_exception_name="TypeError")
  118. def test_del_raises(self):
  119. class B(object):
  120. def __del__(self):
  121. raise TypeError
  122. def func():
  123. b = B()
  124. fn = self.compile_func(func, [])
  125. # does not crash
  126. fn()
  127. def test_wrong_order_setitem(self):
  128. class A(object):
  129. pass
  130. a = A()
  131. a.b = None
  132. class B(object):
  133. def __del__(self):
  134. a.freed += 1
  135. a.b = None
  136. def f(n):
  137. a.freed = 0
  138. a.b = B()
  139. if n:
  140. a.b = None
  141. return a.freed
  142. fn = self.compile_func(f, [int])
  143. res = fn(1)
  144. assert res == 1