aboutsummaryrefslogtreecommitdiff
path: root/include/pybind11/functional.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/pybind11/functional.h')
-rw-r--r--include/pybind11/functional.h81
1 files changed, 57 insertions, 24 deletions
diff --git a/include/pybind11/functional.h b/include/pybind11/functional.h
index 92c17dc2..87ec4d10 100644
--- a/include/pybind11/functional.h
+++ b/include/pybind11/functional.h
@@ -10,6 +10,7 @@
#pragma once
#include "pybind11.h"
+
#include <functional>
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
@@ -19,18 +20,21 @@ template <typename Return, typename... Args>
struct type_caster<std::function<Return(Args...)>> {
using type = std::function<Return(Args...)>;
using retval_type = conditional_t<std::is_same<Return, void>::value, void_type, Return>;
- using function_type = Return (*) (Args...);
+ using function_type = Return (*)(Args...);
public:
bool load(handle src, bool convert) {
if (src.is_none()) {
// Defer accepting None to other overloads (if we aren't in convert mode):
- if (!convert) return false;
+ if (!convert) {
+ return false;
+ }
return true;
}
- if (!isinstance<function>(src))
+ if (!isinstance<function>(src)) {
return false;
+ }
auto func = reinterpret_borrow<function>(src);
@@ -43,24 +47,51 @@ public:
captured variables), in which case the roundtrip can be avoided.
*/
if (auto cfunc = func.cpp_function()) {
- auto c = reinterpret_borrow<capsule>(PyCFunction_GET_SELF(cfunc.ptr()));
- auto rec = (function_record *) c;
-
- if (rec && rec->is_stateless &&
- same_type(typeid(function_type), *reinterpret_cast<const std::type_info *>(rec->data[1]))) {
- struct capture { function_type f; };
- value = ((capture *) &rec->data)->f;
- return true;
+ auto *cfunc_self = PyCFunction_GET_SELF(cfunc.ptr());
+ if (cfunc_self == nullptr) {
+ PyErr_Clear();
+ } else if (isinstance<capsule>(cfunc_self)) {
+ auto c = reinterpret_borrow<capsule>(cfunc_self);
+
+ function_record *rec = nullptr;
+ // Check that we can safely reinterpret the capsule into a function_record
+ if (detail::is_function_record_capsule(c)) {
+ rec = c.get_pointer<function_record>();
+ }
+
+ while (rec != nullptr) {
+ if (rec->is_stateless
+ && same_type(typeid(function_type),
+ *reinterpret_cast<const std::type_info *>(rec->data[1]))) {
+ struct capture {
+ function_type f;
+ };
+ value = ((capture *) &rec->data)->f;
+ return true;
+ }
+ rec = rec->next;
+ }
}
+ // PYPY segfaults here when passing builtin function like sum.
+ // Raising an fail exception here works to prevent the segfault, but only on gcc.
+ // See PR #1413 for full details
}
// ensure GIL is held during functor destruction
struct func_handle {
function f;
- func_handle(function&& f_) : f(std::move(f_)) {}
- func_handle(const func_handle& f_) {
+#if !(defined(_MSC_VER) && _MSC_VER == 1916 && defined(PYBIND11_CPP17))
+ // This triggers a syntax error under very special conditions (very weird indeed).
+ explicit
+#endif
+ func_handle(function &&f_) noexcept
+ : f(std::move(f_)) {
+ }
+ func_handle(const func_handle &f_) { operator=(f_); }
+ func_handle &operator=(const func_handle &f_) {
gil_scoped_acquire acq;
f = f_.f;
+ return *this;
}
~func_handle() {
gil_scoped_acquire acq;
@@ -71,12 +102,11 @@ public:
// to emulate 'move initialization capture' in C++11
struct func_wrapper {
func_handle hfunc;
- func_wrapper(func_handle&& hf): hfunc(std::move(hf)) {}
+ explicit func_wrapper(func_handle &&hf) noexcept : hfunc(std::move(hf)) {}
Return operator()(Args... args) const {
gil_scoped_acquire acq;
- object retval(hfunc.f(std::forward<Args>(args)...));
- /* Visual studio 2015 parser issue: need parentheses around this expression */
- return (retval.template cast<Return>());
+ // casts the returned object as a rvalue to the return type
+ return hfunc.f(std::forward<Args>(args)...).template cast<Return>();
}
};
@@ -86,18 +116,21 @@ public:
template <typename Func>
static handle cast(Func &&f_, return_value_policy policy, handle /* parent */) {
- if (!f_)
- return none().inc_ref();
+ if (!f_) {
+ return none().release();
+ }
auto result = f_.template target<function_type>();
- if (result)
+ if (result) {
return cpp_function(*result, policy).release();
- else
- return cpp_function(std::forward<Func>(f_), policy).release();
+ }
+ return cpp_function(std::forward<Func>(f_), policy).release();
}
- PYBIND11_TYPE_CASTER(type, _("Callable[[") + concat(make_caster<Args>::name...) + _("], ")
- + make_caster<retval_type>::name + _("]"));
+ PYBIND11_TYPE_CASTER(type,
+ const_name("Callable[[") + concat(make_caster<Args>::name...)
+ + const_name("], ") + make_caster<retval_type>::name
+ + const_name("]"));
};
PYBIND11_NAMESPACE_END(detail)