Skip to content

Commit

Permalink
Fix exception check for Cython migration to 3.0 (#558)
Browse files Browse the repository at this point in the history
  • Loading branch information
tqtg committed Dec 6, 2023
1 parent e9d5b7f commit fb83104
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions cornac/models/bpr/recom_bpr.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ from libcpp cimport bool
from libcpp.vector cimport vector


cdef bool has_non_zero(integral[:], integral[:], integral, integral) nogil
cdef bool has_non_zero(integral[:], integral[:], integral, integral) noexcept nogil


cdef extern from "<boost/random.hpp>" namespace "boost::random":
Expand All @@ -29,7 +29,7 @@ cdef extern from "<boost/random.hpp>" namespace "boost::random":

cdef cppclass uniform_int_distribution[T]:
uniform_int_distribution(T, T)
T operator()(mt19937) nogil
T operator()(mt19937) noexcept nogil


cdef class RNGVector(object):
Expand All @@ -38,4 +38,4 @@ cdef class RNGVector(object):
cdef vector[mt19937] rng
cdef vector[uniform_int_distribution[long]] dist

cdef inline long generate(self, int thread_id) nogil
cdef inline long generate(self, int thread_id) noexcept nogil
4 changes: 2 additions & 2 deletions cornac/models/bpr/recom_bpr.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ cdef extern from "recom_bpr.h" namespace "recom_bpr" nogil:

@cython.boundscheck(False)
cdef bool has_non_zero(integral[:] indptr, integral[:] indices,
integral rowid, integral colid) nogil:
integral rowid, integral colid) noexcept nogil:
"""Given a CSR matrix, returns whether the [rowid, colid] contains a non zero.
Assumes the CSR matrix has sorted indices"""
return binary_search(&indices[indptr[rowid]], &indices[indptr[rowid + 1]], colid)
Expand All @@ -56,7 +56,7 @@ cdef class RNGVector(object):
self.rng.push_back(mt19937(rng.randint(2 ** 31)))
self.dist.push_back(uniform_int_distribution[long](0, rows))

cdef inline long generate(self, int thread_id) nogil:
cdef inline long generate(self, int thread_id) noexcept nogil:
return self.dist[thread_id](self.rng[thread_id])


Expand Down
2 changes: 1 addition & 1 deletion cornac/models/comparer/recom_comparer_obj.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ MODEL_TYPES = {"Dominant": 0, "Finer": 1, "Around": 2}


cdef floating _dot(int n, floating *x, int incx,
floating *y, int incy) nogil:
floating *y, int incy) noexcept nogil:
if floating is float:
return sdot(&n, x, &incx, y, &incy)
else:
Expand Down
2 changes: 1 addition & 1 deletion cornac/models/efm/recom_efm.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ from ...utils.init_utils import uniform


cdef floating _dot(int n, floating *x, int incx,
floating *y, int incy) nogil:
floating *y, int incy) noexcept nogil:
if floating is float:
return sdot(&n, x, &incx, y, &incy)
else:
Expand Down
4 changes: 2 additions & 2 deletions cornac/models/lrppm/recom_lrppm.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ from ..mter.recom_mter cimport get_key
cdef extern from "../bpr/recom_bpr.h" namespace "recom_bpr" nogil:
cdef int get_thread_num()

cdef int get_key3(int i_id, int j_id, int k_id) nogil:
cdef int get_key3(int i_id, int j_id, int k_id) noexcept nogil:
return get_key(get_key(i_id, j_id), k_id)

@cython.boundscheck(False)
@cython.wraparound(False)
cdef floating get_score(floating[:, :] U, floating[:, :] I, floating[:, :] UA, floating[:, :] IA,
int n_factors, int u_idx, int i_idx, int a_idx) nogil:
int n_factors, int u_idx, int i_idx, int a_idx) noexcept nogil:
cdef floating score = 0.
cdef int k
for k in range(n_factors):
Expand Down
4 changes: 2 additions & 2 deletions cornac/models/mter/recom_mter.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ from cython cimport floating, integral
from libcpp.vector cimport vector


cdef int get_key(int i_id, int j_id) nogil
cdef int get_key(int i_id, int j_id) noexcept nogil

cdef floating get_score(floating[:, :, :] G, int dim1, int dim2, int dim3,
floating[:, :] U, floating[:, :] I, floating[:, :] A,
int u_idx, int i_idx, int a_idx) nogil
int u_idx, int i_idx, int a_idx) noexcept nogil
4 changes: 2 additions & 2 deletions cornac/models/mter/recom_mter.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ cdef extern from "../bpr/recom_bpr.h" namespace "recom_bpr" nogil:
cdef int get_thread_num()


cdef int get_key(int i_id, int j_id) nogil:
cdef int get_key(int i_id, int j_id) noexcept nogil:
return (i_id + j_id) * (i_id + j_id + 1) // 2 + j_id


@cython.boundscheck(False)
@cython.wraparound(False)
cdef floating get_score(floating[:, :, :] G, int dim1, int dim2, int dim3,
floating[:, :] U, floating[:, :] I, floating[:, :] A,
int u_idx, int i_idx, int a_idx) nogil:
int u_idx, int i_idx, int a_idx) noexcept nogil:
cdef floating score = 0.
for i in range(dim1):
for j in range(dim2):
Expand Down

0 comments on commit fb83104

Please sign in to comment.