diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index f9539843b..992a90f84 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -47,22 +47,24 @@ class type_caster_for_class_ : public type_caster_base {}; template class type_caster : public type_caster_for_class_ {}; -inline std::unordered_map &odr_guard_registry() { - static std::unordered_map reg; +inline std::unordered_map &odr_guard_registry() { + static std::unordered_map reg; return reg; } namespace { template -bool odr_guard_impl(const std::type_index &it_ti, const std::uint64_t &tc_id) { - printf("\nLOOOK %s %llu\n", type_id().c_str(), (long long) tc_id); +bool odr_guard_impl(const std::type_index &it_ti, const char *tc_id) { + printf("\nLOOOK %s %s\n", type_id().c_str(), tc_id); fflush(stdout); - auto [reg_iter, added] = odr_guard_registry().insert({it_ti, tc_id}); - if (!added && reg_iter->second != tc_id) { + std::string tc_id_str{tc_id}; + auto [reg_iter, added] = odr_guard_registry().insert({it_ti, tc_id_str}); + if (!added && reg_iter->second != tc_id_str) { throw std::system_error(std::make_error_code(std::errc::state_not_recoverable), "pybind11::detail::type_caster<" + type_id() - + "> ODR VIOLATION DETECTED"); + + "> ODR VIOLATION DETECTED: Location1=\"" + reg_iter->second + + "\", Location2=\"" + tc_id_str + "\""); } return true; } @@ -75,7 +77,7 @@ 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::universally_unique_identifier.value); + type_caster::source_file_line.text); return 0; }(); @@ -125,7 +127,7 @@ private: public: bool load(handle src, bool convert) { return subcaster.load(src, convert); } static constexpr auto name = caster_t::name; - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) + PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE static handle cast(const std::reference_wrapper &src, return_value_policy policy, handle parent) { // It is definitely wrong to take ownership of this pointer, so mask that rvp @@ -146,6 +148,8 @@ protected: \ public: \ static constexpr auto name = py_name; \ + static constexpr auto source_file_line \ + = ::pybind11::detail::tu_local_const_name(__FILE__ ":" PYBIND11_TOSTRING(__LINE__)); \ template >::value, \ @@ -297,7 +301,6 @@ public: } PYBIND11_TYPE_CASTER(T, const_name::value>("int", "float")); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) }; template @@ -313,7 +316,6 @@ public: return none().inc_ref(); } PYBIND11_TYPE_CASTER(T, const_name("None")); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) }; template <> @@ -361,7 +363,7 @@ public: using cast_op_type = void *&; explicit operator void *&() { return value; } static constexpr auto name = const_name("capsule"); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) + PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE private: void *value = nullptr; @@ -418,7 +420,6 @@ public: return handle(src ? Py_True : Py_False).inc_ref(); } PYBIND11_TYPE_CASTER(bool, const_name("bool")); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) }; // Helper class for UTF-{8,16,32} C++ stl strings: @@ -511,7 +512,6 @@ struct string_caster { } PYBIND11_TYPE_CASTER(StringType, const_name(PYBIND11_STRING_NAME)); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) private: static handle decode_utfN(const char *buffer, ssize_t nbytes) { @@ -684,7 +684,7 @@ public: } static constexpr auto name = const_name(PYBIND11_STRING_NAME); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) + PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE template using cast_op_type = pybind11::detail::cast_op_type<_T>; }; @@ -729,7 +729,7 @@ public: static constexpr auto name = const_name("Tuple[") + concat(make_caster::name...) + const_name("]"); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) + PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE template using cast_op_type = type; @@ -902,7 +902,7 @@ struct move_only_holder_caster { return type_caster_base::cast_holder(ptr, std::addressof(src)); } static constexpr auto name = type_caster_base::name; - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) + PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE }; #ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT @@ -1012,7 +1012,6 @@ struct pyobject_caster { return src.inc_ref(); } PYBIND11_TYPE_CASTER(type, handle_type_name::name); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(3434) }; template diff --git a/include/pybind11/chrono.h b/include/pybind11/chrono.h index 971802846..167ea0e3d 100644 --- a/include/pybind11/chrono.h +++ b/include/pybind11/chrono.h @@ -97,7 +97,6 @@ public: } PYBIND11_TYPE_CASTER(type, const_name("datetime.timedelta")); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) }; inline std::tm *localtime_thread_safe(const std::time_t *time, std::tm *buf) { @@ -209,7 +208,6 @@ public: us.count()); } PYBIND11_TYPE_CASTER(type, const_name("datetime.datetime")); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) }; // Other clocks that are not the system clock are not measured as datetime.datetime objects diff --git a/include/pybind11/complex.h b/include/pybind11/complex.h index 27cead1cc..8a831c12c 100644 --- a/include/pybind11/complex.h +++ b/include/pybind11/complex.h @@ -69,7 +69,6 @@ public: } PYBIND11_TYPE_CASTER(std::complex, const_name("complex")); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) }; PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/detail/init.h b/include/pybind11/detail/init.h index dee0b04b7..c95f5abb1 100644 --- a/include/pybind11/detail/init.h +++ b/include/pybind11/detail/init.h @@ -27,7 +27,7 @@ public: using cast_op_type = value_and_holder &; explicit operator value_and_holder &() { return *value; } static constexpr auto name = const_name(); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) + PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE private: value_and_holder *value = nullptr; diff --git a/include/pybind11/detail/smart_holder_type_casters.h b/include/pybind11/detail/smart_holder_type_casters.h index b8152aa2a..95346cd08 100644 --- a/include/pybind11/detail/smart_holder_type_casters.h +++ b/include/pybind11/detail/smart_holder_type_casters.h @@ -616,7 +616,7 @@ template struct smart_holder_type_caster : smart_holder_type_caster_load, smart_holder_type_caster_class_hooks { static constexpr auto name = const_name(); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) + PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE // static handle cast(T, ...) // is redundant (leads to ambiguous overloads). @@ -778,7 +778,7 @@ template struct smart_holder_type_caster> : smart_holder_type_caster_load, smart_holder_type_caster_class_hooks { static constexpr auto name = const_name(); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) + PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE static handle cast(const std::shared_ptr &src, return_value_policy policy, handle parent) { switch (policy) { @@ -843,7 +843,7 @@ template struct smart_holder_type_caster> : smart_holder_type_caster_load, smart_holder_type_caster_class_hooks { static constexpr auto name = const_name(); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) + PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE static handle cast(const std::shared_ptr &src, return_value_policy policy, handle parent) { @@ -864,7 +864,7 @@ template struct smart_holder_type_caster> : smart_holder_type_caster_load, smart_holder_type_caster_class_hooks { static constexpr auto name = const_name(); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) + PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE static handle cast(std::unique_ptr &&src, return_value_policy policy, handle parent) { if (policy != return_value_policy::automatic @@ -948,7 +948,7 @@ template struct smart_holder_type_caster> : smart_holder_type_caster_load, smart_holder_type_caster_class_hooks { static constexpr auto name = const_name(); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) + PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE static handle cast(std::unique_ptr &&src, return_value_policy policy, handle parent) { diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index 2629fd709..d178a5000 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -905,15 +905,24 @@ struct polymorphic_type_hook : public polymorphic_type_hook_base {}; PYBIND11_NAMESPACE_BEGIN(detail) namespace { -template -struct universally_unique_identifier_holder { - static constexpr std::uint64_t value = Value; + +template +struct tu_local_descr : descr { + using descr_t = descr; + using descr_t::descr_t; }; + +template +constexpr tu_local_descr tu_local_const_name(char const (&text)[N]) { + return tu_local_descr(text); +} +constexpr tu_local_descr<0> tu_local_const_name(char const (&)[1]) { return {}; } + } // namespace -#define PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(...) \ - static constexpr ::pybind11::detail::universally_unique_identifier_holder<__VA_ARGS__> \ - universally_unique_identifier; +#define PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE \ + static constexpr auto source_file_line \ + = ::pybind11::detail::tu_local_const_name(__FILE__ ":" PYBIND11_TOSTRING(__LINE__)); /// Generic type caster for objects stored on the heap template @@ -922,7 +931,7 @@ class type_caster_base : public type_caster_generic { public: static constexpr auto name = const_name(); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) + PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE type_caster_base() : type_caster_base(typeid(type)) {} explicit type_caster_base(const std::type_info &info) : type_caster_generic(info) {} diff --git a/include/pybind11/eigen.h b/include/pybind11/eigen.h index b5096b5c7..93eb6ee0a 100644 --- a/include/pybind11/eigen.h +++ b/include/pybind11/eigen.h @@ -392,7 +392,7 @@ public: } static constexpr auto name = props::descriptor; - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) + PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE // NOLINTNEXTLINE(google-explicit-constructor) operator Type *() { return &value; } @@ -437,7 +437,7 @@ public: } static constexpr auto name = props::descriptor; - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) + PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE // Explicitly delete these: support python -> C++ conversion on these (i.e. these can be return // types but not bound arguments). We still provide them (with an explicitly delete) so that @@ -625,7 +625,7 @@ public: } static constexpr auto name = props::descriptor; - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) + PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE // Explicitly delete these: support python -> C++ conversion on these (i.e. these can be return // types but not bound arguments). We still provide them (with an explicitly delete) so that @@ -702,7 +702,6 @@ struct type_caster::value>> { const_name<(Type::IsRowMajor) != 0>("scipy.sparse.csr_matrix[", "scipy.sparse.csc_matrix[") + npy_format_descriptor::name + const_name("]")); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) }; PYBIND11_NAMESPACE_END(detail) diff --git a/include/pybind11/functional.h b/include/pybind11/functional.h index 5fc5fb317..4034990d8 100644 --- a/include/pybind11/functional.h +++ b/include/pybind11/functional.h @@ -124,7 +124,6 @@ public: const_name("Callable[[") + concat(make_caster::name...) + const_name("], ") + make_caster::name + const_name("]")); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) }; PYBIND11_NAMESPACE_END(detail) diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index de74f8023..0291b02d0 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -1214,7 +1214,6 @@ struct pyobject_caster> { return src.inc_ref(); } PYBIND11_TYPE_CASTER(type, handle_type_name::name); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) }; template diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 8196f2b50..6fe0bea37 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -87,7 +87,6 @@ struct set_caster { } PYBIND11_TYPE_CASTER(type, const_name("Set[") + key_conv::name + const_name("]")); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) }; template @@ -137,7 +136,6 @@ struct map_caster { PYBIND11_TYPE_CASTER(Type, const_name("Dict[") + key_conv::name + const_name(", ") + value_conv::name + const_name("]")); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) }; template @@ -190,7 +188,6 @@ public: } PYBIND11_TYPE_CASTER(Type, const_name("List[") + value_conv::name + const_name("]")); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) }; template @@ -260,7 +257,6 @@ public: const_name("[") + const_name() + const_name("]")) + const_name("]")); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) }; template @@ -319,7 +315,6 @@ struct optional_caster { } PYBIND11_TYPE_CASTER(Type, const_name("Optional[") + value_conv::name + const_name("]")); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) }; #if defined(PYBIND11_HAS_OPTIONAL) @@ -407,7 +402,6 @@ struct variant_caster> { PYBIND11_TYPE_CASTER(Type, const_name("Union[") + detail::concat(make_caster::name...) + const_name("]")); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) }; #if defined(PYBIND11_HAS_VARIANT) diff --git a/include/pybind11/stl/filesystem.h b/include/pybind11/stl/filesystem.h index fff90cf36..e26f42177 100644 --- a/include/pybind11/stl/filesystem.h +++ b/include/pybind11/stl/filesystem.h @@ -99,7 +99,6 @@ public: } PYBIND11_TYPE_CASTER(T, const_name("os.PathLike")); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) }; #endif // PYBIND11_HAS_FILESYSTEM || defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM) diff --git a/tests/pybind11_tests.h b/tests/pybind11_tests.h index 5ed373d6f..a7c00c2f9 100644 --- a/tests/pybind11_tests.h +++ b/tests/pybind11_tests.h @@ -61,7 +61,6 @@ template <> class type_caster { public: PYBIND11_TYPE_CASTER(RValueCaster, const_name("RValueCaster")); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) static handle cast(RValueCaster &&, return_value_policy, handle) { return py::str("rvalue").release(); } diff --git a/tests/test_builtin_casters.cpp b/tests/test_builtin_casters.cpp index 156880c1f..654e7f7f1 100644 --- a/tests/test_builtin_casters.cpp +++ b/tests/test_builtin_casters.cpp @@ -28,7 +28,7 @@ template <> class type_caster { public: static constexpr auto name = const_name(); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) + PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE // Input is unimportant, a new value will always be constructed based on the // cast operator. diff --git a/tests/test_copy_move.cpp b/tests/test_copy_move.cpp index 4c6ac2c74..82e9eec29 100644 --- a/tests/test_copy_move.cpp +++ b/tests/test_copy_move.cpp @@ -106,7 +106,6 @@ PYBIND11_NAMESPACE_BEGIN(detail) template <> struct type_caster { PYBIND11_TYPE_CASTER(MoveOnlyInt, const_name("MoveOnlyInt")); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) bool load(handle src, bool) { value = MoveOnlyInt(src.cast()); return true; @@ -119,7 +118,6 @@ struct type_caster { template <> struct type_caster { PYBIND11_TYPE_CASTER(MoveOrCopyInt, const_name("MoveOrCopyInt")); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) bool load(handle src, bool) { value = MoveOrCopyInt(src.cast()); return true; @@ -136,7 +134,7 @@ protected: public: static constexpr auto name = const_name("CopyOnlyInt"); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) + PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE bool load(handle src, bool) { value = CopyOnlyInt(src.cast()); return true; diff --git a/tests/test_custom_type_casters.cpp b/tests/test_custom_type_casters.cpp index 8a3b32ebc..25540e368 100644 --- a/tests/test_custom_type_casters.cpp +++ b/tests/test_custom_type_casters.cpp @@ -32,7 +32,6 @@ public: #else PYBIND11_TYPE_CASTER(ArgInspector1, const_name("ArgInspector1")); #endif - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) bool load(handle src, bool convert) { value.arg = "loading ArgInspector1 argument " + std::string(convert ? "WITH" : "WITHOUT") @@ -50,7 +49,6 @@ template <> struct type_caster { public: PYBIND11_TYPE_CASTER(ArgInspector2, const_name("ArgInspector2")); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) bool load(handle src, bool convert) { value.arg = "loading ArgInspector2 argument " + std::string(convert ? "WITH" : "WITHOUT") @@ -68,7 +66,6 @@ template <> struct type_caster { public: PYBIND11_TYPE_CASTER(ArgAlwaysConverts, const_name("ArgAlwaysConverts")); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) bool load(handle, bool convert) { return convert; } @@ -100,7 +97,6 @@ namespace detail { template <> struct type_caster { PYBIND11_TYPE_CASTER(DestructionTester, const_name("DestructionTester")); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) bool load(handle, bool) { return true; } static handle cast(const DestructionTester &, return_value_policy, handle) { @@ -123,7 +119,6 @@ namespace py_ = ::pybind11; // don't have any symbol collision when using macro mixin. struct my_caster { PYBIND11_TYPE_CASTER(MyType, py_::detail::const_name("MyType")); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597) bool load(py_::handle, bool) { return true; } static py_::handle cast(const MyType &, py_::return_value_policy, py_::handle) { diff --git a/tests/test_odr_guard_1.cpp b/tests/test_odr_guard_1.cpp index bb55eec32..e56bbabbf 100644 --- a/tests/test_odr_guard_1.cpp +++ b/tests/test_odr_guard_1.cpp @@ -8,7 +8,7 @@ struct type_mrc { struct minimal_real_caster { static constexpr auto name = py::detail::const_name(); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1000) + PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE static py::handle cast(type_mrc const &src, py::return_value_policy /*policy*/, py::handle /*parent*/) { diff --git a/tests/test_odr_guard_1.py b/tests/test_odr_guard_1.py index 398b4ce52..3f0f9ebd4 100644 --- a/tests/test_odr_guard_1.py +++ b/tests/test_odr_guard_1.py @@ -1,5 +1,3 @@ -import pytest - import pybind11_tests.odr_guard_1 as m diff --git a/tests/test_odr_guard_2.cpp b/tests/test_odr_guard_2.cpp index c67d99a5e..f61ce0a10 100644 --- a/tests/test_odr_guard_2.cpp +++ b/tests/test_odr_guard_2.cpp @@ -8,7 +8,7 @@ struct type_mrc { struct minimal_real_caster { static constexpr auto name = py::detail::const_name(); - PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(2000) + PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE static py::handle cast(type_mrc const &src, py::return_value_policy /*policy*/, py::handle /*parent*/) { diff --git a/tests/test_odr_guard_2.py b/tests/test_odr_guard_2.py index 21719ce19..f294da3e6 100644 --- a/tests/test_odr_guard_2.py +++ b/tests/test_odr_guard_2.py @@ -1,5 +1,3 @@ -import pytest - import pybind11_tests.odr_guard_2 as m