Skip to content

Commit 029b157

Browse files
committed
Add buffer_info::compare<T> to make detail::compare_buffer_info<T>::compare more visible & accessible.
1 parent 18e1bd2 commit 029b157

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

include/pybind11/buffer_info.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ inline std::vector<ssize_t> f_strides(const std::vector<ssize_t> &shape, ssize_t
3737
return strides;
3838
}
3939

40+
template <typename T, typename SFINAE = void>
41+
struct compare_buffer_info;
42+
4043
PYBIND11_NAMESPACE_END(detail)
4144

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

156+
template <typename T>
157+
static bool compare(const buffer_info &b) {
158+
return detail::compare_buffer_info<T>::compare(b);
159+
}
160+
153161
private:
154162
struct private_ctr_tag {};
155163

@@ -170,7 +178,7 @@ struct buffer_info {
170178

171179
PYBIND11_NAMESPACE_BEGIN(detail)
172180

173-
template <typename T, typename SFINAE = void>
181+
template <typename T, typename SFINAE>
174182
struct compare_buffer_info {
175183
static bool compare(const buffer_info &b) {
176184
return b.format == format_descriptor<T>::format() && b.itemsize == (ssize_t) sizeof(T);

tests/test_buffers.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
TEST_SUBMODULE(buffers, m) {
1717
m.attr("std_is_same_double_long_double") = std::is_same<double, long double>::value;
1818

19-
m.def("format_descriptor_format_compare",
19+
m.def("format_descriptor_format_buffer_info_compare",
2020
[](const std::string &cpp_name, const py::buffer &buffer) {
2121
// https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables
2222
static auto *format_table = new std::map<std::string, std::string>;
@@ -25,7 +25,7 @@ TEST_SUBMODULE(buffers, m) {
2525
if (format_table->empty()) {
2626
#define PYBIND11_ASSIGN_HELPER(...) \
2727
(*format_table)[#__VA_ARGS__] = py::format_descriptor<__VA_ARGS__>::format(); \
28-
(*compare_table)[#__VA_ARGS__] = py::detail::compare_buffer_info<__VA_ARGS__>::compare;
28+
(*compare_table)[#__VA_ARGS__] = py::buffer_info::compare<__VA_ARGS__>;
2929
PYBIND11_ASSIGN_HELPER(PyObject *)
3030
PYBIND11_ASSIGN_HELPER(bool)
3131
PYBIND11_ASSIGN_HELPER(std::int8_t)

tests/test_buffers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@
4848

4949

5050
@pytest.mark.parametrize(("cpp_name", "np_dtype"), CPP_NAME_NP_DTYPE_TABLE)
51-
def test_format_descriptor_format_compare(cpp_name, np_dtype):
51+
def test_format_descriptor_format_buffer_info_compare(cpp_name, np_dtype):
5252
np_array = np.array([], dtype=np_dtype)
5353
for other_cpp_name, expected_format in CPP_NAME_FORMAT_TABLE:
54-
format, np_array_is_matching = m.format_descriptor_format_compare(
54+
format, np_array_is_matching = m.format_descriptor_format_buffer_info_compare(
5555
other_cpp_name, np_array
5656
)
5757
assert format == expected_format

0 commit comments

Comments
 (0)