type_caster<std::function>: allow None values in both directions

This commit is contained in:
Wenzel Jakob
2016-08-18 11:18:12 +02:00
parent 0b63231bae
commit 8de0437e46
4 changed files with 13 additions and 1 deletions

View File

@@ -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();