Skip to content

Commit

Permalink
Mark functions as noexcept to support Cython 3 (#6936)
Browse files Browse the repository at this point in the history
* Explicitely mark functions as noexcept

* Require Cython version 0.29.32 or higher

* Explicitely mark functions as noexcept

* Bump cython version in pyproject.toml

* Mark explicitelly inline functions in interpolation.pxd as `noexcept`

* Bump Cython version

* Revert changes in pyproject.toml

* Generate pyproject.toml from pyproject.toml.in
  • Loading branch information
matusvalo committed May 16, 2023
1 parent 4d6d1a0 commit a23f756
Show file tree
Hide file tree
Showing 31 changed files with 141 additions and 141 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Expand Up @@ -61,7 +61,7 @@ build = [
'setuptools>=67',
'packaging>=21',
'ninja',
'Cython>=0.29.26',
'Cython>=0.29.32',
'pythran',
'numpy>=1.21.1',
'spin==0.3',
Expand Down Expand Up @@ -138,7 +138,7 @@ requires = [
'wheel',
'setuptools>=67',
'packaging>=21',
'Cython>=0.29.26',
'Cython>=0.29.32',
'pythran',
'lazy_loader>=0.2',
"numpy==1.22.3; python_version=='3.10' and platform_system=='Windows' and platform_python_implementation != 'PyPy'",
Expand Down
2 changes: 1 addition & 1 deletion requirements/build.txt
Expand Up @@ -4,7 +4,7 @@ wheel
setuptools>=67
packaging>=21
ninja
Cython>=0.29.26
Cython>=0.29.32
pythran
numpy>=1.21.1

Expand Down
2 changes: 1 addition & 1 deletion skimage/_shared/geometry.pxd
Expand Up @@ -13,4 +13,4 @@ cdef unsigned char point_in_polygon(np_floats[::1] xp, np_floats[::1] yp,

cdef void points_in_polygon(np_floats[::1] xp, np_floats[::1] yp,
np_floats[::1] x, np_floats[::1] y,
unsigned char[::1] result) nogil
unsigned char[::1] result) noexcept nogil
2 changes: 1 addition & 1 deletion skimage/_shared/geometry.pyx
Expand Up @@ -78,7 +78,7 @@ cdef unsigned char point_in_polygon(np_floats[::1] xp, np_floats[::1] yp,

cdef void points_in_polygon(np_floats[::1] xp, np_floats[::1] yp,
np_floats[::1] x, np_floats[::1] y,
unsigned char[::1] result) nogil:
unsigned char[::1] result) noexcept nogil:
"""Test whether points lie inside a polygon.
Parameters
Expand Down
24 changes: 12 additions & 12 deletions skimage/_shared/interpolation.pxd
Expand Up @@ -22,15 +22,15 @@ import numpy as np
cimport numpy as np
from .fused_numerics cimport np_real_numeric, np_floats

cdef inline Py_ssize_t round(np_floats r) nogil:
cdef inline Py_ssize_t round(np_floats r) noexcept nogil:
return <Py_ssize_t>(
(r + <np_floats>0.5) if (r > <np_floats>0.0) else (r - <np_floats>0.5)
)

cdef inline Py_ssize_t fmax(Py_ssize_t one, Py_ssize_t two) nogil:
cdef inline Py_ssize_t fmax(Py_ssize_t one, Py_ssize_t two) noexcept nogil:
return one if one > two else two

cdef inline Py_ssize_t fmin(Py_ssize_t one, Py_ssize_t two) nogil:
cdef inline Py_ssize_t fmin(Py_ssize_t one, Py_ssize_t two) noexcept nogil:
return one if one < two else two

# Redefine np_real_numeric to force cross type compilation
Expand All @@ -42,7 +42,7 @@ ctypedef fused np_real_numeric_out:
cdef inline void nearest_neighbor_interpolation(
np_real_numeric* image, Py_ssize_t rows, Py_ssize_t cols,
np_floats r, np_floats c, char mode, np_real_numeric cval,
np_real_numeric_out* out) nogil:
np_real_numeric_out* out) noexcept nogil:
"""Nearest neighbor interpolation at a given position in the image.
Parameters
Expand Down Expand Up @@ -72,7 +72,7 @@ cdef inline void nearest_neighbor_interpolation(
cdef inline void bilinear_interpolation(
np_real_numeric* image, Py_ssize_t rows, Py_ssize_t cols,
np_floats r, np_floats c, char mode, np_real_numeric cval,
np_real_numeric_out* out) nogil:
np_real_numeric_out* out) noexcept nogil:
"""Bilinear interpolation at a given position in the image.
Parameters
Expand Down Expand Up @@ -117,7 +117,7 @@ cdef inline void bilinear_interpolation(
out[0] = <np_real_numeric_out> ((1 - dr) * top + dr * bottom)

cdef inline np_floats quadratic_interpolation(np_floats x,
np_real_numeric[3] f) nogil:
np_real_numeric[3] f) noexcept nogil:
"""WARNING: Do not use, not implemented correctly.
Quadratic interpolation.
Expand All @@ -143,7 +143,7 @@ cdef inline np_floats quadratic_interpolation(np_floats x,
cdef inline void biquadratic_interpolation(
np_real_numeric* image, Py_ssize_t rows, Py_ssize_t cols,
np_floats r, np_floats c, char mode, np_real_numeric cval,
np_real_numeric_out* out) nogil:
np_real_numeric_out* out) noexcept nogil:
"""WARNING: Do not use, not implemented correctly.
Biquadratic interpolation at a given position in the image.
Expand Down Expand Up @@ -189,7 +189,7 @@ cdef inline void biquadratic_interpolation(
out[0] = <np_real_numeric_out>quadratic_interpolation(xr, fr)


cdef inline np_floats cubic_interpolation(np_floats x, np_real_numeric[4] f) nogil:
cdef inline np_floats cubic_interpolation(np_floats x, np_real_numeric[4] f) noexcept nogil:
"""Cubic interpolation.
Parameters
Expand Down Expand Up @@ -225,7 +225,7 @@ cdef inline void bicubic_interpolation(np_real_numeric* image,
Py_ssize_t rows, Py_ssize_t cols,
np_floats r, np_floats c, char mode,
np_real_numeric cval,
np_real_numeric_out* out) nogil:
np_real_numeric_out* out) noexcept nogil:
"""Bicubic interpolation at a given position in the image.
Interpolation using Catmull-Rom splines, based on the bicubic convolution
Expand Down Expand Up @@ -282,7 +282,7 @@ cdef inline void bicubic_interpolation(np_real_numeric* image,
cdef inline np_real_numeric get_pixel2d(np_real_numeric* image,
Py_ssize_t rows, Py_ssize_t cols,
long r, long c, char mode,
np_real_numeric cval) nogil:
np_real_numeric cval) noexcept nogil:
"""Get a pixel from the image, taking wrapping mode into consideration.
Parameters
Expand Down Expand Up @@ -318,7 +318,7 @@ cdef inline np_real_numeric get_pixel3d(np_real_numeric* image,
Py_ssize_t rows, Py_ssize_t cols,
Py_ssize_t dims, Py_ssize_t r,
Py_ssize_t c, Py_ssize_t d, char mode,
np_real_numeric cval) nogil:
np_real_numeric cval) noexcept nogil:
"""Get a pixel from the image, taking wrapping mode into consideration.
Parameters
Expand Down Expand Up @@ -350,7 +350,7 @@ cdef inline np_real_numeric get_pixel3d(np_real_numeric* image,
coord_map(dims, d, mode)]


cdef inline Py_ssize_t coord_map(Py_ssize_t dim, long coord, char mode) nogil:
cdef inline Py_ssize_t coord_map(Py_ssize_t dim, long coord, char mode) noexcept nogil:
"""Wrap a coordinate, according to a given mode.
Parameters
Expand Down
4 changes: 2 additions & 2 deletions skimage/filters/_multiotsu.pyx
Expand Up @@ -64,7 +64,7 @@ cdef void _set_var_btwcls_lut(cnp.float32_t [::1] prob,
Py_ssize_t nbins,
cnp.float32_t [::1] var_btwcls,
cnp.float32_t [::1] zeroth_moment,
cnp.float32_t [::1] first_moment) nogil:
cnp.float32_t [::1] first_moment) noexcept nogil:
"""Builds the lookup table containing the variance between classes.
The variance between classes are stored in
Expand Down Expand Up @@ -281,7 +281,7 @@ def _get_multiotsu_thresh_indices(cnp.float32_t [::1] prob,
cdef void _set_moments_lut_first_row(cnp.float32_t [::1] prob,
Py_ssize_t nbins,
cnp.float32_t [::1] zeroth_moment,
cnp.float32_t [::1] first_moment) nogil:
cnp.float32_t [::1] first_moment) noexcept nogil:
"""Builds the first rows of the zeroth and first moments lookup table
necessary to the computation of the variance between class.
Expand Down
6 changes: 3 additions & 3 deletions skimage/filters/rank/bilateral_cy.pyx
Expand Up @@ -14,7 +14,7 @@ cdef inline void _kernel_mean(dtype_t_out* out, Py_ssize_t odepth,
cnp.float64_t pop, dtype_t g,
Py_ssize_t n_bins, Py_ssize_t mid_bin,
cnp.float64_t p0, cnp.float64_t p1,
Py_ssize_t s0, Py_ssize_t s1) nogil:
Py_ssize_t s0, Py_ssize_t s1) noexcept nogil:

