test_errstate.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import pytest
  2. import sysconfig
  3. import numpy as np
  4. from numpy.testing import assert_, assert_raises, IS_WASM
  5. # The floating point emulation on ARM EABI systems lacking a hardware FPU is
  6. # known to be buggy. This is an attempt to identify these hosts. It may not
  7. # catch all possible cases, but it catches the known cases of gh-413 and
  8. # gh-15562.
  9. hosttype = sysconfig.get_config_var('HOST_GNU_TYPE')
  10. arm_softfloat = False if hosttype is None else hosttype.endswith('gnueabi')
  11. class TestErrstate:
  12. @pytest.mark.skipif(IS_WASM, reason="fp errors don't work in wasm")
  13. @pytest.mark.skipif(arm_softfloat,
  14. reason='platform/cpu issue with FPU (gh-413,-15562)')
  15. def test_invalid(self):
  16. with np.errstate(all='raise', under='ignore'):
  17. a = -np.arange(3)
  18. # This should work
  19. with np.errstate(invalid='ignore'):
  20. np.sqrt(a)
  21. # While this should fail!
  22. with assert_raises(FloatingPointError):
  23. np.sqrt(a)
  24. @pytest.mark.skipif(IS_WASM, reason="fp errors don't work in wasm")
  25. @pytest.mark.skipif(arm_softfloat,
  26. reason='platform/cpu issue with FPU (gh-15562)')
  27. def test_divide(self):
  28. with np.errstate(all='raise', under='ignore'):
  29. a = -np.arange(3)
  30. # This should work
  31. with np.errstate(divide='ignore'):
  32. a // 0
  33. # While this should fail!
  34. with assert_raises(FloatingPointError):
  35. a // 0
  36. # As should this, see gh-15562
  37. with assert_raises(FloatingPointError):
  38. a // a
  39. def test_errcall(self):
  40. def foo(*args):
  41. print(args)
  42. olderrcall = np.geterrcall()
  43. with np.errstate(call=foo):
  44. assert_(np.geterrcall() is foo, 'call is not foo')
  45. with np.errstate(call=None):
  46. assert_(np.geterrcall() is None, 'call is not None')
  47. assert_(np.geterrcall() is olderrcall, 'call is not olderrcall')
  48. def test_errstate_decorator(self):
  49. @np.errstate(all='ignore')
  50. def foo():
  51. a = -np.arange(3)
  52. a // 0
  53. foo()