diff --git a/include/pybind11/functional.h b/include/pybind11/functional.h index 10b21aee6d..34d1c6bc92 100644 --- a/include/pybind11/functional.h +++ b/include/pybind11/functional.h @@ -20,7 +20,8 @@ template struct type_caster Return { diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index c319e8e508..98c6f8e173 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -94,6 +94,18 @@ class iterator : public object { }; NAMESPACE_BEGIN(detail) +inline PyObject *get_function(PyObject *value) { + if (value == nullptr) + return nullptr; +#if PY_MAJOR_VERSION >= 3 + if (PyInstanceMethod_Check(value)) + value = PyInstanceMethod_GET_FUNCTION(value); +#endif + if (PyMethod_Check(value)) + value = PyMethod_GET_FUNCTION(value); + return value; +} + class accessor { public: accessor(PyObject *obj, PyObject *key, bool attr) @@ -346,16 +358,8 @@ class function : public object { PYBIND11_OBJECT_DEFAULT(function, object, PyFunction_Check) bool is_cpp_function() { - PyObject *ptr = m_ptr; - if (ptr == nullptr) - return false; -#if PY_MAJOR_VERSION >= 3 - if (PyInstanceMethod_Check(ptr)) - ptr = PyInstanceMethod_GET_FUNCTION(ptr); -#endif - if (PyMethod_Check(ptr)) - ptr = PyMethod_GET_FUNCTION(ptr); - return PyCFunction_Check(ptr); + PyObject *ptr = detail::get_function(m_ptr); + return ptr != nullptr && PyCFunction_Check(ptr); } };