图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
4.5 KiB

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