Fix #5399: iterator increment operator does not skip first item (#5400)

* Fix #5399: iterator increment operator does not skip first item

* Fix postfix increment operator: init() must be called before copying *this
This commit is contained in:
Boris Dalstein
2024-10-12 05:33:13 +02:00
committed by GitHub
parent af67e87393
commit f2907651fa
3 changed files with 32 additions and 5 deletions

View File

@@ -150,6 +150,18 @@ TEST_SUBMODULE(pytypes, m) {
m.def("get_iterator", [] { return py::iterator(); });
// test_iterable
m.def("get_iterable", [] { return py::iterable(); });
m.def("get_first_item_from_iterable", [](const py::iterable &iter) {
// This tests the postfix increment operator
py::iterator it = iter.begin();
py::iterator it2 = it++;
return *it2;
});
m.def("get_second_item_from_iterable", [](const py::iterable &iter) {
// This tests the prefix increment operator
py::iterator it = iter.begin();
++it;
return *it;
});
m.def("get_frozenset_from_iterable",
[](const py::iterable &iter) { return py::frozenset(iter); });
m.def("get_list_from_iterable", [](const py::iterable &iter) { return py::list(iter); });