cdef Py_ssize_t i
cdef Py_ssize_t bilat_pop = 0
Expand All @@ -38,7 +38,7 @@ cdef inline void _kernel_pop(dtype_t_out* out, Py_ssize_t odepth,
cnp.float64_t pop, dtype_t g,
Py_ssize_t n_bins, Py_ssize_t mid_bin,
cnp.float64_t p0, cnp.float64_t p1,
Py_ssize_t s0, Py_ssize_t s1) nogil:
Py_ssize_t s0, Py_ssize_t s1) noexcept nogil:

cdef Py_ssize_t i
cdef Py_ssize_t bilat_pop = 0
Expand All @@ -57,7 +57,7 @@ cdef inline void _kernel_sum(dtype_t_out* out, Py_ssize_t odepth,
cnp.float64_t pop, dtype_t g,
Py_ssize_t n_bins, Py_ssize_t mid_bin,
cnp.float64_t p0, cnp.float64_t p1,
Py_ssize_t s0, Py_ssize_t s1) nogil:
Py_ssize_t s0, Py_ssize_t s1) noexcept nogil:

cdef Py_ssize_t i
cdef Py_ssize_t bilat_pop = 0
Expand Down
6 changes: 3 additions & 3 deletions skimage/filters/rank/core_cy.pxd
Expand Up @@ -12,14 +12,14 @@ ctypedef fused dtype_t_out:
float64_t


