Skip to content

Commit

Permalink
[numpy 2.x] Cope with numpy 2.x changes
Browse files Browse the repository at this point in the history
- np.round_ got removed
- np.alltrue got removed
- np.bool got added back
- initializing dtype from overflowing values is no longer valid

Fix #2189
  • Loading branch information
serge-sans-paille committed Apr 24, 2024
1 parent 6924a91 commit d2610d2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
13 changes: 7 additions & 6 deletions pythran/tests/test_numpy_func1.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_sum11_(self):
@unittest.skipIf(sys.maxsize == (2**31 - 1), "overflow test")
def test_sum12_(self):
self.run_test("def np_sum12_(a): import numpy as np ; return np.sum(a)",
numpy.array([2**32-1, -2**32 +1 , -2**32 + 1], dtype=numpy.uint32), np_sum12_=[NDArray[numpy.uint32,:]])
numpy.array([2**32-1, 2**31 +1 , 2**31 + 1], dtype=numpy.uint32), np_sum12_=[NDArray[numpy.uint32,:]])

def test_sum13_(self):
self.run_test("def np_sum13_(a): import numpy as np ; return np.sum(a)",
Expand All @@ -60,7 +60,7 @@ def test_sum13_(self):
@unittest.skipIf(sys.maxsize == (2**31 - 1), "overflow test")
def test_sum14_(self):
self.run_test("def np_sum14_(a): import numpy as np ; return np.sum(a)",
numpy.array([2**31-1, 2**31 +1 , 2**31 + 1], dtype=numpy.int32), np_sum14_=[NDArray[numpy.int32,:]])
numpy.array([2**30-1, 2**30 +1 , 2**30 + 1], dtype=numpy.int32), np_sum14_=[NDArray[numpy.int32,:]])

def test_sum15_(self):
self.run_test("def np_sum15_(a): import numpy as np ; return np.sum(a, dtype=int)",
Expand Down Expand Up @@ -240,11 +240,12 @@ def np_allclose4(a):
numpy.array([float("inf"), float("inf"), -float('inf')]),
np_allclose4=[NDArray[float,:]])

def test_alltrue0(self):
self.run_test("def np_alltrue0(b): from numpy import alltrue ; return alltrue(b)", numpy.array([True, False, True, True]), np_alltrue0=[NDArray[bool,:]])
if hasattr(numpy, 'alltrue'): # disappeared in numpy 2.x
def test_alltrue0(self):
self.run_test("def np_alltrue0(b): from numpy import alltrue ; return alltrue(b)", numpy.array([True, False, True, True]), np_alltrue0=[NDArray[bool,:]])

def test_alltrue1(self):
self.run_test("def np_alltrue1(a): from numpy import alltrue ; return alltrue(a >= 5)", numpy.array([1, 5, 2, 7]), np_alltrue1=[NDArray[int,:]])
def test_alltrue1(self):
self.run_test("def np_alltrue1(a): from numpy import alltrue ; return alltrue(a >= 5)", numpy.array([1, 5, 2, 7]), np_alltrue1=[NDArray[int,:]])

def test_count_nonzero0(self):
self.run_test("def np_count_nonzero0(a): from numpy import count_nonzero; return count_nonzero(a)",
Expand Down
5 changes: 4 additions & 1 deletion pythran/tests/test_numpy_ufunc_unary.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TestNumpyUFuncUnary(TestEnv):
'isnan', 'invert', 'isfinite',
'log', 'log10', 'log1p', 'log2', 'logical_not',
'negative',
'rad2deg', 'radians', 'reciprocal', 'rint', 'round', 'round_',
'rad2deg', 'radians', 'reciprocal', 'rint', 'round',
'sign', 'signbit',
'sin', 'sinh', 'spacing', 'sqrt', 'square',
'tan', 'tanh', 'trunc',
Expand All @@ -36,6 +36,9 @@ class TestNumpyUFuncUnary(TestEnv):
'scipy.special': ('gammaln', 'gamma', 'i0', 'i0e', 'ndtr', 'ndtri')
}

if hasattr(numpy, 'round_'): # disappeared in numpy 2.x
unary_func_by_module['numpy'] += 'round_',

if scipy is None:
del unary_func_by_module['scipy.special']

Expand Down
1 change: 1 addition & 0 deletions pythran/types/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#
complex: 'std::complex<double>',
bool: 'bool',
numpy.bool: 'bool',
int: 'long',
float: 'double',
str: 'pythonic::types::str',
Expand Down

0 comments on commit d2610d2

Please sign in to comment.