Skip to content

Commit 3374a8f

Browse files
committed
fix: use caster to support potential PyCapsule change in pybind11 3
Signed-off-by: Henry Schreiner <[email protected]>
1 parent 1fbbe16 commit 3374a8f

File tree

2 files changed

+9
-23
lines changed

2 files changed

+9
-23
lines changed

include/bh_python/transform.hpp

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[build-system]
2-
requires = ["scikit-build-core>=0.11", "pybind11>=2.13.3"]
2+
requires = ["scikit-build-core>=0.11", "pybind11 @ git+https://github.com/rwgk/pybind11@pickle_callable"]
33
build-backend = "scikit_build_core.build"
44

55
[project]

0 commit comments

Comments
 (0)