Add test_type_caster_odr_registry_values(), test_type_caster_odr_violation_detected_counter()

This commit is contained in:
Ralf W. Grosse-Kunstleve 2022-06-22 12:37:55 -07:00
parent d2bafaeba7
commit 1263ce9ee2
3 changed files with 45 additions and 3 deletions

View File

@ -41,11 +41,16 @@
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
PYBIND11_NAMESPACE_BEGIN(detail)
inline std::unordered_map<std::type_index, std::string> &odr_guard_registry() {
inline std::unordered_map<std::type_index, std::string> &type_caster_odr_guard_registry() {
static std::unordered_map<std::type_index, std::string> reg;
return reg;
}
inline unsigned &type_caster_odr_violation_detected_counter() {
static unsigned counter = 0;
return counter;
}
inline const char *source_file_line_basename(const char *sfl) {
unsigned i_base = 0;
for (unsigned i = 0; sfl[i] != '\0'; i++) {
@ -72,8 +77,8 @@ inline void type_caster_odr_guard_impl(const std::type_info &intrinsic_type_info
source_file_line);
std::fflush(stdout);
# endif
auto ins
= odr_guard_registry().insert({std::type_index(intrinsic_type_info), source_file_line});
auto ins = type_caster_odr_guard_registry().insert(
{std::type_index(intrinsic_type_info), source_file_line});
auto reg_iter = ins.first;
auto added = ins.second;
if (!added
@ -86,6 +91,7 @@ inline void type_caster_odr_guard_impl(const std::type_info &intrinsic_type_info
if (throw_disabled) {
std::fprintf(stderr, "\nDISABLED std::system_error: %s\n", msg.c_str());
std::fflush(stderr);
type_caster_odr_violation_detected_counter()++;
} else {
throw std::system_error(std::make_error_code(std::errc::state_not_recoverable), msg);
}

View File

@ -46,4 +46,22 @@ struct type_caster<mrc_ns::type_mrc> : mrc_ns::minimal_real_caster {};
TEST_SUBMODULE(odr_guard_1, m) {
m.def("type_mrc_to_python", []() { return mrc_ns::type_mrc(101); });
m.def("type_mrc_from_python", [](const mrc_ns::type_mrc &obj) { return obj.value + 100; });
m.def("type_caster_odr_guard_registry_values", []() {
#ifdef PYBIND11_TYPE_CASTER_ODR_GUARD_ON
py::list values;
for (auto reg_iter : py::detail::type_caster_odr_guard_registry()) {
values.append(py::str(reg_iter.second));
}
return values;
#else
return py::none();
#endif
});
m.def("type_caster_odr_violation_detected_count", []() {
#ifdef PYBIND11_TYPE_CASTER_ODR_GUARD_ON
return py::detail::type_caster_odr_violation_detected_counter();
#else
return py::none();
#endif
});
}

View File

@ -1,3 +1,5 @@
import pytest
import pybind11_tests.odr_guard_1 as m
@ -7,3 +9,19 @@ def test_type_mrc_to_python():
def test_type_mrc_from_python():
assert m.type_mrc_from_python("ignored") == 111
def test_type_caster_odr_registry_values():
reg_values = m.type_caster_odr_guard_registry_values()
if reg_values is None:
pytest.skip("type_caster_odr_guard_registry_values() is None")
else:
assert "test_type_caster_odr_guard_" in "\n".join(reg_values)
def test_type_caster_odr_violation_detected_counter():
num_violations = m.type_caster_odr_violation_detected_count()
if num_violations is None:
pytest.skip("type_caster_odr_violation_detected_count() is None")
else:
assert num_violations == 1