Follow-on to PR #3254, to address user code breakages. (#3263)

* Restoring `const` removed from pytypes.h in PR #3254, adding tests reflective of user code that breaks when those `const` are removed.

* clang-tidy NOLINTs (and one collateral fix).

* Inserting PYBIND11_CONST_FOR_STRICT_PLATFORMS

* Trying `defined(__APPLE__)`

* Trying again: `auto it` for strict platforms.

* Adding NOLINTNEXTLINE(bugprone-macro-parentheses), expanding comments.

* Labeling all changes with `PR #3263`, for easy reference, and to make it easy to undo these changes if we decide to do so in the future.
This commit is contained in:
Ralf W. Grosse-Kunstleve
2021-09-12 19:53:26 -07:00
committed by GitHub
parent 9978ed588b
commit 6c65ab5950
4 changed files with 58 additions and 3 deletions

View File

@@ -462,6 +462,49 @@ TEST_SUBMODULE(pytypes, m) {
m.def("weakref_from_object_and_function",
[](py::object o, py::function f) { return py::weakref(std::move(o), std::move(f)); });
// See PR #3263 for background (https://github.com/pybind/pybind11/pull/3263):
// pytypes.h could be changed to enforce the "most correct" user code below, by removing
// `const` from iterator `reference` using type aliases, but that will break existing
// user code.
#if (defined(__APPLE__) && defined(__clang__)) || defined(PYPY_VERSION)
// This is "most correct" and enforced on these platforms.
# define PYBIND11_AUTO_IT auto it
#else
// This works on many platforms and is (unfortunately) reflective of existing user code.
// NOLINTNEXTLINE(bugprone-macro-parentheses)
# define PYBIND11_AUTO_IT auto &it
#endif
m.def("tuple_iterator", []() {
auto tup = py::make_tuple(5, 7);
int tup_sum = 0;
for (PYBIND11_AUTO_IT : tup) {
tup_sum += it.cast<int>();
}
return tup_sum;
});
m.def("dict_iterator", []() {
py::dict dct;
dct[py::int_(3)] = 5;
dct[py::int_(7)] = 11;
int kv_sum = 0;
for (PYBIND11_AUTO_IT : dct) {
kv_sum += it.first.cast<int>() * 100 + it.second.cast<int>();
}
return kv_sum;
});
m.def("passed_iterator", [](const py::iterator &py_it) {
int elem_sum = 0;
for (PYBIND11_AUTO_IT : py_it) {
elem_sum += it.cast<int>();
}
return elem_sum;
});
#undef PYBIND11_AUTO_IT
// Tests below this line are for pybind11 IMPLEMENTATION DETAILS:
m.def("sequence_item_get_ssize_t", [](const py::object &o) {