Skip to content

Commit 217bc21

Browse files
author
Kasper Peeters
committed
Update pybind for python 3.11 compatibility.
1 parent 38d310f commit 217bc21

File tree

17 files changed

+315
-165
lines changed

17 files changed

+315
-165
lines changed

cmake/version.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
set(CADABRA_VERSION_MAJOR 2)
22
set(CADABRA_VERSION_MINOR 4)
33
set(CADABRA_VERSION_PATCH 2)
4-
set(CADABRA_VERSION_TWEAK 1)
4+
set(CADABRA_VERSION_TWEAK 2)
55
set(COPYRIGHT_YEARS "2001-2022")
66
math(EXPR SYSTEM_BITS "${CMAKE_SIZEOF_VOID_P} * 8")
77
find_program(GIT git PATHS ${GIT_DIR})

jupyterkernel/cadabra2_jupyter/kernel.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ def _send_result(self, res_str):
115115
)
116116

117117
def _send_image(self, img):
118-
raise NotImplementedError
118+
self.send_response(
119+
self.iopub_socket,
120+
"display_data",
121+
{"data": {"image/png": "{}".format(res_str)}, "metadata": {}},
122+
)
119123

120124
def _send_code(self, res_str):
121125
self.send_response(

libs/pybind11/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,16 @@ endif()
9191
option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT})
9292
option(PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT})
9393
option(PYBIND11_NOPYTHON "Disable search for Python" OFF)
94+
option(PYBIND11_SIMPLE_GIL_MANAGEMENT
95+
"Use simpler GIL management logic that does not support disassociation" OFF)
9496
set(PYBIND11_INTERNALS_VERSION
9597
""
9698
CACHE STRING "Override the ABI version, may be used to enable the unstable ABI.")
9799

