Skip to content

MNT: Explicitly set cdef functions as noexcept #53144

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 9, 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/groupby.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,7 @@ cdef inline void _check_below_mincount(
int64_t[:, ::1] nobs,
int64_t min_count,
mincount_t[:, ::1] resx,
) nogil:
) noexcept nogil:
"""
Check if the number of observations for a group is below min_count,
and if so set the result for that group to the appropriate NA-like value.
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/hashing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ cdef uint64_t u8to64_le(uint8_t* p) nogil:


cdef void _sipround(uint64_t* v0, uint64_t* v1,
uint64_t* v2, uint64_t* v3) nogil:
uint64_t* v2, uint64_t* v3) noexcept nogil:
v0[0] += v1[0]
v1[0] = _rotl(v1[0], 13)
v1[0] ^= v0[0]
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/hashtable.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,5 @@ cdef class Int64Vector(Vector):

cdef resize(self)
cpdef ndarray to_array(self)
cdef void append(self, int64_t x)
cdef void append(self, int64_t x) noexcept
cdef extend(self, int64_t[:] x)
6 changes: 3 additions & 3 deletions pandas/_libs/hashtable_class_helper.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ ctypedef struct {{name}}VectorData:
@cython.wraparound(False)
@cython.boundscheck(False)
cdef void append_data_{{dtype}}({{name}}VectorData *data,
{{c_type}} x) nogil:
{{c_type}} x) noexcept nogil:

data.data[data.n] = x
data.n += 1
Expand Down Expand Up @@ -241,7 +241,7 @@ cdef class {{name}}Vector(Vector):
self.external_view_exists = True
return self.ao

cdef void append(self, {{c_type}} x):
cdef void append(self, {{c_type}} x) noexcept:

if needs_resize(self.data):
if self.external_view_exists:
Expand Down Expand Up @@ -311,7 +311,7 @@ cdef class StringVector(Vector):
self.data.m = self.data.n
return ao

cdef void append(self, char *x):
cdef void append(self, char *x) noexcept:

if needs_resize(self.data):
self.resize()
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/join.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def full_outer_join(const intp_t[:] left, const intp_t[:] right,

@cython.wraparound(False)
@cython.boundscheck(False)
cdef void _get_result_indexer(intp_t[::1] sorter, intp_t[::1] indexer) nogil:
cdef void _get_result_indexer(intp_t[::1] sorter, intp_t[::1] indexer) noexcept nogil:
"""NOTE: overwrites indexer with the result to avoid allocating another array"""
cdef:
Py_ssize_t i, n, idx
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ cdef check_overflows(_TSObject obj, NPY_DATETIMEUNIT reso=NPY_FR_ns):
# ----------------------------------------------------------------------
# Localization

cdef void _localize_tso(_TSObject obj, tzinfo tz, NPY_DATETIMEUNIT reso):
cdef void _localize_tso(_TSObject obj, tzinfo tz, NPY_DATETIMEUNIT reso) noexcept:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really noexcept? I think the Localizer constructor can raise

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Cython 0.29.X the default exception handling is noexcept hence the exception was not propagated even before this PR. If an exception should be propagated, one should consider changing return type from void to int to avoid checking exception after every function call.

"""
Given the UTC nanosecond timestamp in obj.value, find the wall-clock
representation of that timestamp in the given timezone.
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 @@ -80,7 +80,7 @@ cdef extern from "src/datetime/pd_datetime.h":
INFER_FORMAT

# You must call this before using the PandasDateTime CAPI functions
cdef inline void import_pandas_datetime():
cdef inline void import_pandas_datetime() noexcept:
PandasDateTime_IMPORT

