Skip to content

Commit

Permalink
move to local internals
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanSteinberg committed Oct 21, 2022
1 parent 8ca0e10 commit e400014
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
17 changes: 14 additions & 3 deletions include/pybind11/detail/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -477,7 +477,7 @@ PYBIND11_NOINLINE internals &get_internals() {
struct local_internals {
type_map<type_info *> registered_types_cpp;
std::forward_list<ExceptionTranslator> 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
Expand Down Expand Up @@ -509,7 +509,10 @@ struct local_internals {
loader_life_support_tls_key
= static_cast<shared_loader_life_support_data *>(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.
Expand All @@ -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
Expand Down
12 changes: 3 additions & 9 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,9 @@ inline bool apply_exception_translators(std::forward_list<ExceptionTranslator> &
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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -683,7 +677,7 @@ class cpp_function : public function {

/* Iterator over the list of potentially admissible overloads */
const function_record *overloads = reinterpret_cast<function_record *>(
PyCapsule_GetPointer(self, function_capsule_name())),
PyCapsule_GetPointer(self, get_function_capsule_name())),
*it = overloads;
assert(overloads != nullptr);

Expand Down

0 comments on commit e400014

Please sign in to comment.