From e76cdab6dd77bad411e6ac9372ee527aff89ef17 Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Sat, 2 Jan 2021 10:15:39 +0100 Subject: Update pybind11 to version 2.6.1 --- 3rdparty/pybind11/tests/test_class.cpp | 131 +++++++++++++++++++++++++++++---- 1 file changed, 117 insertions(+), 14 deletions(-) (limited to '3rdparty/pybind11/tests/test_class.cpp') diff --git a/3rdparty/pybind11/tests/test_class.cpp b/3rdparty/pybind11/tests/test_class.cpp index 499d0cc5..890fab73 100644 --- a/3rdparty/pybind11/tests/test_class.cpp +++ b/3rdparty/pybind11/tests/test_class.cpp @@ -103,7 +103,7 @@ TEST_SUBMODULE(class_, m) { BaseClass() = default; BaseClass(const BaseClass &) = default; BaseClass(BaseClass &&) = default; - virtual ~BaseClass() {} + virtual ~BaseClass() = default; }; struct DerivedClass1 : BaseClass { }; struct DerivedClass2 : BaseClass { }; @@ -134,6 +134,32 @@ TEST_SUBMODULE(class_, m) { ); }); + struct Invalid {}; + + // test_type + m.def("check_type", [](int category) { + // Currently not supported (via a fail at compile time) + // See https://github.com/pybind/pybind11/issues/2486 + // if (category == 2) + // return py::type::of(); + if (category == 1) + return py::type::of(); + else + return py::type::of(); + }); + + m.def("get_type_of", [](py::object ob) { + return py::type::of(ob); + }); + + m.def("get_type_classic", [](py::handle h) { + return h.get_type(); + }); + + m.def("as_type", [](py::object ob) { + return py::type(ob); + }); + // test_mismatched_holder struct MismatchBase1 { }; struct MismatchDerived1 : MismatchBase1 { }; @@ -142,12 +168,12 @@ TEST_SUBMODULE(class_, m) { struct MismatchDerived2 : MismatchBase2 { }; m.def("mismatched_holder_1", []() { - auto mod = py::module::import("__main__"); + auto mod = py::module_::import("__main__"); py::class_>(mod, "MismatchBase1"); py::class_(mod, "MismatchDerived1"); }); m.def("mismatched_holder_2", []() { - auto mod = py::module::import("__main__"); + auto mod = py::module_::import("__main__"); py::class_(mod, "MismatchBase2"); py::class_, MismatchBase2>(mod, "MismatchDerived2"); @@ -227,6 +253,8 @@ TEST_SUBMODULE(class_, m) { static void *operator new(size_t s, void *ptr) { py::print("C placement-new", s); return ptr; } static void operator delete(void *p, size_t s) { py::print("C delete", s); return ::operator delete(p); } virtual ~AliasedHasOpNewDelSize() = default; + AliasedHasOpNewDelSize() = default; + AliasedHasOpNewDelSize(const AliasedHasOpNewDelSize&) = delete; }; struct PyAliasedHasOpNewDelSize : AliasedHasOpNewDelSize { PyAliasedHasOpNewDelSize() = default; @@ -277,6 +305,8 @@ TEST_SUBMODULE(class_, m) { class ProtectedB { public: virtual ~ProtectedB() = default; + ProtectedB() = default; + ProtectedB(const ProtectedB &) = delete; protected: virtual int foo() const { return value; } @@ -287,7 +317,7 @@ TEST_SUBMODULE(class_, m) { class TrampolineB : public ProtectedB { public: - int foo() const override { PYBIND11_OVERLOAD(int, ProtectedB, foo, ); } + int foo() const override { PYBIND11_OVERRIDE(int, ProtectedB, foo, ); } }; class PublicistB : public ProtectedB { @@ -323,7 +353,7 @@ TEST_SUBMODULE(class_, m) { // test_reentrant_implicit_conversion_failure // #1035: issue with runaway reentrant implicit conversion struct BogusImplicitConversion { - BogusImplicitConversion(const BogusImplicitConversion &) { } + BogusImplicitConversion(const BogusImplicitConversion &) = default; }; py::class_(m, "BogusImplicitConversion") @@ -367,19 +397,92 @@ TEST_SUBMODULE(class_, m) { .def(py::init<>()) .def("ptr", &Aligned::ptr); #endif + + // test_final + struct IsFinal final {}; + py::class_(m, "IsFinal", py::is_final()); + + // test_non_final_final + struct IsNonFinalFinal {}; + py::class_(m, "IsNonFinalFinal", py::is_final()); + + // test_exception_rvalue_abort + struct PyPrintDestructor { + PyPrintDestructor() = default; + ~PyPrintDestructor() { + py::print("Print from destructor"); + } + void throw_something() { throw std::runtime_error("error"); } + }; + py::class_(m, "PyPrintDestructor") + .def(py::init<>()) + .def("throw_something", &PyPrintDestructor::throw_something); + + // test_multiple_instances_with_same_pointer + struct SamePointer {}; + static SamePointer samePointer; + py::class_>(m, "SamePointer") + .def(py::init([]() { return &samePointer; })) + .def("__del__", [](SamePointer&) { py::print("__del__ called"); }); + + struct Empty {}; + py::class_(m, "Empty") + .def(py::init<>()); + + // test_base_and_derived_nested_scope + struct BaseWithNested { + struct Nested {}; + }; + + struct DerivedWithNested : BaseWithNested { + struct Nested {}; + }; + + py::class_ baseWithNested_class(m, "BaseWithNested"); + py::class_ derivedWithNested_class(m, "DerivedWithNested"); + py::class_(baseWithNested_class, "Nested") + .def_static("get_name", []() { return "BaseWithNested::Nested"; }); + py::class_(derivedWithNested_class, "Nested") + .def_static("get_name", []() { return "DerivedWithNested::Nested"; }); + + // test_register_duplicate_class + struct Duplicate {}; + struct OtherDuplicate {}; + struct DuplicateNested {}; + struct OtherDuplicateNested {}; + m.def("register_duplicate_class_name", [](py::module_ m) { + py::class_(m, "Duplicate"); + py::class_(m, "Duplicate"); + }); + m.def("register_duplicate_class_type", [](py::module_ m) { + py::class_(m, "OtherDuplicate"); + py::class_(m, "YetAnotherDuplicate"); + }); + m.def("register_duplicate_nested_class_name", [](py::object gt) { + py::class_(gt, "DuplicateNested"); + py::class_(gt, "DuplicateNested"); + }); + m.def("register_duplicate_nested_class_type", [](py::object gt) { + py::class_(gt, "OtherDuplicateNested"); + py::class_(gt, "YetAnotherDuplicateNested"); + }); } -template class BreaksBase { public: virtual ~BreaksBase() = default; }; +template class BreaksBase { public: + virtual ~BreaksBase() = default; + BreaksBase() = default; + BreaksBase(const BreaksBase&) = delete; +}; template class BreaksTramp : public BreaksBase {}; // These should all compile just fine: -typedef py::class_, std::unique_ptr>, BreaksTramp<1>> DoesntBreak1; -typedef py::class_, BreaksTramp<2>, std::unique_ptr>> DoesntBreak2; -typedef py::class_, std::unique_ptr>> DoesntBreak3; -typedef py::class_, BreaksTramp<4>> DoesntBreak4; -typedef py::class_> DoesntBreak5; -typedef py::class_, std::shared_ptr>, BreaksTramp<6>> DoesntBreak6; -typedef py::class_, BreaksTramp<7>, std::shared_ptr>> DoesntBreak7; -typedef py::class_, std::shared_ptr>> DoesntBreak8; +using DoesntBreak1 = py::class_, std::unique_ptr>, BreaksTramp<1>>; +using DoesntBreak2 = py::class_, BreaksTramp<2>, std::unique_ptr>>; +using DoesntBreak3 = py::class_, std::unique_ptr>>; +using DoesntBreak4 = py::class_, BreaksTramp<4>>; +using DoesntBreak5 = py::class_>; +using DoesntBreak6 = py::class_, std::shared_ptr>, BreaksTramp<6>>; +using DoesntBreak7 = py::class_, BreaksTramp<7>, std::shared_ptr>>; +using DoesntBreak8 = py::class_, std::shared_ptr>>; #define CHECK_BASE(N) static_assert(std::is_same>::value, \ "DoesntBreak" #N " has wrong type!") CHECK_BASE(1); CHECK_BASE(2); CHECK_BASE(3); CHECK_BASE(4); CHECK_BASE(5); CHECK_BASE(6); CHECK_BASE(7); CHECK_BASE(8); -- cgit v1.2.3