Skip to content

Commit

Permalink
MAINT: add missing noexcept clauses (1/2)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tornaria authored and charris committed Mar 25, 2024
1 parent ceab92f commit 19fba02
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
24 changes: 12 additions & 12 deletions numpy/__init__.cython-30.pxd
Expand Up @@ -242,7 +242,7 @@ cdef extern from "numpy/arrayobject.h":
# Instead, we use properties that map to the corresponding C-API functions.

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

@property
cdef inline int ndim(self) nogil:
cdef inline int ndim(self) noexcept nogil:
"""Returns the number of dimensions in the array.
"""
return PyArray_NDIM(self)

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

@property
cdef inline npy_intp *strides(self) nogil:
cdef inline npy_intp *strides(self) noexcept nogil:
"""Returns a pointer to the strides of the array.
The number of elements matches the number of dimensions of the array (ndim).
"""
return PyArray_STRIDES(self)

@property
cdef inline npy_intp size(self) nogil:
cdef inline npy_intp size(self) noexcept nogil:
"""Returns the total size (in number of elements) of the array.
"""
return PyArray_SIZE(self)

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

int _import_umath() except -1

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

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


cdef inline bint is_timedelta64_object(object obj):
cdef inline bint is_timedelta64_object(object obj) noexcept:
"""
Cython equivalent of `isinstance(obj, np.timedelta64)`
Expand All @@ -1011,7 +1011,7 @@ cdef inline bint is_timedelta64_object(object obj):
return PyObject_TypeCheck(obj, &PyTimedeltaArrType_Type)


cdef inline bint is_datetime64_object(object obj):
cdef inline bint is_datetime64_object(object obj) noexcept:
"""
Cython equivalent of `isinstance(obj, np.datetime64)`
Expand All @@ -1026,7 +1026,7 @@ cdef inline bint is_datetime64_object(object obj):
return PyObject_TypeCheck(obj, &PyDatetimeArrType_Type)


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


cdef inline npy_timedelta get_timedelta64_value(object obj) nogil:
cdef inline npy_timedelta get_timedelta64_value(object obj) noexcept nogil:
"""
returns the int64 value underlying scalar numpy timedelta64 object
"""
return (<PyTimedeltaScalarObject*>obj).obval


cdef inline NPY_DATETIMEUNIT get_datetime64_unit(object obj) nogil:
cdef inline NPY_DATETIMEUNIT get_datetime64_unit(object obj) noexcept nogil:
"""
returns the unit part of the dtype for a numpy datetime64 object.
"""
Expand Down
2 changes: 1 addition & 1 deletion numpy/random/_bounded_integers.pxd.in
Expand Up @@ -6,7 +6,7 @@ ctypedef np.npy_bool bool_t

from numpy.random cimport bitgen_t

cdef inline uint64_t _gen_mask(uint64_t max_val) nogil:
cdef inline uint64_t _gen_mask(uint64_t max_val) noexcept nogil:
"""Mask generator for use in bounded random numbers"""
# Smallest bit mask >= max
cdef uint64_t mask = max_val
Expand Down

0 comments on commit 19fba02

Please sign in to comment.