diff --git a/include/bh_python/transform.hpp b/include/bh_python/transform.hpp index 17f8e483e..e279b7924 100644 --- a/include/bh_python/transform.hpp +++ b/include/bh_python/transform.hpp @@ -65,32 +65,19 @@ struct func_transform { throw py::type_error("Only ctypes double(double) and C++ functions allowed " "(must be function)"); - auto func = py::reinterpret_borrow(src); - - if(auto cfunc = func.cpp_function()) { - auto c = py::reinterpret_borrow( - PyCFunction_GET_SELF(cfunc.ptr())); - - auto rec = c.get_pointer(); - - if(rec && rec->is_stateless - && py::detail::same_type( - typeid(raw_t*), - *reinterpret_cast(rec->data[1]))) { - struct capture { - raw_t* f; - }; - return std::make_tuple((reinterpret_cast(&rec->data))->f, - src); - } - + py::detail::make_caster> func_caster; + if(!func_caster.load(src, /*convert*/ false)) { // Note that each error is slightly different just to help with debugging throw py::type_error("Only ctypes double(double) and C++ functions allowed " "(must be stateless)"); } - - throw py::type_error("Only ctypes double(double) and C++ functions allowed " - "(must be cpp function)"); + auto func = static_cast&>(func_caster); + auto cfunc = func.target(); + if(cfunc == nullptr) { + throw py::type_error( + "Retrieving double(double) function failed (must be stateless)"); + } + return std::make_tuple(*cfunc, src); } func_transform(py::object f, py::object i, py::object c, py::str n)