From 69abfb5164860d4fff8cecc3475fb65a5fdd7e2f Mon Sep 17 00:00:00 2001 From: Manuel Schneider Date: Mon, 6 Oct 2025 12:16:15 +0200 Subject: [PATCH 1/6] Test PR --- tests/test_with_catch/test_interpreter.cpp | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/test_with_catch/test_interpreter.cpp b/tests/test_with_catch/test_interpreter.cpp index d227eecddc..5afd71c856 100644 --- a/tests/test_with_catch/test_interpreter.cpp +++ b/tests/test_with_catch/test_interpreter.cpp @@ -91,6 +91,48 @@ PYBIND11_EMBEDDED_MODULE(throw_error_already_set, ) { d["missing"].cast(); } +// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> + + +struct Item +{ + Item() = default; + virtual ~Item() = default; + virtual int getInt() const = 0; +}; + +struct ItemTrampoline : public Item, py::trampoline_self_life_support +{ + int getInt() const override { PYBIND11_OVERRIDE_PURE(int, Item, getInt, ); } +}; + +PYBIND11_EMBEDDED_MODULE(embedded_smart_holder, m) { + + py::classh(m, "Item") + .def(py::init<>()) + .def("getInt", &Item::getInt) + ; +} + +TEST_CASE("Embedded smart holder test") { + + auto module = py::module_::import("embedded_smart_holder"); + + auto o = module.attr("Item")(); // create py + + auto i = o.cast>(); // cast cpp + + if (i->getInt() != 42) // test cpp + throw std::runtime_error("Not 42"); + + o = py::object(); // release py + + if (i->getInt() != 42) // test cpp + throw std::runtime_error("Not 42 after release"); +} + +// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + TEST_CASE("PYTHONPATH is used to update sys.path") { // The setup for this TEST_CASE is in catch.cpp! auto sys_path = py::str(py::module_::import("sys").attr("path")).cast(); From 50cfe1343e696cdb6c58a1eb1cb67005c78a2aa1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 6 Oct 2025 10:16:43 +0000 Subject: [PATCH 2/6] style: pre-commit fixes --- tests/test_with_catch/test_interpreter.cpp | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/tests/test_with_catch/test_interpreter.cpp b/tests/test_with_catch/test_interpreter.cpp index 5afd71c856..3e82c6c980 100644 --- a/tests/test_with_catch/test_interpreter.cpp +++ b/tests/test_with_catch/test_interpreter.cpp @@ -93,41 +93,35 @@ PYBIND11_EMBEDDED_MODULE(throw_error_already_set, ) { // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> - -struct Item -{ +struct Item { Item() = default; virtual ~Item() = default; virtual int getInt() const = 0; }; -struct ItemTrampoline : public Item, py::trampoline_self_life_support -{ +struct ItemTrampoline : public Item, py::trampoline_self_life_support { int getInt() const override { PYBIND11_OVERRIDE_PURE(int, Item, getInt, ); } }; PYBIND11_EMBEDDED_MODULE(embedded_smart_holder, m) { - py::classh(m, "Item") - .def(py::init<>()) - .def("getInt", &Item::getInt) - ; + py::classh(m, "Item").def(py::init<>()).def("getInt", &Item::getInt); } TEST_CASE("Embedded smart holder test") { auto module = py::module_::import("embedded_smart_holder"); - auto o = module.attr("Item")(); // create py + auto o = module.attr("Item")(); // create py - auto i = o.cast>(); // cast cpp + auto i = o.cast>(); // cast cpp - if (i->getInt() != 42) // test cpp + if (i->getInt() != 42) // test cpp throw std::runtime_error("Not 42"); o = py::object(); // release py - if (i->getInt() != 42) // test cpp + if (i->getInt() != 42) // test cpp throw std::runtime_error("Not 42 after release"); } From e7f771b8a4ade34ae110d850e6c8d3cf7abb6275 Mon Sep 17 00:00:00 2001 From: Manuel Schneider Date: Mon, 6 Oct 2025 12:30:32 +0200 Subject: [PATCH 3/6] update --- tests/test_with_catch/test_interpreter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_with_catch/test_interpreter.cpp b/tests/test_with_catch/test_interpreter.cpp index 3e82c6c980..d033794f8c 100644 --- a/tests/test_with_catch/test_interpreter.cpp +++ b/tests/test_with_catch/test_interpreter.cpp @@ -99,7 +99,7 @@ struct Item { virtual int getInt() const = 0; }; -struct ItemTrampoline : public Item, py::trampoline_self_life_support { +struct ItemTrampoline : public Item, public py::trampoline_self_life_support { int getInt() const override { PYBIND11_OVERRIDE_PURE(int, Item, getInt, ); } }; From 9d5a967452a79a703d9a7fd38988f0f132161038 Mon Sep 17 00:00:00 2001 From: Manuel Schneider Date: Mon, 6 Oct 2025 12:39:04 +0200 Subject: [PATCH 4/6] update --- tests/test_with_catch/test_interpreter.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_with_catch/test_interpreter.cpp b/tests/test_with_catch/test_interpreter.cpp index d033794f8c..fce4b30b25 100644 --- a/tests/test_with_catch/test_interpreter.cpp +++ b/tests/test_with_catch/test_interpreter.cpp @@ -93,13 +93,15 @@ PYBIND11_EMBEDDED_MODULE(throw_error_already_set, ) { // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -struct Item { +class Item { +public: Item() = default; virtual ~Item() = default; virtual int getInt() const = 0; }; -struct ItemTrampoline : public Item, public py::trampoline_self_life_support { +class ItemTrampoline : public Item, public py::trampoline_self_life_support { +public: int getInt() const override { PYBIND11_OVERRIDE_PURE(int, Item, getInt, ); } }; From ac666e8a1e4a76e5b6fe26ec4ba00d0ace52cdd2 Mon Sep 17 00:00:00 2001 From: Manuel Schneider Date: Tue, 7 Oct 2025 08:22:11 +0200 Subject: [PATCH 5/6] update --- tests/test_with_catch/test_interpreter.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/tests/test_with_catch/test_interpreter.cpp b/tests/test_with_catch/test_interpreter.cpp index fce4b30b25..4f24202648 100644 --- a/tests/test_with_catch/test_interpreter.cpp +++ b/tests/test_with_catch/test_interpreter.cpp @@ -111,19 +111,25 @@ PYBIND11_EMBEDDED_MODULE(embedded_smart_holder, m) { } TEST_CASE("Embedded smart holder test") { + py::exec(R"( +import embedded_smart_holder - auto module = py::module_::import("embedded_smart_holder"); +class ItemDerived(embedded_smart_holder.Item): + def getInt(self): + return 42 +)"); - auto o = module.attr("Item")(); // create py + auto py_item_derived = py::globals()["ItemDerived"](); - auto i = o.cast>(); // cast cpp + auto item = py_item_derived.cast>()->getInt(); // cast cpp - if (i->getInt() != 42) // test cpp + if (item->getInt() != 42) // test cpp throw std::runtime_error("Not 42"); - o = py::object(); // release py + py_item_derived = py::object(); // release py + py::module::import("gc").attr("collect")(); - if (i->getInt() != 42) // test cpp + if (item->getInt() != 42) // test cpp throw std::runtime_error("Not 42 after release"); } From 6f2aa5d38e7ed2e6ec22c09a1ad4c51e462d7fa9 Mon Sep 17 00:00:00 2001 From: Manuel Schneider Date: Tue, 7 Oct 2025 12:06:35 +0200 Subject: [PATCH 6/6] update --- tests/test_with_catch/test_interpreter.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_with_catch/test_interpreter.cpp b/tests/test_with_catch/test_interpreter.cpp index 4f24202648..b34630e3d8 100644 --- a/tests/test_with_catch/test_interpreter.cpp +++ b/tests/test_with_catch/test_interpreter.cpp @@ -100,7 +100,7 @@ class Item { virtual int getInt() const = 0; }; -class ItemTrampoline : public Item, public py::trampoline_self_life_support { +class ItemTrampoline : Item, public py::trampoline_self_life_support { public: int getInt() const override { PYBIND11_OVERRIDE_PURE(int, Item, getInt, ); } }; @@ -121,7 +121,7 @@ class ItemDerived(embedded_smart_holder.Item): auto py_item_derived = py::globals()["ItemDerived"](); - auto item = py_item_derived.cast>()->getInt(); // cast cpp + auto item = py_item_derived.cast>(); // cast cpp if (item->getInt() != 42) // test cpp throw std::runtime_error("Not 42");