VERY MESSY SNAPSHOT of WIP, this was the starting point for cl/454658864, which has more changes on top.

This commit is contained in:
Ralf W. Grosse-Kunstleve 2022-06-14 12:22:11 -07:00
parent d1960a18cf
commit 509506955b
19 changed files with 144 additions and 41 deletions

View File

@ -47,30 +47,60 @@ 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::type_index> &odr_guard_registry() {
static std::unordered_map<std::type_index, std::type_index> reg;
inline std::unordered_map<std::type_index, std::uint64_t> &odr_guard_registry() {
static std::unordered_map<std::type_index, std::uint64_t> reg;
return reg;
}
namespace {
template <typename IntrinsicType>
bool odr_guard_impl(const std::type_index & it_ti, const std::uint64_t& tc_id) {
auto match = odr_guard_registry().find(it_ti);
printf("\nLOOOK %s %llu\n", type_id<IntrinsicType>().c_str(), (long long) tc_id);
fflush(stdout);
if (match == odr_guard_registry().end()) {
odr_guard_registry().insert({it_ti, tc_id});
} else if (match->second != tc_id) {
throw std::system_error(std::make_error_code(std::errc::state_not_recoverable),
"pybind11::detail::type_caster<" + type_id<IntrinsicType>()
+ "> ODR VIOLATION DETECTED");
}
return true;
}
template <typename IntrinsicType>
struct type_caster_odr_guard : type_caster<IntrinsicType> {
type_caster_odr_guard() {
auto it_ti = std::type_index(typeid(IntrinsicType));
auto tc_ti = std::type_index(typeid(type_caster<IntrinsicType>));
auto match = odr_guard_registry().find(it_ti);
if (match == odr_guard_registry().end()) {
odr_guard_registry().insert({it_ti, tc_ti});
} else if (match->second != tc_ti) {
throw std::system_error(std::make_error_code(std::errc::state_not_recoverable),
"pybind11::detail::type_caster<" + type_id<IntrinsicType>()
+ "> ODR VIOLATION DETECTED");
}
odr_guard_hook = !!odr_guard_hook;
}
// type_caster_odr_guard(const type_caster_odr_guard &) = default;
// type_caster_odr_guard(type_caster_odr_guard &&) = default;
template <typename CType, typename... Arg>
static handle cast(CType &&src, return_value_policy policy, handle parent,
Arg &&...arg) {
odr_guard_hook = !!odr_guard_hook;
return type_caster<IntrinsicType>::cast(std::forward<CType>(src), policy, parent,
std::forward<Arg>(arg)...);
}
static bool odr_guard_hook;
};
template <typename IntrinsicType>
bool type_caster_odr_guard<IntrinsicType>::odr_guard_hook = [](){
return odr_guard_impl<IntrinsicType>(
std::type_index(typeid(IntrinsicType)),
type_caster<IntrinsicType>::universally_unique_identifier);
}();
template <typename type>
using make_caster = type_caster_odr_guard<intrinsic_t<type>>;
} // namespace
template <typename T>
struct type_uses_smart_holder_type_caster {
static constexpr bool value
@ -106,6 +136,7 @@ private:
public:
bool load(handle src, bool convert) { return subcaster.load(src, convert); }
static constexpr auto name = caster_t::name;
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
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
@ -277,6 +308,7 @@ public:
}
PYBIND11_TYPE_CASTER(T, const_name<std::is_integral<T>::value>("int", "float"));
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
};
template <typename T>
@ -292,6 +324,7 @@ public:
return none().inc_ref();
}
PYBIND11_TYPE_CASTER(T, const_name("None"));
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
};
template <>
@ -339,6 +372,7 @@ public:
using cast_op_type = void *&;
explicit operator void *&() { return value; }
static constexpr auto name = const_name("capsule");
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
private:
void *value = nullptr;
@ -395,6 +429,7 @@ public:
return handle(src ? Py_True : Py_False).inc_ref();
}
PYBIND11_TYPE_CASTER(bool, const_name("bool"));
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
};
// Helper class for UTF-{8,16,32} C++ stl strings:
@ -487,6 +522,7 @@ struct string_caster {
}
PYBIND11_TYPE_CASTER(StringType, const_name(PYBIND11_STRING_NAME));
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
private:
static handle decode_utfN(const char *buffer, ssize_t nbytes) {
@ -659,6 +695,7 @@ public:
}
static constexpr auto name = const_name(PYBIND11_STRING_NAME);
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
template <typename _T>
using cast_op_type = pybind11::detail::cast_op_type<_T>;
};
@ -703,6 +740,7 @@ public:
static constexpr auto name
= const_name("Tuple[") + concat(make_caster<Ts>::name...) + const_name("]");
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
template <typename T>
using cast_op_type = type;
@ -875,6 +913,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;
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
};
#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
@ -984,6 +1023,7 @@ struct pyobject_caster {
return src.inc_ref();
}
PYBIND11_TYPE_CASTER(type, handle_type_name<type>::name);
static constexpr std::uint64_t universally_unique_identifier = 3434;
};
template <typename T>

