Skip to content

Commit 6233808

Browse files
authored
Update Pybind11 to v2.12.0 for numpy v2 support (#2970)
1 parent 1a51998 commit 6233808

40 files changed

+3720
-1347
lines changed

dlib/external/pybind11/CMakeLists.txt

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,25 @@
55
# All rights reserved. Use of this source code is governed by a
66
# BSD-style license that can be found in the LICENSE file.
77

8-
cmake_minimum_required(VERSION 3.4)
8+
# Propagate this policy (FindPythonInterp removal) so it can be detected later
9+
if(NOT CMAKE_VERSION VERSION_LESS "3.27")
10+
cmake_policy(GET CMP0148 _pybind11_cmp0148)
11+
endif()
12+
13+
cmake_minimum_required(VERSION 3.5)
914

10-
# The `cmake_minimum_required(VERSION 3.4...3.22)` syntax does not work with
15+
# The `cmake_minimum_required(VERSION 3.5...3.27)` syntax does not work with
1116
# some versions of VS that have a patched CMake 3.11. This forces us to emulate
1217
# the behavior using the following workaround:
13-
if(${CMAKE_VERSION} VERSION_LESS 3.22)
18+
if(${CMAKE_VERSION} VERSION_LESS 3.27)
1419
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
1520
else()
16-
cmake_policy(VERSION 3.22)
21+
cmake_policy(VERSION 3.27)
22+
endif()
23+
24+
if(_pybind11_cmp0148)
25+
cmake_policy(SET CMP0148 ${_pybind11_cmp0148})
26+
unset(_pybind11_cmp0148)
1727
endif()
1828

1929
# Avoid infinite recursion if tests include this as a subdirectory
@@ -82,27 +92,58 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
8292
set(pybind11_system "")
8393

8494
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
95+
if(CMAKE_VERSION VERSION_LESS "3.18")
96+
set(_pybind11_findpython_default OFF)
97+
else()
98+
set(_pybind11_findpython_default ON)
99+
endif()
85100
else()
86101
set(PYBIND11_MASTER_PROJECT OFF)
87102
set(pybind11_system SYSTEM)
103+
set(_pybind11_findpython_default OFF)
88104
endif()
89105

90106
# Options
91107
option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT})
92108
option(PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT})
93109
option(PYBIND11_NOPYTHON "Disable search for Python" OFF)
110+
option(PYBIND11_DISABLE_HANDLE_TYPE_NAME_DEFAULT_IMPLEMENTATION
111+
"To enforce that a handle_type_name<> specialization exists" OFF)
112+
option(PYBIND11_SIMPLE_GIL_MANAGEMENT
113+
"Use simpler GIL management logic that does not support disassociation" OFF)
114+
option(PYBIND11_NUMPY_1_ONLY
115+
"Disable NumPy 2 support to avoid changes to previous pybind11 versions." OFF)
94116
set(PYBIND11_INTERNALS_VERSION
95117
""
96118
CACHE STRING "Override the ABI version, may be used to enable the unstable ABI.")
97119

120+
if(PYBIND11_DISABLE_HANDLE_TYPE_NAME_DEFAULT_IMPLEMENTATION)
121+
add_compile_definitions(PYBIND11_DISABLE_HANDLE_TYPE_NAME_DEFAULT_IMPLEMENTATION)
122+
endif()
123+
if(PYBIND11_SIMPLE_GIL_MANAGEMENT)
124+
add_compile_definitions(PYBIND11_SIMPLE_GIL_MANAGEMENT)
125+
endif()
126+
if(PYBIND11_NUMPY_1_ONLY)
127+
add_compile_definitions(PYBIND11_NUMPY_1_ONLY)
128+
endif()
129+
98130
cmake_dependent_option(
99131
USE_PYTHON_INCLUDE_DIR
100132
"Install pybind11 headers in Python include directory instead of default installation prefix"
101133
OFF "PYBIND11_INSTALL" OFF)
102134

