mirror of
https://github.com/pybind/pybind11.git
synced 2026-05-13 09:46:10 +00:00
type_caster<std::function>: allow None values in both directions
This commit is contained in:
@@ -20,6 +20,9 @@ template <typename Return, typename... Args> struct type_caster<std::function<Re
|
||||
typedef typename std::conditional<std::is_same<Return, void>::value, void_type, Return>::type retval_type;
|
||||
public:
|
||||
bool load(handle src_, bool) {
|
||||
if (src_.ptr() == Py_None)
|
||||
return true;
|
||||
|
||||
src_ = detail::get_function(src_);
|
||||
if (!src_ || !PyCallable_Check(src_.ptr()))
|
||||
return false;
|
||||
@@ -58,6 +61,9 @@ public:
|
||||
|
||||
template <typename Func>
|
||||
static handle cast(Func &&f_, return_value_policy policy, handle /* parent */) {
|
||||
if (!f_)
|
||||
return handle(Py_None).inc_ref();
|
||||
|
||||
auto result = f_.template target<Return (*)(Args...)>();
|
||||
if (result)
|
||||
return cpp_function(*result, policy).release();
|
||||
|
||||
Reference in New Issue
Block a user