cdef dtype_t _max(dtype_t a, dtype_t b) nogil
cdef dtype_t _min(dtype_t a, dtype_t b) nogil
cdef dtype_t _max(dtype_t a, dtype_t b) noexcept nogil
cdef dtype_t _min(dtype_t a, dtype_t b) noexcept nogil


cdef void _core(void kernel(dtype_t_out*, Py_ssize_t, Py_ssize_t[::1],
float64_t, dtype_t, Py_ssize_t, Py_ssize_t,
float64_t, float64_t, Py_ssize_t,
Py_ssize_t) nogil,
Py_ssize_t) noexcept nogil,
dtype_t[:, ::1] image,
char[:, ::1] footprint,
char[:, ::1] mask,
Expand Down
12 changes: 6 additions & 6 deletions skimage/filters/rank/core_cy.pyx
Expand Up @@ -9,29 +9,29 @@ cimport numpy as cnp
cnp.import_array()


cdef inline dtype_t _max(dtype_t a, dtype_t b) nogil:
cdef inline dtype_t _max(dtype_t a, dtype_t b) noexcept nogil:
return a if a >= b else b


cdef inline dtype_t _min(dtype_t a, dtype_t b) nogil:
cdef inline dtype_t _min(dtype_t a, dtype_t b) noexcept nogil:
return a if a <= b else b


cdef inline void histogram_increment(Py_ssize_t[::1] histo, cnp.float64_t* pop,
dtype_t value) nogil:
dtype_t value) noexcept nogil:
histo[value] += 1
pop[0] += 1


cdef inline void histogram_decrement(Py_ssize_t[::1] histo, cnp.float64_t* pop,
dtype_t value) nogil:
dtype_t value) noexcept nogil:
histo[value] -= 1
pop[0] -= 1