103-
cmake_dependent_option(PYBIND11_FINDPYTHON "Force new FindPython" OFF
135+
cmake_dependent_option(PYBIND11_FINDPYTHON "Force new FindPython" ${_pybind11_findpython_default}
104136
"NOT CMAKE_VERSION VERSION_LESS 3.12" OFF)
105137

138+
# Allow PYTHON_EXECUTABLE if in FINDPYTHON mode and building pybind11's tests
139+
# (makes transition easier while we support both modes).
140+
if(PYBIND11_MASTER_PROJECT
141+
AND PYBIND11_FINDPYTHON
142+
AND DEFINED PYTHON_EXECUTABLE
143+
AND NOT DEFINED Python_EXECUTABLE)
144+
set(Python_EXECUTABLE "${PYTHON_EXECUTABLE}")
145+
endif()
146+
106147
# NB: when adding a header don't forget to also add it to setup.py
107148
set(PYBIND11_HEADERS
108149
include/pybind11/detail/class.h
@@ -120,9 +161,13 @@ set(PYBIND11_HEADERS
120161
include/pybind11/complex.h
121162
include/pybind11/options.h
122163
include/pybind11/eigen.h
164+
include/pybind11/eigen/common.h
165+
include/pybind11/eigen/matrix.h
166+
include/pybind11/eigen/tensor.h
123167
include/pybind11/embed.h
124168
include/pybind11/eval.h
125169
include/pybind11/gil.h
170+
include/pybind11/gil_safe_call_once.h
126171
include/pybind11/iostream.h
127172
include/pybind11/functional.h
128173
include/pybind11/numpy.h
@@ -131,7 +176,9 @@ set(PYBIND11_HEADERS
131176
include/pybind11/pytypes.h
132177
include/pybind11/stl.h
133178
include/pybind11/stl_bind.h
134-
include/pybind11/stl/filesystem.h)
179+
include/pybind11/stl/filesystem.h
180+
include/pybind11/type_caster_pyobject_ptr.h
181+
include/pybind11/typing.h)
135182

136183
# Compare with grep and warn if mismatched
137184
if(PYBIND11_MASTER_PROJECT AND NOT CMAKE_VERSION VERSION_LESS 3.12)
@@ -198,6 +245,9 @@ else()
198245
endif()
199246

200247
include("${CMAKE_CURRENT_SOURCE_DIR}/tools/pybind11Common.cmake")
248+
# https://github.com/jtojnar/cmake-snips/#concatenating-paths-when-building-pkg-config-files
249+
# TODO: cmake 3.20 adds the cmake_path() function, which obsoletes this snippet
250+
include("${CMAKE_CURRENT_SOURCE_DIR}/tools/JoinPaths.cmake")
201251

202252
# Relative directory setting
203253
if(USE_PYTHON_INCLUDE_DIR AND DEFINED Python_INCLUDE_DIRS)
@@ -262,6 +312,30 @@ if(PYBIND11_INSTALL)
262312
NAMESPACE "pybind11::"
263313
DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})
264314

315+
# pkg-config support
316+
if(NOT prefix_for_pc_file)
317+
if(IS_ABSOLUTE "${CMAKE_INSTALL_DATAROOTDIR}")
318+
set(prefix_for_pc_file "${CMAKE_INSTALL_PREFIX}")
319+
else()
320+
set(pc_datarootdir "${CMAKE_INSTALL_DATAROOTDIR}")
321+
if(CMAKE_VERSION VERSION_LESS 3.20)
322+
set(prefix_for_pc_file "\${pcfiledir}/..")
323+
while(pc_datarootdir)
324+
get_filename_component(pc_datarootdir "${pc_datarootdir}" DIRECTORY)
325+
string(APPEND prefix_for_pc_file "/..")
326+
endwhile()
327+
else()
328+
cmake_path(RELATIVE_PATH CMAKE_INSTALL_PREFIX BASE_DIRECTORY CMAKE_INSTALL_DATAROOTDIR
329+
OUTPUT_VARIABLE prefix_for_pc_file)
330+
endif()
331+
endif()
332+
endif()
333+
join_paths(includedir_for_pc_file "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}")
334+
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/tools/pybind11.pc.in"
335+
"${CMAKE_CURRENT_BINARY_DIR}/pybind11.pc" @ONLY)
336+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/pybind11.pc"
337+
DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig/")
338+
265339
# Uninstall target
266340
if(PYBIND11_MASTER_PROJECT)
267341
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/tools/cmake_uninstall.cmake.in"

dlib/external/pybind11/README.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ with everything stripped away that isn't relevant for binding
3636
generation. Without comments, the core header files only require ~4K
3737
lines of code and depend on Python (3.6+, or PyPy) and the C++
3838
standard library. This compact implementation was possible thanks to
39-
some of the new C++11 language features (specifically: tuples, lambda
40-
functions and variadic templates). Since its creation, this library has
41-
grown beyond Boost.Python in many ways, leading to dramatically simpler
42-
binding code in many common situations.
39+
some C++11 language features (specifically: tuples, lambda functions and
40+
variadic templates). Since its creation, this library has grown beyond
41+
Boost.Python in many ways, leading to dramatically simpler binding code in many
42+
common situations.
4343

4444
Tutorial and reference documentation is provided at
4545
`pybind11.readthedocs.io <https://pybind11.readthedocs.io/en/latest>`_.
@@ -71,6 +71,7 @@ pybind11 can map the following core C++ features to Python:
7171
- Internal references with correct reference counting
7272
- C++ classes with virtual (and pure virtual) methods can be extended
7373
in Python
74+
- Integrated NumPy support (NumPy 2 requires pybind11 2.12+)
7475

