PageRenderTime 151ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/pypy/module/gc/test/test_referents.py

https://bitbucket.org/pypy/pypy/
Python | 118 lines | 103 code | 11 blank | 4 comment | 22 complexity | 63e1e38ae41c1b4eb631a4e5b2b4a9a0 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. from pypy.conftest import option
  2. class AppTestReferents(object):
  3. def setup_class(cls):
  4. from rpython.rlib import rgc
  5. cls._backup = [rgc.get_rpy_roots]
  6. w = cls.space.wrap
  7. space = cls.space
  8. class RandomRPythonObject(object):
  9. pass
  10. l4 = space.newlist([w(4)])
  11. l2 = space.newlist([w(2)])
  12. l7 = space.newlist([w(7)])
  13. cls.ALL_ROOTS = [l4, space.newlist([l2, l7]), RandomRPythonObject(),
  14. space.newtuple([l7])]
  15. cls.w_ALL_ROOTS = cls.space.newlist(cls.ALL_ROOTS)
  16. rgc.get_rpy_roots = lambda: (
  17. map(rgc._GcRef, cls.ALL_ROOTS) + [rgc.NULL_GCREF]*17)
  18. cls.w_runappdirect = cls.space.wrap(option.runappdirect)
  19. def teardown_class(cls):
  20. from rpython.rlib import rgc
  21. rgc.get_rpy_roots = cls._backup[0]
  22. def test_get_objects(self):
  23. import gc
  24. lst = gc.get_objects()
  25. i4, l27, ro, rt = self.ALL_ROOTS
  26. i2, i7 = l27
  27. found = 0
  28. for x in lst:
  29. if x is i4: found |= 1
  30. if x is i2: found |= 2
  31. if x is i7: found |= 4
  32. if x is l27: found |= 8
  33. assert found == 15
  34. for x in lst:
  35. if type(x) is gc.GcRef:
  36. assert 0, "get_objects() returned a GcRef"
  37. def test_get_rpy_roots(self):
  38. import gc
  39. lst = gc.get_rpy_roots()
  40. if self.runappdirect:
  41. pass # unsure what to test
  42. else:
  43. assert lst[0] == [4]
  44. assert lst[1] == [[2], [7]]
  45. assert type(lst[2]) is gc.GcRef
  46. assert lst[3] == ([7],)
  47. assert len(lst) == 4
  48. def test_get_rpy_referents(self):
  49. import gc
  50. y = [12345]
  51. x = [y]
  52. lst = gc.get_rpy_referents(x)
  53. # After translation, 'lst' should contain the RPython-level list
  54. # (as a GcStruct). Before translation, the 'wrappeditems' list.
  55. print lst
  56. lst2 = [x for x in lst if type(x) is gc.GcRef]
  57. assert lst2 != []
  58. # In any case, we should land on 'y' after one or two extra levels
  59. # of indirection.
  60. lst3 = []
  61. for x in lst2: lst3 += gc.get_rpy_referents(x)
  62. if y not in lst3:
  63. lst4 = []
  64. for x in lst3: lst4 += gc.get_rpy_referents(x)
  65. if y not in lst4:
  66. assert 0, "does not seem to reach 'y'"
  67. def test_get_rpy_memory_usage(self):
  68. import gc
  69. n = gc.get_rpy_memory_usage(12345)
  70. print n
  71. assert 4 <= n <= 64
  72. def test_get_rpy_type_index(self):
  73. import gc
  74. class Foo(object):
  75. pass
  76. n1 = gc.get_rpy_type_index(12345)
  77. n2 = gc.get_rpy_type_index(23456)
  78. n3 = gc.get_rpy_type_index(1.2)
  79. n4 = gc.get_rpy_type_index(Foo())
  80. print n1, n2, n3, n4
  81. assert n1 == n2
  82. assert n1 != n3
  83. assert n1 != n4
  84. assert n3 != n4
  85. def test_get_referents(self):
  86. import gc
  87. y = [12345]
  88. z = [23456]
  89. x = [y, z]
  90. lst = gc.get_referents(x)
  91. assert y in lst and z in lst
  92. def test_get_referrers(self):
  93. import gc
  94. l27 = self.ALL_ROOTS[1]
  95. i2, i7 = l27
  96. lst = gc.get_referrers(i7)
  97. for x in lst:
  98. if x is l27:
  99. break # found
  100. else:
  101. assert 0, "the list [2, 7] is not found as gc.get_referrers(7)"
  102. l7t = self.ALL_ROOTS[3]
  103. for x in lst:
  104. if x is l7t:
  105. break # found
  106. else:
  107. assert 0, "the tuple (7,) is not found as gc.get_referrers(7)"