diff --git a/src/kaa/custom_transitions.pxi b/src/kaa/custom_transitions.pxi index 2f27aad6..e2d1b3b2 100644 --- a/src/kaa/custom_transitions.pxi +++ b/src/kaa/custom_transitions.pxi @@ -1,4 +1,5 @@ import cython +from libc.stdint cimport uintptr_t from libcpp.memory cimport unique_ptr from cpython.ref cimport PyObject @@ -57,19 +58,43 @@ cdef cppclass CPyNodeCustomTransition(CNodeTransitionCustomizable): this.internal_duration = duration this.warping = warping - unique_ptr[CTransitionStateBase] prepare_state(CNodePtr c_node_ptr) const: - cdef object state_object = (this.prepare_func.py_callback)(get_node_wrapper(c_node_ptr)) + unique_ptr[CTransitionStateBase] prepare_state(CNodePtr c_node_ptr) nogil const: + cdef CPyNodeCustomTransitionState* state_ptr = \ + this._py_prepare_state(c_node_ptr).unwrap_result() + return \ unique_ptr[CPyNodeCustomTransitionState]( + state_ptr + ) + + CPythonicCallbackResult[uintptr_t] _py_prepare_state(CNodePtr c_node_ptr) with gil const: + cdef object state_object + try: + state_object = (this.prepare_func.py_callback)(get_node_wrapper(c_node_ptr)) + except BaseException as exc: + return CPythonicCallbackResult[uintptr_t](exc) + else: + return CPythonicCallbackResult[uintptr_t]( + new CPyNodeCustomTransitionState(state_object) ) - void evaluate(CTransitionStateBase* state, CNodePtr c_node_ptr, const double t) const: + void evaluate(CTransitionStateBase* state, CNodePtr c_node_ptr, const double t) nogil const: + this._py_evaluate(state, c_node_ptr, t).unwrap_result() + + CPythonicCallbackResult[void] _py_evaluate( + CTransitionStateBase* state, CNodePtr c_node_ptr, const double t + ) with gil const: cdef CPyNodeCustomTransitionState* custom_state = \ state - (this.evaluate_func.py_callback)(custom_state.state_object, - get_node_wrapper(c_node_ptr), t) + try: + (this.evaluate_func.py_callback)(custom_state.state_object, + get_node_wrapper(c_node_ptr), t) + except BaseException as exc: + return CPythonicCallbackResult[void](exc) + else: + return CPythonicCallbackResult[void]() @cython.final diff --git a/src/kaa/kaacore/custom_transitions.pxd b/src/kaa/kaacore/custom_transitions.pxd index 75662b82..d2c221ea 100644 --- a/src/kaa/kaacore/custom_transitions.pxd +++ b/src/kaa/kaacore/custom_transitions.pxd @@ -18,8 +18,8 @@ cdef extern from "kaacore/transitions.h": CNodeTransitionCustomizable(const double duration, const CTransitionWarping& warping) \ except +raise_py_error - unique_ptr[CTransitionStateBase] prepare_state(CNodePtr node) const - void evaluate(CTransitionStateBase* state, CNodePtr node, const double t) const + unique_ptr[CTransitionStateBase] prepare_state(CNodePtr node) nogil const + void evaluate(CTransitionStateBase* state, CNodePtr node, const double t) nogil const ctypedef function[void(CNodePtr)] CNodeTransitionCallbackFunc "kaacore::NodeTransitionCallbackFunc";