Skip to content

Commit 6c65ab5

Browse files
authored
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.
1 parent 9978ed5 commit 6c65ab5

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

include/pybind11/pybind11.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,7 @@ iterator make_iterator(Iterator first, Sentinel last, Extra &&... extra) {
19871987
throw stop_iteration();
19881988
}
19891989
return *s.it;
1990+
// NOLINTNEXTLINE(readability-const-return-type) // PR #3263
19901991
}, std::forward<Extra>(extra)..., Policy);
19911992
}
19921993

include/pybind11/pytypes.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,9 @@ class generic_iterator : public Policy {
733733
generic_iterator() = default;
734734
generic_iterator(handle seq, ssize_t index) : Policy(seq, index) { }
735735

736+
// NOLINTNEXTLINE(readability-const-return-type) // PR #3263
736737
reference operator*() const { return Policy::dereference(); }
738+
// NOLINTNEXTLINE(readability-const-return-type) // PR #3263
737739
reference operator[](difference_type n) const { return *(*this + n); }
738740
pointer operator->() const { return **this; }
739741

@@ -773,11 +775,12 @@ class sequence_fast_readonly {
773775
protected:
774776
using iterator_category = std::random_access_iterator_tag;
775777
using value_type = handle;
776-
using reference = handle;
778+
using reference = const handle; // PR #3263
777779
using pointer = arrow_proxy<const handle>;
778780

779781
sequence_fast_readonly(handle obj, ssize_t n) : ptr(PySequence_Fast_ITEMS(obj.ptr()) + n) { }
780782

783+
// NOLINTNEXTLINE(readability-const-return-type) // PR #3263
781784
reference dereference() const { return *ptr; }
782785
void increment() { ++ptr; }
783786
void decrement() { --ptr; }
@@ -816,12 +819,13 @@ class dict_readonly {
816819
protected:
817820
using iterator_category = std::forward_iterator_tag;
818821
using value_type = std::pair<handle, handle>;
819-
using reference = value_type;
822+
using reference = const value_type; // PR #3263
820823
using pointer = arrow_proxy<const value_type>;
821824

822825
dict_readonly() = default;
823826
dict_readonly(handle obj, ssize_t pos) : obj(obj), pos(pos) { increment(); }
824827

828+
// NOLINTNEXTLINE(readability-const-return-type) // PR #3263
825829
reference dereference() const { return {key, value}; }
826830
void increment() {
827831
if (PyDict_Next(obj.ptr(), &pos, &key, &value) == 0) {
@@ -966,7 +970,7 @@ class iterator : public object {
966970
using iterator_category = std::input_iterator_tag;
967971
using difference_type = ssize_t;
968972
using value_type = handle;
969-
using reference = handle;
973+
using reference = const handle; // PR #3263
970974
using pointer = const handle *;
971975

972976
PYBIND11_OBJECT_DEFAULT(iterator, object, PyIter_Check)
@@ -982,6 +986,7 @@ class iterator : public object {
982986
return rv;
983987
}
984988

989+
// NOLINTNEXTLINE(readability-const-return-type) // PR #3263
985990
reference operator*() const {
986991
if (m_ptr && !value.ptr()) {
987992
auto& self = const_cast<iterator &>(*this);

tests/test_pytypes.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,49 @@ TEST_SUBMODULE(pytypes, m) {
462462
m.def("weakref_from_object_and_function",
463463
[](py::object o, py::function f) { return py::weakref(std::move(o), std::move(f)); });
464464

465+
// See PR #3263 for background (https://github.com/pybind/pybind11/pull/3263):
466+
// pytypes.h could be changed to enforce the "most correct" user code below, by removing
467+
// `const` from iterator `reference` using type aliases, but that will break existing
468+
// user code.
469+
#if (defined(__APPLE__) && defined(__clang__)) || defined(PYPY_VERSION)
470+
// This is "most correct" and enforced on these platforms.
471+
# define PYBIND11_AUTO_IT auto it
472+
#else
473+
// This works on many platforms and is (unfortunately) reflective of existing user code.
474+
// NOLINTNEXTLINE(bugprone-macro-parentheses)
475+
# define PYBIND11_AUTO_IT auto &it
476+
#endif
477+
478+
m.def("tuple_iterator", []() {
479+
auto tup = py::make_tuple(5, 7);
480+
int tup_sum = 0;
481+
for (PYBIND11_AUTO_IT : tup) {
482+
tup_sum += it.cast<int>();
483+
}
484+
return tup_sum;
485+
});
486+
487+
m.def("dict_iterator", []() {
488+
py::dict dct;
489+
dct[py::int_(3)] = 5;
490+
dct[py::int_(7)] = 11;
491+
int kv_sum = 0;
492+
for (PYBIND11_AUTO_IT : dct) {
493+
kv_sum += it.first.cast<int>() * 100 + it.second.cast<int>();
494+
}
495+
return kv_sum;
496+
});
497+
498+
m.def("passed_iterator", [](const py::iterator &py_it) {
499+
int elem_sum = 0;
500+
for (PYBIND11_AUTO_IT : py_it) {
501+
elem_sum += it.cast<int>();
502+
}
503+
return elem_sum;
504+
});
505+
506+
#undef PYBIND11_AUTO_IT
507+
465508
// Tests below this line are for pybind11 IMPLEMENTATION DETAILS:
466509

467510
m.def("sequence_item_get_ssize_t", [](const py::object &o) {

tests/test_pytypes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,12 @@ def callback(wr):
623623
assert callback.called
624624

625625

626+
def test_cpp_iterators():
627+
assert m.tuple_iterator() == 12
628+
assert m.dict_iterator() == 305 + 711
629+
assert m.passed_iterator(iter((-7, 3))) == -4
630+
631+
626632
def test_implementation_details():
627633
lst = [39, 43, 92, 49, 22, 29, 93, 98, 26, 57, 8]
628634
tup = tuple(lst)

0 commit comments

Comments
 (0)