Skip to content

MNT: Mark all nogil functions as noexcept #53379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/_libs/algos.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ from pandas._libs.dtypes cimport (
)


cdef numeric_t kth_smallest_c(numeric_t* arr, Py_ssize_t k, Py_ssize_t n) nogil
cdef numeric_t kth_smallest_c(numeric_t* arr, Py_ssize_t k, Py_ssize_t n) noexcept nogil

cdef enum TiebreakEnumType:
TIEBREAK_AVERAGE
Expand Down
7 changes: 4 additions & 3 deletions pandas/_libs/algos.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def groupsort_indexer(const intp_t[:] index, Py_ssize_t ngroups):
return indexer.base, counts.base


cdef Py_ssize_t swap(numeric_t *a, numeric_t *b) nogil:
cdef Py_ssize_t swap(numeric_t *a, numeric_t *b) noexcept nogil:
cdef:
numeric_t t

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


cdef numeric_t kth_smallest_c(numeric_t* arr, Py_ssize_t k, Py_ssize_t n) nogil:
cdef numeric_t kth_smallest_c(numeric_t* arr,
Py_ssize_t k, Py_ssize_t n) noexcept nogil:
"""
See kth_smallest.__doc__. The additional parameter n specifies the maximum
number of elements considered in arr, needed for compatibility with usage
Expand Down Expand Up @@ -1062,7 +1063,7 @@ cdef void rank_sorted_1d(
# https://github.com/cython/cython/issues/1630, only trailing arguments can
# currently be omitted for cdef functions, which is why we keep this at the end
const intp_t[:] labels=None,
) nogil:
) noexcept nogil:
"""
See rank_1d.__doc__. Handles only actual ranking, so sorting and masking should
be handled in the caller. Note that `out` and `grp_sizes` are modified inplace.
Expand Down
9 changes: 5 additions & 4 deletions pandas/_libs/groupby.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ cdef enum InterpolationEnumType:
INTERPOLATION_MIDPOINT


cdef float64_t median_linear_mask(float64_t* a, int n, uint8_t* mask) nogil:
cdef float64_t median_linear_mask(float64_t* a, int n, uint8_t* mask) noexcept nogil:
cdef:
int i, j, na_count = 0
float64_t* tmp
Expand Down Expand Up @@ -99,7 +99,7 @@ cdef float64_t median_linear_mask(float64_t* a, int n, uint8_t* mask) nogil:
return result


cdef float64_t median_linear(float64_t* a, int n) nogil:
cdef float64_t median_linear(float64_t* a, int n) noexcept nogil:
cdef:
int i, j, na_count = 0
float64_t* tmp
Expand Down Expand Up @@ -136,7 +136,7 @@ cdef float64_t median_linear(float64_t* a, int n) nogil:
return result


cdef float64_t calc_median_linear(float64_t* a, int n, int na_count) nogil:
cdef float64_t calc_median_linear(float64_t* a, int n, int na_count) noexcept nogil:
cdef:
float64_t result

Expand Down Expand Up @@ -1293,7 +1293,8 @@ ctypedef fused numeric_object_complex_t:
complex128_t


cdef bint _treat_as_na(numeric_object_complex_t val, bint is_datetimelike) nogil:
cdef bint _treat_as_na(numeric_object_complex_t val,
bint is_datetimelike) noexcept nogil:
if numeric_object_complex_t is object:
# Should never be used, but we need to avoid the `val != val` below
# or else cython will raise about gil acquisition.
Expand Down
6 changes: 3 additions & 3 deletions pandas/_libs/hashing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ def hash_object_array(
return result.base # .base to retrieve underlying np.ndarray


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


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

@cython.cdivision(True)
cdef uint64_t low_level_siphash(uint8_t* data, size_t datalen,
uint8_t* key) nogil:
uint8_t* key) noexcept nogil:
cdef uint64_t v0 = 0x736f6d6570736575ULL
cdef uint64_t v1 = 0x646f72616e646f6dULL
cdef uint64_t v2 = 0x6c7967656e657261ULL
Expand Down
8 changes: 4 additions & 4 deletions pandas/_libs/hashtable_class_helper.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ complex_types = ['complex64',
}}

