Skip to content

Commit

Permalink
Add buffer_info::compare<T> to make `detail::compare_buffer_info<T>…
Browse files Browse the repository at this point in the history
…::compare` more visible & accessible.
  • Loading branch information
Ralf W. Grosse-Kunstleve committed May 19, 2023
1 parent 18e1bd2 commit 029b157
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
10 changes: 9 additions & 1 deletion include/pybind11/buffer_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ inline std::vector<ssize_t> f_strides(const std::vector<ssize_t> &shape, ssize_t
return strides;
}

template <typename T, typename SFINAE = void>
struct compare_buffer_info;

PYBIND11_NAMESPACE_END(detail)

/// Information record describing a Python buffer object
Expand Down Expand Up @@ -150,6 +153,11 @@ struct buffer_info {
Py_buffer *view() const { return m_view; }
Py_buffer *&view() { return m_view; }

template <typename T>
static bool compare(const buffer_info &b) {
return detail::compare_buffer_info<T>::compare(b);
}

private:
struct private_ctr_tag {};

Expand All @@ -170,7 +178,7 @@ struct buffer_info {

PYBIND11_NAMESPACE_BEGIN(detail)

template <typename T, typename SFINAE = void>
template <typename T, typename SFINAE>
struct compare_buffer_info {
static bool compare(const buffer_info &b) {
return b.format == format_descriptor<T>::format() && b.itemsize == (ssize_t) sizeof(T);
Expand Down
4 changes: 2 additions & 2 deletions tests/test_buffers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
TEST_SUBMODULE(buffers, m) {
m.attr("std_is_same_double_long_double") = std::is_same<double, long double>::value;

m.def("format_descriptor_format_compare",
m.def("format_descriptor_format_buffer_info_compare",
[](const std::string &cpp_name, const py::buffer &buffer) {
// https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables
static auto *format_table = new std::map<std::string, std::string>;
Expand All @@ -25,7 +25,7 @@ TEST_SUBMODULE(buffers, m) {
if (format_table->empty()) {
#define PYBIND11_ASSIGN_HELPER(...) \
(*format_table)[#__VA_ARGS__] = py::format_descriptor<__VA_ARGS__>::format(); \
(*compare_table)[#__VA_ARGS__] = py::detail::compare_buffer_info<__VA_ARGS__>::compare;
(*compare_table)[#__VA_ARGS__] = py::buffer_info::compare<__VA_ARGS__>;
PYBIND11_ASSIGN_HELPER(PyObject *)
PYBIND11_ASSIGN_HELPER(bool)
PYBIND11_ASSIGN_HELPER(std::int8_t)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_buffers.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@


@pytest.mark.parametrize(("cpp_name", "np_dtype"), CPP_NAME_NP_DTYPE_TABLE)
def test_format_descriptor_format_compare(cpp_name, np_dtype):
def test_format_descriptor_format_buffer_info_compare(cpp_name, np_dtype):
np_array = np.array([], dtype=np_dtype)
for other_cpp_name, expected_format in CPP_NAME_FORMAT_TABLE:
format, np_array_is_matching = m.format_descriptor_format_compare(
format, np_array_is_matching = m.format_descriptor_format_buffer_info_compare(
other_cpp_name, np_array
)
assert format == expected_format
Expand Down

0 comments on commit 029b157

Please sign in to comment.