View File

@ -97,6 +97,7 @@ public:
}
PYBIND11_TYPE_CASTER(type, const_name("datetime.timedelta"));
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
};
inline std::tm *localtime_thread_safe(const std::time_t *time, std::tm *buf) {
@ -208,6 +209,7 @@ public:
us.count());
}
PYBIND11_TYPE_CASTER(type, const_name("datetime.datetime"));
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
};
// Other clocks that are not the system clock are not measured as datetime.datetime objects

View File

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

View File

@ -27,6 +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>();
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
private:
value_and_holder *value = nullptr;

View File

@ -616,6 +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>();
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
// static handle cast(T, ...)
// is redundant (leads to ambiguous overloads).
@ -777,6 +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>();
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
static handle cast(const std::shared_ptr<T> &src, return_value_policy policy, handle parent) {
switch (policy) {
@ -841,6 +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>();
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
static handle
cast(const std::shared_ptr<T const> &src, return_value_policy policy, handle parent) {
@ -861,6 +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>();
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
static handle cast(std::unique_ptr<T, D> &&src, return_value_policy policy, handle parent) {
if (policy != return_value_policy::automatic
@ -944,6 +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>();
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
static handle
cast(std::unique_ptr<T const, D> &&src, return_value_policy policy, handle parent) {

View File

@ -911,6 +911,7 @@ class type_caster_base : public type_caster_generic {
public:
static constexpr auto name = const_name<type>();
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
type_caster_base() : type_caster_base(typeid(type)) {}
explicit type_caster_base(const std::type_info &info) : type_caster_generic(info) {}

View File

@ -392,6 +392,7 @@ public:
}
static constexpr auto name = props::descriptor;
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
// NOLINTNEXTLINE(google-explicit-constructor)
operator Type *() { return &value; }
@ -436,6 +437,7 @@ public:
}
static constexpr auto name = props::descriptor;
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
// 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
@ -623,6 +625,7 @@ public:
}
static constexpr auto name = props::descriptor;
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
// 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
@ -699,6 +702,7 @@ 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("]"));
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
};
PYBIND11_NAMESPACE_END(detail)

View File

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

View File

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

View File

@ -87,6 +87,7 @@ struct set_caster {
}
PYBIND11_TYPE_CASTER(type, const_name("Set[") + key_conv::name + const_name("]"));
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
};
template <typename Type, typename Key, typename Value>
@ -136,6 +137,7 @@ struct map_caster {
PYBIND11_TYPE_CASTER(Type,
const_name("Dict[") + key_conv::name + const_name(", ") + value_conv::name
+ const_name("]"));
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
};
template <typename Type, typename Value>
@ -188,6 +190,7 @@ public:
}
PYBIND11_TYPE_CASTER(Type, const_name("List[") + value_conv::name + const_name("]"));
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
};
template <typename Type, typename Alloc>
@ -257,6 +260,7 @@ public:
const_name("[") + const_name<Size>()
+ const_name("]"))
+ const_name("]"));
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
};
template <typename Type, size_t Size>
@ -315,6 +319,7 @@ struct optional_caster {
}
PYBIND11_TYPE_CASTER(Type, const_name("Optional[") + value_conv::name + const_name("]"));
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
};
#if defined(PYBIND11_HAS_OPTIONAL)
@ -401,6 +406,7 @@ struct variant_caster<V<Ts...>> {
PYBIND11_TYPE_CASTER(Type,
const_name("Union[") + detail::concat(make_caster<Ts>::name...)
+ const_name("]"));
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
};
#if defined(PYBIND11_HAS_VARIANT)

View File

@ -99,6 +99,7 @@ public:
}
PYBIND11_TYPE_CASTER(T, const_name("os.PathLike"));
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
};
#endif // PYBIND11_HAS_FILESYSTEM || defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM)