{{for name in complex_types}}
cdef kh{{name}}_t to_kh{{name}}_t({{name}}_t val) nogil:
cdef kh{{name}}_t to_kh{{name}}_t({{name}}_t val) noexcept nogil:
cdef kh{{name}}_t res
res.real = val.real
res.imag = val.imag
Expand Down Expand Up @@ -42,7 +42,7 @@ c_types = ['khcomplex128_t',

{{for c_type in c_types}}

cdef bint is_nan_{{c_type}}({{c_type}} val) nogil:
cdef bint is_nan_{{c_type}}({{c_type}} val) noexcept nogil:
{{if c_type in {'khcomplex128_t', 'khcomplex64_t'} }}
return val.real != val.real or val.imag != val.imag
{{elif c_type in {'float64_t', 'float32_t'} }}
Expand All @@ -55,7 +55,7 @@ cdef bint is_nan_{{c_type}}({{c_type}} val) nogil:
{{if c_type in {'khcomplex128_t', 'khcomplex64_t', 'float64_t', 'float32_t'} }}
# are_equivalent_{{c_type}} is cimported via khash.pxd
{{else}}
cdef bint are_equivalent_{{c_type}}({{c_type}} val1, {{c_type}} val2) nogil:
cdef bint are_equivalent_{{c_type}}({{c_type}} val1, {{c_type}} val2) noexcept nogil:
return val1 == val2
{{endif}}

Expand Down Expand Up @@ -163,7 +163,7 @@ ctypedef fused vector_data:
Complex64VectorData
StringVectorData

cdef bint needs_resize(vector_data *data) nogil:
cdef bint needs_resize(vector_data *data) noexcept nogil:
return data.n == data.m

# ----------------------------------------------------------------------
Expand Down
16 changes: 8 additions & 8 deletions pandas/_libs/tslibs/ccalendar.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ from numpy cimport (

ctypedef (int32_t, int32_t, int32_t) iso_calendar_t

cdef int dayofweek(int y, int m, int d) nogil
cdef bint is_leapyear(int64_t year) nogil
cpdef int32_t get_days_in_month(int year, Py_ssize_t month) nogil
cpdef int32_t get_week_of_year(int year, int month, int day) nogil
cpdef iso_calendar_t get_iso_calendar(int year, int month, int day) nogil
cpdef int32_t get_day_of_year(int year, int month, int day) nogil
cpdef int get_lastbday(int year, int month) nogil
cpdef int get_firstbday(int year, int month) nogil
cdef int dayofweek(int y, int m, int d) noexcept nogil
cdef bint is_leapyear(int64_t year) noexcept nogil
cpdef int32_t get_days_in_month(int year, Py_ssize_t month) noexcept nogil
cpdef int32_t get_week_of_year(int year, int month, int day) noexcept nogil
cpdef iso_calendar_t get_iso_calendar(int year, int month, int day) noexcept nogil
cpdef int32_t get_day_of_year(int year, int month, int day) noexcept nogil
cpdef int get_lastbday(int year, int month) noexcept nogil
cpdef int get_firstbday(int year, int month) noexcept nogil

cdef dict c_MONTH_NUMBERS

Expand Down
16 changes: 8 additions & 8 deletions pandas/_libs/tslibs/ccalendar.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ weekday_to_int = {int_to_weekday[key]: key for key in int_to_weekday}

@cython.wraparound(False)
@cython.boundscheck(False)
cpdef int32_t get_days_in_month(int year, Py_ssize_t month) nogil:
cpdef int32_t get_days_in_month(int year, Py_ssize_t month) noexcept nogil:
"""
Return the number of days in the given month of the given year.

Expand All @@ -77,7 +77,7 @@ cpdef int32_t get_days_in_month(int year, Py_ssize_t month) nogil:
@cython.wraparound(False)
@cython.boundscheck(False)
@cython.cdivision
cdef int dayofweek(int y, int m, int d) nogil:
cdef int dayofweek(int y, int m, int d) noexcept nogil:
"""
Find the day of week for the date described by the Y/M/D triple y, m, d
using Sakamoto's method, from wikipedia.
Expand Down Expand Up @@ -114,7 +114,7 @@ cdef int dayofweek(int y, int m, int d) nogil:
return (day + 6) % 7


cdef bint is_leapyear(int64_t year) nogil:
cdef bint is_leapyear(int64_t year) noexcept nogil:
"""
Returns 1 if the given year is a leap year, 0 otherwise.

Expand All @@ -132,7 +132,7 @@ cdef bint is_leapyear(int64_t year) nogil:

@cython.wraparound(False)
@cython.boundscheck(False)
cpdef int32_t get_week_of_year(int year, int month, int day) nogil:
cpdef int32_t get_week_of_year(int year, int month, int day) noexcept nogil:
"""
Return the ordinal week-of-year for the given day.

Expand All @@ -155,7 +155,7 @@ cpdef int32_t get_week_of_year(int year, int month, int day) nogil:

@cython.wraparound(False)
@cython.boundscheck(False)
cpdef iso_calendar_t get_iso_calendar(int year, int month, int day) nogil:
cpdef iso_calendar_t get_iso_calendar(int year, int month, int day) noexcept nogil:
"""
Return the year, week, and day of year corresponding to ISO 8601

Expand Down Expand Up @@ -209,7 +209,7 @@ cpdef iso_calendar_t get_iso_calendar(int year, int month, int day) nogil:

@cython.wraparound(False)
@cython.boundscheck(False)
cpdef int32_t get_day_of_year(int year, int month, int day) nogil:
cpdef int32_t get_day_of_year(int year, int month, int day) noexcept nogil:
"""
Return the ordinal day-of-year for the given day.

Expand Down Expand Up @@ -243,7 +243,7 @@ cpdef int32_t get_day_of_year(int year, int month, int day) nogil:
# ---------------------------------------------------------------------
# Business Helpers

cpdef int get_lastbday(int year, int month) nogil:
cpdef int get_lastbday(int year, int month) noexcept nogil:
"""
Find the last day of the month that is a business day.

Expand All @@ -264,7 +264,7 @@ cpdef int get_lastbday(int year, int month) nogil:
return days_in_month - max(((wkday + days_in_month - 1) % 7) - 4, 0)


cpdef int get_firstbday(int year, int month) nogil:
cpdef int get_firstbday(int year, int month) noexcept nogil:
"""
Find the first day of the month that is a business day.

Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/dtypes.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ from pandas._libs.tslibs.np_datetime cimport NPY_DATETIMEUNIT

cpdef str npy_unit_to_abbrev(NPY_DATETIMEUNIT unit)
cpdef NPY_DATETIMEUNIT abbrev_to_npy_unit(str abbrev)
cdef NPY_DATETIMEUNIT freq_group_code_to_npy_unit(int freq) nogil
cdef NPY_DATETIMEUNIT freq_group_code_to_npy_unit(int freq) noexcept nogil
cpdef int64_t periods_per_day(NPY_DATETIMEUNIT reso=*) except? -1
cpdef int64_t periods_per_second(NPY_DATETIMEUNIT reso) except? -1
cpdef NPY_DATETIMEUNIT get_supported_reso(NPY_DATETIMEUNIT reso)
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/dtypes.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ cpdef NPY_DATETIMEUNIT abbrev_to_npy_unit(str abbrev):
raise ValueError(f"Unrecognized unit {abbrev}")


cdef NPY_DATETIMEUNIT freq_group_code_to_npy_unit(int freq) nogil:
cdef NPY_DATETIMEUNIT freq_group_code_to_npy_unit(int freq) noexcept nogil:
"""
Convert the freq to the corresponding NPY_DATETIMEUNIT to pass
to npy_datetimestruct_to_datetime.
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/fields.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def get_date_name_field(
return out


cdef bint _is_on_month(int month, int compare_month, int modby) nogil:
cdef bint _is_on_month(int month, int compare_month, int modby) noexcept nogil:
"""
Analogous to DateOffset.is_on_offset checking for the month part of a date.
"""
Expand Down
6 changes: 3 additions & 3 deletions pandas/_libs/tslibs/np_datetime.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ cdef int64_t pydate_to_dt64(
)
cdef void pydate_to_dtstruct(date val, npy_datetimestruct *dts) noexcept

cdef npy_datetime get_datetime64_value(object obj) nogil
cdef npy_timedelta get_timedelta64_value(object obj) nogil
cdef NPY_DATETIMEUNIT get_datetime64_unit(object obj) nogil
cdef npy_datetime get_datetime64_value(object obj) noexcept nogil
cdef npy_timedelta get_timedelta64_value(object obj) noexcept nogil
cdef NPY_DATETIMEUNIT get_datetime64_unit(object obj) noexcept nogil

cdef int string_to_dts(
str val,
Expand Down
6 changes: 3 additions & 3 deletions pandas/_libs/tslibs/np_datetime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ cdef extern from "src/datetime/pd_datetime.h":
# ----------------------------------------------------------------------
# numpy object inspection

cdef npy_datetime get_datetime64_value(object obj) nogil:
cdef npy_datetime get_datetime64_value(object obj) noexcept nogil:
"""
returns the int64 value underlying scalar numpy datetime64 object

Expand All @@ -69,14 +69,14 @@ cdef npy_datetime get_datetime64_value(object obj) nogil:
return (<PyDatetimeScalarObject*>obj).obval


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


cdef NPY_DATETIMEUNIT get_datetime64_unit(object obj) nogil:
cdef NPY_DATETIMEUNIT get_datetime64_unit(object obj) noexcept nogil:
"""
returns the unit part of the dtype for a numpy datetime64 object.
"""
Expand Down
8 changes: 4 additions & 4 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4390,14 +4390,14 @@ cdef datetime _shift_day(datetime other, int days):
return localize_pydatetime(shifted, tz)


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


cdef int month_add_months(npy_datetimestruct dts, int months) nogil:
cdef int month_add_months(npy_datetimestruct dts, int months) noexcept nogil:
"""
New month number after shifting npy_datetimestruct
number of months.
Expand Down Expand Up @@ -4619,7 +4619,7 @@ def shift_month(stamp: datetime, months: int, day_opt: object = None) -> datetim
return stamp.replace(year=year, month=month, day=day)


cdef int get_day_of_month(npy_datetimestruct* dts, str day_opt) nogil:
cdef int get_day_of_month(npy_datetimestruct* dts, str day_opt) noexcept nogil:
"""
Find the day in `other`'s month that satisfies a DateOffset's is_on_offset
policy, as described by the `day_opt` argument.
Expand Down Expand Up @@ -4664,7 +4664,7 @@ cdef int get_day_of_month(npy_datetimestruct* dts, str day_opt) nogil:
return get_lastbday(dts.year, dts.month)


cpdef int roll_convention(int other, int n, int compare) nogil:
cpdef int roll_convention(int other, int n, int compare) noexcept nogil:
"""
Possibly increment or decrement the number of periods to shift
based on rollforward/rollbackward conventions.
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/period.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ from .np_datetime cimport npy_datetimestruct


cdef bint is_period_object(object obj)
cdef int64_t get_period_ordinal(npy_datetimestruct *dts, int freq) nogil
cdef int64_t get_period_ordinal(npy_datetimestruct *dts, int freq) noexcept nogil
Loading