PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/pypy/module/gc/test/test_gc.py

https://bitbucket.org/pypy/pypy/
Python | 145 lines | 120 code | 22 blank | 3 comment | 8 complexity | 8b2d840cb5cda31daba8cad829b1d077 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. import py
  2. class AppTestGC(object):
  3. def test_collect(self):
  4. import gc
  5. gc.collect() # mostly a "does not crash" kind of test
  6. gc.collect(0) # mostly a "does not crash" kind of test
  7. def test_disable_finalizers(self):
  8. import gc
  9. class X(object):
  10. created = 0
  11. deleted = 0
  12. def __init__(self):
  13. X.created += 1
  14. def __del__(self):
  15. X.deleted += 1
  16. class OldX:
  17. created = 0
  18. deleted = 0
  19. def __init__(self):
  20. OldX.created += 1
  21. def __del__(self):
  22. OldX.deleted += 1
  23. def runtest(should_be_enabled):
  24. runtest1(should_be_enabled, X)
  25. runtest1(should_be_enabled, OldX)
  26. def runtest1(should_be_enabled, Cls):
  27. gc.collect()
  28. if should_be_enabled:
  29. assert Cls.deleted == Cls.created
  30. else:
  31. old_deleted = Cls.deleted
  32. Cls(); Cls(); Cls()
  33. gc.collect()
  34. if should_be_enabled:
  35. assert Cls.deleted == Cls.created
  36. else:
  37. assert Cls.deleted == old_deleted
  38. runtest(True)
  39. gc.disable_finalizers()
  40. runtest(False)
  41. runtest(False)
  42. gc.enable_finalizers()
  43. runtest(True)
  44. # test nesting
  45. gc.disable_finalizers()
  46. gc.disable_finalizers()
  47. runtest(False)
  48. gc.enable_finalizers()
  49. runtest(False)
  50. gc.enable_finalizers()
  51. runtest(True)
  52. raises(ValueError, gc.enable_finalizers)
  53. runtest(True)
  54. def test_enable(self):
  55. import gc
  56. assert gc.isenabled()
  57. gc.disable()
  58. assert not gc.isenabled()
  59. gc.enable()
  60. assert gc.isenabled()
  61. gc.enable()
  62. assert gc.isenabled()
  63. class AppTestGcDumpHeap(object):
  64. pytestmark = py.test.mark.xfail(run=False)
  65. def setup_class(cls):
  66. import py
  67. from rpython.tool.udir import udir
  68. from rpython.rlib import rgc
  69. class X(object):
  70. def __init__(self, count, size, links):
  71. self.count = count
  72. self.size = size
  73. self.links = links
  74. def fake_heap_stats():
  75. return [X(1, 12, [0, 0]), X(2, 10, [10, 0])]
  76. cls._heap_stats = rgc._heap_stats
  77. rgc._heap_stats = fake_heap_stats
  78. fname = udir.join('gcdump.log')
  79. cls.w_fname = cls.space.wrap(str(fname))
  80. cls._fname = fname
  81. def teardown_class(cls):
  82. import py
  83. from rpython.rlib import rgc
  84. rgc._heap_stats = cls._heap_stats
  85. assert py.path.local(cls._fname).read() == '1 12 0,0\n2 10 10,0\n'
  86. def test_gc_heap_stats(self):
  87. import gc
  88. gc.dump_heap_stats(self.fname)
  89. class AppTestGcMethodCache(object):
  90. def test_clear_method_cache(self):
  91. import gc, weakref
  92. rlist = []
  93. def f():
  94. class C(object):
  95. def f(self):
  96. pass
  97. C().f() # Fill the method cache
  98. rlist.append(weakref.ref(C))
  99. for i in range(10):
  100. f()
  101. gc.collect() # the classes C should all go away here
  102. # the last class won't go in mapdict, as long as the code object of f
  103. # is around
  104. rlist.pop()
  105. for r in rlist:
  106. assert r() is None
  107. def test_clear_index_cache(self):
  108. import gc, weakref
  109. rlist = []
  110. def f():
  111. class C(object):
  112. def f(self):
  113. pass
  114. c = C()
  115. c.x = 1
  116. getattr(c, "x") # fill the index cache without using the local cache
  117. getattr(c, "x")
  118. rlist.append(weakref.ref(C))
  119. for i in range(5):
  120. f()
  121. gc.collect() # the classes C should all go away here
  122. for r in rlist:
  123. assert r() is None