cdef bint cmp_scalar(int64_t lhs, int64_t rhs, int op) except -1
Expand All @@ -90,11 +90,11 @@ cdef check_dts_bounds(npy_datetimestruct *dts, NPY_DATETIMEUNIT unit=?)
cdef int64_t pydatetime_to_dt64(
datetime val, npy_datetimestruct *dts, NPY_DATETIMEUNIT reso=?
)
cdef void pydatetime_to_dtstruct(datetime dt, npy_datetimestruct *dts)
cdef void pydatetime_to_dtstruct(datetime dt, npy_datetimestruct *dts) noexcept
cdef int64_t pydate_to_dt64(
date val, npy_datetimestruct *dts, NPY_DATETIMEUNIT reso=?
)
cdef void pydate_to_dtstruct(date val, npy_datetimestruct *dts)
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
Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/tslibs/np_datetime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def py_td64_to_tdstruct(int64_t td64, NPY_DATETIMEUNIT unit):
return tds # <- returned as a dict to python


cdef void pydatetime_to_dtstruct(datetime dt, npy_datetimestruct *dts):
cdef void pydatetime_to_dtstruct(datetime dt, npy_datetimestruct *dts) noexcept:
if PyDateTime_CheckExact(dt):
dts.year = PyDateTime_GET_YEAR(dt)
else:
Expand All @@ -256,7 +256,7 @@ cdef int64_t pydatetime_to_dt64(datetime val,
return npy_datetimestruct_to_datetime(reso, dts)


cdef void pydate_to_dtstruct(date val, npy_datetimestruct *dts):
cdef void pydate_to_dtstruct(date val, npy_datetimestruct *dts) noexcept:
dts.year = PyDateTime_GET_YEAR(val)
dts.month = PyDateTime_GET_MONTH(val)
dts.day = PyDateTime_GET_DAY(val)
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ cdef str _fill_token(token: str, padding: int):
return token_filled


cdef void _maybe_warn_about_dayfirst(format: str, bint dayfirst):
cdef void _maybe_warn_about_dayfirst(format: str, bint dayfirst) noexcept:
"""Warn if guessed datetime format doesn't respect dayfirst argument."""
cdef:
int day_index = format.find("%d")
Expand Down
11 changes: 6 additions & 5 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ cdef int get_freq_group_index(int freq) nogil:
return freq // 1000


cdef void adjust_dts_for_month(npy_datetimestruct* dts, int from_end) nogil:
cdef void adjust_dts_for_month(npy_datetimestruct* dts, int from_end) noexcept nogil:
if from_end != 12:
dts.month += from_end
if dts.month > 12:
Expand All @@ -700,7 +700,7 @@ cdef void adjust_dts_for_month(npy_datetimestruct* dts, int from_end) nogil:
dts.year -= 1


cdef void adjust_dts_for_qtr(npy_datetimestruct* dts, int to_end) nogil:
cdef void adjust_dts_for_qtr(npy_datetimestruct* dts, int to_end) noexcept nogil:
if to_end != 12:
dts.month -= to_end
if dts.month <= 0:
Expand Down Expand Up @@ -803,7 +803,8 @@ cdef int64_t get_period_ordinal(npy_datetimestruct *dts, int freq) nogil:
return npy_datetimestruct_to_datetime(unit, dts)


cdef void get_date_info(int64_t ordinal, int freq, npy_datetimestruct *dts) nogil:
cdef void get_date_info(int64_t ordinal,
int freq, npy_datetimestruct *dts) noexcept nogil:
cdef:
int64_t unix_date, nanos
npy_datetimestruct dts2
Expand Down Expand Up @@ -985,7 +986,7 @@ def periodarr_to_dt64arr(const int64_t[:] periodarr, int freq):


cdef void get_asfreq_info(int from_freq, int to_freq,
bint is_end, asfreq_info *af_info) nogil:
bint is_end, asfreq_info *af_info) noexcept nogil:
"""
Construct the `asfreq_info` object used to convert an ordinal from
`from_freq` to `to_freq`.
Expand Down Expand Up @@ -1084,7 +1085,7 @@ cdef void _period_asfreq(
int freq2,
bint end,
Py_ssize_t increment=1,
):
) noexcept:
"""See period_asfreq.__doc__"""
cdef:
Py_ssize_t i
Expand Down
22 changes: 11 additions & 11 deletions pandas/_libs/tslibs/util.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ cdef extern from "numpy/npy_common.h":
int64_t NPY_MIN_INT64


cdef inline int64_t get_nat():
cdef inline int64_t get_nat() noexcept:
return NPY_MIN_INT64


# --------------------------------------------------------------------
# Type Checking

cdef inline bint is_integer_object(object obj) nogil:
cdef inline bint is_integer_object(object obj) noexcept nogil:
"""
Cython equivalent of

