From ea7097b0eb100b1b1206346a897346245a10856c Mon Sep 17 00:00:00 2001 From: lisong Date: Sun, 13 Apr 2025 21:50:23 +0800 Subject: [PATCH 1/2] fix: can't overload static function. --- include/pybind11/internal/function.h | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/include/pybind11/internal/function.h b/include/pybind11/internal/function.h index 653b2dad..05894fed 100644 --- a/include/pybind11/internal/function.h +++ b/include/pybind11/internal/function.h @@ -591,10 +591,8 @@ class property : public object { object(type::of()(getter, setter)) {} }; -class staticmethod : public object { - PKBIND_TYPE_IMPL(object, staticmethod, tp_staticmethod); - - staticmethod(handle method) : object(type::of()(method)) {} +class staticmethod : public cpp_function { + PKBIND_TYPE_IMPL(cpp_function, staticmethod, tp_staticmethod); }; namespace impl { @@ -619,8 +617,7 @@ void bind_function(handle obj, const char* name_, Fn&& fn, const Extras&... extr py_setdict( obj.ptr(), name, - staticmethod(cpp_function(is_method, name_, std::forward(fn), extras...).ptr()) - .ptr()); + staticmethod(is_method, name_, std::forward(fn), extras...).ptr()); } else { if constexpr(has_named_args && is_method) { py_setdict( From fa5443e9d15f15a42cd627e97383f3dc346fc443 Mon Sep 17 00:00:00 2001 From: lisong Date: Sun, 13 Apr 2025 21:50:23 +0800 Subject: [PATCH 2/2] fix: can't overload static function. --- include/pybind11/internal/function.h | 9 +++------ include/pybind11/tests/function.cpp | 13 ++++++++++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/include/pybind11/internal/function.h b/include/pybind11/internal/function.h index 653b2dad..05894fed 100644 --- a/include/pybind11/internal/function.h +++ b/include/pybind11/internal/function.h @@ -591,10 +591,8 @@ class property : public object { object(type::of()(getter, setter)) {} }; -class staticmethod : public object { - PKBIND_TYPE_IMPL(object, staticmethod, tp_staticmethod); - - staticmethod(handle method) : object(type::of()(method)) {} +class staticmethod : public cpp_function { + PKBIND_TYPE_IMPL(cpp_function, staticmethod, tp_staticmethod); }; namespace impl { @@ -619,8 +617,7 @@ void bind_function(handle obj, const char* name_, Fn&& fn, const Extras&... extr py_setdict( obj.ptr(), name, - staticmethod(cpp_function(is_method, name_, std::forward(fn), extras...).ptr()) - .ptr()); + staticmethod(is_method, name_, std::forward(fn), extras...).ptr()); } else { if constexpr(has_named_args && is_method) { py_setdict( diff --git a/include/pybind11/tests/function.cpp b/include/pybind11/tests/function.cpp index 1b7d4891..50da0204 100644 --- a/include/pybind11/tests/function.cpp +++ b/include/pybind11/tests/function.cpp @@ -195,6 +195,18 @@ TEST_F(PYBIND11_TEST, overload) { EXPECT_EVAL_EQ("cal(1, 2)", 3); EXPECT_EVAL_EQ("cal(1, 2, 3)", 6); + + struct Point { + static int sum(int x) { return x; } + + static int sum(int x, int y) { return x + y; } + }; + + py::class_(m, "Point") + .def_static("sum", py::overload_cast(&Point::sum)) + .def_static("sum", py::overload_cast(&Point::sum)); + EXPECT_EVAL_EQ("Point.sum(1)", 1); + EXPECT_EVAL_EQ("Point.sum(1, 2)", 3); } TEST_F(PYBIND11_TEST, return_value_policy) { @@ -399,4 +411,3 @@ TEST_F(PYBIND11_TEST, overload_cast) { } } // namespace -