Skip to content

Commit 19fba02

Browse files
tornariacharris
authored andcommitted
MAINT: add missing noexcept clauses (1/2)
After cython/cython#6087 it's much easier to figure out the missing noexcept clauses. Indeed, cython up to 3.0.9 has a warning that gives lots of false positives, but with the PR above (already merged in cython master and backported to 3.0.x) all the warnings are indeed cases of missing noexcept To test use this file `test_cimport.pyx`: ``` # cython: language_level=3 cimport numpy cimport numpy.random cimport numpy.random._bounded_integers cimport numpy.random._common cimport numpy.random.bit_generator cimport numpy.random.c_distributions ``` and build with `cython -X legacy_implicit_noexcept=True test_cimport.pyx` This commit applies cleanly to the 1.26.x branch and is meant to backport. The next commit fixes the remaining instances.
1 parent ceab92f commit 19fba02

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

numpy/__init__.cython-30.pxd

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ cdef extern from "numpy/arrayobject.h":
242242
# Instead, we use properties that map to the corresponding C-API functions.
243243

244244
@property
245-
cdef inline PyObject* base(self) nogil:
245+
cdef inline PyObject* base(self) noexcept nogil:
246246
"""Returns a borrowed reference to the object owning the data/memory.
247247
"""
248248
return PyArray_BASE(self)
@@ -254,34 +254,34 @@ cdef extern from "numpy/arrayobject.h":
254254
return <dtype>PyArray_DESCR(self)
255255

256256
@property
257-
cdef inline int ndim(self) nogil:
257+
cdef inline int ndim(self) noexcept nogil:
258258
"""Returns the number of dimensions in the array.
259259
"""
260260
return PyArray_NDIM(self)
261261

262262
@property
263-
cdef inline npy_intp *shape(self) nogil:
263+
cdef inline npy_intp *shape(self) noexcept nogil:
264264
"""Returns a pointer to the dimensions/shape of the array.
265265
The number of elements matches the number of dimensions of the array (ndim).
266266
Can return NULL for 0-dimensional arrays.
267267
"""
268268
return PyArray_DIMS(self)
269269

270270
@property
271-
cdef inline npy_intp *strides(self) nogil:
271+
cdef inline npy_intp *strides(self) noexcept nogil:
272272
"""Returns a pointer to the strides of the array.
273273
The number of elements matches the number of dimensions of the array (ndim).
274274
"""
275275
return PyArray_STRIDES(self)
276276

277277
@property
278-
cdef inline npy_intp size(self) nogil:
278+
cdef inline npy_intp size(self) noexcept nogil:
279279
"""Returns the total size (in number of elements) of the array.
280280
"""
281281
return PyArray_SIZE(self)
282282

283283
@property
284-
cdef inline char* data(self) nogil:
284+
cdef inline char* data(self) noexcept nogil:
285285
"""The pointer to the data buffer as a char*.
286286
This is provided for legacy reasons to avoid direct struct field access.
287287
For new code that needs this access, you probably want to cast the result
@@ -965,7 +965,7 @@ cdef extern from "numpy/ufuncobject.h":
965965

966966
int _import_umath() except -1
967967

968-
cdef inline void set_array_base(ndarray arr, object base):
968+
cdef inline void set_array_base(ndarray arr, object base) except *:
969969
Py_INCREF(base) # important to do this before stealing the reference below!
970970
PyArray_SetBaseObject(arr, base)
971971

@@ -996,7 +996,7 @@ cdef inline int import_ufunc() except -1:
996996
raise ImportError("numpy.core.umath failed to import")
997997

998998

999-
cdef inline bint is_timedelta64_object(object obj):
999+
cdef inline bint is_timedelta64_object(object obj) noexcept:
10001000
"""
10011001
Cython equivalent of `isinstance(obj, np.timedelta64)`
10021002
@@ -1011,7 +1011,7 @@ cdef inline bint is_timedelta64_object(object obj):
10111011
return PyObject_TypeCheck(obj, &PyTimedeltaArrType_Type)
10121012

10131013

1014-
cdef inline bint is_datetime64_object(object obj):
1014+
cdef inline bint is_datetime64_object(object obj) noexcept:
10151015
"""
10161016
Cython equivalent of `isinstance(obj, np.datetime64)`
10171017
@@ -1026,7 +1026,7 @@ cdef inline bint is_datetime64_object(object obj):
10261026
return PyObject_TypeCheck(obj, &PyDatetimeArrType_Type)
10271027

10281028

1029-
cdef inline npy_datetime get_datetime64_value(object obj) nogil:
1029+
cdef inline npy_datetime get_datetime64_value(object obj) noexcept nogil:
10301030
"""
10311031
returns the int64 value underlying scalar numpy datetime64 object
10321032
@@ -1036,14 +1036,14 @@ cdef inline npy_datetime get_datetime64_value(object obj) nogil:
10361036
return (<PyDatetimeScalarObject*>obj).obval
10371037

10381038

1039-
cdef inline npy_timedelta get_timedelta64_value(object obj) nogil:
1039+
cdef inline npy_timedelta get_timedelta64_value(object obj) noexcept nogil:
10401040
"""
10411041
returns the int64 value underlying scalar numpy timedelta64 object
10421042
"""
10431043
return (<PyTimedeltaScalarObject*>obj).obval
10441044

10451045

1046-
cdef inline NPY_DATETIMEUNIT get_datetime64_unit(object obj) nogil:
1046+
cdef inline NPY_DATETIMEUNIT get_datetime64_unit(object obj) noexcept nogil:
10471047
"""
10481048
returns the unit part of the dtype for a numpy datetime64 object.
10491049
"""

numpy/random/_bounded_integers.pxd.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ctypedef np.npy_bool bool_t
66

77
from numpy.random cimport bitgen_t
88

9-
cdef inline uint64_t _gen_mask(uint64_t max_val) nogil:
9+
cdef inline uint64_t _gen_mask(uint64_t max_val) noexcept nogil:
1010
"""Mask generator for use in bounded random numbers"""
1111
# Smallest bit mask >= max
1212
cdef uint64_t mask = max_val

0 commit comments

Comments
 (0)