Skip to content

Commit a4d61b4

Browse files
committed
Change the name of the new buffer_info member function to item_type_is_equivalent_to. Add comment defining "equivalent" by example.
1 parent ba7063e commit a4d61b4

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

include/pybind11/buffer_info.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,14 @@ struct buffer_info {
153153
Py_buffer *view() const { return m_view; }
154154
Py_buffer *&view() { return m_view; }
155155

156+
/* True if the buffer item type is equivalent to `T`. */
157+
// To define "equivalent" by example:
158+
// `buffer_info::item_type_is_equivalent_to<int>(b)` and
159+
// `buffer_info::item_type_is_equivalent_to<long>(b)` may both be true
160+
// on some platforms, but `int` and `unsigned` will never be equivalent.
161+
// For the ground truth, please inspect `detail::compare_buffer_info<>`.
156162
template <typename T>
157-
static bool compare(const buffer_info &b) {
163+
static bool item_type_is_equivalent_to(const buffer_info &b) {
158164
return detail::compare_buffer_info<T>::compare(b);
159165
}
160166

tests/test_buffers.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616
TEST_SUBMODULE(buffers, m) {
1717
m.attr("long_double_and_double_have_same_size") = (sizeof(long double) == sizeof(double));
1818

19-
m.def("format_descriptor_format_buffer_info_compare",
19+
m.def("format_descriptor_format_buffer_info_equiv",
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>;
23-
static auto *compare_table
23+
static auto *equiv_table
2424
= new std::map<std::string, bool (*)(const py::buffer_info &)>;
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::buffer_info::compare<__VA_ARGS__>;
28+
(*equiv_table)[#__VA_ARGS__] = py::buffer_info::item_type_is_equivalent_to<__VA_ARGS__>;
2929
PYBIND11_ASSIGN_HELPER(PyObject *)
3030
PYBIND11_ASSIGN_HELPER(bool)
3131
PYBIND11_ASSIGN_HELPER(std::int8_t)
@@ -45,7 +45,7 @@ TEST_SUBMODULE(buffers, m) {
4545
#undef PYBIND11_ASSIGN_HELPER
4646
}
4747
return std::pair<std::string, bool>((*format_table)[cpp_name],
48-
(*compare_table)[cpp_name](buffer.request()));
48+
(*equiv_table)[cpp_name](buffer.request()));
4949
});
5050

5151
// test_from_python / test_to_python:

tests/test_buffers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050

5151
@pytest.mark.parametrize(("cpp_name", "np_dtype"), CPP_NAME_NP_DTYPE_TABLE)
52-
def test_format_descriptor_format_buffer_info_compare(cpp_name, np_dtype):
52+
def test_format_descriptor_format_buffer_info_equiv(cpp_name, np_dtype):
5353
if np_dtype is None:
5454
pytest.skip(
5555
f"cpp_name=`{cpp_name}`: `long double` and `double` have same size."
@@ -58,7 +58,7 @@ def test_format_descriptor_format_buffer_info_compare(cpp_name, np_dtype):
5858
pytest.skip(f"np.{np_dtype} does not exist.")
5959
np_array = np.array([], dtype=np_dtype)
6060
for other_cpp_name, expected_format in CPP_NAME_FORMAT_TABLE:
61-
format, np_array_is_matching = m.format_descriptor_format_buffer_info_compare(
61+
format, np_array_is_matching = m.format_descriptor_format_buffer_info_equiv(
6262
other_cpp_name, np_array
6363
)
6464
assert format == expected_format

0 commit comments

Comments
 (0)