test_utils.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import inspect
  2. import sys
  3. import pytest
  4. from numpy.core import arange
  5. from numpy.testing import assert_, assert_equal, assert_raises_regex
  6. from numpy.lib import deprecate
  7. import numpy.lib.utils as utils
  8. from io import StringIO
  9. @pytest.mark.skipif(sys.flags.optimize == 2, reason="Python running -OO")
  10. def test_lookfor():
  11. out = StringIO()
  12. utils.lookfor('eigenvalue', module='numpy', output=out,
  13. import_modules=False)
  14. out = out.getvalue()
  15. assert_('numpy.linalg.eig' in out)
  16. @deprecate
  17. def old_func(self, x):
  18. return x
  19. @deprecate(message="Rather use new_func2")
  20. def old_func2(self, x):
  21. return x
  22. def old_func3(self, x):
  23. return x
  24. new_func3 = deprecate(old_func3, old_name="old_func3", new_name="new_func3")
  25. def old_func4(self, x):
  26. """Summary.
  27. Further info.
  28. """
  29. return x
  30. new_func4 = deprecate(old_func4)
  31. def old_func5(self, x):
  32. """Summary.
  33. Bizarre indentation.
  34. """
  35. return x
  36. new_func5 = deprecate(old_func5, message="This function is\ndeprecated.")
  37. def old_func6(self, x):
  38. """
  39. Also in PEP-257.
  40. """
  41. return x
  42. new_func6 = deprecate(old_func6)
  43. def test_deprecate_decorator():
  44. assert_('deprecated' in old_func.__doc__)
  45. def test_deprecate_decorator_message():
  46. assert_('Rather use new_func2' in old_func2.__doc__)
  47. def test_deprecate_fn():
  48. assert_('old_func3' in new_func3.__doc__)
  49. assert_('new_func3' in new_func3.__doc__)
  50. @pytest.mark.skipif(sys.flags.optimize == 2, reason="-OO discards docstrings")
  51. @pytest.mark.parametrize('old_func, new_func', [
  52. (old_func4, new_func4),
  53. (old_func5, new_func5),
  54. (old_func6, new_func6),
  55. ])
  56. def test_deprecate_help_indentation(old_func, new_func):
  57. _compare_docs(old_func, new_func)
  58. # Ensure we don't mess up the indentation
  59. for knd, func in (('old', old_func), ('new', new_func)):
  60. for li, line in enumerate(func.__doc__.split('\n')):
  61. if li == 0:
  62. assert line.startswith(' ') or not line.startswith(' '), knd
  63. elif line:
  64. assert line.startswith(' '), knd
  65. def _compare_docs(old_func, new_func):
  66. old_doc = inspect.getdoc(old_func)
  67. new_doc = inspect.getdoc(new_func)
  68. index = new_doc.index('\n\n') + 2
  69. assert_equal(new_doc[index:], old_doc)
  70. @pytest.mark.skipif(sys.flags.optimize == 2, reason="-OO discards docstrings")
  71. def test_deprecate_preserve_whitespace():
  72. assert_('\n Bizarre' in new_func5.__doc__)
  73. def test_safe_eval_nameconstant():
  74. # Test if safe_eval supports Python 3.4 _ast.NameConstant
  75. utils.safe_eval('None')
  76. class TestByteBounds:
  77. def test_byte_bounds(self):
  78. # pointer difference matches size * itemsize
  79. # due to contiguity
  80. a = arange(12).reshape(3, 4)
  81. low, high = utils.byte_bounds(a)
  82. assert_equal(high - low, a.size * a.itemsize)
  83. def test_unusual_order_positive_stride(self):
  84. a = arange(12).reshape(3, 4)
  85. b = a.T
  86. low, high = utils.byte_bounds(b)
  87. assert_equal(high - low, b.size * b.itemsize)
  88. def test_unusual_order_negative_stride(self):
  89. a = arange(12).reshape(3, 4)
  90. b = a.T[::-1]
  91. low, high = utils.byte_bounds(b)
  92. assert_equal(high - low, b.size * b.itemsize)
  93. def test_strided(self):
  94. a = arange(12)
  95. b = a[::2]
  96. low, high = utils.byte_bounds(b)
  97. # the largest pointer address is lost (even numbers only in the
  98. # stride), and compensate addresses for striding by 2
  99. assert_equal(high - low, b.size * 2 * b.itemsize - b.itemsize)
  100. def test_assert_raises_regex_context_manager():
  101. with assert_raises_regex(ValueError, 'no deprecation warning'):
  102. raise ValueError('no deprecation warning')
  103. def test_info_method_heading():
  104. # info(class) should only print "Methods:" heading if methods exist
  105. class NoPublicMethods:
  106. pass
  107. class WithPublicMethods:
  108. def first_method():
  109. pass
  110. def _has_method_heading(cls):
  111. out = StringIO()
  112. utils.info(cls, output=out)
  113. return 'Methods:' in out.getvalue()
  114. assert _has_method_heading(WithPublicMethods)
  115. assert not _has_method_heading(NoPublicMethods)