Skip to content

Commit f51c638

Browse files
committed
Re-organize internals.h
1 parent d068cc6 commit f51c638

File tree

1 file changed

+13
-50
lines changed

1 file changed

+13
-50
lines changed

include/pybind11/detail/internals.h

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -211,54 +211,6 @@ struct internals {
211211
#endif
212212
};
213213

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-
262214
/// Additional type information which does not fit into the PyTypeObject.
263215
/// Changes to this struct also require bumping `PYBIND11_INTERNALS_VERSION`.
264216
struct type_info {
@@ -362,6 +314,9 @@ inline internals **&get_internals_pp() {
362314
return internals_pp;
363315
}
364316

317+
std::forward_list<ExceptionTranslator>& get_exception_translators();
318+
std::forward_list<ExceptionTranslator>& get_local_exception_translators();
319+
365320
// Apply all the extensions translators from a list
366321
// Return true if one of the translators completed without raising an exception
367322
// itself. Return of false indicates that if there are other translators
@@ -388,12 +343,12 @@ template <class T,
388343
bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
389344
std::exception_ptr nested = exc.nested_ptr();
390345
if (nested != nullptr && nested != p) {
391-
auto &local_translators = get_local_internals().registered_exception_translators;
346+
auto &local_translators = get_local_exception_translators();
392347
if (apply_exception_translators(local_translators, nested)) {
393348
return true;
394349
}
395350

396-
auto &translators = get_internals().registered_exception_translators;
351+
auto &translators = get_exception_translators();
397352
if (apply_exception_translators(translators, nested)) {
398353
return true;
399354
}
@@ -566,6 +521,10 @@ PYBIND11_NOINLINE internals &get_internals() {
566521
return **internals_pp;
567522
}
568523

524+
inline std::forward_list<ExceptionTranslator>& get_exception_translators() {
525+
return get_internals().registered_exception_translators;
526+
}
527+
569528
// the internals struct (above) is shared between all the modules. local_internals are only
570529
// for a single module. Any changes made to internals may require an update to
571530
// PYBIND11_INTERNALS_VERSION, breaking backwards compatibility. local_internals is, by design,
@@ -622,6 +581,10 @@ inline local_internals &get_local_internals() {
622581
return *locals;
623582
}
624583

584+
inline std::forward_list<ExceptionTranslator>& get_local_exception_translators() {
585+
return get_local_internals().registered_exception_translators;
586+
}
587+
625588
/// Constructs a std::string with the given arguments, stores it in `internals`, and returns its
626589
/// `c_str()`. Such strings objects have a long storage duration -- the internal strings are only
627590
/// cleared when the program exits or after interpreter shutdown (when embedding), and so are

0 commit comments

Comments
 (0)