Merge branch 'pybind:master' into master

This commit is contained in:
Steve R. Sun 2024-08-22 09:23:17 +08:00 committed by GitHub
commit 4ee5a89cfd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 122 additions and 12 deletions

View File

@ -9,8 +9,8 @@
#pragma once #pragma once
#include "../attr.h" #include <pybind11/attr.h>
#include "../options.h" #include <pybind11/options.h>
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
PYBIND11_NAMESPACE_BEGIN(detail) PYBIND11_NAMESPACE_BEGIN(detail)

View File

@ -12,10 +12,10 @@
#include "common.h" #include "common.h"
#if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT) #if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
# include "../gil.h" # include <pybind11/gil.h>
#endif #endif
#include "../pytypes.h" #include <pybind11/pytypes.h>
#include <exception> #include <exception>
#include <mutex> #include <mutex>

View File

@ -9,7 +9,8 @@
#pragma once #pragma once
#include "../pytypes.h" #include <pybind11/pytypes.h>
#include "common.h" #include "common.h"
#include "descr.h" #include "descr.h"
#include "internals.h" #include "internals.h"

View File

@ -9,7 +9,8 @@
#pragma once #pragma once
#include "../numpy.h" #include <pybind11/numpy.h>
#include "common.h" #include "common.h"
/* HINT: To suppress warnings originating from the Eigen headers, use -isystem. /* HINT: To suppress warnings originating from the Eigen headers, use -isystem.

View File

@ -7,7 +7,8 @@
#pragma once #pragma once
#include "../numpy.h" #include <pybind11/numpy.h>
#include "common.h" #include "common.h"
#if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER) #if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER)

View File

@ -1259,6 +1259,7 @@ protected:
using pointer = arrow_proxy<const handle>; using pointer = arrow_proxy<const handle>;
sequence_fast_readonly(handle obj, ssize_t n) : ptr(PySequence_Fast_ITEMS(obj.ptr()) + n) {} sequence_fast_readonly(handle obj, ssize_t n) : ptr(PySequence_Fast_ITEMS(obj.ptr()) + n) {}
sequence_fast_readonly() = default;
// NOLINTNEXTLINE(readability-const-return-type) // PR #3263 // NOLINTNEXTLINE(readability-const-return-type) // PR #3263
reference dereference() const { return *ptr; } reference dereference() const { return *ptr; }
@ -1281,6 +1282,7 @@ protected:
using pointer = arrow_proxy<const sequence_accessor>; using pointer = arrow_proxy<const sequence_accessor>;
sequence_slow_readwrite(handle obj, ssize_t index) : obj(obj), index(index) {} sequence_slow_readwrite(handle obj, ssize_t index) : obj(obj), index(index) {}
sequence_slow_readwrite() = default;
reference dereference() const { return {obj, static_cast<size_t>(index)}; } reference dereference() const { return {obj, static_cast<size_t>(index)}; }
void increment() { ++index; } void increment() { ++index; }

View File

@ -4,11 +4,11 @@
#pragma once #pragma once
#include "../pybind11.h" #include <pybind11/cast.h>
#include "../detail/common.h" #include <pybind11/detail/common.h>
#include "../detail/descr.h" #include <pybind11/detail/descr.h>
#include "../cast.h" #include <pybind11/pybind11.h>
#include "../pytypes.h" #include <pybind11/pytypes.h>
#include <string> #include <string>

View File

@ -13,6 +13,14 @@
#include <utility> #include <utility>
//__has_include has been part of C++17, no need to check it
#if defined(PYBIND11_CPP20) && __has_include(<ranges>)
# if !defined(PYBIND11_COMPILER_CLANG) || __clang_major__ >= 16 // llvm/llvm-project#52696
# define PYBIND11_TEST_PYTYPES_HAS_RANGES
# include <ranges>
# endif
#endif
namespace external { namespace external {
namespace detail { namespace detail {
bool check(PyObject *o) { return PyFloat_Check(o) != 0; } bool check(PyObject *o) { return PyFloat_Check(o) != 0; }
@ -923,4 +931,59 @@ TEST_SUBMODULE(pytypes, m) {
#else #else
m.attr("defined_PYBIND11_TYPING_H_HAS_STRING_LITERAL") = false; m.attr("defined_PYBIND11_TYPING_H_HAS_STRING_LITERAL") = false;
#endif #endif
#if defined(PYBIND11_TEST_PYTYPES_HAS_RANGES)
// test_tuple_ranges
m.def("tuple_iterator_default_initialization", []() {
using TupleIterator = decltype(std::declval<py::tuple>().begin());
static_assert(std::random_access_iterator<TupleIterator>);
return TupleIterator{} == TupleIterator{};
});
m.def("transform_tuple_plus_one", [](py::tuple &tpl) {
py::list ret{};
for (auto it : tpl | std::views::transform([](auto &o) { return py::cast<int>(o) + 1; })) {
ret.append(py::int_(it));
}
return ret;
});
// test_list_ranges
m.def("list_iterator_default_initialization", []() {
using ListIterator = decltype(std::declval<py::list>().begin());
static_assert(std::random_access_iterator<ListIterator>);
return ListIterator{} == ListIterator{};
});
m.def("transform_list_plus_one", [](py::list &lst) {
py::list ret{};
for (auto it : lst | std::views::transform([](auto &o) { return py::cast<int>(o) + 1; })) {
ret.append(py::int_(it));
}
return ret;
});
// test_dict_ranges
m.def("dict_iterator_default_initialization", []() {
using DictIterator = decltype(std::declval<py::dict>().begin());
static_assert(std::forward_iterator<DictIterator>);
return DictIterator{} == DictIterator{};
});
m.def("transform_dict_plus_one", [](py::dict &dct) {
py::list ret{};
for (auto it : dct | std::views::transform([](auto &o) {
return std::pair{py::cast<int>(o.first) + 1,
py::cast<int>(o.second) + 1};
})) {
ret.append(py::make_tuple(py::int_(it.first), py::int_(it.second)));
}
return ret;
});
m.attr("defined_PYBIND11_TEST_PYTYPES_HAS_RANGES") = true;
#else
m.attr("defined_PYBIND11_TEST_PYTYPES_HAS_RANGES") = false;
#endif
} }

View File

@ -1048,3 +1048,45 @@ def test_typevar(doc):
assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T" assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T"
assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T"
@pytest.mark.skipif(
not m.defined_PYBIND11_TEST_PYTYPES_HAS_RANGES,
reason="<ranges> not available.",
)
@pytest.mark.parametrize(
("tested_tuple", "expected"),
[((1,), [2]), ((3, 4), [4, 5]), ((7, 8, 9), [8, 9, 10])],
)
def test_tuple_ranges(tested_tuple, expected):
assert m.tuple_iterator_default_initialization()
assert m.transform_tuple_plus_one(tested_tuple) == expected
@pytest.mark.skipif(
not m.defined_PYBIND11_TEST_PYTYPES_HAS_RANGES,
reason="<ranges> not available.",
)
@pytest.mark.parametrize(
("tested_list", "expected"), [([1], [2]), ([3, 4], [4, 5]), ([7, 8, 9], [8, 9, 10])]
)
def test_list_ranges(tested_list, expected):
assert m.list_iterator_default_initialization()
assert m.transform_list_plus_one(tested_list) == expected
@pytest.mark.skipif(
not m.defined_PYBIND11_TEST_PYTYPES_HAS_RANGES,
reason="<ranges> not available.",
)
@pytest.mark.parametrize(
("tested_dict", "expected"),
[
({1: 2}, [(2, 3)]),
({3: 4, 5: 6}, [(4, 5), (6, 7)]),
({7: 8, 9: 10, 11: 12}, [(8, 9), (10, 11), (12, 13)]),
],
)
def test_dict_ranges(tested_dict, expected):
assert m.dict_iterator_default_initialization()
assert m.transform_dict_plus_one(tested_dict) == expected