Skip to content

Commit 24cebfa

Browse files
committed
Merge remote-tracking branch 'upstream/master' into jiwaszki/warnings_feature
2 parents f98b5d4 + c2291e5 commit 24cebfa

24 files changed

+184
-41
lines changed

docs/advanced/cast/stl.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ the declaration
162162

163163
.. code-block:: cpp
164164
165-
PYBIND11_MAKE_OPAQUE(std::vector<int>);
165+
PYBIND11_MAKE_OPAQUE(std::vector<int>)
166166
167167
before any binding code (e.g. invocations to ``class_::def()``, etc.). This
168168
macro must be specified at the top level (and outside of any namespaces), since
@@ -207,8 +207,8 @@ The following example showcases usage of :file:`pybind11/stl_bind.h`:
207207
// Don't forget this
208208
#include <pybind11/stl_bind.h>
209209
210-
PYBIND11_MAKE_OPAQUE(std::vector<int>);
211-
PYBIND11_MAKE_OPAQUE(std::map<std::string, double>);
210+
PYBIND11_MAKE_OPAQUE(std::vector<int>)
211+
PYBIND11_MAKE_OPAQUE(std::map<std::string, double>)
212212
213213
// ...
214214

docs/advanced/smart_ptrs.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ top namespace level before any binding code:
124124

125125
.. code-block:: cpp
126126
127-
PYBIND11_DECLARE_HOLDER_TYPE(T, SmartPtr<T>);
127+
PYBIND11_DECLARE_HOLDER_TYPE(T, SmartPtr<T>)
128128
129129
The first argument of :func:`PYBIND11_DECLARE_HOLDER_TYPE` should be a
130130
placeholder name that is used as a template parameter of the second argument.
@@ -136,7 +136,7 @@ by default. Specify
136136

137137
.. code-block:: cpp
138138
139-
PYBIND11_DECLARE_HOLDER_TYPE(T, SmartPtr<T>, true);
139+
PYBIND11_DECLARE_HOLDER_TYPE(T, SmartPtr<T>, true)
140140
141141
if ``SmartPtr<T>`` can always be initialized from a ``T*`` pointer without the
142142
risk of inconsistencies (such as multiple independent ``SmartPtr`` instances
@@ -154,7 +154,7 @@ specialized:
154154
.. code-block:: cpp
155155
156156
// Always needed for custom holder types
157-
PYBIND11_DECLARE_HOLDER_TYPE(T, SmartPtr<T>);
157+
PYBIND11_DECLARE_HOLDER_TYPE(T, SmartPtr<T>)
158158
159159
// Only needed if the type's `.get()` goes by another name
160160
namespace PYBIND11_NAMESPACE { namespace detail {

docs/basics.rst

+7
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ For brevity, all code examples assume that the following two lines are present:
7878
7979
namespace py = pybind11;
8080
81+
.. note::
82+
83+
``pybind11/pybind11.h`` includes ``Python.h``, as such it must be the first file
84+
included in any source file or header for `the same reasons as Python.h`_.
85+
86+
.. _`the same reasons as Python.h`: https://docs.python.org/3/extending/extending.html#a-simple-example
87+
8188
Some features may require additional headers, but those will be specified as needed.
8289

8390
.. _simple_example:

docs/changelog.rst

+17
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@ New Features:
2828
* Support for CMake older than 3.15 removed. CMake 3.15-3.30 supported.
2929
`#5304 <https://github.com/pybind/pybind11/pull/5304>`_
3030

31+
* The ``array_caster`` in pybind11/stl.h was enhanced to support value types that are not default-constructible.
32+
`#5305 <https://github.com/pybind/pybind11/pull/5305>`_
33+
34+
Version 2.13.5 (August 22, 2024)
35+
--------------------------------
36+
37+
Bug fixes:
38+
39+
* Fix includes when using Windows long paths (``\\?\`` prefix).
40+
`#5321 <https://github.com/pybind/pybind11/pull/5321>`_
41+
42+
* Support ``-Wpedantic`` in C++20 mode.
43+
`#5322 <https://github.com/pybind/pybind11/pull/5322>`_
44+
45+
* Fix and test ``<ranges>`` support for ``py::tuple`` and ``py::list``.
46+
`#5314 <https://github.com/pybind/pybind11/pull/5314>`_
47+
3148
Version 2.13.4 (August 14, 2024)
3249
--------------------------------
3350

include/pybind11/detail/class.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
#pragma once
1111

12-
#include "../attr.h"
13-
#include "../options.h"
12+
#include <pybind11/attr.h>
13+
#include <pybind11/options.h>
1414

1515
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
1616
PYBIND11_NAMESPACE_BEGIN(detail)

include/pybind11/detail/common.h

+3
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,8 @@ PYBIND11_WARNING_POP
479479
}
480480
481481
\endrst */
482+
PYBIND11_WARNING_PUSH
483+
PYBIND11_WARNING_DISABLE_CLANG("-Wgnu-zero-variadic-macro-arguments")
482484
#define PYBIND11_MODULE(name, variable, ...) \
483485
static ::pybind11::module_::module_def PYBIND11_CONCAT(pybind11_module_def_, name) \
484486
PYBIND11_MAYBE_UNUSED; \
@@ -499,6 +501,7 @@ PYBIND11_WARNING_POP
499501
PYBIND11_CATCH_INIT_EXCEPTIONS \
500502
} \
501503
void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ & (variable))
504+
PYBIND11_WARNING_POP
502505

