1111
1212#include " ../pytypes.h"
1313
14+ // / Tracks the `internals` and `type_info` ABI version independent of the main library version.
15+ // /
16+ // / Some portions of the code use an ABI that is conditional depending on this
17+ // / version number. That allows ABI-breaking changes to be "pre-implemented".
18+ // / Once the default version number is incremented, the conditional logic that
19+ // / no longer applies can be removed. Additionally, users that need not
20+ // / maintain ABI compatibility can increase the version number in order to take
21+ // / advantage of any functionality/efficiency improvements that depend on the
22+ // / newer ABI.
23+ // /
24+ // / WARNING: If you choose to manually increase the ABI version, note that
25+ // / pybind11 may not be tested as thoroughly with a non-default ABI version, and
26+ // / further ABI-incompatible changes may be made before the ABI is officially
27+ // / changed to the new version.
28+ #ifndef PYBIND11_INTERNALS_VERSION
29+ # define PYBIND11_INTERNALS_VERSION 4
30+ #endif
31+
1432PYBIND11_NAMESPACE_BEGIN (PYBIND11_NAMESPACE )
1533
1634using ExceptionTranslator = void (*)(std::exception_ptr);
@@ -25,30 +43,54 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass);
2543// The old Python Thread Local Storage (TLS) API is deprecated in Python 3.7 in favor of the new
2644// Thread Specific Storage (TSS) API.
2745#if PY_VERSION_HEX >= 0x03070000
28- # define PYBIND11_TLS_KEY_INIT (var ) Py_tss_t *var = nullptr
29- # define PYBIND11_TLS_GET_VALUE (key ) PyThread_tss_get((key))
30- # define PYBIND11_TLS_REPLACE_VALUE (key, value ) PyThread_tss_set((key), (value))
31- # define PYBIND11_TLS_DELETE_VALUE (key ) PyThread_tss_set((key), nullptr )
32- # define PYBIND11_TLS_FREE (key ) PyThread_tss_free(key)
46+ // Avoid unnecessary allocation of `Py_tss_t`, since we cannot use
47+ // `Py_LIMITED_API` anyway.
48+ # if PYBIND11_INTERNALS_VERSION > 4
49+ # define PYBIND11_TLS_KEY_REF Py_tss_t &
50+ // Clang on macOS warns due to `Py_tss_NEEDS_INIT` not specifying an initializer
51+ // for every field.
52+ # define PYBIND11_TLS_KEY_INIT (var ) \
53+ _Pragma (" GCC diagnostic push" ) /* */ \
54+ _Pragma(" GCC diagnostic ignored \" -Wmissing-field-initializers\" " ) /* */ \
55+ Py_tss_t var \
56+ = Py_tss_NEEDS_INIT; \
57+ _Pragma (" GCC diagnostic pop" )
58+ # define PYBIND11_TLS_KEY_CREATE (var ) (PyThread_tss_create(&(var)) == 0 )
59+ # define PYBIND11_TLS_GET_VALUE (key ) PyThread_tss_get(&(key))
60+ # define PYBIND11_TLS_REPLACE_VALUE (key, value ) PyThread_tss_set(&(key), (value))
61+ # define PYBIND11_TLS_DELETE_VALUE (key ) PyThread_tss_set(&(key), nullptr )
62+ # define PYBIND11_TLS_FREE (key ) PyThread_tss_delete(&(key))
63+ # else
64+ # define PYBIND11_TLS_KEY_REF Py_tss_t *
65+ # define PYBIND11_TLS_KEY_INIT (var ) Py_tss_t *var = nullptr
66+ # define PYBIND11_TLS_KEY_CREATE (var ) \
67+ (((var) = PyThread_tss_alloc()) != nullptr && (PyThread_tss_create((var)) == 0 ))
68+ # define PYBIND11_TLS_GET_VALUE (key ) PyThread_tss_get((key))
69+ # define PYBIND11_TLS_REPLACE_VALUE (key, value ) PyThread_tss_set((key), (value))
70+ # define PYBIND11_TLS_DELETE_VALUE (key ) PyThread_tss_set((key), nullptr )
71+ # define PYBIND11_TLS_FREE (key ) PyThread_tss_free(key)
72+ # endif
3373#else
34- // Usually an int but a long on Cygwin64 with Python 3.x
35- # define PYBIND11_TLS_KEY_INIT (var ) decltype (PyThread_create_key()) var = 0
74+ // Usually an int but a long on Cygwin64 with Python 3.x
75+ # define PYBIND11_TLS_KEY_REF decltype (PyThread_create_key())
76+ # define PYBIND11_TLS_KEY_INIT (var ) PYBIND11_TLS_KEY_REF var = 0
77+ # define PYBIND11_TLS_KEY_CREATE (var ) (((var) = PyThread_create_key()) != -1 )
3678# define PYBIND11_TLS_GET_VALUE (key ) PyThread_get_key_value((key))
37- # if PY_MAJOR_VERSION < 3
38- # define PYBIND11_TLS_DELETE_VALUE (key ) \
39- PyThread_delete_key_value (key)
40- # define PYBIND11_TLS_REPLACE_VALUE (key, value ) \
41- do { \
42- PyThread_delete_key_value ((key)); \
43- PyThread_set_key_value ((key), (value)); \
44- } while (false )
79+ # if PY_MAJOR_VERSION < 3 || defined(PYPY_VERSION)
80+ // On CPython < 3.4 and on PyPy, `PyThread_set_key_value` strangely does not set
81+ // the value if it has already been set. Instead, it must first be deleted and
82+ // then set again.
83+ # define PYBIND11_TLS_DELETE_VALUE (key ) PyThread_delete_key_value(key)
84+ # define PYBIND11_TLS_REPLACE_VALUE (key, value ) \
85+ do { \
86+ PyThread_delete_key_value ((key)); \
87+ PyThread_set_key_value ((key), (value)); \
88+ } while (false )
4589# else
46- # define PYBIND11_TLS_DELETE_VALUE (key ) \
47- PyThread_set_key_value ((key), nullptr)
48- # define PYBIND11_TLS_REPLACE_VALUE (key, value ) \
49- PyThread_set_key_value ((key), (value))
90+ # define PYBIND11_TLS_DELETE_VALUE (key ) PyThread_set_key_value((key), nullptr )
91+ # define PYBIND11_TLS_REPLACE_VALUE (key, value ) PyThread_set_key_value((key), (value))
5092# endif
51- # define PYBIND11_TLS_FREE (key ) (void )key
93+ # define PYBIND11_TLS_FREE (key ) (void ) key
5294#endif
5395
5496// Python loads modules by default with dlopen with the RTLD_LOCAL flag; under libc++ and possibly
@@ -106,22 +148,31 @@ struct internals {
106148 std::unordered_map<const PyObject *, std::vector<PyObject *>> patients;
107149 std::forward_list<ExceptionTranslator> registered_exception_translators;
108150 std::unordered_map<std::string, void *> shared_data; // Custom data to be shared across extensions
151+ #if PYBIND11_INTERNALS_VERSION == 4
109152 std::vector<PyObject *> unused_loader_patient_stack_remove_at_v5;
153+ #endif
110154 std::forward_list<std::string> static_strings; // Stores the std::strings backing detail::c_str()
111155 PyTypeObject *static_property_type;
112156 PyTypeObject *default_metaclass;
113157 PyObject *instance_base;
114158#if defined(WITH_THREAD)
115159 PYBIND11_TLS_KEY_INIT (tstate);
160+ # if PYBIND11_INTERNALS_VERSION > 4
161+ PYBIND11_TLS_KEY_INIT (loader_life_support_tls_key);
162+ # endif // PYBIND11_INTERNALS_VERSION > 4
116163 PyInterpreterState *istate = nullptr ;
117164 ~internals () {
165+ # if PYBIND11_INTERNALS_VERSION > 4
166+ PYBIND11_TLS_FREE (loader_life_support_tls_key);
167+ # endif // PYBIND11_INTERNALS_VERSION > 4
168+
118169 // This destructor is called *after* Py_Finalize() in finalize_interpreter().
119- // That *SHOULD BE* fine. The following details what happens when PyThread_tss_free is called.
120- // PYBIND11_TLS_FREE is PyThread_tss_free on python 3.7+. On older python, it does nothing.
121- // PyThread_tss_free calls PyThread_tss_delete and PyMem_RawFree.
122- // PyThread_tss_delete just calls TlsFree (on Windows) or pthread_key_delete (on *NIX). Neither
123- // of those have anything to do with CPython internals.
124- // PyMem_RawFree *requires* that the `tstate` be allocated with the CPython allocator.
170+ // That *SHOULD BE* fine. The following details what happens when PyThread_tss_free is
171+ // called. PYBIND11_TLS_FREE is PyThread_tss_free on python 3.7+. On older python, it does
172+ // nothing. PyThread_tss_free calls PyThread_tss_delete and PyMem_RawFree.
173+ // PyThread_tss_delete just calls TlsFree (on Windows) or pthread_key_delete (on *NIX).
174+ // Neither of those have anything to do with CPython internals. PyMem_RawFree *requires*
175+ // that the `tstate` be allocated with the CPython allocator.
125176 PYBIND11_TLS_FREE (tstate);
126177 }
127178#endif
@@ -153,9 +204,6 @@ struct type_info {
153204 bool module_local : 1 ;
154205};
155206
156- // / Tracks the `internals` and `type_info` ABI version independent of the main library version
157- #define PYBIND11_INTERNALS_VERSION 4
158-
159207// / On MSVC, debug and release builds are not ABI-compatible!
160208#if defined(_MSC_VER) && defined(_DEBUG)
161209# define PYBIND11_BUILD_TYPE " _debug"
@@ -291,21 +339,21 @@ PYBIND11_NOINLINE internals &get_internals() {
291339 internals_ptr = new internals ();
292340#if defined(WITH_THREAD)
293341
294- # if PY_VERSION_HEX < 0x03090000
295- PyEval_InitThreads ();
296- # endif
342+ # if PY_VERSION_HEX < 0x03090000
343+ PyEval_InitThreads ();
344+ # endif
297345 PyThreadState *tstate = PyThreadState_Get ();
298- # if PY_VERSION_HEX >= 0x03070000
299- internals_ptr-> tstate = PyThread_tss_alloc ( );
300- if (!internals_ptr-> tstate || ( PyThread_tss_create (internals_ptr-> tstate ) != 0 ))
301- pybind11_fail ( " get_internals: could not successfully initialize the tstate TSS key! " );
302- PyThread_tss_set (internals_ptr-> tstate , tstate);
303- # else
304- internals_ptr->tstate = PyThread_create_key ();
305- if (internals_ptr-> tstate == - 1 )
306- pybind11_fail ( " get_internals: could not successfully initialize the tstate TLS key!" );
307- PyThread_set_key_value (internals_ptr-> tstate , tstate);
308- # endif
346+ if (! PYBIND11_TLS_KEY_CREATE (internals_ptr-> tstate )) {
347+ pybind11_fail ( " get_internals: could not successfully initialize the tstate TSS key! " );
348+ }
349+ PYBIND11_TLS_REPLACE_VALUE (internals_ptr-> tstate , tstate );
350+
351+ # if PYBIND11_INTERNALS_VERSION > 4
352+ if (! PYBIND11_TLS_KEY_CREATE ( internals_ptr->loader_life_support_tls_key )) {
353+ pybind11_fail ( " get_internals: could not successfully initialize the "
354+ " loader_life_support TSS key!" );
355+ }
356+ # endif
309357 internals_ptr->istate = tstate->interp ;
310358#endif
311359 builtins[id] = capsule (internals_pp);
@@ -317,16 +365,48 @@ PYBIND11_NOINLINE internals &get_internals() {
317365 return **internals_pp;
318366}
319367
320-
321368// the internals struct (above) is shared between all the modules. local_internals are only
322369// for a single module. Any changes made to internals may require an update to
323370// PYBIND11_INTERNALS_VERSION, breaking backwards compatibility. local_internals is, by design,
324371// restricted to a single module. Whether a module has local internals or not should not
325372// impact any other modules, because the only things accessing the local internals is the
326373// module that contains them.
327374struct local_internals {
328- type_map<type_info *> registered_types_cpp;
329- std::forward_list<ExceptionTranslator> registered_exception_translators;
375+ type_map<type_info *> registered_types_cpp;
376+ std::forward_list<ExceptionTranslator> registered_exception_translators;
377+ #if defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION == 4
378+
379+ // For ABI compatibility, we can't store the loader_life_support TLS key in
380+ // the `internals` struct directly. Instead, we store it in `shared_data` and
381+ // cache a copy in `local_internals`. If we allocated a separate TLS key for
382+ // each instance of `local_internals`, we could end up allocating hundreds of
383+ // TLS keys if hundreds of different pybind11 modules are loaded (which is a
384+ // plausible number).
385+ PYBIND11_TLS_KEY_INIT (loader_life_support_tls_key);
386+
387+ // Holds the shared TLS key for the loader_life_support stack.
388+ struct shared_loader_life_support_data {
389+ PYBIND11_TLS_KEY_INIT (loader_life_support_tls_key);
390+ shared_loader_life_support_data () {
391+ if (!PYBIND11_TLS_KEY_CREATE (loader_life_support_tls_key)) {
392+ pybind11_fail (" local_internals: could not successfully initialize the "
393+ " loader_life_support TLS key!" );
394+ }
395+ }
396+ // We can't help but leak the TLS key, because Python never unloads extension modules.
397+ };
398+
399+ local_internals () {
400+ auto &internals = get_internals ();
401+ // Get or create the `loader_life_support_stack_key`.
402+ auto &ptr = internals.shared_data [" _life_support" ];
403+ if (!ptr) {
404+ ptr = new shared_loader_life_support_data;
405+ }
406+ loader_life_support_tls_key
407+ = static_cast <shared_loader_life_support_data *>(ptr)->loader_life_support_tls_key ;
408+ }
409+ #endif // defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION == 4
330410};
331411
332412// / Works like `get_internals`, but for things which are locally registered.
0 commit comments