From 6a6eb6cac8ed376d3c707231c4f460561f0d4c89 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 21 Jun 2022 19:22:28 -0700 Subject: [PATCH] define PYBIND11_DETAIL_ODR_GUARD_IMPL_THROW_DISABLED true in test_odr_guard_1,2.cpp --- include/pybind11/cast.h | 6 ++--- include/pybind11/detail/common.h | 1 - .../pybind11/detail/type_caster_odr_guard.h | 22 +++++++++++-------- tests/test_odr_guard_1.cpp | 1 + tests/test_odr_guard_2.cpp | 1 + 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 8db22b844..0ad19e6ea 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -25,11 +25,8 @@ #include #include #include -#include #include #include -#include -#include #include #include @@ -60,7 +57,8 @@ struct type_caster_odr_guard : type_caster { template int type_caster_odr_guard::translation_unit_local = []() { odr_guard_impl(std::type_index(typeid(IntrinsicType)), - type_caster::source_file_line.text); + type_caster::source_file_line.text, + PYBIND11_DETAIL_ODR_GUARD_IMPL_THROW_DISABLED); return 0; }(); diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index b815f8e7d..7a2aaeae1 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -302,7 +302,6 @@ #define PYBIND11_TRY_NEXT_OVERLOAD ((PyObject *) 1) // special failure return code #define PYBIND11_STRINGIFY(x) #x #define PYBIND11_TOSTRING(x) PYBIND11_STRINGIFY(x) -#define PYBIND11_SOURCE_FILE_LINE __FILE__ ":" PYBIND11_TOSTRING(__LINE__) #define PYBIND11_CONCAT(first, second) first##second #define PYBIND11_ENSURE_INTERNALS_READY pybind11::detail::get_internals(); diff --git a/include/pybind11/detail/type_caster_odr_guard.h b/include/pybind11/detail/type_caster_odr_guard.h index f0a42bccd..0b9ad545d 100644 --- a/include/pybind11/detail/type_caster_odr_guard.h +++ b/include/pybind11/detail/type_caster_odr_guard.h @@ -55,8 +55,14 @@ inline const char *source_file_line_basename(const char *sfl) { return sfl + i_base; } +# ifndef PYBIND11_DETAIL_ODR_GUARD_IMPL_THROW_DISABLED +# define PYBIND11_DETAIL_ODR_GUARD_IMPL_THROW_DISABLED false +# endif + template -bool odr_guard_impl(const std::type_index &it_ti, const char *source_file_line) { +void odr_guard_impl(const std::type_index &it_ti, + const char *source_file_line, + bool throw_disabled) { // std::cout cannot be used here: static initialization could be incomplete. # define PYBIND11_DETAIL_ODR_GUARD_IMPL_PRINTF_OFF # ifdef PYBIND11_DETAIL_ODR_GUARD_IMPL_PRINTF_ON @@ -77,15 +83,13 @@ bool odr_guard_impl(const std::type_index &it_ti, const char *source_file_line) + type_id() + ">: SourceLocation1=\"" + reg_iter->second + "\", SourceLocation2=\"" + source_file_line + "\""); -# define PYBIND11_TYPE_CASTER_ODR_GUARD_THROW_OFF -# ifdef PYBIND11_TYPE_CASTER_ODR_GUARD_THROW_ON - throw err; -# else - fprintf(stderr, "\nDISABLED std::system_error: %s\n", err.what()); - fflush(stderr); -# endif + if (throw_disabled) { + fprintf(stderr, "\nDISABLED std::system_error: %s\n", err.what()); + fflush(stderr); + } else { + throw err; + } } - return true; } namespace { diff --git a/tests/test_odr_guard_1.cpp b/tests/test_odr_guard_1.cpp index c76389064..77c8265ce 100644 --- a/tests/test_odr_guard_1.cpp +++ b/tests/test_odr_guard_1.cpp @@ -1,3 +1,4 @@ +#define PYBIND11_DETAIL_ODR_GUARD_IMPL_THROW_DISABLED true #include "pybind11_tests.h" namespace mrc_ns { // minimal real caster diff --git a/tests/test_odr_guard_2.cpp b/tests/test_odr_guard_2.cpp index 1200c8e0d..3c14f4681 100644 --- a/tests/test_odr_guard_2.cpp +++ b/tests/test_odr_guard_2.cpp @@ -1,3 +1,4 @@ +#define PYBIND11_DETAIL_ODR_GUARD_IMPL_THROW_DISABLED true #include "pybind11_tests.h" namespace mrc_ns { // minimal real caster