Merge pull request #359 from crazybie/main

[binding]fix: cannot overload  static functions.
This commit is contained in:
BLUELOVETH 2025-04-13 22:42:11 +08:00 committed by GitHub
commit 13884aaadf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 7 deletions

View File

@ -591,10 +591,8 @@ class property : public object {
object(type::of<property>()(getter, setter)) {} object(type::of<property>()(getter, setter)) {}
}; };
class staticmethod : public object { class staticmethod : public cpp_function {
PKBIND_TYPE_IMPL(object, staticmethod, tp_staticmethod); PKBIND_TYPE_IMPL(cpp_function, staticmethod, tp_staticmethod);
staticmethod(handle method) : object(type::of<staticmethod>()(method)) {}
}; };
namespace impl { namespace impl {
@ -619,8 +617,7 @@ void bind_function(handle obj, const char* name_, Fn&& fn, const Extras&... extr
py_setdict( py_setdict(
obj.ptr(), obj.ptr(),
name, name,
staticmethod(cpp_function(is_method, name_, std::forward<Fn>(fn), extras...).ptr()) staticmethod(is_method, name_, std::forward<Fn>(fn), extras...).ptr());
.ptr());
} else { } else {
if constexpr(has_named_args && is_method) { if constexpr(has_named_args && is_method) {
py_setdict( py_setdict(

View File

@ -195,6 +195,18 @@ TEST_F(PYBIND11_TEST, overload) {
EXPECT_EVAL_EQ("cal(1, 2)", 3); EXPECT_EVAL_EQ("cal(1, 2)", 3);
EXPECT_EVAL_EQ("cal(1, 2, 3)", 6); 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_<Point>(m, "Point")
.def_static("sum", py::overload_cast<int>(&Point::sum))
.def_static("sum", py::overload_cast<int, int>(&Point::sum));
EXPECT_EVAL_EQ("Point.sum(1)", 1);
EXPECT_EVAL_EQ("Point.sum(1, 2)", 3);
} }
TEST_F(PYBIND11_TEST, return_value_policy) { TEST_F(PYBIND11_TEST, return_value_policy) {
@ -399,4 +411,3 @@ TEST_F(PYBIND11_TEST, overload_cast) {
} }
} // namespace } // namespace