From d23c821b209c370b917b78186c613d9a40174b08 Mon Sep 17 00:00:00 2001 From: Yannick Jadoul Date: Tue, 11 Jun 2019 10:59:57 +0200 Subject: [PATCH] Make static member functions, added with `def_static`, `staticmethod` descriptor instances (#1732) --- include/pybind11/pybind11.h | 2 +- include/pybind11/pytypes.h | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 1b784673..76e243a9 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1121,7 +1121,7 @@ public: "def_static(...) called with a non-static member function pointer"); cpp_function cf(std::forward(f), name(name_), scope(*this), sibling(getattr(*this, name_, none())), extra...); - attr(cf.name()) = cf; + attr(cf.name()) = staticmethod(cf); return *this; } diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 81906f43..b4f4be92 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -742,6 +742,8 @@ inline bool PyEllipsis_Check(PyObject *o) { return o == Py_Ellipsis; } inline bool PyUnicode_Check_Permissive(PyObject *o) { return PyUnicode_Check(o) || PYBIND11_BYTES_CHECK(o); } +inline bool PyStaticMethod_Check(PyObject *o) { return o->ob_type == &PyStaticMethod_Type; } + class kwargs_proxy : public handle { public: explicit kwargs_proxy(handle h) : handle(h) { } @@ -1281,6 +1283,11 @@ public: bool is_cpp_function() const { return (bool) cpp_function(); } }; +class staticmethod : public object { +public: + PYBIND11_OBJECT_CVT(staticmethod, object, detail::PyStaticMethod_Check, PyStaticMethod_New) +}; + class buffer : public object { public: PYBIND11_OBJECT_DEFAULT(buffer, object, PyObject_CheckBuffer)