Skip to content

Commit f9cbd91

Browse files
authored
fix: use caster to support potential PyCapsule change in pybind11 3 (#994)
Signed-off-by: Henry Schreiner <[email protected]>
1 parent 1fbbe16 commit f9cbd91

File tree

1 file changed

+9
-22
lines changed

1 file changed

+9
-22
lines changed

include/bh_python/transform.hpp

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,32 +65,19 @@ 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(
78+
"Retrieving double(double) function failed (must be stateless)");
79+
}
80+
return std::make_tuple(*cfunc, src);
9481
}
9582

9683
func_transform(py::object f, py::object i, py::object c, py::str n)

0 commit comments

Comments
 (0)