Skip to content

Commit fdc89eb

Browse files
committed
Update pybind11
1 parent ac17781 commit fdc89eb

File tree

13 files changed

+419
-149
lines changed

13 files changed

+419
-149
lines changed

libs/pybind11/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ set(PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS} CACHE INTERNAL "")
3838
set(PYTHON_LIBRARIES ${PYTHON_LIBRARIES} CACHE INTERNAL "")
3939
set(PYTHON_MODULE_PREFIX ${PYTHON_MODULE_PREFIX} CACHE INTERNAL "")
4040
set(PYTHON_MODULE_EXTENSION ${PYTHON_MODULE_EXTENSION} CACHE INTERNAL "")
41+
set(PYTHON_VERSION_MAJOR ${PYTHON_VERSION_MAJOR} CACHE INTERNAL "")
42+
set(PYTHON_VERSION_MINOR ${PYTHON_VERSION_MINOR} CACHE INTERNAL "")
4143

4244
# NB: when adding a header don't forget to also add it to setup.py
4345
set(PYBIND11_HEADERS

libs/pybind11/include/pybind11/attr.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,14 @@ struct type_record {
214214
/// How large is the underlying C++ type?
215215
size_t type_size = 0;
216216

217+
/// What is the alignment of the underlying C++ type?
218+
size_t type_align = 0;
219+
217220
/// How large is the type's holder?
218221
size_t holder_size = 0;
219222

220223
/// The global operator new can be overridden with a class-specific variant
221-
void *(*operator_new)(size_t) = ::operator new;
224+
void *(*operator_new)(size_t) = nullptr;
222225

223226
/// Function pointer to class_<..>::init_instance
224227
void (*init_instance)(instance *, const void *) = nullptr;
@@ -278,7 +281,7 @@ struct type_record {
278281
}
279282
};
280283

281-
inline function_call::function_call(function_record &f, handle p) :
284+
inline function_call::function_call(const function_record &f, handle p) :
282285
func(f), parent(p) {
283286
args.reserve(f.nargs);
284287
args_convert.reserve(f.nargs);

libs/pybind11/include/pybind11/cast.h

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,17 @@ class type_caster_generic {
571571
// Lazy allocation for unallocated values:
572572
if (vptr == nullptr) {
573573
auto *type = v_h.type ? v_h.type : typeinfo;
574-
vptr = type->operator_new(type->type_size);
574+
if (type->operator_new) {
575+
vptr = type->operator_new(type->type_size);
576+
} else {
577+
#if defined(PYBIND11_CPP17)
578+
if (type->type_align > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
579+
vptr = ::operator new(type->type_size,
580+
(std::align_val_t) type->type_align);
581+
else
582+
#endif
583+
vptr = ::operator new(type->type_size);
584+
}
575585
}
576586
value = vptr;
577587
}
@@ -1601,7 +1611,8 @@ template <typename T> using move_never = none_of<move_always<T>, move_if_unrefer
16011611
// everything else returns a reference/pointer to a local variable.
16021612
template <typename type> using cast_is_temporary_value_reference = bool_constant<
16031613
(std::is_reference<type>::value || std::is_pointer<type>::value) &&
1604-
!std::is_base_of<type_caster_generic, make_caster<type>>::value
1614+
!std::is_base_of<type_caster_generic, make_caster<type>>::value &&
1615+
!std::is_same<intrinsic_t<type>, void>::value
16051616
>;
16061617

16071618
// When a value returned from a C++ function is being cast back to Python, we almost always want to
@@ -1614,8 +1625,9 @@ template <typename Return, typename SFINAE = void> struct return_value_policy_ov
16141625
template <typename Return> struct return_value_policy_override<Return,
16151626
detail::enable_if_t<std::is_base_of<type_caster_generic, make_caster<Return>>::value, void>> {
16161627
static return_value_policy policy(return_value_policy p) {
1617-
return !std::is_lvalue_reference<Return>::value && !std::is_pointer<Return>::value
1618-
? return_value_policy::move : p;
1628+
return !std::is_lvalue_reference<Return>::value &&
1629+
!std::is_pointer<Return>::value
1630+
? return_value_policy::move : p;
16191631
}
16201632
};
16211633

@@ -1843,7 +1855,7 @@ struct function_record;
18431855

18441856
/// Internal data associated with a single function call
18451857
struct function_call {
1846-
function_call(function_record &f, handle p); // Implementation in attr.h
1858+
function_call(const function_record &f, handle p); // Implementation in attr.h
18471859

18481860
/// The function data:
18491861
const function_record &func;

libs/pybind11/include/pybind11/detail/common.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@
2727
# endif
2828
#endif
2929

30-
#if !defined(_MSC_VER) && !defined(__INTEL_COMPILER)
30+
#if !(defined(_MSC_VER) && __cplusplus == 199711L) && !defined(__INTEL_COMPILER)
3131
# if __cplusplus >= 201402L
3232
# define PYBIND11_CPP14
33-
# if __cplusplus > 201402L /* Temporary: should be updated to >= the final C++17 value once known */
33+
# if __cplusplus >= 201703L
3434
# define PYBIND11_CPP17
3535
# endif
3636
# endif
37-
#elif defined(_MSC_VER)
37+
#elif defined(_MSC_VER) && __cplusplus == 199711L
3838
// MSVC sets _MSVC_LANG rather than __cplusplus (supposedly until the standard is fully implemented)
39+
// Unless you use the /Zc:__cplusplus flag on Visual Studio 2017 15.7 Preview 3 or newer
3940
# if _MSVC_LANG >= 201402L
4041
# define PYBIND11_CPP14
4142
# if _MSVC_LANG > 201402L && _MSC_VER >= 1910
@@ -476,7 +477,7 @@ template <typename...> struct void_t_impl { using type = void; };
476477
template <typename... Ts> using void_t = typename void_t_impl<Ts...>::type;
477478

478479
/// Compile-time all/any/none of that check the boolean value of all template types
479-
#ifdef __cpp_fold_expressions
480+
#if defined(__cpp_fold_expressions) && !(defined(_MSC_VER) && (_MSC_VER < 1916))
480481
template <class... Ts> using all_of = bool_constant<(Ts::value && ...)>;
481482
template <class... Ts> using any_of = bool_constant<(Ts::value || ...)>;
482483
#elif !defined(_MSC_VER)

libs/pybind11/include/pybind11/detail/internals.h

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,28 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass);
2121
// The old Python Thread Local Storage (TLS) API is deprecated in Python 3.7 in favor of the new
2222
// Thread Specific Storage (TSS) API.
2323
#if PY_VERSION_HEX >= 0x03070000
24-
#define PYBIND11_TLS_KEY_INIT(var) Py_tss_t *var = nullptr
25-
#define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get((key))
26-
#define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set((key), (tstate))
27-
#define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set((key), nullptr)
24+
# define PYBIND11_TLS_KEY_INIT(var) Py_tss_t *var = nullptr
25+
# define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get((key))
26+
# define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set((key), (tstate))
27+
# define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set((key), nullptr)
2828
#else
2929
// Usually an int but a long on Cygwin64 with Python 3.x
30-
#define PYBIND11_TLS_KEY_INIT(var) decltype(PyThread_create_key()) var = 0
31-
#define PYBIND11_TLS_GET_VALUE(key) PyThread_get_key_value((key))
32-
#if PY_MAJOR_VERSION < 3
33-
#define PYBIND11_TLS_REPLACE_VALUE(key, value) do { PyThread_delete_key_value((key)); PyThread_set_key_value((key), (value)); } while (false)
34-
#else
35-
#define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_set_key_value((key), (value))
36-
#endif
37-
#define PYBIND11_TLS_DELETE_VALUE(key) PyThread_set_key_value((key), nullptr)
30+
# define PYBIND11_TLS_KEY_INIT(var) decltype(PyThread_create_key()) var = 0
31+
# define PYBIND11_TLS_GET_VALUE(key) PyThread_get_key_value((key))
32+
# if PY_MAJOR_VERSION < 3
33+
# define PYBIND11_TLS_DELETE_VALUE(key) \
34+
PyThread_delete_key_value(key)
35+
# define PYBIND11_TLS_REPLACE_VALUE(key, value) \
36+
do { \
37+
PyThread_delete_key_value((key)); \
38+
PyThread_set_key_value((key), (value)); \
39+
} while (false)
40+
# else
41+
# define PYBIND11_TLS_DELETE_VALUE(key) \
42+
PyThread_set_key_value((key), nullptr)
43+
# define PYBIND11_TLS_REPLACE_VALUE(key, value) \
44+
PyThread_set_key_value((key), (value))
45+
# endif
3846
#endif
3947

4048
// Python loads modules by default with dlopen with the RTLD_LOCAL flag; under libc++ and possibly
@@ -108,7 +116,7 @@ struct internals {
108116
struct type_info {
109117
PyTypeObject *type;
110118
const std::type_info *cpptype;
111-
size_t type_size, holder_size_in_ptrs;
119+
size_t type_size, type_align, holder_size_in_ptrs;
112120
void *(*operator_new)(size_t);
113121
void (*init_instance)(instance *, const void *);
114122
void (*dealloc)(value_and_holder &v_h);
@@ -130,7 +138,13 @@ struct type_info {
130138
};
131139

132140
/// Tracks the `internals` and `type_info` ABI version independent of the main library version
133-
#define PYBIND11_INTERNALS_VERSION 2
141+
#define PYBIND11_INTERNALS_VERSION 3
142+
143+
#if defined(_DEBUG)
144+
# define PYBIND11_BUILD_TYPE "_debug"
145+
#else
146+
# define PYBIND11_BUILD_TYPE ""
147+
#endif
134148

135149
#if defined(WITH_THREAD)
136150
# define PYBIND11_INTERNALS_KIND ""
@@ -139,10 +153,10 @@ struct type_info {
139153
#endif
140154

141155
#define PYBIND11_INTERNALS_ID "__pybind11_internals_v" \
142-
PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_INTERNALS_KIND "__"
156+
PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_INTERNALS_KIND PYBIND11_BUILD_TYPE "__"
143157

144158
#define PYBIND11_MODULE_LOCAL_ID "__pybind11_module_local_v" \
145-
PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_INTERNALS_KIND "__"
159+
PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_INTERNALS_KIND PYBIND11_BUILD_TYPE "__"
146160

147161
/// Each module locally stores a pointer to the `internals` data. The data
148162
/// itself is shared among modules with the same `PYBIND11_INTERNALS_ID`.

libs/pybind11/include/pybind11/embed.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,14 @@ NAMESPACE_END(detail)
9090
Initialize the Python interpreter. No other pybind11 or CPython API functions can be
9191
called before this is done; with the exception of `PYBIND11_EMBEDDED_MODULE`. The
9292
optional parameter can be used to skip the registration of signal handlers (see the
93-
Python documentation for details). Calling this function again after the interpreter
93+
`Python documentation`_ for details). Calling this function again after the interpreter
9494
has already been initialized is a fatal error.
95+
96+
If initializing the Python interpreter fails, then the program is terminated. (This
97+
is controlled by the CPython runtime and is an exception to pybind11's normal behavior
98+
of throwing exceptions on errors.)
99+
100+
.. _Python documentation: https://docs.python.org/3/c-api/init.html#c.Py_InitializeEx
95101
\endrst */
96102
inline void initialize_interpreter(bool init_signal_handlers = true) {
97103
if (Py_IsInitialized())

libs/pybind11/include/pybind11/iostream.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class pythonbuf : public std::streambuf {
3434
*pptr() = traits_type::to_char_type(c);
3535
pbump(1);
3636
}
37-
return sync() ? traits_type::not_eof(c) : traits_type::eof();
37+
return sync() == 0 ? traits_type::not_eof(c) : traits_type::eof();
3838
}
3939

4040
int sync() {
@@ -194,7 +194,7 @@ inline class_<detail::OstreamRedirect> add_ostream_redirect(module m, std::strin
194194
return class_<detail::OstreamRedirect>(m, name.c_str(), module_local())
195195
.def(init<bool,bool>(), arg("stdout")=true, arg("stderr")=true)
196196
.def("__enter__", &detail::OstreamRedirect::enter)
197-
.def("__exit__", [](detail::OstreamRedirect &self, args) { self.exit(); });
197+
.def("__exit__", [](detail::OstreamRedirect &self_, args) { self_.exit(); });
198198
}
199199

200200
NAMESPACE_END(PYBIND11_NAMESPACE)

libs/pybind11/include/pybind11/numpy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1593,7 +1593,7 @@ Helper vectorize(Return (Class::*f)(Args...)) {
15931593
return Helper(std::mem_fn(f));
15941594
}
15951595

1596-
// Vectorize a class method (non-const):
1596+
// Vectorize a class method (const):
15971597
template <typename Return, typename Class, typename... Args,
15981598
typename Helper = detail::vectorize_helper<decltype(std::mem_fn(std::declval<Return (Class::*)(Args...) const>())), Return, const Class *, Args...>>
15991599
Helper vectorize(Return (Class::*f)(Args...) const) {

0 commit comments

Comments
 (0)