44
44
# endif
45
45
#endif
46
46
47
- // This requirement is mainly to reduce the support burden (see PR #4570).
48
- static_assert (PY_VERSION_HEX < 0x030C0000 || PYBIND11_INTERNALS_VERSION >= 5 ,
49
- " pybind11 ABI version 5 is the minimum for Python 3.12+" );
50
- static_assert (PYBIND11_INTERNALS_VERSION >= 4 ,
51
- " pybind11 ABI version 4 is the minimum for all platforms." );
47
+ #if PYBIND11_INTERNALS_VERSION < 6
48
+ # error "PYBIND11_INTERNALS_VERSION 6 is the minimum for all platforms for pybind11v3."
49
+ #endif
52
50
53
51
PYBIND11_NAMESPACE_BEGIN (PYBIND11_NAMESPACE)
54
52
@@ -67,49 +65,37 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass);
67
65
// Thread Specific Storage (TSS) API.
68
66
// Avoid unnecessary allocation of `Py_tss_t`, since we cannot use
69
67
// `Py_LIMITED_API` anyway.
70
- #if PYBIND11_INTERNALS_VERSION > 4
71
- # define PYBIND11_TLS_KEY_REF Py_tss_t &
72
- # if defined(__clang__)
73
- # define PYBIND11_TLS_KEY_INIT (var ) \
74
- _Pragma (" clang diagnostic push" ) /* */ \
75
- _Pragma(" clang diagnostic ignored \" -Wmissing-field-initializers\" " ) /* */ \
76
- Py_tss_t var \
77
- = Py_tss_NEEDS_INIT; \
78
- _Pragma (" clang diagnostic pop" )
79
- # elif defined(__GNUC__) && !defined(__INTEL_COMPILER)
80
- # define PYBIND11_TLS_KEY_INIT (var ) \
81
- _Pragma (" GCC diagnostic push" ) /* */ \
82
- _Pragma(" GCC diagnostic ignored \" -Wmissing-field-initializers\" " ) /* */ \
83
- Py_tss_t var \
84
- = Py_tss_NEEDS_INIT; \
85
- _Pragma (" GCC diagnostic pop" )
86
- # else
87
- # define PYBIND11_TLS_KEY_INIT (var ) Py_tss_t var = Py_tss_NEEDS_INIT;
88
- # endif
89
- # define PYBIND11_TLS_KEY_CREATE (var ) (PyThread_tss_create(&(var)) == 0 )
90
- # define PYBIND11_TLS_GET_VALUE (key ) PyThread_tss_get(&(key))
91
- # define PYBIND11_TLS_REPLACE_VALUE (key, value ) PyThread_tss_set(&(key), (value))
92
- # define PYBIND11_TLS_DELETE_VALUE (key ) PyThread_tss_set(&(key), nullptr )
93
- # define PYBIND11_TLS_FREE (key ) PyThread_tss_delete(&(key))
68
+ #define PYBIND11_TLS_KEY_REF Py_tss_t &
69
+ #if defined(__clang__)
70
+ # define PYBIND11_TLS_KEY_INIT (var ) \
71
+ _Pragma (" clang diagnostic push" ) /* */ \
72
+ _Pragma(" clang diagnostic ignored \" -Wmissing-field-initializers\" " ) /* */ \
73
+ Py_tss_t var \
74
+ = Py_tss_NEEDS_INIT; \
75
+ _Pragma (" clang diagnostic pop" )
76
+ #elif defined(__GNUC__) && !defined(__INTEL_COMPILER)
77
+ # define PYBIND11_TLS_KEY_INIT (var ) \
78
+ _Pragma (" GCC diagnostic push" ) /* */ \
79
+ _Pragma(" GCC diagnostic ignored \" -Wmissing-field-initializers\" " ) /* */ \
80
+ Py_tss_t var \
81
+ = Py_tss_NEEDS_INIT; \
82
+ _Pragma (" GCC diagnostic pop" )
94
83
#else
95
- # define PYBIND11_TLS_KEY_REF Py_tss_t *
96
- # define PYBIND11_TLS_KEY_INIT (var ) Py_tss_t *var = nullptr ;
97
- # define PYBIND11_TLS_KEY_CREATE (var ) \
98
- (((var) = PyThread_tss_alloc()) != nullptr && (PyThread_tss_create((var)) == 0 ))
99
- # define PYBIND11_TLS_GET_VALUE (key ) PyThread_tss_get((key))
100
- # define PYBIND11_TLS_REPLACE_VALUE (key, value ) PyThread_tss_set((key), (value))
101
- # define PYBIND11_TLS_DELETE_VALUE (key ) PyThread_tss_set((key), nullptr )
102
- # define PYBIND11_TLS_FREE (key ) PyThread_tss_free(key)
84
+ # define PYBIND11_TLS_KEY_INIT (var ) Py_tss_t var = Py_tss_NEEDS_INIT;
103
85
#endif
86
+ #define PYBIND11_TLS_KEY_CREATE (var ) (PyThread_tss_create(&(var)) == 0 )
87
+ #define PYBIND11_TLS_GET_VALUE (key ) PyThread_tss_get(&(key))
88
+ #define PYBIND11_TLS_REPLACE_VALUE (key, value ) PyThread_tss_set(&(key), (value))
89
+ #define PYBIND11_TLS_DELETE_VALUE (key ) PyThread_tss_set(&(key), nullptr )
90
+ #define PYBIND11_TLS_FREE (key ) PyThread_tss_delete(&(key))
104
91
105
92
// Python loads modules by default with dlopen with the RTLD_LOCAL flag; under libc++ and possibly
106
93
// other STLs, this means `typeid(A)` from one module won't equal `typeid(A)` from another module
107
94
// even when `A` is the same, non-hidden-visibility type (e.g. from a common include). Under
108
95
// libstdc++, this doesn't happen: equality and the type_index hash are based on the type name,
109
96
// which works. If not under a known-good stl, provide our own name-based hash and equality
110
97
// functions that use the type name.
111
- #if (PYBIND11_INTERNALS_VERSION <= 4 && defined(__GLIBCXX__)) \
112
- || (PYBIND11_INTERNALS_VERSION >= 5 && !defined(_LIBCPP_VERSION))
98
+ #if !defined(_LIBCPP_VERSION)
113
99
inline bool same_type (const std::type_info &lhs, const std::type_info &rhs) { return lhs == rhs; }
114
100
using type_hash = std::hash<std::type_index>;
115
101
using type_equal_to = std::equal_to<std::type_index>;
@@ -197,35 +183,26 @@ struct internals {
197
183
std::forward_list<ExceptionTranslator> registered_exception_translators;
198
184
std::unordered_map<std::string, void *> shared_data; // Custom data to be shared across
199
185
// extensions
200
- #if PYBIND11_INTERNALS_VERSION == 4
201
- std::vector<PyObject *> unused_loader_patient_stack_remove_at_v5;
202
- #endif
203
- std::forward_list<std::string> static_strings; // Stores the std::strings backing
204
- // detail::c_str()
186
+ std::forward_list<std::string> static_strings; // Stores the std::strings backing
187
+ // detail::c_str()
205
188
PyTypeObject *static_property_type;
206
189
PyTypeObject *default_metaclass;
207
190
PyObject *instance_base;
208
191
// Unused if PYBIND11_SIMPLE_GIL_MANAGEMENT is defined:
209
192
PYBIND11_TLS_KEY_INIT (tstate)
210
- #if PYBIND11_INTERNALS_VERSION > 4
211
193
PYBIND11_TLS_KEY_INIT (loader_life_support_tls_key)
212
- #endif // PYBIND11_INTERNALS_VERSION > 4
213
194
// Unused if PYBIND11_SIMPLE_GIL_MANAGEMENT is defined:
214
195
PyInterpreterState *istate = nullptr ;
215
196
216
- #if PYBIND11_INTERNALS_VERSION > 4
217
197
// Note that we have to use a std::string to allocate memory to ensure a unique address
218
198
// We want unique addresses since we use pointer equality to compare function records
219
199
std::string function_record_capsule_name = internals_function_record_capsule_name;
220
- #endif
221
200
222
201
internals () = default ;
223
202
internals (const internals &other) = delete ;
224
203
internals &operator =(const internals &other) = delete ;
225
204
~internals () {
226
- #if PYBIND11_INTERNALS_VERSION > 4
227
205
PYBIND11_TLS_FREE (loader_life_support_tls_key);
228
- #endif // PYBIND11_INTERNALS_VERSION > 4
229
206
230
207
// This destructor is called *after* Py_Finalize() in finalize_interpreter().
231
208
// That *SHOULD BE* fine. The following details what happens when PyThread_tss_free is
@@ -414,7 +391,7 @@ inline void translate_local_exception(std::exception_ptr p) {
414
391
415
392
inline object get_python_state_dict () {
416
393
object state_dict;
417
- #if PYBIND11_INTERNALS_VERSION <= 4 || defined(PYPY_VERSION) || defined(GRAALVM_PYTHON)
394
+ #if defined(PYPY_VERSION) || defined(GRAALVM_PYTHON)
418
395
state_dict = reinterpret_borrow<object>(PyEval_GetBuiltins ());
419
396
#else
420
397
# if PY_VERSION_HEX < 0x03090000
@@ -512,13 +489,12 @@ PYBIND11_NOINLINE internals &get_internals() {
512
489
}
513
490
PYBIND11_TLS_REPLACE_VALUE (internals_ptr->tstate , tstate);
514
491
515
- #if PYBIND11_INTERNALS_VERSION > 4
516
492
// NOLINTNEXTLINE(bugprone-assignment-in-if-condition)
517
493
if (!PYBIND11_TLS_KEY_CREATE (internals_ptr->loader_life_support_tls_key )) {
518
494
pybind11_fail (" get_internals: could not successfully initialize the "
519
495
" loader_life_support TSS key!" );
520
496
}
521
- # endif
497
+
522
498
internals_ptr->istate = tstate->interp ;
523
499
state_dict[PYBIND11_INTERNALS_ID] = capsule (reinterpret_cast <void *>(internals_pp));
524
500
internals_ptr->registered_exception_translators .push_front (&translate_exception);
@@ -548,40 +524,6 @@ PYBIND11_NOINLINE internals &get_internals() {
548
524
struct local_internals {
549
525
type_map<type_info *> registered_types_cpp;
550
526
std::forward_list<ExceptionTranslator> registered_exception_translators;
551
- #if PYBIND11_INTERNALS_VERSION == 4
552
-
553
- // For ABI compatibility, we can't store the loader_life_support TLS key in
554
- // the `internals` struct directly. Instead, we store it in `shared_data` and
555
- // cache a copy in `local_internals`. If we allocated a separate TLS key for
556
- // each instance of `local_internals`, we could end up allocating hundreds of
557
- // TLS keys if hundreds of different pybind11 modules are loaded (which is a
558
- // plausible number).
559
- PYBIND11_TLS_KEY_INIT (loader_life_support_tls_key)
560
-
561
- // Holds the shared TLS key for the loader_life_support stack.
562
- struct shared_loader_life_support_data {
563
- PYBIND11_TLS_KEY_INIT (loader_life_support_tls_key)
564
- shared_loader_life_support_data () {
565
- // NOLINTNEXTLINE(bugprone-assignment-in-if-condition)
566
- if (!PYBIND11_TLS_KEY_CREATE (loader_life_support_tls_key)) {
567
- pybind11_fail (" local_internals: could not successfully initialize the "
568
- " loader_life_support TLS key!" );
569
- }
570
- }
571
- // We can't help but leak the TLS key, because Python never unloads extension modules.
572
- };
573
-
574
- local_internals () {
575
- auto &internals = get_internals ();
576
- // Get or create the `loader_life_support_stack_key`.
577
- auto &ptr = internals.shared_data [" _life_support" ];
578
- if (!ptr) {
579
- ptr = new shared_loader_life_support_data;
580
- }
581
- loader_life_support_tls_key
582
- = static_cast <shared_loader_life_support_data *>(ptr)->loader_life_support_tls_key ;
583
- }
584
- #endif // PYBIND11_INTERNALS_VERSION == 4
585
527
};
586
528
587
529
// / Works like `get_internals`, but for things which are locally registered.
@@ -688,7 +630,7 @@ const char *c_str(Args &&...args) {
688
630
689
631
inline const char *get_function_record_capsule_name () {
690
632
// On GraalPy, pointer equality of the names is currently not guaranteed
691
- #if PYBIND11_INTERNALS_VERSION > 4 && !defined(GRAALVM_PYTHON)
633
+ #if !defined(GRAALVM_PYTHON)
692
634
return get_internals ().function_record_capsule_name .c_str ();
693
635
#else
694
636
return nullptr ;
0 commit comments