503506
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
504507

include/pybind11/detail/internals.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
#include "common.h"
1313

1414
#if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
15-
# include "../gil.h"
15+
# include <pybind11/gil.h>
1616
#endif
1717

18-
#include "../pytypes.h"
18+
#include <pybind11/pytypes.h>
1919

2020
#include <exception>
2121
#include <mutex>

include/pybind11/detail/type_caster_base.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
#pragma once
1111

12-
#include "../pytypes.h"
12+
#include <pybind11/pytypes.h>
13+
1314
#include "common.h"
1415
#include "descr.h"
1516
#include "internals.h"

include/pybind11/eigen/matrix.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
#pragma once
1111

12-
#include "../numpy.h"
12+
#include <pybind11/numpy.h>
13+
1314
#include "common.h"
1415

1516
/* HINT: To suppress warnings originating from the Eigen headers, use -isystem.

include/pybind11/eigen/tensor.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
#pragma once
99

10-
#include "../numpy.h"
10+
#include <pybind11/numpy.h>
11+
1112
#include "common.h"
1213

1314
#if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER)

include/pybind11/pytypes.h

+2
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,7 @@ class sequence_fast_readonly {
12591259
using pointer = arrow_proxy<const handle>;
12601260

12611261
sequence_fast_readonly(handle obj, ssize_t n) : ptr(PySequence_Fast_ITEMS(obj.ptr()) + n) {}
1262+
sequence_fast_readonly() = default;
12621263

12631264
// NOLINTNEXTLINE(readability-const-return-type) // PR #3263
12641265
reference dereference() const { return *ptr; }
@@ -1281,6 +1282,7 @@ class sequence_slow_readwrite {
12811282
using pointer = arrow_proxy<const sequence_accessor>;
12821283

12831284
sequence_slow_readwrite(handle obj, ssize_t index) : obj(obj), index(index) {}
1285+
sequence_slow_readwrite() = default;
12841286

12851287
reference dereference() const { return {obj, static_cast<size_t>(index)}; }
12861288
void increment() { ++index; }

include/pybind11/stl/filesystem.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
#pragma once
66

7-
#include "../pybind11.h"
8-
#include "../detail/common.h"
9-
#include "../detail/descr.h"
10-
#include "../cast.h"
11-
#include "../pytypes.h"
7+
#include <pybind11/cast.h>
8+
#include <pybind11/detail/common.h>
9+
#include <pybind11/detail/descr.h>
10+
#include <pybind11/pybind11.h>
11+
#include <pybind11/pytypes.h>
1212

1313
#include <string>
1414

tests/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,9 @@ function(pybind11_enable_warnings target_name)
383383
-Wdeprecated
384384
-Wundef
385385
-Wnon-virtual-dtor)
386+
if(DEFINED CMAKE_CXX_STANDARD AND NOT CMAKE_CXX_STANDARD VERSION_LESS 20)
387+
target_compile_options(${target_name} PRIVATE -Wpedantic)
388+
endif()
386389
endif()
387390

388391
if(PYBIND11_WERROR)

tests/local_bindings.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,21 @@ class LocalSimpleException : public std::exception {
5656
std::string message = "";
5757
};
5858

59-
PYBIND11_MAKE_OPAQUE(LocalVec);
60-
PYBIND11_MAKE_OPAQUE(LocalVec2);
61-
PYBIND11_MAKE_OPAQUE(LocalMap);
62-
PYBIND11_MAKE_OPAQUE(NonLocalVec);
63-
// PYBIND11_MAKE_OPAQUE(NonLocalVec2); // same type as LocalVec2
64-
PYBIND11_MAKE_OPAQUE(NonLocalMap);
65-
PYBIND11_MAKE_OPAQUE(NonLocalMap2);
59+
PYBIND11_MAKE_OPAQUE(LocalVec)
60+
PYBIND11_MAKE_OPAQUE(LocalVec2)
61+
PYBIND11_MAKE_OPAQUE(LocalMap)
62+
PYBIND11_MAKE_OPAQUE(NonLocalVec)
63+
// PYBIND11_MAKE_OPAQUE(NonLocalVec2) // same type as LocalVec2
64+
PYBIND11_MAKE_OPAQUE(NonLocalMap)
65+
PYBIND11_MAKE_OPAQUE(NonLocalMap2)
6666

6767
// Simple bindings (used with the above):
6868
template <typename T, int Adjust = 0, typename... Args>
6969
py::class_<T> bind_local(Args &&...args) {
7070
return py::class_<T>(std::forward<Args>(args)...).def(py::init<int>()).def("get", [](T &i) {
7171
return i.i + Adjust;
7272
});
73-
};
73+
}
7474

7575
// Simulate a foreign library base class (to match the example in the docs):
7676
namespace pets {

tests/test_callbacks.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ TEST_SUBMODULE(callbacks, m) {
148148
m.def("dummy_function2", [](int i, int j) { return i + j; });
149149
m.def(
150150
"roundtrip",
151-
[](std::function<int(int)> f, bool expect_none = false) {
151+
[](std::function<int(int)> f, bool expect_none) {
152152
if (expect_none && f) {
153153
throw std::runtime_error("Expected None to be converted to empty std::function");
154154
}

tests/test_eigen_matrix.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void reset_refs() {
5555
}
5656

5757
// Returns element 2,1 from a matrix (used to test copy/nocopy)
58-
double get_elem(const Eigen::Ref<const Eigen::MatrixXd> &m) { return m(2, 1); };
58+
double get_elem(const Eigen::Ref<const Eigen::MatrixXd> &m) { return m(2, 1); }
5959

6060
// Returns a matrix with 10*r + 100*c added to each matrix element (to help test that the matrix
6161
// reference is referencing rows/columns correctly).
@@ -76,7 +76,7 @@ struct CustomOperatorNew {
7676
Eigen::Matrix4d a = Eigen::Matrix4d::Zero();
7777
Eigen::Matrix4d b = Eigen::Matrix4d::Identity();
7878

79-
EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
79+
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
8080
};
8181

8282
TEST_SUBMODULE(eigen_matrix, m) {

tests/test_opaque_types.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// This also deliberately doesn't use the below StringList type alias to test
1919
// that MAKE_OPAQUE can handle a type containing a `,`. (The `std::allocator`
2020
// bit is just the default `std::vector` allocator).
21-
PYBIND11_MAKE_OPAQUE(std::vector<std::string, std::allocator<std::string>>);
21+
PYBIND11_MAKE_OPAQUE(std::vector<std::string, std::allocator<std::string>>)
2222

2323
using StringList = std::vector<std::string, std::allocator<std::string>>;
2424

tests/test_pytypes.cpp

+63
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313

1414
#include <utility>
1515

16+
//__has_include has been part of C++17, no need to check it
17+
#if defined(PYBIND11_CPP20) && __has_include(<ranges>)
18+
# if !defined(PYBIND11_COMPILER_CLANG) || __clang_major__ >= 16 // llvm/llvm-project#52696
19+
# define PYBIND11_TEST_PYTYPES_HAS_RANGES
20+
# include <ranges>
21+
# endif
22+
#endif
23+
1624
namespace external {
1725
namespace detail {
1826
bool check(PyObject *o) { return PyFloat_Check(o) != 0; }
@@ -923,4 +931,59 @@ TEST_SUBMODULE(pytypes, m) {
923931
#else
924932
m.attr("defined_PYBIND11_TYPING_H_HAS_STRING_LITERAL") = false;
925933
#endif
934+
935+
#if defined(PYBIND11_TEST_PYTYPES_HAS_RANGES)
936+
937+
// test_tuple_ranges
938+
m.def("tuple_iterator_default_initialization", []() {
939+
using TupleIterator = decltype(std::declval<py::tuple>().begin());
940+
static_assert(std::random_access_iterator<TupleIterator>);
941+
return TupleIterator{} == TupleIterator{};
942+
});
943+
944+
m.def("transform_tuple_plus_one", [](py::tuple &tpl) {
945+
py::list ret{};
946+
for (auto it : tpl | std::views::transform([](auto &o) { return py::cast<int>(o) + 1; })) {
947+
ret.append(py::int_(it));
948+
}
949+
return ret;
950+
});
951+
952+
// test_list_ranges
953+
m.def("list_iterator_default_initialization", []() {
954+
using ListIterator = decltype(std::declval<py::list>().begin());
955+
static_assert(std::random_access_iterator<ListIterator>);
956+
return ListIterator{} == ListIterator{};
957+
});
958+
959+
m.def("transform_list_plus_one", [](py::list &lst) {
960+
py::list ret{};
961+
for (auto it : lst | std::views::transform([](auto &o) { return py::cast<int>(o) + 1; })) {
962+
ret.append(py::int_(it));
963+
}
964+
return ret;
965+
});
966+
967+
// test_dict_ranges
968+
m.def("dict_iterator_default_initialization", []() {
969+
using DictIterator = decltype(std::declval<py::dict>().begin());
970+
static_assert(std::forward_iterator<DictIterator>);
971+
return DictIterator{} == DictIterator{};
972+
});
973+
974+
m.def("transform_dict_plus_one", [](py::dict &dct) {
975+
py::list ret{};
976+
for (auto it : dct | std::views::transform([](auto &o) {
977+
return std::pair{py::cast<int>(o.first) + 1,
978+
py::cast<int>(o.second) + 1};
979+
})) {
980+
ret.append(py::make_tuple(py::int_(it.first), py::int_(it.second)));
981+
}
982+
return ret;
983+
});
984+
985+
m.attr("defined_PYBIND11_TEST_PYTYPES_HAS_RANGES") = true;
986+
#else
987+
m.attr("defined_PYBIND11_TEST_PYTYPES_HAS_RANGES") = false;
988+
#endif
926989
}

tests/test_pytypes.py

+42
Original file line numberDiff line numberDiff line change
@@ -1048,3 +1048,45 @@ def test_typevar(doc):
10481048
assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T"
10491049

10501050
assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T"
1051+
1052+
1053+
@pytest.mark.skipif(
1054+
not m.defined_PYBIND11_TEST_PYTYPES_HAS_RANGES,
1055+
reason="<ranges> not available.",
1056+
)
1057+
@pytest.mark.parametrize(
1058+
("tested_tuple", "expected"),
1059+
[((1,), [2]), ((3, 4), [4, 5]), ((7, 8, 9), [8, 9, 10])],
1060+
)
1061+
def test_tuple_ranges(tested_tuple, expected):
1062+
assert m.tuple_iterator_default_initialization()
1063+
assert m.transform_tuple_plus_one(tested_tuple) == expected
1064+
1065+
1066+
@pytest.mark.skipif(
1067+
not m.defined_PYBIND11_TEST_PYTYPES_HAS_RANGES,
1068+
reason="<ranges> not available.",
1069+
)
1070+
@pytest.mark.parametrize(
1071+
("tested_list", "expected"), [([1], [2]), ([3, 4], [4, 5]), ([7, 8, 9], [8, 9, 10])]
1072+
)
1073+
def test_list_ranges(tested_list, expected):
1074+
assert m.list_iterator_default_initialization()
1075+
assert m.transform_list_plus_one(tested_list) == expected
1076+
1077+
1078+
@pytest.mark.skipif(
1079+
not m.defined_PYBIND11_TEST_PYTYPES_HAS_RANGES,
1080+
reason="<ranges> not available.",
1081+
)
1082+
@pytest.mark.parametrize(
1083+
("tested_dict", "expected"),
1084+
[
1085+
({1: 2}, [(2, 3)]),
1086+
({3: 4, 5: 6}, [(4, 5), (6, 7)]),
1087+
({7: 8, 9: 10, 11: 12}, [(8, 9), (10, 11), (12, 13)]),
1088+
],
1089+
)
1090+
def test_dict_ranges(tested_dict, expected):
1091+
assert m.dict_iterator_default_initialization()
1092+
assert m.transform_dict_plus_one(tested_dict) == expected

tests/test_sequences_and_iterators.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ class NonCopyableInt {
8686
};
8787
using NonCopyableIntPair = std::pair<NonCopyableInt, NonCopyableInt>;
8888

89-
PYBIND11_MAKE_OPAQUE(std::vector<NonCopyableInt>);
90-
PYBIND11_MAKE_OPAQUE(std::vector<NonCopyableIntPair>);
89+
PYBIND11_MAKE_OPAQUE(std::vector<NonCopyableInt>)
90+
PYBIND11_MAKE_OPAQUE(std::vector<NonCopyableIntPair>)
9191

9292
template <typename PythonType>
9393
py::list test_random_access_iterator(PythonType x) {

0 commit comments

Comments
 (0)