From 3ea37002b10600bef7849f827844e9897ca01c3a Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 28 Jun 2022 07:05:23 -0700 Subject: [PATCH] Eliminate need for `PYBIND11_DETAIL_TYPE_CASTER_ACCESS_TRANSLATION_UNIT_LOCAL` macro. Copying code first developed by @amauryfa. I tried this at an earlier stage, but by itself this was insufficient. In the meantime I added in the TU-local mechanisms: trying again. Passes local testing: ``` DISABLED std::system_error: ODR VIOLATION DETECTED: pybind11::detail::type_caster: SourceLocation1="/usr/local/google/home/rwgk/forked/pybind11/tests/test_type_caster_odr_guard_1.cpp:18", SourceLocation2="/usr/local/google/home/rwgk/forked/pybind11/tests/test_type_caster_odr_guard_2.cpp:19" C++ Info: Debian Clang 13.0.1 C++17 __pybind11_internals_v4_clang_libstdcpp_cxxabi1002_sh_def__ =========================================================== test session starts ============================================================ platform linux -- Python 3.9.12, pytest-6.2.3, py-1.10.0, pluggy-0.13.1 -- /usr/bin/python3 cachedir: .pytest_cache rootdir: /usr/local/google/home/rwgk/forked/pybind11/tests, configfile: pytest.ini collected 6 items test_type_caster_odr_guard_1.py::test_type_mrc_to_python PASSED test_type_caster_odr_guard_1.py::test_type_mrc_from_python PASSED test_type_caster_odr_guard_1.py::test_type_caster_odr_registry_values PASSED test_type_caster_odr_guard_1.py::test_type_caster_odr_violation_detected_counter PASSED test_type_caster_odr_guard_2.py::test_type_mrc_to_python PASSED test_type_caster_odr_guard_2.py::test_type_mrc_from_python PASSED ============================================================ 6 passed in 0.01s ============================================================= ``` --- include/pybind11/detail/type_caster_odr_guard.h | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/include/pybind11/detail/type_caster_odr_guard.h b/include/pybind11/detail/type_caster_odr_guard.h index 12472b3b7..c77e6e5b0 100644 --- a/include/pybind11/detail/type_caster_odr_guard.h +++ b/include/pybind11/detail/type_caster_odr_guard.h @@ -143,6 +143,19 @@ struct get_type_caster_source_file_line< template struct type_caster_odr_guard : TypeCasterType { static tu_local_no_data_always_false translation_unit_local; + + type_caster_odr_guard() { + if (translation_unit_local) { + } + } + + template + static handle cast(CType &&src, return_value_policy policy, handle parent, Arg &&...arg) { + if (translation_unit_local) { + } + return TypeCasterType::cast( + std::forward(src), policy, parent, std::forward(arg)...); + } }; template @@ -163,8 +176,6 @@ PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) static constexpr auto source_file_line \ = ::pybind11::detail::tu_local_const_name(__FILE__ ":" PYBIND11_TOSTRING(__LINE__)); -# define PYBIND11_DETAIL_TYPE_CASTER_ACCESS_TRANSLATION_UNIT_LOCAL(...) \ - if (::pybind11::detail::make_caster<__VA_ARGS__>::translation_unit_local) { \ - } +# define PYBIND11_DETAIL_TYPE_CASTER_ACCESS_TRANSLATION_UNIT_LOCAL(...) #endif