Files
pybind11/tests/test_cross_module_rtti/test_cross_module_rtti.cpp
Peter Steneteg b19489145b fix: expose required symbol using clang (#5700)
* test: Added test case for visibility of common symbols across shared libraries

* style: pre-commit fixes

* tests: cmake target name fix

* tests: Added visibility test to ci

* tests: set the default visibility to hidden

* prototype/proof-of-concept fix: PYBIND11_EXPORT_GUARDED_DELETE

* Fix silly oversight: actually use PYBIND11_EXPORT_GUARDED_DELETE

* Update struct_smart_holder.h

* style: pre-commit fixes

* Update include/pybind11/detail/struct_smart_holder.h

* Update struct_smart_holder.h

* ci: fix addition to reusable-standard.yml

* Update CMakeLists.txt

* refactor: rename tests to test_cross_module_rtti

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
2025-06-03 16:02:19 -04:00

51 lines
1.9 KiB
C++

#include <pybind11/embed.h>
#include <pybind11/pybind11.h>
#include <catch.hpp>
#include <lib.h>
static constexpr auto script = R"(
import test_cross_module_rtti_bindings
class Bar(test_cross_module_rtti_bindings.Base):
def __init__(self, a, b):
test_cross_module_rtti_bindings.Base.__init__(self, a, b)
def get(self):
return 4 * self.a + self.b
def get_bar(a, b):
return Bar(a, b)
)";
TEST_CASE("Simple case where without is_alias") {
// "Simple" case this will not have `python_instance_is_alias` set in type_cast_base.h:771
auto bindings = pybind11::module_::import("test_cross_module_rtti_bindings");
auto holder = bindings.attr("get_foo")(1, 2);
auto foo = holder.cast<std::shared_ptr<lib::Base>>();
REQUIRE(foo->get() == 4); // 2 * 1 + 2 = 4
}
TEST_CASE("Complex case where with it_alias") {
// "Complex" case this will have `python_instance_is_alias` set in type_cast_base.h:771
pybind11::exec(script);
auto main = pybind11::module::import("__main__");
// The critical part of "Bar" is that it will have the `is_alias` `instance` flag set.
// I'm not quite sure what is required to get that flag, this code is derived from a
// larger code where this issue was observed.
auto holder2 = main.attr("get_bar")(1, 2);
// this will trigger `std::get_deleter<memory::guarded_delete>` in type_cast_base.h:772
// This will fail since the program will see two different typeids for `memory::guarded_delete`
// on from the bindings module and one from "main", which will both have
// `__is_type_name_unique` as true and but still have different values. Hence we will not find
// the deleter and the cast fill fail. See "__eq(__type_name_t __lhs, __type_name_t __rhs)" in
// typeinfo in libc++
auto bar = holder2.cast<std::shared_ptr<lib::Base>>();
REQUIRE(bar->get() == 6); // 4 * 1 + 2 = 6
}