Replace PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER with PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE, baked into PYBIND11_TYPE_CASTER macro.

This commit is contained in:
Ralf W. Grosse-Kunstleve 2022-06-20 23:57:33 -07:00
parent e06518da7d
commit 3a95ae1165
19 changed files with 46 additions and 63 deletions

View File

@ -47,22 +47,24 @@ class type_caster_for_class_ : public type_caster_base<T> {};
template <typename type, typename SFINAE = void>
class type_caster : public type_caster_for_class_<type> {};
inline std::unordered_map<std::type_index, std::uint64_t> &odr_guard_registry() {
static std::unordered_map<std::type_index, std::uint64_t> reg;
inline std::unordered_map<std::type_index, std::string> &odr_guard_registry() {
static std::unordered_map<std::type_index, std::string> reg;
return reg;
}
namespace {
template <typename IntrinsicType>
bool odr_guard_impl(const std::type_index &it_ti, const std::uint64_t &tc_id) {
printf("\nLOOOK %s %llu\n", type_id<IntrinsicType>().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<IntrinsicType>().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<IntrinsicType>()
+ "> 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<IntrinsicType> {
template <typename IntrinsicType>
int type_caster_odr_guard<IntrinsicType>::translation_unit_local = []() {
odr_guard_impl<IntrinsicType>(std::type_index(typeid(IntrinsicType)),
type_caster<IntrinsicType>::universally_unique_identifier.value);
type_caster<IntrinsicType>::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<type> &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 <typename T_, \
::pybind11::detail::enable_if_t< \
std::is_same<type, ::pybind11::detail::remove_cv_t<T_>>::value, \
@ -297,7 +301,6 @@ public:
}
PYBIND11_TYPE_CASTER(T, const_name<std::is_integral<T>::value>("int", "float"));
PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597)
};
template <typename T>
@ -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 <typename _T>
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<Ts>::name...) + const_name("]");
PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597)
PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE
template <typename T>
using cast_op_type = type;
@ -902,7 +902,7 @@ struct move_only_holder_caster {
return type_caster_base<type>::cast_holder(ptr, std::addressof(src));
}
static constexpr auto name = type_caster_base<type>::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<type>::name);
PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(3434)
};
template <typename T>

View File

@ -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

View File

@ -69,7 +69,6 @@ public:
}
PYBIND11_TYPE_CASTER(std::complex<T>, const_name("complex"));
PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597)
};
PYBIND11_NAMESPACE_END(detail)
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)

View File

@ -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<value_and_holder>();
PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597)
PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE
private:
value_and_holder *value = nullptr;

View File

