From 3d1ea2609c4b12a79537e3be1d3a62a6e8782175 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Sat, 19 Oct 2024 23:29:19 -0400 Subject: [PATCH] [Python] Restore compatibility with Cython 0.29.31 --- interfaces/cython/cantera/func1.pxd | 2 +- interfaces/cython/cantera/func1.pyx | 40 +++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/interfaces/cython/cantera/func1.pxd b/interfaces/cython/cantera/func1.pxd index 28ae037732..9757c95c3d 100644 --- a/interfaces/cython/cantera/func1.pxd +++ b/interfaces/cython/cantera/func1.pxd @@ -59,6 +59,6 @@ cdef class Func1: cdef object exception cpdef void _set_callback(self, object) except * @staticmethod - cdef shared_ptr[CxxFunc1] _make_cxx_func1(string, tuple) + cdef shared_ptr[CxxFunc1] _make_cxx_func1(string, tuple) except * @staticmethod cdef Func1 _make_func1(shared_ptr[CxxFunc1]) diff --git a/interfaces/cython/cantera/func1.pyx b/interfaces/cython/cantera/func1.pyx index 55c2832c3e..223f023430 100644 --- a/interfaces/cython/cantera/func1.pyx +++ b/interfaces/cython/cantera/func1.pyx @@ -152,7 +152,7 @@ cdef class Func1: return pystr(self.func.type()) @staticmethod - cdef shared_ptr[CxxFunc1] _make_cxx_func1(string cxx_string, tuple args): + cdef shared_ptr[CxxFunc1] _make_cxx_func1(string cxx_string, tuple args) except *: """Create C++ functor from type specifier and arguments.""" cdef shared_ptr[CxxFunc1] func cdef Func1 f0 @@ -244,7 +244,15 @@ cdef class Func1: if not isinstance(other, Func1): other = Func1(other) cdef Func1 f1 = other - return Func1._make_func1(CxxNewSumFunction(self._func, f1._func)) + + # @todo: When dropping support for Cython < 3.0.0, 'f0' can be removed and + # 'self._func' can be used directly in the C++ call + cdef Func1 f0 + if isinstance(self, Func1): + f0 = self + else: + f0 = Func1(self) + return Func1._make_func1(CxxNewSumFunction(f0._func, f1._func)) def __radd__(self, other): if not isinstance(other, Func1): @@ -256,7 +264,15 @@ cdef class Func1: if not isinstance(other, Func1): other = Func1(other) cdef Func1 f1 = other - return Func1._make_func1(CxxNewDiffFunction(self._func, f1._func)) + + # @todo: When dropping support for Cython < 3.0.0, 'f0' can be removed and + # 'self._func' can be used directly in the C++ call + cdef Func1 f0 + if isinstance(self, Func1): + f0 = self + else: + f0 = Func1(self) + return Func1._make_func1(CxxNewDiffFunction(f0._func, f1._func)) def __rsub__(self, other): if not isinstance(other, Func1): @@ -268,7 +284,14 @@ cdef class Func1: if not isinstance(other, Func1): other = Func1(other) cdef Func1 f1 = other - return Func1._make_func1(CxxNewProdFunction(self._func, f1._func)) + # @todo: When dropping support for Cython < 3.0.0, 'f0' can be removed and + # 'self._func' can be used directly in the C++ call + cdef Func1 f0 + if isinstance(self, Func1): + f0 = self + else: + f0 = Func1(self) + return Func1._make_func1(CxxNewProdFunction(f0._func, f1._func)) def __rmul__(self, other): if not isinstance(other, Func1): @@ -280,7 +303,14 @@ cdef class Func1: if not isinstance(other, Func1): other = Func1(other) cdef Func1 f1 = other - return Func1._make_func1(CxxNewRatioFunction(self._func, f1._func)) + # @todo: When dropping support for Cython < 3.0.0, 'f0' can be removed and + # 'self._func' can be used directly in the C++ call + cdef Func1 f0 + if isinstance(self, Func1): + f0 = self + else: + f0 = Func1(self) + return Func1._make_func1(CxxNewRatioFunction(f0._func, f1._func)) def __rtruediv__(self, other): if not isinstance(other, Func1):