7576
Goodies
7677
-------
@@ -135,7 +136,7 @@ This project was created by `Wenzel
135136
Jakob <http://rgl.epfl.ch/people/wjakob>`_. Significant features and/or
136137
improvements to the code were contributed by Jonas Adler, Lori A. Burns,
137138
Sylvain Corlay, Eric Cousineau, Aaron Gokaslan, Ralf Grosse-Kunstleve, Trent Houliston, Axel
138-
Huebl, @hulucc, Yannick Jadoul, Sergey Lyskov Johan Mabille, Tomasz Miąsko,
139+
Huebl, @hulucc, Yannick Jadoul, Sergey Lyskov, Johan Mabille, Tomasz Miąsko,
139140
Dean Moldovan, Ben Pritchard, Jason Rhinelander, Boris Schäling, Pim
140141
Schellart, Henry Schreiner, Ivan Smirnov, Boris Staletic, and Patrick Stewart.
141142

dlib/external/pybind11/include/pybind11/attr.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ struct is_method {
2626
explicit is_method(const handle &c) : class_(c) {}
2727
};
2828

29+
/// Annotation for setters
30+
struct is_setter {};
31+
2932
/// Annotation for operators
3033
struct is_operator {};
3134

@@ -188,8 +191,8 @@ struct argument_record {
188191
struct function_record {
189192
function_record()
190193
: is_constructor(false), is_new_style_constructor(false), is_stateless(false),
191-
is_operator(false), is_method(false), has_args(false), has_kwargs(false),
192-
prepend(false) {}
194+
is_operator(false), is_method(false), is_setter(false), has_args(false),
195+
has_kwargs(false), prepend(false) {}
193196

194197
/// Function name
195198
char *name = nullptr; /* why no C++ strings? They generate heavier code.. */
@@ -230,6 +233,9 @@ struct function_record {
230233
/// True if this is a method
231234
bool is_method : 1;
232235

236+
/// True if this is a setter
237+
bool is_setter : 1;
238+
233239
/// True if the function has a '*args' argument
234240
bool has_args : 1;
235241

@@ -399,7 +405,7 @@ struct process_attribute<doc> : process_attribute_default<doc> {
399405
template <>
400406
struct process_attribute<const char *> : process_attribute_default<const char *> {
401407
static void init(const char *d, function_record *r) { r->doc = const_cast<char *>(d); }
402-
static void init(const char *d, type_record *r) { r->doc = const_cast<char *>(d); }
408+
static void init(const char *d, type_record *r) { r->doc = d; }
403409
};
404410
template <>
405411
struct process_attribute<char *> : process_attribute<const char *> {};
@@ -426,6 +432,12 @@ struct process_attribute<is_method> : process_attribute_default<is_method> {
426432
}
427433
};
428434

435+
/// Process an attribute which indicates that this function is a setter
436+
template <>
437+
struct process_attribute<is_setter> : process_attribute_default<is_setter> {
438+
static void init(const is_setter &, function_record *r) { r->is_setter = true; }
439+
};
440+
429441
/// Process an attribute which indicates the parent scope of a method
430442
template <>
431443
struct process_attribute<scope> : process_attribute_default<scope> {

dlib/external/pybind11/include/pybind11/buffer_info.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ inline std::vector<ssize_t> f_strides(const std::vector<ssize_t> &shape, ssize_t
3737
return strides;
3838
}
3939

40+
template <typename T, typename SFINAE = void>
41+
struct compare_buffer_info;
42+
4043
PYBIND11_NAMESPACE_END(detail)
4144

4245
/// Information record describing a Python buffer object
@@ -150,6 +153,17 @@ struct buffer_info {
150153
Py_buffer *view() const { return m_view; }
151154
Py_buffer *&view() { return m_view; }
152155

156+
/* True if the buffer item type is equivalent to `T`. */
157+
// To define "equivalent" by example:
158+
// `buffer_info::item_type_is_equivalent_to<int>(b)` and
159+
// `buffer_info::item_type_is_equivalent_to<long>(b)` may both be true
160+
// on some platforms, but `int` and `unsigned` will never be equivalent.
161+
// For the ground truth, please inspect `detail::compare_buffer_info<>`.
162+
template <typename T>
163+
bool item_type_is_equivalent_to() const {
164+
return detail::compare_buffer_info<T>::compare(*this);
165+
}
166+
153167
private:
154168
struct private_ctr_tag {};
155169

@@ -170,9 +184,10 @@ struct buffer_info {
170184

171185
PYBIND11_NAMESPACE_BEGIN(detail)
172186

173-
template <typename T, typename SFINAE = void>
187+
template <typename T, typename SFINAE>
174188
struct compare_buffer_info {
175189
static bool compare(const buffer_info &b) {
190+
// NOLINTNEXTLINE(bugprone-sizeof-expression) Needed for `PyObject *`
176191
return b.format == format_descriptor<T>::format() && b.itemsize == (ssize_t) sizeof(T);
177192
}
178193
};

0 commit comments

Comments
 (0)