@@ -65,32 +65,18 @@ struct func_transform {
6565 throw py::type_error (" Only ctypes double(double) and C++ functions allowed "
6666 " (must be function)" );
6767
68- auto func = py::reinterpret_borrow<py::function>(src);
69-
70- if (auto cfunc = func.cpp_function ()) {
71- auto c = py::reinterpret_borrow<py::capsule>(
72- PyCFunction_GET_SELF (cfunc.ptr ()));
73-
74- auto rec = c.get_pointer <py::detail::function_record>();
75-
76- if (rec && rec->is_stateless
77- && py::detail::same_type (
78- typeid (raw_t *),
79- *reinterpret_cast <const std::type_info*>(rec->data [1 ]))) {
80- struct capture {
81- raw_t * f;
82- };
83- return std::make_tuple ((reinterpret_cast <capture*>(&rec->data ))->f ,
84- src);
85- }
86-
68+ py::detail::make_caster<std::function<raw_t >> func_caster;
69+ if (!func_caster.load (src, /* convert*/ false )) {
8770 // Note that each error is slightly different just to help with debugging
8871 throw py::type_error (" Only ctypes double(double) and C++ functions allowed "
8972 " (must be stateless)" );
9073 }
91-
92- throw py::type_error (" Only ctypes double(double) and C++ functions allowed "
93- " (must be cpp function)" );
74+ auto func = static_cast <std::function<raw_t > &>(func_caster);
75+ auto cfunc = func.target <raw_t *>();
76+ if (cfunc == nullptr ) {
77+ throw py::type_error (" Retrieving double(double) function failed (must be stateless)" );
78+ }
79+ return std::make_tuple (*cfunc, src);
9480 }
9581
9682 func_transform (py::object f, py::object i, py::object c, py::str n)
0 commit comments