test__gcutils.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. """ Test for assert_deallocated context manager and gc utilities
  2. """
  3. import gc
  4. from threading import Lock
  5. from scipy._lib._gcutils import (set_gc_state, gc_state, assert_deallocated,
  6. ReferenceError, IS_PYPY)
  7. from numpy.testing import assert_equal
  8. import pytest
  9. @pytest.fixture
  10. def gc_lock():
  11. return Lock()
  12. def test_set_gc_state(gc_lock):
  13. with gc_lock:
  14. gc_status = gc.isenabled()
  15. try:
  16. for state in (True, False):
  17. gc.enable()
  18. set_gc_state(state)
  19. assert_equal(gc.isenabled(), state)
  20. gc.disable()
  21. set_gc_state(state)
  22. assert_equal(gc.isenabled(), state)
  23. finally:
  24. if gc_status:
  25. gc.enable()
  26. def test_gc_state(gc_lock):
  27. # Test gc_state context manager
  28. with gc_lock:
  29. gc_status = gc.isenabled()
  30. try:
  31. for pre_state in (True, False):
  32. set_gc_state(pre_state)
  33. for with_state in (True, False):
  34. # Check the gc state is with_state in with block
  35. with gc_state(with_state):
  36. assert_equal(gc.isenabled(), with_state)
  37. # And returns to previous state outside block
  38. assert_equal(gc.isenabled(), pre_state)
  39. # Even if the gc state is set explicitly within the block
  40. with gc_state(with_state):
  41. assert_equal(gc.isenabled(), with_state)
  42. set_gc_state(not with_state)
  43. assert_equal(gc.isenabled(), pre_state)
  44. finally:
  45. if gc_status:
  46. gc.enable()
  47. @pytest.mark.skipif(IS_PYPY, reason="Test not meaningful on PyPy")
  48. def test_assert_deallocated(gc_lock):
  49. # Ordinary use
  50. class C:
  51. def __init__(self, arg0, arg1, name='myname'):
  52. self.name = name
  53. with gc_lock:
  54. for gc_current in (True, False):
  55. with gc_state(gc_current):
  56. # We are deleting from with-block context, so that's OK
  57. with assert_deallocated(C, 0, 2, 'another name') as c:
  58. assert_equal(c.name, 'another name')
  59. del c
  60. # Or not using the thing in with-block context, also OK
  61. with assert_deallocated(C, 0, 2, name='third name'):
  62. pass
  63. assert_equal(gc.isenabled(), gc_current)
  64. @pytest.mark.skipif(IS_PYPY, reason="Test not meaningful on PyPy")
  65. def test_assert_deallocated_nodel():
  66. class C:
  67. pass
  68. with pytest.raises(ReferenceError):
  69. # Need to delete after using if in with-block context
  70. # Note: assert_deallocated(C) needs to be assigned for the test
  71. # to function correctly. It is assigned to _, but _ itself is
  72. # not referenced in the body of the with, it is only there for
  73. # the refcount.
  74. with assert_deallocated(C) as _:
  75. pass
  76. @pytest.mark.skipif(IS_PYPY, reason="Test not meaningful on PyPy")
  77. def test_assert_deallocated_circular():
  78. class C:
  79. def __init__(self):
  80. self._circular = self
  81. with pytest.raises(ReferenceError):
  82. # Circular reference, no automatic garbage collection
  83. with assert_deallocated(C) as c:
  84. del c
  85. @pytest.mark.skipif(IS_PYPY, reason="Test not meaningful on PyPy")
  86. def test_assert_deallocated_circular2():
  87. class C:
  88. def __init__(self):
  89. self._circular = self
  90. with pytest.raises(ReferenceError):
  91. # Still circular reference, no automatic garbage collection
  92. with assert_deallocated(C):
  93. pass