Expand All @@ -73,7 +73,7 @@ cdef inline bint is_integer_object(object obj) nogil:
and not is_timedelta64_object(obj))


cdef inline bint is_float_object(object obj) nogil:
cdef inline bint is_float_object(object obj) noexcept nogil:
"""
Cython equivalent of `isinstance(val, (float, np.float_))`

Expand All @@ -89,7 +89,7 @@ cdef inline bint is_float_object(object obj) nogil:
(PyObject_TypeCheck(obj, &PyFloatingArrType_Type)))


cdef inline bint is_complex_object(object obj) nogil:
cdef inline bint is_complex_object(object obj) noexcept nogil:
"""
Cython equivalent of `isinstance(val, (complex, np.complex_))`

Expand All @@ -105,7 +105,7 @@ cdef inline bint is_complex_object(object obj) nogil:
PyObject_TypeCheck(obj, &PyComplexFloatingArrType_Type))


cdef inline bint is_bool_object(object obj) nogil:
cdef inline bint is_bool_object(object obj) noexcept nogil:
"""
Cython equivalent of `isinstance(val, (bool, np.bool_))`

Expand All @@ -121,11 +121,11 @@ cdef inline bint is_bool_object(object obj) nogil:
PyObject_TypeCheck(obj, &PyBoolArrType_Type))


cdef inline bint is_real_number_object(object obj) nogil:
cdef inline bint is_real_number_object(object obj) noexcept nogil:
return is_bool_object(obj) or is_integer_object(obj) or is_float_object(obj)


cdef inline bint is_timedelta64_object(object obj) nogil:
cdef inline bint is_timedelta64_object(object obj) noexcept nogil:
"""
Cython equivalent of `isinstance(val, np.timedelta64)`

Expand All @@ -140,7 +140,7 @@ cdef inline bint is_timedelta64_object(object obj) nogil:
return PyObject_TypeCheck(obj, &PyTimedeltaArrType_Type)


cdef inline bint is_datetime64_object(object obj) nogil:
cdef inline bint is_datetime64_object(object obj) noexcept nogil:
"""
Cython equivalent of `isinstance(val, np.datetime64)`

Expand All @@ -155,7 +155,7 @@ cdef inline bint is_datetime64_object(object obj) nogil:
return PyObject_TypeCheck(obj, &PyDatetimeArrType_Type)


cdef inline bint is_array(object val):
cdef inline bint is_array(object val) noexcept:
"""
Cython equivalent of `isinstance(val, np.ndarray)`

Expand Down Expand Up @@ -217,11 +217,11 @@ cdef inline const char* get_c_string(str py_string) except NULL:
return get_c_string_buf_and_size(py_string, NULL)


cdef inline bytes string_encode_locale(str py_string):
cdef inline bytes string_encode_locale(str py_string) noexcept:
"""As opposed to PyUnicode_Encode, use current system locale to encode."""
return PyUnicode_EncodeLocale(py_string, NULL)


cdef inline object char_to_string_locale(const char* data):
cdef inline object char_to_string_locale(const char* data) noexcept:
"""As opposed to PyUnicode_FromString, use current system locale to decode."""
return PyUnicode_DecodeLocale(data, NULL)
2 changes: 1 addition & 1 deletion pandas/_libs/window/aggregations.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ cdef float64_t calc_sum(int64_t minp, int64_t nobs, float64_t sum_x,

cdef void add_sum(float64_t val, int64_t *nobs, float64_t *sum_x,
float64_t *compensation, int64_t *num_consecutive_same_value,
float64_t *prev_value) nogil:
float64_t *prev_value) noexcept nogil:
""" add a value from the sum calc using Kahan summation """

cdef:
Expand Down