2022-06-20 21:21:22 +00:00
|
|
|
#include "pybind11_tests.h"
|
|
|
|
|
|
|
|
namespace mrc_ns { // minimal real caster
|
|
|
|
|
|
|
|
struct type_mrc {
|
2022-06-21 23:26:39 +00:00
|
|
|
explicit type_mrc(int v = -9999) : value(v) {}
|
|
|
|
int value;
|
2022-06-20 21:21:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct minimal_real_caster {
|
|
|
|
static constexpr auto name = py::detail::const_name<type_mrc>();
|
2022-06-21 06:57:33 +00:00
|
|
|
PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE
|
2022-06-20 21:21:22 +00:00
|
|
|
|
|
|
|
static py::handle
|
|
|
|
cast(type_mrc const &src, py::return_value_policy /*policy*/, py::handle /*parent*/) {
|
2022-06-20 22:02:02 +00:00
|
|
|
return py::int_(src.value + 2020).release(); // ODR violation.
|
2022-06-20 21:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Maximizing simplicity. This will go terribly wrong for other arg types.
|
|
|
|
template <typename>
|
|
|
|
using cast_op_type = const type_mrc &;
|
|
|
|
|
|
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
|
|
|
operator type_mrc const &() {
|
|
|
|
static type_mrc obj;
|
2022-06-20 22:02:02 +00:00
|
|
|
obj.value = 22; // ODR violation.
|
2022-06-20 21:21:22 +00:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool load(py::handle src, bool /*convert*/) {
|
|
|
|
// Only accepts str, but the value is ignored.
|
|
|
|
return py::isinstance<py::str>(src);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mrc_ns
|
|
|
|
|
|
|
|
namespace pybind11 {
|
|
|
|
namespace detail {
|
|
|
|
template <>
|
2022-06-20 22:02:02 +00:00
|
|
|
struct type_caster<mrc_ns::type_mrc> : mrc_ns::minimal_real_caster {};
|
2022-06-20 21:21:22 +00:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace pybind11
|
|
|
|
|
|
|
|
TEST_SUBMODULE(odr_guard_2, m) {
|
2022-06-21 23:26:39 +00:00
|
|
|
m.def("type_mrc_to_python", []() { return mrc_ns::type_mrc(202); });
|
2022-06-20 21:21:22 +00:00
|
|
|
m.def("type_mrc_from_python", [](const mrc_ns::type_mrc &obj) { return obj.value + 200; });
|
|
|
|
}
|