cdef inline char is_in_mask(Py_ssize_t rows, Py_ssize_t cols,
Py_ssize_t r, Py_ssize_t c,
char* mask) nogil:
char* mask) noexcept nogil:
"""Check whether given coordinate is within image and mask is true."""
if r < 0 or r > rows - 1 or c < 0 or c > cols - 1:
return 0
Expand All @@ -46,7 +46,7 @@ cdef inline char is_in_mask(Py_ssize_t rows, Py_ssize_t cols,

cdef void _core(void kernel(dtype_t_out*, Py_ssize_t, Py_ssize_t[::1], cnp.float64_t,
dtype_t, Py_ssize_t, Py_ssize_t, cnp.float64_t,
cnp.float64_t, Py_ssize_t, Py_ssize_t) nogil,
cnp.float64_t, Py_ssize_t, Py_ssize_t) noexcept nogil,
dtype_t[:, ::1] image,
char[:, ::1] footprint,
char[:, ::1] mask,
Expand Down
6 changes: 3 additions & 3 deletions skimage/filters/rank/core_cy_3d.pxd
Expand Up @@ -12,14 +12,14 @@ ctypedef fused dtype_t_out:
float64_t


cdef dtype_t _max(dtype_t a, dtype_t b) nogil
cdef dtype_t _min(dtype_t a, dtype_t b) nogil
cdef dtype_t _max(dtype_t a, dtype_t b) noexcept nogil
cdef dtype_t _min(dtype_t a, dtype_t b) noexcept nogil


cdef void _core_3D(void kernel(dtype_t_out*, Py_ssize_t, Py_ssize_t[::1],
float64_t, dtype_t, Py_ssize_t, Py_ssize_t,
float64_t, float64_t, Py_ssize_t,
Py_ssize_t) nogil,
Py_ssize_t) noexcept nogil,
dtype_t[:, :, ::1] image,
char[:, :, ::1] footprint,
char[:, :, ::1] mask,
Expand Down
14 changes: 7 additions & 7 deletions skimage/filters/rank/core_cy_3d.pyx
Expand Up @@ -8,11 +8,11 @@ cimport numpy as cnp

cnp.import_array()

cdef inline dtype_t _max(dtype_t a, dtype_t b) nogil:
cdef inline dtype_t _max(dtype_t a, dtype_t b) noexcept nogil:
return a if a >= b else b


cdef inline dtype_t _min(dtype_t a, dtype_t b) nogil:
cdef inline dtype_t _min(dtype_t a, dtype_t b) noexcept nogil:
return a if a <= b else b


Expand All @@ -24,7 +24,7 @@ cdef inline void _count_attack_border_elements(char[:, :, ::1] footprint,
Py_ssize_t scols,
Py_ssize_t centre_p,
Py_ssize_t centre_r,
Py_ssize_t centre_c):
Py_ssize_t centre_c) noexcept:

cdef Py_ssize_t r, c, p

Expand Down Expand Up @@ -88,7 +88,7 @@ cdef inline void _build_initial_histogram_from_neighborhood(dtype_t[:, :, ::1] i
Py_ssize_t scols,
Py_ssize_t centre_p,
Py_ssize_t centre_r,
Py_ssize_t centre_c):
Py_ssize_t centre_c) noexcept:

cdef Py_ssize_t r, c, j

Expand All @@ -115,7 +115,7 @@ cdef inline void _update_histogram(dtype_t[:, :, ::1] image,
Py_ssize_t p, Py_ssize_t r, Py_ssize_t c,
Py_ssize_t planes, Py_ssize_t rows,
Py_ssize_t cols,
Py_ssize_t axis_inc) nogil:
Py_ssize_t axis_inc) noexcept nogil:

cdef Py_ssize_t pp, rr, cc, j

Expand Down Expand Up @@ -147,7 +147,7 @@ cdef inline void _update_histogram(dtype_t[:, :, ::1] image,

cdef inline char is_in_mask_3D(Py_ssize_t planes, Py_ssize_t rows,
Py_ssize_t cols, Py_ssize_t p, Py_ssize_t r,
Py_ssize_t c, char* mask) nogil:
Py_ssize_t c, char* mask) noexcept nogil:
"""Check whether given coordinate is within image and mask is true."""
if (r < 0 or r > rows - 1 or c < 0 or c > cols - 1 or
p < 0 or p > planes - 1):
Expand All @@ -160,7 +160,7 @@ cdef inline char is_in_mask_3D(Py_ssize_t planes, Py_ssize_t rows,

cdef void _core_3D(void kernel(dtype_t_out*, Py_ssize_t, Py_ssize_t[::1], cnp.float64_t,
dtype_t, Py_ssize_t, Py_ssize_t, cnp.float64_t,
cnp.float64_t, Py_ssize_t, Py_ssize_t) nogil,
cnp.float64_t, Py_ssize_t, Py_ssize_t) noexcept nogil,
dtype_t[:, :, ::1] image,
char[:, :, ::1] footprint,
char[:, :, ::1] mask,
Expand Down

0 comments on commit a23f756

Please sign in to comment.