100+
if(PYBIND11_SIMPLE_GIL_MANAGEMENT)
101+
add_compile_definitions(PYBIND11_SIMPLE_GIL_MANAGEMENT)
102+
endif()
103+
98104
cmake_dependent_option(
99105
USE_PYTHON_INCLUDE_DIR
100106
"Install pybind11 headers in Python include directory instead of default installation prefix"

libs/pybind11/include/pybind11/cast.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
#include <vector>
3030

3131
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
32+
33+
PYBIND11_WARNING_DISABLE_MSVC(4127)
34+
3235
PYBIND11_NAMESPACE_BEGIN(detail)
3336

3437
template <typename type, typename SFINAE = void>
@@ -389,7 +392,7 @@ struct string_caster {
389392

390393
// For UTF-8 we avoid the need for a temporary `bytes` object by using
391394
// `PyUnicode_AsUTF8AndSize`.
392-
if (PYBIND11_SILENCE_MSVC_C4127(UTF_N == 8)) {
395+
if (UTF_N == 8) {
393396
Py_ssize_t size = -1;
394397
const auto *buffer
395398
= reinterpret_cast<const CharT *>(PyUnicode_AsUTF8AndSize(load_src.ptr(), &size));
@@ -416,7 +419,7 @@ struct string_caster {
416419
= reinterpret_cast<const CharT *>(PYBIND11_BYTES_AS_STRING(utfNbytes.ptr()));
417420
size_t length = (size_t) PYBIND11_BYTES_SIZE(utfNbytes.ptr()) / sizeof(CharT);
418421
// Skip BOM for UTF-16/32
419-
if (PYBIND11_SILENCE_MSVC_C4127(UTF_N > 8)) {
422+
if (UTF_N > 8) {
420423
buffer++;
421424
length--;
422425
}
@@ -572,7 +575,7 @@ struct type_caster<CharT, enable_if_t<is_std_char_type<CharT>::value>> {
572575
// figure out how long the first encoded character is in bytes to distinguish between these
573576
// two errors. We also allow want to allow unicode characters U+0080 through U+00FF, as
574577
// those can fit into a single char value.
575-
if (PYBIND11_SILENCE_MSVC_C4127(StringCaster::UTF_N == 8) && str_len > 1 && str_len <= 4) {
578+
if (StringCaster::UTF_N == 8 && str_len > 1 && str_len <= 4) {
576579
auto v0 = static_cast<unsigned char>(value[0]);
577580
// low bits only: 0-127
578581
// 0b110xxxxx - start of 2-byte sequence
@@ -598,7 +601,7 @@ struct type_caster<CharT, enable_if_t<is_std_char_type<CharT>::value>> {
598601
// UTF-16 is much easier: we can only have a surrogate pair for values above U+FFFF, thus a
599602
// surrogate pair with total length 2 instantly indicates a range error (but not a "your
600603
// string was too long" error).
601-
else if (PYBIND11_SILENCE_MSVC_C4127(StringCaster::UTF_N == 16) && str_len == 2) {
604+
else if (StringCaster::UTF_N == 16 && str_len == 2) {
602605
one_char = static_cast<CharT>(value[0]);
603606
if (one_char >= 0xD800 && one_char < 0xE000) {
604607
throw value_error("Character code point not in range(0x10000)");

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

Lines changed: 74 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,69 @@
1717
// Additional convention: 0xD = dev
1818
#define PYBIND11_VERSION_HEX 0x020B00D1
1919

20-
#define PYBIND11_NAMESPACE_BEGIN(name) namespace name {
21-
#define PYBIND11_NAMESPACE_END(name) }
20+
// Define some generic pybind11 helper macros for warning management.
21+
//
22+
// Note that compiler-specific push/pop pairs are baked into the
23+
// PYBIND11_NAMESPACE_BEGIN/PYBIND11_NAMESPACE_END pair of macros. Therefore manual
24+
// PYBIND11_WARNING_PUSH/PYBIND11_WARNING_POP are usually only needed in `#include` sections.
25+
//
26+
// If you find you need to suppress a warning, please try to make the suppression as local as
27+
// possible using these macros. Please also be sure to push/pop with the pybind11 macros. Please
28+
// only use compiler specifics if you need to check specific versions, e.g. Apple Clang vs. vanilla
29+
// Clang.
30+
#if defined(_MSC_VER)
31+
# define PYBIND11_COMPILER_MSVC
32+
# define PYBIND11_PRAGMA(...) __pragma(__VA_ARGS__)
33+
# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(warning(push))
34+
# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(warning(pop))
35+
#elif defined(__INTEL_COMPILER)
36+
# define PYBIND11_COMPILER_INTEL
37+
# define PYBIND11_PRAGMA(...) _Pragma(# __VA_ARGS__)
38+
# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(warning push)
39+
# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(warning pop)
40+
#elif defined(__clang__)
41+
# define PYBIND11_COMPILER_CLANG
42+
# define PYBIND11_PRAGMA(...) _Pragma(# __VA_ARGS__)
43+
# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(clang diagnostic push)
44+
# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(clang diagnostic push)
45+
#elif defined(__GNUC__)
46+
# define PYBIND11_COMPILER_GCC
47+
# define PYBIND11_PRAGMA(...) _Pragma(# __VA_ARGS__)
48+
# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(GCC diagnostic push)
49+
# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(GCC diagnostic pop)
50+
#endif
51+
52+
#ifdef PYBIND11_COMPILER_MSVC
53+
# define PYBIND11_WARNING_DISABLE_MSVC(name) PYBIND11_PRAGMA(warning(disable : name))
54+
#else
55+
# define PYBIND11_WARNING_DISABLE_MSVC(name)
56+
#endif
57+
58+
#ifdef PYBIND11_COMPILER_CLANG
59+
# define PYBIND11_WARNING_DISABLE_CLANG(name) PYBIND11_PRAGMA(clang diagnostic ignored name)
60+
#else
61+
# define PYBIND11_WARNING_DISABLE_CLANG(name)
62+
#endif
63+
64+
#ifdef PYBIND11_COMPILER_GCC
65+
# define PYBIND11_WARNING_DISABLE_GCC(name) PYBIND11_PRAGMA(GCC diagnostic ignored name)
66+
#else
67+
# define PYBIND11_WARNING_DISABLE_GCC(name)
68+
#endif
69+
70+
#ifdef PYBIND11_COMPILER_INTEL
71+
# define PYBIND11_WARNING_DISABLE_INTEL(name) PYBIND11_PRAGMA(warning disable name)
72+
#else
73+
# define PYBIND11_WARNING_DISABLE_INTEL(name)
74+
#endif
75+
76+
#define PYBIND11_NAMESPACE_BEGIN(name) \
77+
namespace name { \
78+
PYBIND11_WARNING_PUSH
79+
80+
#define PYBIND11_NAMESPACE_END(name) \
81+
PYBIND11_WARNING_POP \
82+
}
2283

2384
// Robust support for some features and loading modules compiled against different pybind versions
2485
// requires forcing hidden visibility on pybind code, so we enforce this by setting the attribute
@@ -96,13 +157,10 @@
96157
#endif
97158

98159
#if !defined(PYBIND11_EXPORT_EXCEPTION)
99-
# ifdef __MINGW32__
100-
// workaround for:
101-
// error: 'dllexport' implies default visibility, but xxx has already been declared with a
102-
// different visibility
103-
# define PYBIND11_EXPORT_EXCEPTION
104-
# else
160+
# if defined(__apple_build_version__)
105161
# define PYBIND11_EXPORT_EXCEPTION PYBIND11_EXPORT
162+
# else
163+
# define PYBIND11_EXPORT_EXCEPTION
106164
# endif
107165
#endif
108166

@@ -154,9 +212,9 @@
154212

155213
/// Include Python header, disable linking to pythonX_d.lib on Windows in debug mode
156214
#if defined(_MSC_VER)
157-
# pragma warning(push)
215+
PYBIND11_WARNING_PUSH
216+
PYBIND11_WARNING_DISABLE_MSVC(4505)
158217
// C4505: 'PySlice_GetIndicesEx': unreferenced local function has been removed (PyPy only)
159-
# pragma warning(disable : 4505)
160218
# if defined(_DEBUG) && !defined(Py_DEBUG)
161219
// Workaround for a VS 2022 issue.
162220
// NOTE: This workaround knowingly violates the Python.h include order requirement:
@@ -206,6 +264,7 @@
206264
#endif
207265

208266
#include <Python.h>
267+
// Reminder: WITH_THREAD is always defined if PY_VERSION_HEX >= 0x03070000
209268
#if PY_VERSION_HEX < 0x03060000
210269
# error "PYTHON < 3.6 IS UNSUPPORTED. pybind11 v2.9 was the last to support Python 2 and 3.5."
211270
#endif
@@ -229,12 +288,16 @@
229288
# undef copysign
230289
#endif
231290

291+
#if defined(PYPY_VERSION) && !defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
292+
# define PYBIND11_SIMPLE_GIL_MANAGEMENT
293+
#endif
294+
232295
#if defined(_MSC_VER)
233296
# if defined(PYBIND11_DEBUG_MARKER)
234297
# define _DEBUG
235298
# undef PYBIND11_DEBUG_MARKER
236299
# endif
237-
# pragma warning(pop)
300+
PYBIND11_WARNING_POP
238301
#endif
239302

240303
#include <cstddef>
@@ -899,22 +962,13 @@ using expand_side_effects = bool[];
899962

900963
PYBIND11_NAMESPACE_END(detail)
901964

902-
#if defined(_MSC_VER)
903-
# pragma warning(push)
904-
# pragma warning(disable : 4275)
905-
// warning C4275: An exported class was derived from a class that wasn't exported.
906-
// Can be ignored when derived from a STL class.
907-
#endif
908965
/// C++ bindings of builtin Python exceptions
909966
class PYBIND11_EXPORT_EXCEPTION builtin_exception : public std::runtime_error {
910967
public:
911968
using std::runtime_error::runtime_error;
912969
/// Set the error using the Python C API
913970
virtual void set_error() const = 0;
914971
};
915-
#if defined(_MSC_VER)
916-
# pragma warning(pop)
917-
#endif
918972

919973
#define PYBIND11_RUNTIME_EXCEPTION(name, type) \
920974
class PYBIND11_EXPORT_EXCEPTION name : public builtin_exception { \
@@ -1143,17 +1197,6 @@ constexpr
11431197
# define PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(...)
11441198
#endif
11451199

1146-
#if defined(_MSC_VER) // All versions (as of July 2021).
1147-
1148-
// warning C4127: Conditional expression is constant
1149-
constexpr inline bool silence_msvc_c4127(bool cond) { return cond; }
1150-
1151-
# define PYBIND11_SILENCE_MSVC_C4127(...) ::pybind11::detail::silence_msvc_c4127(__VA_ARGS__)
1152-
1153-
#else
1154-
# define PYBIND11_SILENCE_MSVC_C4127(...) __VA_ARGS__
1155-
#endif
1156-
11571200
#if defined(__clang__) \
11581201
&& (defined(__apple_build_version__) /* AppleClang 13.0.0.13000029 was the only data point \
11591202
available. */ \

libs/pybind11/include/pybind11/detail/init.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#include "class.h"
1313

1414
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
15+
16+
PYBIND11_WARNING_DISABLE_MSVC(4127)
17+
1518
PYBIND11_NAMESPACE_BEGIN(detail)
1619

1720
template <>
@@ -115,7 +118,7 @@ template <typename Class>
115118
void construct(value_and_holder &v_h, Cpp<Class> *ptr, bool need_alias) {
116119
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias);
117120
no_nullptr(ptr);
118-
if (PYBIND11_SILENCE_MSVC_C4127(Class::has_alias) && need_alias && !is_alias<Class>(ptr)) {
121+
if (Class::has_alias && need_alias && !is_alias<Class>(ptr)) {
119122
// We're going to try to construct an alias by moving the cpp type. Whether or not
120123
// that succeeds, we still need to destroy the original cpp pointer (either the
121124
// moved away leftover, if the alias construction works, or the value itself if we
@@ -156,7 +159,7 @@ void construct(value_and_holder &v_h, Holder<Class> holder, bool need_alias) {
156159
auto *ptr = holder_helper<Holder<Class>>::get(holder);
157160
no_nullptr(ptr);
158161
// If we need an alias, check that the held pointer is actually an alias instance
159-
if (PYBIND11_SILENCE_MSVC_C4127(Class::has_alias) && need_alias && !is_alias<Class>(ptr)) {
162+
if (Class::has_alias && need_alias && !is_alias<Class>(ptr)) {
160163
throw type_error("pybind11::init(): construction failed: returned holder-wrapped instance "
161164
"is not an alias instance");
162165
}
@@ -174,7 +177,7 @@ void construct(value_and_holder &v_h, Cpp<Class> &&result, bool need_alias) {
174177
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias);
175178
static_assert(std::is_move_constructible<Cpp<Class>>::value,
176179
"pybind11::init() return-by-value factory function requires a movable class");
177-
if (PYBIND11_SILENCE_MSVC_C4127(Class::has_alias) && need_alias) {
180+
if (Class::has_alias && need_alias) {
178181
construct_alias_from_cpp<Class>(is_alias_constructible<Class>{}, v_h, std::move(result));
179182
} else {
180183
v_h.value_ptr() = new Cpp<Class>(std::move(result));

0 commit comments

Comments
 (0)