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<mrc_ns::type_mrc>: 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 =============================================================
```
This commit is contained in:
Ralf W. Grosse-Kunstleve 2022-06-28 07:05:23 -07:00
parent 3fc4833433
commit 3ea37002b1

View File

@ -143,6 +143,19 @@ struct get_type_caster_source_file_line<
template <typename IntrinsicType, typename TypeCasterType> template <typename IntrinsicType, typename TypeCasterType>
struct type_caster_odr_guard : TypeCasterType { struct type_caster_odr_guard : TypeCasterType {
static tu_local_no_data_always_false translation_unit_local; static tu_local_no_data_always_false translation_unit_local;
type_caster_odr_guard() {
if (translation_unit_local) {
}
}
template <typename CType, typename... Arg>
static handle cast(CType &&src, return_value_policy policy, handle parent, Arg &&...arg) {
if (translation_unit_local) {
}
return TypeCasterType::cast(
std::forward<CType>(src), policy, parent, std::forward<Arg>(arg)...);
}
}; };
template <typename IntrinsicType, typename TypeCasterType> template <typename IntrinsicType, typename TypeCasterType>
@ -163,8 +176,6 @@ PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
static constexpr auto source_file_line \ static constexpr auto source_file_line \
= ::pybind11::detail::tu_local_const_name(__FILE__ ":" PYBIND11_TOSTRING(__LINE__)); = ::pybind11::detail::tu_local_const_name(__FILE__ ":" PYBIND11_TOSTRING(__LINE__));
# define PYBIND11_DETAIL_TYPE_CASTER_ACCESS_TRANSLATION_UNIT_LOCAL(...) \ # define PYBIND11_DETAIL_TYPE_CASTER_ACCESS_TRANSLATION_UNIT_LOCAL(...)
if (::pybind11::detail::make_caster<__VA_ARGS__>::translation_unit_local) { \
}
#endif #endif