Commit 2bf606ce by Ralf W. Grosse-Kunstleve Committed by Copybara-Service

Make use of new `py::buffer_info::item_type_is_equivalent_to<T>()`

The new member function was added with https://github.com/pybind/pybind11/pull/4674

PiperOrigin-RevId: 534952040
parent 7d17c90c
......@@ -386,8 +386,6 @@ template <>
struct type_caster<absl::CivilYear>
: public absl_civil_date_caster<absl::CivilYear> {};
// Using internal namespace to avoid name collisons in case this code is
// accepted upsteam (pybind11).
namespace internal {
template <typename T>
......@@ -397,33 +395,6 @@ static constexpr bool is_buffer_interface_compatible_type =
std::is_same<T, std::complex<float>>::value ||
std::is_same<T, std::complex<double>>::value;
template <typename T, typename SFINAE = void>
struct format_descriptor_char1 : format_descriptor<T> {};
template <typename T>
struct format_descriptor_char1<
T,
detail::enable_if_t<detail::is_same_ignoring_cvref<T, PyObject*>::value>> {
static constexpr const char c = 'O';
static constexpr const char value[2] = {c, '\0'};
static std::string format() { return std::string(1, c); }
};
template <typename T, typename SFINAE = void>
struct format_descriptor_char2 {
static constexpr const char c = '\0';
};
template <typename T>
struct format_descriptor_char2<std::complex<T>> : format_descriptor<T> {};
template <typename T>
inline bool buffer_view_matches_format_descriptor(const char* view_format) {
return view_format[0] == format_descriptor_char1<T>::c ||
(view_format[0] == 'Z' &&
view_format[1] == format_descriptor_char2<T>::c);
}
} // namespace internal
// Returns {true, a span referencing the data contained by src} without copying
......@@ -438,7 +409,7 @@ std::tuple<bool, absl::Span<T>> LoadSpanFromBuffer(handle src) {
if (PyObject_GetBuffer(src.ptr(), &view, flags) == 0) {
auto cleanup = absl::MakeCleanup([&view] { PyBuffer_Release(&view); });
if (view.ndim == 1 && view.strides[0] == sizeof(T) &&
internal::buffer_view_matches_format_descriptor<T>(view.format)) {
buffer_info(&view, /*ownview=*/false).item_type_is_equivalent_to<T>()) {
return {true, absl::MakeSpan(static_cast<T*>(view.buf), view.shape[0])};
}
} else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment