40
40
# define PYBIND11_INTERNALS_VERSION 6
41
41
#endif
42
42
43
- // This requirement is mainly to reduce the support burden (see PR #4570).
44
- static_assert (PY_VERSION_HEX < 0x030C0000 || PYBIND11_INTERNALS_VERSION >= 5 ,
45
- " pybind11 ABI version 5 is the minimum for Python 3.12+" );
46
- static_assert (PYBIND11_INTERNALS_VERSION >= 4 ,
47
- " pybind11 ABI version 4 is the minimum for all platforms." );
43
+ #if PYBIND11_INTERNALS_VERSION < 6
44
+ # error "PYBIND11_INTERNALS_VERSION 6 is the minimum for all platforms for pybind11v3."
45
+ #endif
48
46
49
47
PYBIND11_NAMESPACE_BEGIN (PYBIND11_NAMESPACE)
50
48
@@ -63,49 +61,37 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass);
63
61
// Thread Specific Storage (TSS) API.
64
62
// Avoid unnecessary allocation of `Py_tss_t`, since we cannot use
65
63
// `Py_LIMITED_API` anyway.
66
- #if PYBIND11_INTERNALS_VERSION > 4
67
- # define PYBIND11_TLS_KEY_REF Py_tss_t &
68
- # if defined(__clang__)
69
- # define PYBIND11_TLS_KEY_INIT (var ) \
70
- _Pragma (" clang diagnostic push" ) /* */ \
71
- _Pragma(" clang diagnostic ignored \" -Wmissing-field-initializers\" " ) /* */ \
72
- Py_tss_t var \
73
- = Py_tss_NEEDS_INIT; \
74
- _Pragma (" clang diagnostic pop" )
75
- # elif defined(__GNUC__) && !defined(__INTEL_COMPILER)
76
- # define PYBIND11_TLS_KEY_INIT (var ) \
77
- _Pragma (" GCC diagnostic push" ) /* */ \
78
- _Pragma(" GCC diagnostic ignored \" -Wmissing-field-initializers\" " ) /* */ \
79
- Py_tss_t var \
80
- = Py_tss_NEEDS_INIT; \
81
- _Pragma (" GCC diagnostic pop" )
82
- # else
83
- # define PYBIND11_TLS_KEY_INIT (var ) Py_tss_t var = Py_tss_NEEDS_INIT;
84
- # endif
85
- # define PYBIND11_TLS_KEY_CREATE (var ) (PyThread_tss_create(&(var)) == 0 )
86
- # define PYBIND11_TLS_GET_VALUE (key ) PyThread_tss_get(&(key))
87
- # define PYBIND11_TLS_REPLACE_VALUE (key, value ) PyThread_tss_set(&(key), (value))
88
- # define PYBIND11_TLS_DELETE_VALUE (key ) PyThread_tss_set(&(key), nullptr )
89
- # define PYBIND11_TLS_FREE (key ) PyThread_tss_delete(&(key))
64
+ #define PYBIND11_TLS_KEY_REF Py_tss_t &
65
+ #if defined(__clang__)
66
+ # define PYBIND11_TLS_KEY_INIT (var ) \
67
+ _Pragma (" clang diagnostic push" ) /* */ \
68
+ _Pragma(" clang diagnostic ignored \" -Wmissing-field-initializers\" " ) /* */ \
69
+ Py_tss_t var \
70
+ = Py_tss_NEEDS_INIT; \
71
+ _Pragma (" clang diagnostic pop" )
72
+ #elif defined(__GNUC__) && !defined(__INTEL_COMPILER)
73
+ # define PYBIND11_TLS_KEY_INIT (var ) \
74
+ _Pragma (" GCC diagnostic push" ) /* */ \
75
+ _Pragma(" GCC diagnostic ignored \" -Wmissing-field-initializers\" " ) /* */ \
76
+ Py_tss_t var \
77
+ = Py_tss_NEEDS_INIT; \
78
+ _Pragma (" GCC diagnostic pop" )
90
79
#else
91
- # define PYBIND11_TLS_KEY_REF Py_tss_t *
92
- # define PYBIND11_TLS_KEY_INIT (var ) Py_tss_t *var = nullptr ;
93
- # define PYBIND11_TLS_KEY_CREATE (var ) \
94
- (((var) = PyThread_tss_alloc()) != nullptr && (PyThread_tss_create((var)) == 0 ))
95
- # define PYBIND11_TLS_GET_VALUE (key ) PyThread_tss_get((key))
96
- # define PYBIND11_TLS_REPLACE_VALUE (key, value ) PyThread_tss_set((key), (value))
97
- # define PYBIND11_TLS_DELETE_VALUE (key ) PyThread_tss_set((key), nullptr )
98
- # define PYBIND11_TLS_FREE (key ) PyThread_tss_free(key)
80
+ # define PYBIND11_TLS_KEY_INIT (var ) Py_tss_t var = Py_tss_NEEDS_INIT;
99
81
#endif
82
+ #define PYBIND11_TLS_KEY_CREATE (var ) (PyThread_tss_create(&(var)) == 0 )
83
+ #define PYBIND11_TLS_GET_VALUE (key ) PyThread_tss_get(&(key))
84
+ #define PYBIND11_TLS_REPLACE_VALUE (key, value ) PyThread_tss_set(&(key), (value))
85
+ #define PYBIND11_TLS_DELETE_VALUE (key ) PyThread_tss_set(&(key), nullptr )
86
+ #define PYBIND11_TLS_FREE (key ) PyThread_tss_delete(&(key))
100
87
101
88
// Python loads modules by default with dlopen with the RTLD_LOCAL flag; under libc++ and possibly
102
89
// other STLs, this means `typeid(A)` from one module won't equal `typeid(A)` from another module
103
90
// even when `A` is the same, non-hidden-visibility type (e.g. from a common include). Under
104
91
// libstdc++, this doesn't happen: equality and the type_index hash are based on the type name,
105
92
// which works. If not under a known-good stl, provide our own name-based hash and equality
106
93
// functions that use the type name.
107
- #if (PYBIND11_INTERNALS_VERSION <= 4 && defined(__GLIBCXX__)) \
108
- || (PYBIND11_INTERNALS_VERSION >= 5 && !defined(_LIBCPP_VERSION))
94
+ #if !defined(_LIBCPP_VERSION)
109
95
inline bool same_type (const std::type_info &lhs, const std::type_info &rhs) { return lhs == rhs; }
110
96
using type_hash = std::hash<std::type_index>;
111
97
using type_equal_to = std::equal_to<std::type_index>;
@@ -193,35 +179,26 @@ struct internals {
193
179
std::forward_list<ExceptionTranslator> registered_exception_translators;
194
180
std::unordered_map<std::string, void *> shared_data; // Custom data to be shared across
195
181
// extensions
196
- #if PYBIND11_INTERNALS_VERSION == 4
197
- std::vector<PyObject *> unused_loader_patient_stack_remove_at_v5;
198
- #endif
199
- std::forward_list<std::string> static_strings; // Stores the std::strings backing
200
- // detail::c_str()
182
+ std::forward_list<std::string> static_strings; // Stores the std::strings backing
183
+ // detail::c_str()
201
184
PyTypeObject *static_property_type;
202
185
PyTypeObject *default_metaclass;
203
186
PyObject *instance_base;
204
187
// Unused if PYBIND11_SIMPLE_GIL_MANAGEMENT is defined:
205
188
PYBIND11_TLS_KEY_INIT (tstate)
206
- #if PYBIND11_INTERNALS_VERSION > 4
207
189
PYBIND11_TLS_KEY_INIT (loader_life_support_tls_key)
208
- #endif // PYBIND11_INTERNALS_VERSION > 4
209
190
// Unused if PYBIND11_SIMPLE_GIL_MANAGEMENT is defined:
210
191
PyInterpreterState *istate = nullptr ;
211
192
212
- #if PYBIND11_INTERNALS_VERSION > 4
213
193
// Note that we have to use a std::string to allocate memory to ensure a unique address
214
194
// We want unique addresses since we use pointer equality to compare function records
215
195
std::string function_record_capsule_name = internals_function_record_capsule_name;
216
- #endif
217
196
218
197
internals () = default ;
219
198
internals (const internals &other) = delete ;
220
199
internals &operator =(const internals &other) = delete ;
221
200
~internals () {
222
- #if PYBIND11_INTERNALS_VERSION > 4
223
201
PYBIND11_TLS_FREE (loader_life_support_tls_key);
224
- #endif // PYBIND11_INTERNALS_VERSION > 4
225
202
226
203
// This destructor is called *after* Py_Finalize() in finalize_interpreter().
227
204
// That *SHOULD BE* fine. The following details what happens when PyThread_tss_free is
@@ -386,7 +363,7 @@ inline void translate_local_exception(std::exception_ptr p) {
386
363
387
364
inline object get_python_state_dict () {
388
365
object state_dict;
389
- #if PYBIND11_INTERNALS_VERSION <= 4 || defined(PYPY_VERSION) || defined(GRAALVM_PYTHON)
366
+ #if defined(PYPY_VERSION) || defined(GRAALVM_PYTHON)
390
367
state_dict = reinterpret_borrow<object>(PyEval_GetBuiltins ());
391
368
#else
392
369
# if PY_VERSION_HEX < 0x03090000
@@ -484,13 +461,12 @@ PYBIND11_NOINLINE internals &get_internals() {
484
461
}
485
462
PYBIND11_TLS_REPLACE_VALUE (internals_ptr->tstate , tstate);
486
463
487
- #if PYBIND11_INTERNALS_VERSION > 4
488
464
// NOLINTNEXTLINE(bugprone-assignment-in-if-condition)
489
465
if (!PYBIND11_TLS_KEY_CREATE (internals_ptr->loader_life_support_tls_key )) {
490
466
pybind11_fail (" get_internals: could not successfully initialize the "
491
467
" loader_life_support TSS key!" );
492
468
}
493
- # endif
469
+
494
470
internals_ptr->istate = tstate->interp ;
495
471
state_dict[PYBIND11_INTERNALS_ID] = capsule (reinterpret_cast <void *>(internals_pp));
496
472
internals_ptr->registered_exception_translators .push_front (&translate_exception);
@@ -520,40 +496,6 @@ PYBIND11_NOINLINE internals &get_internals() {
520
496
struct local_internals {
521
497
type_map<type_info *> registered_types_cpp;
522
498
std::forward_list<ExceptionTranslator> registered_exception_translators;
523
- #if PYBIND11_INTERNALS_VERSION == 4
524
-
525
- // For ABI compatibility, we can't store the loader_life_support TLS key in
526
- // the `internals` struct directly. Instead, we store it in `shared_data` and
527
- // cache a copy in `local_internals`. If we allocated a separate TLS key for
528
- // each instance of `local_internals`, we could end up allocating hundreds of
529
- // TLS keys if hundreds of different pybind11 modules are loaded (which is a
530
- // plausible number).
531
- PYBIND11_TLS_KEY_INIT (loader_life_support_tls_key)
532
-
533
- // Holds the shared TLS key for the loader_life_support stack.
534
- struct shared_loader_life_support_data {
535
- PYBIND11_TLS_KEY_INIT (loader_life_support_tls_key)
536
- shared_loader_life_support_data () {
537
- // NOLINTNEXTLINE(bugprone-assignment-in-if-condition)
538
- if (!PYBIND11_TLS_KEY_CREATE (loader_life_support_tls_key)) {
539
- pybind11_fail (" local_internals: could not successfully initialize the "
540
- " loader_life_support TLS key!" );
541
- }
542
- }
543
- // We can't help but leak the TLS key, because Python never unloads extension modules.
544
- };
545
-
546
- local_internals () {
547
- auto &internals = get_internals ();
548
- // Get or create the `loader_life_support_stack_key`.
549
- auto &ptr = internals.shared_data [" _life_support" ];
550
- if (!ptr) {
551
- ptr = new shared_loader_life_support_data;
552
- }
553
- loader_life_support_tls_key
554
- = static_cast <shared_loader_life_support_data *>(ptr)->loader_life_support_tls_key ;
555
- }
556
- #endif // PYBIND11_INTERNALS_VERSION == 4
557
499
};
558
500
559
501
// / Works like `get_internals`, but for things which are locally registered.
@@ -660,7 +602,7 @@ const char *c_str(Args &&...args) {
660
602
661
603
inline const char *get_function_record_capsule_name () {
662
604
// On GraalPy, pointer equality of the names is currently not guaranteed
663
- #if PYBIND11_INTERNALS_VERSION > 4 && !defined(GRAALVM_PYTHON)
605
+ #if !defined(GRAALVM_PYTHON)
664
606
return get_internals ().function_record_capsule_name .c_str ();
665
607
#else
666
608
return nullptr ;
0 commit comments