From e4000146ab95740762bae9a47d25793f1e4b0a1f Mon Sep 17 00:00:00 2001 From: Ethan Steinberg Date: Fri, 21 Oct 2022 16:52:55 +0000 Subject: [PATCH] move to local internals --- include/pybind11/detail/internals.h | 17 ++++++++++++++--- include/pybind11/pybind11.h | 12 +++--------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index d47084e263d..ac8b5ae05ae 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -28,7 +28,7 @@ /// further ABI-incompatible changes may be made before the ABI is officially /// changed to the new version. #ifndef PYBIND11_INTERNALS_VERSION -# define PYBIND11_INTERNALS_VERSION 4 +# define PYBIND11_INTERNALS_VERSION 5 #endif PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) @@ -477,7 +477,7 @@ PYBIND11_NOINLINE internals &get_internals() { struct local_internals { type_map registered_types_cpp; std::forward_list registered_exception_translators; -#if defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION == 4 +#if defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION >= 4 // For ABI compatibility, we can't store the loader_life_support TLS key in // the `internals` struct directly. Instead, we store it in `shared_data` and @@ -509,7 +509,10 @@ struct local_internals { loader_life_support_tls_key = static_cast(ptr)->loader_life_support_tls_key; } -#endif // defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION == 4 +#endif // defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION >= 4 +#if PYBIND11_INTERNALS_VERSION == 5 + const char* function_capsule_name = strdup("pybind11_function_capsule"); +#endif }; /// Works like `get_internals`, but for things which are locally registered. @@ -523,6 +526,14 @@ inline local_internals &get_local_internals() { return *locals; } +inline const char* get_function_capsule_name() { +#if PYBIND11_INTERNALS_VERSION == 5 + return get_local_internals().function_capsule_name; +#else + return nullptr; +#endif +} + /// Constructs a std::string with the given arguments, stores it in `internals`, and returns its /// `c_str()`. Such strings objects have a long storage duration -- the internal strings are only /// cleared when the program exits or after interpreter shutdown (when embedding), and so are diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 877b7e16eb0..2b26c8f5150 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -69,15 +69,9 @@ inline bool apply_exception_translators(std::forward_list & return false; } -// Need to use a wrapper function to ensure 1 address -inline const char *function_capsule_name() { - static char name[] = "pybind11_function_capsule"; - return name; -} - inline bool is_function_record_capsule(const capsule &cap) { // Compare the pointers, not the values to ensure that each extension is unique - return cap.name() == function_capsule_name(); + return cap.name() == get_function_capsule_name(); } #if defined(_MSC_VER) @@ -514,7 +508,7 @@ class cpp_function : public function { capsule rec_capsule(unique_rec.release(), [](void *ptr) { destruct((detail::function_record *) ptr); }); - rec_capsule.set_name(detail::function_capsule_name()); + rec_capsule.set_name(detail::get_function_capsule_name()); guarded_strdup.release(); object scope_module; @@ -683,7 +677,7 @@ class cpp_function : public function { /* Iterator over the list of potentially admissible overloads */ const function_record *overloads = reinterpret_cast( - PyCapsule_GetPointer(self, function_capsule_name())), + PyCapsule_GetPointer(self, get_function_capsule_name())), *it = overloads; assert(overloads != nullptr);