test_errstate.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import pytest
  2. import sysconfig
  3. import numpy as np
  4. from numpy.testing import assert_, assert_raises
  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(arm_softfloat,
  13. reason='platform/cpu issue with FPU (gh-413,-15562)')
  14. def test_invalid(self):
  15. with np.errstate(all='raise', under='ignore'):
  16. a = -np.arange(3)
  17. # This should work
  18. with np.errstate(invalid='ignore'):
  19. np.sqrt(a)
  20. # While this should fail!
  21. with assert_raises(FloatingPointError):
  22. np.sqrt(a)
  23. @pytest.mark.skipif(arm_softfloat,
  24. reason='platform/cpu issue with FPU (gh-15562)')
  25. def test_divide(self):
  26. with np.errstate(all='raise', under='ignore'):
  27. a = -np.arange(3)
  28. # This should work
  29. with np.errstate(divide='ignore'):
  30. a // 0
  31. # While this should fail!
  32. with assert_raises(FloatingPointError):
  33. a // 0
  34. # As should this, see gh-15562
  35. with assert_raises(FloatingPointError):
  36. a // a
  37. def test_errcall(self):
  38. def foo(*args):
  39. print(args)
  40. olderrcall = np.geterrcall()
  41. with np.errstate(call=foo):
  42. assert_(np.geterrcall() is foo, 'call is not foo')
  43. with np.errstate(call=None):
  44. assert_(np.geterrcall() is None, 'call is not None')
  45. assert_(np.geterrcall() is olderrcall, 'call is not olderrcall')
  46. def test_errstate_decorator(self):
  47. @np.errstate(all='ignore')
  48. def foo():
  49. a = -np.arange(3)
  50. a // 0
  51. foo()