Skip to content

fix: <ranges> support for py::tuple and py::list #5314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: move <ranges> test macro to test_pytypes.h
  • Loading branch information
ObeliskGate committed Aug 18, 2024
commit 6793aa53a3d10807936a92e76e0b012dfdd37f2e
12 changes: 0 additions & 12 deletions include/pybind11/detail/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,18 +271,6 @@ PYBIND11_WARNING_DISABLE_MSVC(4505)
# endif
#endif

#if defined(PYBIND11_CPP20)
# if __has_include(<ranges>) // __has_include has been part of C++17, no need to check it
# if !defined(PYBIND11_COMPILER_CLANG)
# define PYBIND11_HAS_RANGES
# else
# if __clang_major__ >= 16 // llvm/llvm-project#52696
# define PYBIND11_HAS_RANGES
# endif
# endif
# endif
#endif

#include <Python.h>
#if PY_VERSION_HEX < 0x03080000
# error "PYTHON < 3.8 IS UNSUPPORTED. pybind11 v2.13 was the last to support Python 3.7."
Expand Down
19 changes: 16 additions & 3 deletions tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@
#include "pybind11_tests.h"

#include <utility>
#if defined(PYBIND11_HAS_RANGES)

#if defined(PYBIND11_CPP20)
# if __has_include(<ranges>) // __has_include has been part of C++17, no need to check it
# if !defined(PYBIND11_COMPILER_CLANG)
# define PYBIND11_TEST_PYTYPES_HAS_RANGES
# else
# if __clang_major__ >= 16 // llvm/llvm-project#52696
# define PYBIND11_TEST_PYTYPES_HAS_RANGES
# endif
# endif
# endif
#endif

#if defined(PYBIND11_TEST_PYTYPES_HAS_RANGES)
# include <ranges>
#endif

Expand Down Expand Up @@ -927,8 +940,7 @@ TEST_SUBMODULE(pytypes, m) {
m.attr("defined_PYBIND11_TYPING_H_HAS_STRING_LITERAL") = false;
#endif

#if defined(PYBIND11_HAS_RANGES) // test_ranges
m.attr("defined_PYBIND11_HAS_RANGES") = true;
#if defined(PYBIND11_TEST_PYTYPES_HAS_RANGES) // test_ranges
m.def("iterator_default_initialization", []() {
using TupleIterator = decltype(py::tuple{}.begin());
using ListIterator = decltype(py::list{}.begin());
Expand Down Expand Up @@ -960,6 +972,7 @@ TEST_SUBMODULE(pytypes, m) {
py::print("{} : {}"_s.format(it.first, it.second));
}
});
m.attr("defined_PYBIND11_TEST_PYTYPES_HAS_RANGES") = true;
#else
m.attr("defined_PYBIND11_HAS_RANGES") = false;
#endif
Expand Down
3 changes: 2 additions & 1 deletion tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,8 @@ def test_typevar(doc):


@pytest.mark.skipif(
not m.defined_PYBIND11_HAS_RANGES, reason="C++20 <ranges> not available."
not m.defined_PYBIND11_TEST_PYTYPES_HAS_RANGES,
reason="<ranges> not available.",
)
def test_ranges(capture):
assert m.iterator_default_initialization
Expand Down