test__gcutils.py 3.2 KB

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