Skip to content

Commit db9083c

Browse files
authored
MNT: Mark all nogil functions as noexcept (#53379)
* MNT: Mark all `nogil` functions as `noexcept` * Make linter happy
1 parent db119eb commit db9083c

17 files changed

+130
-126
lines changed

pandas/_libs/algos.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ from pandas._libs.dtypes cimport (
44
)
55

66

7-
cdef numeric_t kth_smallest_c(numeric_t* arr, Py_ssize_t k, Py_ssize_t n) nogil
7+
cdef numeric_t kth_smallest_c(numeric_t* arr, Py_ssize_t k, Py_ssize_t n) noexcept nogil
88

99
cdef enum TiebreakEnumType:
1010
TIEBREAK_AVERAGE

pandas/_libs/algos.pyx

+4-3
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ def groupsort_indexer(const intp_t[:] index, Py_ssize_t ngroups):
259259
return indexer.base, counts.base
260260

261261

262-
cdef Py_ssize_t swap(numeric_t *a, numeric_t *b) nogil:
262+
cdef Py_ssize_t swap(numeric_t *a, numeric_t *b) noexcept nogil:
263263
cdef:
264264
numeric_t t
265265

@@ -270,7 +270,8 @@ cdef Py_ssize_t swap(numeric_t *a, numeric_t *b) nogil:
270270
return 0
271271

272272

273-
cdef numeric_t kth_smallest_c(numeric_t* arr, Py_ssize_t k, Py_ssize_t n) nogil:
273+
cdef numeric_t kth_smallest_c(numeric_t* arr,
274+
Py_ssize_t k, Py_ssize_t n) noexcept nogil:
274275
"""
275276
See kth_smallest.__doc__. The additional parameter n specifies the maximum
276277
number of elements considered in arr, needed for compatibility with usage
@@ -1062,7 +1063,7 @@ cdef void rank_sorted_1d(
10621063
# https://github.com/cython/cython/issues/1630, only trailing arguments can
10631064
# currently be omitted for cdef functions, which is why we keep this at the end
10641065
const intp_t[:] labels=None,
1065-
) nogil:
1066+
) noexcept nogil:
10661067
"""
10671068
See rank_1d.__doc__. Handles only actual ranking, so sorting and masking should
10681069
be handled in the caller. Note that `out` and `grp_sizes` are modified inplace.

pandas/_libs/groupby.pyx

+5-4
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ cdef enum InterpolationEnumType:
6262
INTERPOLATION_MIDPOINT
6363

6464

65-
cdef float64_t median_linear_mask(float64_t* a, int n, uint8_t* mask) nogil:
65+
cdef float64_t median_linear_mask(float64_t* a, int n, uint8_t* mask) noexcept nogil:
6666
cdef:
6767
int i, j, na_count = 0
6868
float64_t* tmp
@@ -99,7 +99,7 @@ cdef float64_t median_linear_mask(float64_t* a, int n, uint8_t* mask) nogil:
9999
return result
100100

101101

102-
cdef float64_t median_linear(float64_t* a, int n) nogil:
102+
cdef float64_t median_linear(float64_t* a, int n) noexcept nogil:
103103
cdef:
104104
int i, j, na_count = 0
105105
float64_t* tmp
@@ -136,7 +136,7 @@ cdef float64_t median_linear(float64_t* a, int n) nogil:
136136
return result
137137

138138

139-
cdef float64_t calc_median_linear(float64_t* a, int n, int na_count) nogil:
139+
cdef float64_t calc_median_linear(float64_t* a, int n, int na_count) noexcept nogil:
140140
cdef:
141141
float64_t result
142142

@@ -1300,7 +1300,8 @@ ctypedef fused numeric_object_complex_t:
13001300
complex128_t
13011301

13021302

1303-
cdef bint _treat_as_na(numeric_object_complex_t val, bint is_datetimelike) nogil:
1303+
cdef bint _treat_as_na(numeric_object_complex_t val,
1304+
bint is_datetimelike) noexcept nogil:
13041305
if numeric_object_complex_t is object:
13051306
# Should never be used, but we need to avoid the `val != val` below
13061307
# or else cython will raise about gil acquisition.

pandas/_libs/hashing.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ def hash_object_array(
110110
return result.base # .base to retrieve underlying np.ndarray
111111

112112

113-
cdef uint64_t _rotl(uint64_t x, uint64_t b) nogil:
113+
cdef uint64_t _rotl(uint64_t x, uint64_t b) noexcept nogil:
114114
return (x << b) | (x >> (64 - b))
115115

116116

117-
cdef uint64_t u8to64_le(uint8_t* p) nogil:
117+
cdef uint64_t u8to64_le(uint8_t* p) noexcept nogil:
118118
return (<uint64_t>p[0] |
119119
<uint64_t>p[1] << 8 |
120120
<uint64_t>p[2] << 16 |
@@ -145,7 +145,7 @@ cdef void _sipround(uint64_t* v0, uint64_t* v1,
145145

146146
@cython.cdivision(True)
147147
cdef uint64_t low_level_siphash(uint8_t* data, size_t datalen,
148-
uint8_t* key) nogil:
148+
uint8_t* key) noexcept nogil:
149149
cdef uint64_t v0 = 0x736f6d6570736575ULL
150150
cdef uint64_t v1 = 0x646f72616e646f6dULL
151151
cdef uint64_t v2 = 0x6c7967656e657261ULL

pandas/_libs/hashtable_class_helper.pxi.in

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ complex_types = ['complex64',
1313
}}
1414

1515
{{for name in complex_types}}
16-
cdef kh{{name}}_t to_kh{{name}}_t({{name}}_t val) nogil:
16+
cdef kh{{name}}_t to_kh{{name}}_t({{name}}_t val) noexcept nogil:
1717
cdef kh{{name}}_t res
1818
res.real = val.real
1919
res.imag = val.imag
@@ -42,7 +42,7 @@ c_types = ['khcomplex128_t',
4242

4343
{{for c_type in c_types}}
4444

45-
cdef bint is_nan_{{c_type}}({{c_type}} val) nogil:
45+
cdef bint is_nan_{{c_type}}({{c_type}} val) noexcept nogil:
4646
{{if c_type in {'khcomplex128_t', 'khcomplex64_t'} }}
4747
return val.real != val.real or val.imag != val.imag
4848
{{elif c_type in {'float64_t', 'float32_t'} }}
@@ -55,7 +55,7 @@ cdef bint is_nan_{{c_type}}({{c_type}} val) nogil:
5555
{{if c_type in {'khcomplex128_t', 'khcomplex64_t', 'float64_t', 'float32_t'} }}
5656
# are_equivalent_{{c_type}} is cimported via khash.pxd
5757
{{else}}
58-
cdef bint are_equivalent_{{c_type}}({{c_type}} val1, {{c_type}} val2) nogil:
58+
cdef bint are_equivalent_{{c_type}}({{c_type}} val1, {{c_type}} val2) noexcept nogil:
5959
return val1 == val2
6060
{{endif}}
6161

@@ -163,7 +163,7 @@ ctypedef fused vector_data:
163163
Complex64VectorData
164164
StringVectorData
165165

166-
cdef bint needs_resize(vector_data *data) nogil:
166+
cdef bint needs_resize(vector_data *data) noexcept nogil:
167167
return data.n == data.m
168168

169169
# ----------------------------------------------------------------------

pandas/_libs/tslibs/ccalendar.pxd

+8-8
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ from numpy cimport (
66

77
ctypedef (int32_t, int32_t, int32_t) iso_calendar_t
88

9-
cdef int dayofweek(int y, int m, int d) nogil
10-
cdef bint is_leapyear(int64_t year) nogil
11-
cpdef int32_t get_days_in_month(int year, Py_ssize_t month) nogil
12-
cpdef int32_t get_week_of_year(int year, int month, int day) nogil
13-
cpdef iso_calendar_t get_iso_calendar(int year, int month, int day) nogil
14-
cpdef int32_t get_day_of_year(int year, int month, int day) nogil
15-
cpdef int get_lastbday(int year, int month) nogil
16-
cpdef int get_firstbday(int year, int month) nogil
9+
cdef int dayofweek(int y, int m, int d) noexcept nogil
10+
cdef bint is_leapyear(int64_t year) noexcept nogil
11+
cpdef int32_t get_days_in_month(int year, Py_ssize_t month) noexcept nogil
12+
cpdef int32_t get_week_of_year(int year, int month, int day) noexcept nogil
13+
cpdef iso_calendar_t get_iso_calendar(int year, int month, int day) noexcept nogil
14+
cpdef int32_t get_day_of_year(int year, int month, int day) noexcept nogil
15+
cpdef int get_lastbday(int year, int month) noexcept nogil
16+
cpdef int get_firstbday(int year, int month) noexcept nogil
1717

1818
cdef dict c_MONTH_NUMBERS
1919

pandas/_libs/tslibs/ccalendar.pyx

+8-8
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ weekday_to_int = {int_to_weekday[key]: key for key in int_to_weekday}
5353

5454
@cython.wraparound(False)
5555
@cython.boundscheck(False)
56-
cpdef int32_t get_days_in_month(int year, Py_ssize_t month) nogil:
56+
cpdef int32_t get_days_in_month(int year, Py_ssize_t month) noexcept nogil:
5757
"""
5858
Return the number of days in the given month of the given year.
5959
@@ -77,7 +77,7 @@ cpdef int32_t get_days_in_month(int year, Py_ssize_t month) nogil:
7777
@cython.wraparound(False)
7878
@cython.boundscheck(False)
7979
@cython.cdivision
80-
cdef int dayofweek(int y, int m, int d) nogil:
80+
cdef int dayofweek(int y, int m, int d) noexcept nogil:
8181
"""
8282
Find the day of week for the date described by the Y/M/D triple y, m, d
8383
using Sakamoto's method, from wikipedia.
@@ -114,7 +114,7 @@ cdef int dayofweek(int y, int m, int d) nogil:
114114
return (day + 6) % 7
115115

116116

117-
cdef bint is_leapyear(int64_t year) nogil:
117+
cdef bint is_leapyear(int64_t year) noexcept nogil:
118118
"""
119119
Returns 1 if the given year is a leap year, 0 otherwise.
120120
@@ -132,7 +132,7 @@ cdef bint is_leapyear(int64_t year) nogil:
132132

133133
@cython.wraparound(False)
134134
@cython.boundscheck(False)
135-
cpdef int32_t get_week_of_year(int year, int month, int day) nogil:
135+
cpdef int32_t get_week_of_year(int year, int month, int day) noexcept nogil:
136136
"""
137137
Return the ordinal week-of-year for the given day.
138138
@@ -155,7 +155,7 @@ cpdef int32_t get_week_of_year(int year, int month, int day) nogil:
155155

156156
@cython.wraparound(False)
157157
@cython.boundscheck(False)
158-
cpdef iso_calendar_t get_iso_calendar(int year, int month, int day) nogil:
158+
cpdef iso_calendar_t get_iso_calendar(int year, int month, int day) noexcept nogil:
159159
"""
160160
Return the year, week, and day of year corresponding to ISO 8601
161161
@@ -209,7 +209,7 @@ cpdef iso_calendar_t get_iso_calendar(int year, int month, int day) nogil:
209209

210210
@cython.wraparound(False)
211211
@cython.boundscheck(False)
212-
cpdef int32_t get_day_of_year(int year, int month, int day) nogil:
212+
cpdef int32_t get_day_of_year(int year, int month, int day) noexcept nogil:
213213
"""
214214
Return the ordinal day-of-year for the given day.
215215
@@ -243,7 +243,7 @@ cpdef int32_t get_day_of_year(int year, int month, int day) nogil:
243243
# ---------------------------------------------------------------------
244244
# Business Helpers
245245

246-
cpdef int get_lastbday(int year, int month) nogil:
246+
cpdef int get_lastbday(int year, int month) noexcept nogil:
247247
"""
248248
Find the last day of the month that is a business day.
249249
@@ -264,7 +264,7 @@ cpdef int get_lastbday(int year, int month) nogil:
264264
return days_in_month - max(((wkday + days_in_month - 1) % 7) - 4, 0)
265265

266266

267-
cpdef int get_firstbday(int year, int month) nogil:
267+
cpdef int get_firstbday(int year, int month) noexcept nogil:
268268
"""
269269
Find the first day of the month that is a business day.
270270

pandas/_libs/tslibs/dtypes.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ from pandas._libs.tslibs.np_datetime cimport NPY_DATETIMEUNIT
55

66
cpdef str npy_unit_to_abbrev(NPY_DATETIMEUNIT unit)
77
cpdef NPY_DATETIMEUNIT abbrev_to_npy_unit(str abbrev)
8-
cdef NPY_DATETIMEUNIT freq_group_code_to_npy_unit(int freq) nogil
8+
cdef NPY_DATETIMEUNIT freq_group_code_to_npy_unit(int freq) noexcept nogil
99
cpdef int64_t periods_per_day(NPY_DATETIMEUNIT reso=*) except? -1
1010
cpdef int64_t periods_per_second(NPY_DATETIMEUNIT reso) except? -1
1111
cpdef NPY_DATETIMEUNIT get_supported_reso(NPY_DATETIMEUNIT reso)

pandas/_libs/tslibs/dtypes.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ cpdef NPY_DATETIMEUNIT abbrev_to_npy_unit(str abbrev):
399399
raise ValueError(f"Unrecognized unit {abbrev}")
400400

401401

402-
cdef NPY_DATETIMEUNIT freq_group_code_to_npy_unit(int freq) nogil:
402+
cdef NPY_DATETIMEUNIT freq_group_code_to_npy_unit(int freq) noexcept nogil:
403403
"""
404404
Convert the freq to the corresponding NPY_DATETIMEUNIT to pass
405405
to npy_datetimestruct_to_datetime.

pandas/_libs/tslibs/fields.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def get_date_name_field(
193193
return out
194194

195195

196-
cdef bint _is_on_month(int month, int compare_month, int modby) nogil:
196+
cdef bint _is_on_month(int month, int compare_month, int modby) noexcept nogil:
197197
"""
198198
Analogous to DateOffset.is_on_offset checking for the month part of a date.
199199
"""

pandas/_libs/tslibs/np_datetime.pxd

+3-3
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ cdef int64_t pydate_to_dt64(
9696
)
9797
cdef void pydate_to_dtstruct(date val, npy_datetimestruct *dts) noexcept
9898

99-
cdef npy_datetime get_datetime64_value(object obj) nogil
100-
cdef npy_timedelta get_timedelta64_value(object obj) nogil
101-
cdef NPY_DATETIMEUNIT get_datetime64_unit(object obj) nogil
99+
cdef npy_datetime get_datetime64_value(object obj) noexcept nogil
100+
cdef npy_timedelta get_timedelta64_value(object obj) noexcept nogil
101+
cdef NPY_DATETIMEUNIT get_datetime64_unit(object obj) noexcept nogil
102102

103103
cdef int string_to_dts(
104104
str val,

pandas/_libs/tslibs/np_datetime.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ cdef extern from "src/datetime/pd_datetime.h":
5959
# ----------------------------------------------------------------------
6060
# numpy object inspection
6161

62-
cdef npy_datetime get_datetime64_value(object obj) nogil:
62+
cdef npy_datetime get_datetime64_value(object obj) noexcept nogil:
6363
"""
6464
returns the int64 value underlying scalar numpy datetime64 object
6565
@@ -69,14 +69,14 @@ cdef npy_datetime get_datetime64_value(object obj) nogil:
6969
return (<PyDatetimeScalarObject*>obj).obval
7070

7171

72-
cdef npy_timedelta get_timedelta64_value(object obj) nogil:
72+
cdef npy_timedelta get_timedelta64_value(object obj) noexcept nogil:
7373
"""
7474
returns the int64 value underlying scalar numpy timedelta64 object
7575
"""
7676
return (<PyTimedeltaScalarObject*>obj).obval
7777

7878

79-
cdef NPY_DATETIMEUNIT get_datetime64_unit(object obj) nogil:
79+
cdef NPY_DATETIMEUNIT get_datetime64_unit(object obj) noexcept nogil:
8080
"""
8181
returns the unit part of the dtype for a numpy datetime64 object.
8282
"""

pandas/_libs/tslibs/offsets.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -4470,14 +4470,14 @@ cdef datetime _shift_day(datetime other, int days):
44704470
return localize_pydatetime(shifted, tz)
44714471

44724472

4473-
cdef int year_add_months(npy_datetimestruct dts, int months) nogil:
4473+
cdef int year_add_months(npy_datetimestruct dts, int months) noexcept nogil:
44744474
"""
44754475
New year number after shifting npy_datetimestruct number of months.
44764476
"""
44774477
return dts.year + (dts.month + months - 1) // 12
44784478

44794479

4480-
cdef int month_add_months(npy_datetimestruct dts, int months) nogil:
4480+
cdef int month_add_months(npy_datetimestruct dts, int months) noexcept nogil:
44814481
"""
44824482
New month number after shifting npy_datetimestruct
44834483
number of months.
@@ -4699,7 +4699,7 @@ def shift_month(stamp: datetime, months: int, day_opt: object = None) -> datetim
46994699
return stamp.replace(year=year, month=month, day=day)
47004700

47014701

4702-
cdef int get_day_of_month(npy_datetimestruct* dts, str day_opt) nogil:
4702+
cdef int get_day_of_month(npy_datetimestruct* dts, str day_opt) noexcept nogil:
47034703
"""
47044704
Find the day in `other`'s month that satisfies a DateOffset's is_on_offset
47054705
policy, as described by the `day_opt` argument.
@@ -4744,7 +4744,7 @@ cdef int get_day_of_month(npy_datetimestruct* dts, str day_opt) nogil:
47444744
return get_lastbday(dts.year, dts.month)
47454745

47464746

4747-
cpdef int roll_convention(int other, int n, int compare) nogil:
4747+
cpdef int roll_convention(int other, int n, int compare) noexcept nogil:
47484748
"""
47494749
Possibly increment or decrement the number of periods to shift
47504750
based on rollforward/rollbackward conventions.

pandas/_libs/tslibs/period.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ from .np_datetime cimport npy_datetimestruct
44

55

66
cdef bint is_period_object(object obj)
7-
cdef int64_t get_period_ordinal(npy_datetimestruct *dts, int freq) nogil
7+
cdef int64_t get_period_ordinal(npy_datetimestruct *dts, int freq) noexcept nogil

0 commit comments

Comments
 (0)