@@ -211,54 +211,6 @@ struct internals {
211
211
#endif
212
212
};
213
213
214
- internals &get_internals ();
215
-
216
- // the internals struct (above) is shared between all the modules. local_internals are only
217
- // for a single module. Any changes made to internals may require an update to
218
- // PYBIND11_INTERNALS_VERSION, breaking backwards compatibility. local_internals is, by design,
219
- // restricted to a single module. Whether a module has local internals or not should not
220
- // impact any other modules, because the only things accessing the local internals is the
221
- // module that contains them.
222
- struct local_internals {
223
- type_map<type_info *> registered_types_cpp;
224
- std::forward_list<ExceptionTranslator> registered_exception_translators;
225
- #if defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION == 4
226
-
227
- // For ABI compatibility, we can't store the loader_life_support TLS key in
228
- // the `internals` struct directly. Instead, we store it in `shared_data` and
229
- // cache a copy in `local_internals`. If we allocated a separate TLS key for
230
- // each instance of `local_internals`, we could end up allocating hundreds of
231
- // TLS keys if hundreds of different pybind11 modules are loaded (which is a
232
- // plausible number).
233
- PYBIND11_TLS_KEY_INIT (loader_life_support_tls_key)
234
-
235
- // Holds the shared TLS key for the loader_life_support stack.
236
- struct shared_loader_life_support_data {
237
- PYBIND11_TLS_KEY_INIT (loader_life_support_tls_key)
238
- shared_loader_life_support_data () {
239
- if (!PYBIND11_TLS_KEY_CREATE (loader_life_support_tls_key)) {
240
- pybind11_fail (" local_internals: could not successfully initialize the "
241
- " loader_life_support TLS key!" );
242
- }
243
- }
244
- // We can't help but leak the TLS key, because Python never unloads extension modules.
245
- };
246
-
247
- local_internals () {
248
- auto &internals = get_internals ();
249
- // Get or create the `loader_life_support_stack_key`.
250
- auto &ptr = internals.shared_data [" _life_support" ];
251
- if (!ptr) {
252
- ptr = new shared_loader_life_support_data;
253
- }
254
- loader_life_support_tls_key
255
- = static_cast <shared_loader_life_support_data *>(ptr)->loader_life_support_tls_key ;
256
- }
257
- #endif // defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION == 4
258
- };
259
-
260
- local_internals &get_local_internals ();
261
-
262
214
// / Additional type information which does not fit into the PyTypeObject.
263
215
// / Changes to this struct also require bumping `PYBIND11_INTERNALS_VERSION`.
264
216
struct type_info {
@@ -362,6 +314,9 @@ inline internals **&get_internals_pp() {
362
314
return internals_pp;
363
315
}
364
316
317
+ std::forward_list<ExceptionTranslator>& get_exception_translators ();
318
+ std::forward_list<ExceptionTranslator>& get_local_exception_translators ();
319
+
365
320
// Apply all the extensions translators from a list
366
321
// Return true if one of the translators completed without raising an exception
367
322
// itself. Return of false indicates that if there are other translators
@@ -388,12 +343,12 @@ template <class T,
388
343
bool handle_nested_exception (const T &exc, const std::exception_ptr &p) {
389
344
std::exception_ptr nested = exc.nested_ptr ();
390
345
if (nested != nullptr && nested != p) {
391
- auto &local_translators = get_local_internals (). registered_exception_translators ;
346
+ auto &local_translators = get_local_exception_translators () ;
392
347
if (apply_exception_translators (local_translators, nested)) {
393
348
return true ;
394
349
}
395
350
396
- auto &translators = get_internals (). registered_exception_translators ;
351
+ auto &translators = get_exception_translators () ;
397
352
if (apply_exception_translators (translators, nested)) {
398
353
return true ;
399
354
}
@@ -566,6 +521,10 @@ PYBIND11_NOINLINE internals &get_internals() {
566
521
return **internals_pp;
567
522
}
568
523
524
+ inline std::forward_list<ExceptionTranslator>& get_exception_translators () {
525
+ return get_internals ().registered_exception_translators ;
526
+ }
527
+
569
528
// the internals struct (above) is shared between all the modules. local_internals are only
570
529
// for a single module. Any changes made to internals may require an update to
571
530
// PYBIND11_INTERNALS_VERSION, breaking backwards compatibility. local_internals is, by design,
@@ -622,6 +581,10 @@ inline local_internals &get_local_internals() {
622
581
return *locals;
623
582
}
624
583
584
+ inline std::forward_list<ExceptionTranslator>& get_local_exception_translators () {
585
+ return get_local_internals ().registered_exception_translators ;
586
+ }
587
+
625
588
// / Constructs a std::string with the given arguments, stores it in `internals`, and returns its
626
589
// / `c_str()`. Such strings objects have a long storage duration -- the internal strings are only
627
590
// / cleared when the program exits or after interpreter shutdown (when embedding), and so are
0 commit comments