@ -616,7 +616,7 @@ template <typename T>
struct smart_holder_type_caster : smart_holder_type_caster_load<T>,
smart_holder_type_caster_class_hooks {
static constexpr auto name = const_name<T>();
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 <typename T>
struct smart_holder_type_caster<std::shared_ptr<T>> : smart_holder_type_caster_load<T>,
smart_holder_type_caster_class_hooks {
static constexpr auto name = const_name<T>();
PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597)
PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE
static handle cast(const std::shared_ptr<T> &src, return_value_policy policy, handle parent) {
switch (policy) {
@ -843,7 +843,7 @@ template <typename T>
struct smart_holder_type_caster<std::shared_ptr<T const>> : smart_holder_type_caster_load<T>,
smart_holder_type_caster_class_hooks {
static constexpr auto name = const_name<T>();
PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597)
PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE
static handle
cast(const std::shared_ptr<T const> &src, return_value_policy policy, handle parent) {
@ -864,7 +864,7 @@ template <typename T, typename D>
struct smart_holder_type_caster<std::unique_ptr<T, D>> : smart_holder_type_caster_load<T>,
smart_holder_type_caster_class_hooks {
static constexpr auto name = const_name<T>();
PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597)
PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE
static handle cast(std::unique_ptr<T, D> &&src, return_value_policy policy, handle parent) {
if (policy != return_value_policy::automatic
@ -948,7 +948,7 @@ template <typename T, typename D>
struct smart_holder_type_caster<std::unique_ptr<T const, D>>
: smart_holder_type_caster_load<T>, smart_holder_type_caster_class_hooks {
static constexpr auto name = const_name<T>();
PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597)
PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE
static handle
cast(std::unique_ptr<T const, D> &&src, return_value_policy policy, handle parent) {

View File

@ -905,15 +905,24 @@ struct polymorphic_type_hook : public polymorphic_type_hook_base<itype> {};
PYBIND11_NAMESPACE_BEGIN(detail)
namespace {
template <std::uint64_t Value>
struct universally_unique_identifier_holder {
static constexpr std::uint64_t value = Value;
template <size_t N, typename... Ts>
struct tu_local_descr : descr<N, Ts...> {
using descr_t = descr<N, Ts...>;
using descr_t::descr_t;
};
template <size_t N>
constexpr tu_local_descr<N - 1> tu_local_const_name(char const (&text)[N]) {
return tu_local_descr<N - 1>(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 <typename type>
@ -922,7 +931,7 @@ class type_caster_base : public type_caster_generic {
public:
static constexpr auto name = const_name<type>();
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) {}

View File

@ -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<Type, enable_if_t<is_eigen_sparse<Type>::value>> {
const_name<(Type::IsRowMajor) != 0>("scipy.sparse.csr_matrix[",
"scipy.sparse.csc_matrix[")
+ npy_format_descriptor<Scalar>::name + const_name("]"));
PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597)
};
PYBIND11_NAMESPACE_END(detail)

View File

@ -124,7 +124,6 @@ public:
const_name("Callable[[") + concat(make_caster<Args>::name...)
+ const_name("], ") + make_caster<retval_type>::name
+ const_name("]"));
PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597)
};
PYBIND11_NAMESPACE_END(detail)

View File

@ -1214,7 +1214,6 @@ struct pyobject_caster<array_t<T, ExtraFlags>> {
return src.inc_ref();
}
PYBIND11_TYPE_CASTER(type, handle_type_name<type>::name);
PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597)
};
template <typename T>

View File

@ -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 <typename Type, typename Key, typename Value>
@ -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 <typename Type, typename Value>
@ -190,7 +188,6 @@ public:
}
PYBIND11_TYPE_CASTER(Type, const_name("List[") + value_conv::name + const_name("]"));
PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597)
};
template <typename Type, typename Alloc>
@ -260,7 +257,6 @@ public:
const_name("[") + const_name<Size>()
+ const_name("]"))
+ const_name("]"));
PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597)
};
template <typename Type, size_t Size>
@ -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<V<Ts...>> {
PYBIND11_TYPE_CASTER(Type,
const_name("Union[") + detail::concat(make_caster<Ts>::name...)
+ const_name("]"));
PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597)
};
#if defined(PYBIND11_HAS_VARIANT)

View File

@ -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)

View File

@ -61,7 +61,6 @@ template <>
class type_caster<RValueCaster> {
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();
}

View File

@ -28,7 +28,7 @@ template <>
class type_caster<ConstRefCasted> {
public:
static constexpr auto name = const_name<ConstRefCasted>();
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.

View File

@ -106,7 +106,6 @@ PYBIND11_NAMESPACE_BEGIN(detail)
template <>
struct type_caster<MoveOnlyInt> {
PYBIND11_TYPE_CASTER(MoveOnlyInt, const_name("MoveOnlyInt"));
PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597)
bool load(handle src, bool) {
value = MoveOnlyInt(src.cast<int>());
return true;
@ -119,7 +118,6 @@ struct type_caster<MoveOnlyInt> {
template <>
struct type_caster<MoveOrCopyInt> {
PYBIND11_TYPE_CASTER(MoveOrCopyInt, const_name("MoveOrCopyInt"));
PYBIND11_TYPE_CASTER_UNIQUE_IDENTIFIER(1655073597)
bool load(handle src, bool) {
value = MoveOrCopyInt(src.cast<int>());
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<int>());
return true;

View File

@ -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<ArgInspector2> {
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<ArgAlwaysConverts> {
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<DestructionTester> {
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) {

View File

@ -8,7 +8,7 @@ struct type_mrc {
struct minimal_real_caster {
static constexpr auto name = py::detail::const_name<type_mrc>();
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*/) {

View File

@ -1,5 +1,3 @@
import pytest
import pybind11_tests.odr_guard_1 as m

View File

@ -8,7 +8,7 @@ struct type_mrc {
struct minimal_real_caster {
static constexpr auto name = py::detail::const_name<type_mrc>();
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*/) {

View File

@ -1,5 +1,3 @@
import pytest
import pybind11_tests.odr_guard_2 as m