From 7c2461eefd9bad93601a431a45aa8785ebf26b2f Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Sun, 20 Nov 2016 05:26:02 +0100 Subject: [PATCH] resolve issue involving inheritance + def_static + override (fixes #511) --- include/pybind11/pybind11.h | 2 +- tests/test_issues.cpp | 20 ++++++++++++++++++++ tests/test_issues.py | 12 ++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 0079052f7..1221ba6dd 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -260,7 +260,7 @@ protected: chain = (detail::function_record *) rec_capsule; /* Never append a method to an overload chain of a parent class; instead, hide the parent's overloads in this case */ - if (chain->class_ != rec->class_) + if (chain->scope != rec->scope) chain = nullptr; } // Don't trigger for things like the default __init__, which are wrapper_descriptors that we are intentionally replacing diff --git a/tests/test_issues.cpp b/tests/test_issues.cpp index 084ea316d..33f8a43af 100644 --- a/tests/test_issues.cpp +++ b/tests/test_issues.cpp @@ -351,6 +351,26 @@ void init_issues(py::module &m) { /// Issue #484: number conversion generates unhandled exceptions m2.def("test_complex", [](float x) { py::print("{}"_s.format(x)); }); m2.def("test_complex", [](std::complex x) { py::print("({}, {})"_s.format(x.real(), x.imag())); }); + + /// Issue #511: problem with inheritance + overwritten def_static + struct MyBase { + static std::unique_ptr make() { + return std::unique_ptr(new MyBase()); + } + }; + + struct MyDerived : MyBase { + static std::unique_ptr make() { + return std::unique_ptr(new MyDerived()); + } + }; + + py::class_(m2, "MyBase") + .def_static("make", &MyBase::make); + + py::class_(m2, "MyDerived") + .def_static("make", &MyDerived::make) + .def_static("make2", &MyDerived::make); } // MSVC workaround: trying to use a lambda here crashes MSCV diff --git a/tests/test_issues.py b/tests/test_issues.py index a2cf53082..1b29ceb31 100644 --- a/tests/test_issues.py +++ b/tests/test_issues.py @@ -237,3 +237,15 @@ def test_complex_cast(capture): 1.0 (0.0, 2.0) """ + + +def test_inheritance_override_def_static(): + from pybind11_tests.issues import MyBase, MyDerived + + b = MyBase.make() + d1 = MyDerived.make2() + d2 = MyDerived.make() + + assert isinstance(b, MyBase) + assert isinstance(d1, MyDerived) + assert isinstance(d2, MyDerived)