View File

@ -61,6 +61,7 @@ template <>
class type_caster<RValueCaster> {
public:
PYBIND11_TYPE_CASTER(RValueCaster, const_name("RValueCaster"));
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
static handle cast(RValueCaster &&, return_value_policy, handle) {
return py::str("rvalue").release();
}

View File

@ -9,29 +9,32 @@
#include "pybind11_tests.h"
#define USE_MRC_AAA
#ifdef USE_MRC_AAA
namespace mrc_ns { // minimal real caster
struct minimal_real_caster;
template <typename ValType>
struct type_mrc {
int value = -9999;
};
template <typename CType>
struct minimal_real_caster {
static constexpr auto name = py::detail::const_name<type_mrc>();
static constexpr auto name = py::detail::const_name<CType>();
static constexpr std::uint64_t universally_unique_identifier = 1000000;
static py::handle
cast(type_mrc const &src, py::return_value_policy /*policy*/, py::handle /*parent*/) {
cast(CType const &src, py::return_value_policy /*policy*/, py::handle /*parent*/) {
return py::int_(src.value + 1010).release();
}
// Maximizing simplicity. This will go terribly wrong for other arg types.
template <typename>
using cast_op_type = const type_mrc &;
using cast_op_type = const CType &;
// NOLINTNEXTLINE(google-explicit-constructor)
operator type_mrc const &() {
static type_mrc obj;
operator CType const &() {
static CType obj;
obj.value = 11;
return obj;
}
@ -46,10 +49,11 @@ struct minimal_real_caster {
namespace pybind11 {
namespace detail {
template <>
struct type_caster<mrc_ns::type_mrc> : mrc_ns::minimal_real_caster {};
template <typename ValType>
struct type_caster<mrc_ns::type_mrc<ValType>> : mrc_ns::minimal_real_caster<mrc_ns::type_mrc<ValType>> {};
} // namespace detail
} // namespace pybind11
#endif
TEST_SUBMODULE(async_module, m) {
struct DoesNotSupportAsync {};
@ -64,6 +68,8 @@ TEST_SUBMODULE(async_module, m) {
f.attr("set_result")(5);
return f.attr("__await__")();
});
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; });
#ifdef USE_MRC_AAA
m.def("type_mrc_to_python", []() { return mrc_ns::type_mrc<int>{101}; });
m.def("type_mrc_from_python", [](const mrc_ns::type_mrc<int> &obj) { return obj.value + 100; });
#endif
}

View File

@ -24,6 +24,15 @@ def test_await_missing(event_loop):
event_loop.run_until_complete(get_await_result(m.DoesNotSupportAsync()))
def test_mrc():
assert m.type_mrc_to_python() == 1111
assert m.type_mrc_from_python("ignored") == 111
def test_type_mrc_to_python():
if hasattr(m, "type_mrc_to_python"):
assert m.type_mrc_to_python() == 1111
else:
pytype.skip("type_mrc_to_python")
def test_type_mrc_from_python():
if hasattr(m, "type_mrc_from_python"):
assert m.type_mrc_from_python("ignored") == 111
else:
pytype.skip("type_mrc_from_python")

View File

@ -12,29 +12,32 @@
#include "constructor_stats.h"
#include "pybind11_tests.h"
#define USE_MRC_BBB
#ifdef USE_MRC_BBB
namespace mrc_ns { // minimal real caster
struct minimal_real_caster;
template <typename ValType>
struct type_mrc {
int value = -9999;
ValType value = -9999;
};
template <typename CType>
struct minimal_real_caster {
static constexpr auto name = py::detail::const_name<type_mrc>();
static constexpr auto name = py::detail::const_name<CType>();
static constexpr std::uint64_t universally_unique_identifier = 2000000;
static py::handle
cast(type_mrc const &src, py::return_value_policy /*policy*/, py::handle /*parent*/) {
cast(CType const &src, py::return_value_policy /*policy*/, py::handle /*parent*/) {
return py::int_(src.value + 2020).release();
}
// Maximizing simplicity. This will go terribly wrong for other arg types.
template <typename>
using cast_op_type = const type_mrc &;
using cast_op_type = const CType &;
// NOLINTNEXTLINE(google-explicit-constructor)
operator type_mrc const &() {
static type_mrc obj;
operator CType const &() {
static CType obj;
obj.value = 22;
return obj;
}
@ -49,10 +52,11 @@ struct minimal_real_caster {
namespace pybind11 {
namespace detail {
template <>
struct type_caster<mrc_ns::type_mrc> : mrc_ns::minimal_real_caster {};
template <typename ValType>
struct type_caster<mrc_ns::type_mrc<ValType>> : mrc_ns::minimal_real_caster<mrc_ns::type_mrc<ValType>> {};
} // namespace detail
} // namespace pybind11
#endif
TEST_SUBMODULE(buffers, m) {
// test_from_python / test_to_python:
@ -264,6 +268,8 @@ TEST_SUBMODULE(buffers, m) {
m.def("get_buffer_info", [](const py::buffer &buffer) { return buffer.request(); });
m.def("type_mrc_to_python", []() { return mrc_ns::type_mrc{202}; });
m.def("type_mrc_from_python", [](const mrc_ns::type_mrc &obj) { return obj.value + 200; });
#ifdef USE_MRC_BBB
m.def("type_mrc_to_python", []() { return mrc_ns::type_mrc<int>{202}; });
m.def("type_mrc_from_python", [](const mrc_ns::type_mrc<int> &obj) { return obj.value + 200; });
#endif
}

View File

@ -163,6 +163,15 @@ def test_ctypes_from_buffer():
assert not cinfo.readonly
def test_mrc():
assert m.type_mrc_to_python() == 1111
assert m.type_mrc_from_python("ignored") == 111
def test_type_mrc_to_python():
if hasattr(m, "type_mrc_to_python"):
assert m.type_mrc_to_python() == 2222
else:
pytype.skip("type_mrc_to_python")
def test_type_mrc_from_python():
if hasattr(m, "type_mrc_from_python"):
assert m.type_mrc_from_python("ignored") == 222
else:
pytype.skip("type_mrc_from_python")

View File

@ -28,6 +28,7 @@ template <>
class type_caster<ConstRefCasted> {
public:
static constexpr auto name = const_name<ConstRefCasted>();
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
// Input is unimportant, a new value will always be constructed based on the
// cast operator.

View File

@ -106,6 +106,7 @@ PYBIND11_NAMESPACE_BEGIN(detail)
template <>
struct type_caster<MoveOnlyInt> {
PYBIND11_TYPE_CASTER(MoveOnlyInt, const_name("MoveOnlyInt"));
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
bool load(handle src, bool) {
value = MoveOnlyInt(src.cast<int>());
return true;
@ -118,6 +119,7 @@ struct type_caster<MoveOnlyInt> {
template <>
struct type_caster<MoveOrCopyInt> {
PYBIND11_TYPE_CASTER(MoveOrCopyInt, const_name("MoveOrCopyInt"));
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
bool load(handle src, bool) {
value = MoveOrCopyInt(src.cast<int>());
return true;
@ -134,6 +136,7 @@ protected:
public:
static constexpr auto name = const_name("CopyOnlyInt");
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
bool load(handle src, bool) {
value = CopyOnlyInt(src.cast<int>());
return true;

View File

@ -32,6 +32,7 @@ public:
#else
PYBIND11_TYPE_CASTER(ArgInspector1, const_name("ArgInspector1"));
#endif
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
bool load(handle src, bool convert) {
value.arg = "loading ArgInspector1 argument " + std::string(convert ? "WITH" : "WITHOUT")
@ -49,6 +50,7 @@ template <>
struct type_caster<ArgInspector2> {
public:
PYBIND11_TYPE_CASTER(ArgInspector2, const_name("ArgInspector2"));
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
bool load(handle src, bool convert) {
value.arg = "loading ArgInspector2 argument " + std::string(convert ? "WITH" : "WITHOUT")
@ -66,6 +68,7 @@ template <>
struct type_caster<ArgAlwaysConverts> {
public:
PYBIND11_TYPE_CASTER(ArgAlwaysConverts, const_name("ArgAlwaysConverts"));
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
bool load(handle, bool convert) { return convert; }
@ -97,6 +100,7 @@ namespace detail {
template <>
struct type_caster<DestructionTester> {
PYBIND11_TYPE_CASTER(DestructionTester, const_name("DestructionTester"));
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
bool load(handle, bool) { return true; }
static handle cast(const DestructionTester &, return_value_policy, handle) {
@ -119,6 +123,7 @@ 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"));
static constexpr std::uint64_t universally_unique_identifier = 1655073597;
bool load(py_::handle, bool) { return true; }
static py_::handle cast(const MyType &, py_::return_value_policy, py_::handle) {