Skip to content

Commit 9b27220

Browse files
committed
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 5edf018 commit 9b27220

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
@@ -363,7 +363,7 @@ cdef extern from "numpy/arrayobject.h":
363363
# Instead, we use properties that map to the corresponding C-API functions.
364364

365365
@property
366-
cdef inline PyObject* base(self) nogil:
366+
cdef inline PyObject* base(self) noexcept nogil:
367367
"""Returns a borrowed reference to the object owning the data/memory.
368368
"""
369369
return PyArray_BASE(self)
@@ -375,34 +375,34 @@ cdef extern from "numpy/arrayobject.h":
375375
return <dtype>PyArray_DESCR(self)
376376

377377
@property
378-
cdef inline int ndim(self) nogil:
378+
cdef inline int ndim(self) noexcept nogil:
379379
"""Returns the number of dimensions in the array.
380380
"""
381381
return PyArray_NDIM(self)
382382

383383
@property
384-
cdef inline npy_intp *shape(self) nogil:
384+
cdef inline npy_intp *shape(self) noexcept nogil:
385385
"""Returns a pointer to the dimensions/shape of the array.
386386
The number of elements matches the number of dimensions of the array (ndim).
387387
Can return NULL for 0-dimensional arrays.
388388
"""
389389
return PyArray_DIMS(self)
390390

391391
@property
392-
cdef inline npy_intp *strides(self) nogil:
392+
cdef inline npy_intp *strides(self) noexcept nogil:
393393
"""Returns a pointer to the strides of the array.
394394
The number of elements matches the number of dimensions of the array (ndim).
395395
"""
396396
return PyArray_STRIDES(self)
397397

398398
@property
399-
cdef inline npy_intp size(self) nogil:
399+
cdef inline npy_intp size(self) noexcept nogil:
400400
"""Returns the total size (in number of elements) of the array.
401401
"""
402402
return PyArray_SIZE(self)
403403

404404
@property
405-
cdef inline char* data(self) nogil:
405+
cdef inline char* data(self) noexcept nogil:
406406
"""The pointer to the data buffer as a char*.
407407
This is provided for legacy reasons to avoid direct struct field access.
408408
For new code that needs this access, you probably want to cast the result
@@ -1007,7 +1007,7 @@ cdef extern from "numpy/ufuncobject.h":
10071007

10081008
int _import_umath() except -1
10091009

1010-
cdef inline void set_array_base(ndarray arr, object base):
1010+
cdef inline void set_array_base(ndarray arr, object base) except *:
10111011
Py_INCREF(base) # important to do this before stealing the reference below!
10121012
PyArray_SetBaseObject(arr, base)
10131013

@@ -1038,7 +1038,7 @@ cdef inline int import_ufunc() except -1:
10381038
raise ImportError("numpy._core.umath failed to import")
10391039

10401040

1041-
cdef inline bint is_timedelta64_object(object obj):
1041+
cdef inline bint is_timedelta64_object(object obj) noexcept:
10421042
"""
10431043
Cython equivalent of `isinstance(obj, np.timedelta64)`
10441044
@@ -1053,7 +1053,7 @@ cdef inline bint is_timedelta64_object(object obj):
10531053
return PyObject_TypeCheck(obj, &PyTimedeltaArrType_Type)
10541054

10551055

1056-
cdef inline bint is_datetime64_object(object obj):
1056+
cdef inline bint is_datetime64_object(object obj) noexcept:
10571057
"""
10581058
Cython equivalent of `isinstance(obj, np.datetime64)`
10591059
@@ -1068,7 +1068,7 @@ cdef inline bint is_datetime64_object(object obj):
10681068
return PyObject_TypeCheck(obj, &PyDatetimeArrType_Type)
10691069

10701070

1071-
cdef inline npy_datetime get_datetime64_value(object obj) nogil:
1071+
cdef inline npy_datetime get_datetime64_value(object obj) noexcept nogil:
10721072
"""
10731073
returns the int64 value underlying scalar numpy datetime64 object
10741074
@@ -1078,14 +1078,14 @@ cdef inline npy_datetime get_datetime64_value(object obj) nogil:
10781078
return (<PyDatetimeScalarObject*>obj).obval
10791079

10801080

1081-
cdef inline npy_timedelta get_timedelta64_value(object obj) nogil:
1081+
cdef inline npy_timedelta get_timedelta64_value(object obj) noexcept nogil:
10821082
"""
10831083
returns the int64 value underlying scalar numpy timedelta64 object
10841084
"""
10851085
return (<PyTimedeltaScalarObject*>obj).obval
10861086

10871087

1088-
cdef inline NPY_DATETIMEUNIT get_datetime64_unit(object obj) nogil:
1088+
cdef inline NPY_DATETIMEUNIT get_datetime64_unit(object obj) noexcept nogil:
10891089
"""
10901090
returns the unit part of the dtype for a numpy datetime64 object.
10911091
"""

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)