test__gcutils.py 3.3 KB

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