Skip to content

Commit 3f428f6

Browse files
Add name to ensure correctness
1 parent 25211e5 commit 3f428f6

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

include/pybind11/functional.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct type_caster<std::function<Return(Args...)>> {
5959
// We would need to refactor to store a special string such as
6060
// pybind11_function_record Doing so is almost certainly an ABI break though Best
6161
// we can do without an ABI break is ignore named capsules
62-
if (c.name() == nullptr) {
62+
if (c.name() == function_capsule_name()) {
6363
rec = static_cast<function_record *>(c);
6464
}
6565

include/pybind11/pybind11.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ inline bool apply_exception_translators(std::forward_list<ExceptionTranslator> &
6969
return false;
7070
}
7171

72+
// Need to use a wrapper function to ensure 1 address
73+
inline const char* function_capsule_name() {
74+
static const char* name = "pybind11_function_capsule";
75+
return name;
76+
}
77+
7278
#if defined(_MSC_VER)
7379
# define PYBIND11_COMPAT_STRDUP _strdup
7480
#else
@@ -472,7 +478,7 @@ class cpp_function : public function {
472478
chain = nullptr;
473479
} else {
474480
auto rec_capsule = reinterpret_borrow<capsule>(self);
475-
if (rec_capsule.name() == nullptr) {
481+
if (rec_capsule.name() == detail::function_capsule_name()) {
476482
chain = static_cast<detail::function_record *>(rec_capsule);
477483
/* Never append a method to an overload chain of a parent class;
478484
instead, hide the parent's overloads in this case */
@@ -503,6 +509,7 @@ class cpp_function : public function {
503509

504510
capsule rec_capsule(unique_rec.release(),
505511
[](void *ptr) { destruct((detail::function_record *) ptr); });
512+
rec_capsule.set_name(detail::function_capsule_name());
506513
guarded_strdup.release();
507514

508515
object scope_module;
@@ -670,7 +677,7 @@ class cpp_function : public function {
670677
using namespace detail;
671678

672679
/* Iterator over the list of potentially admissible overloads */
673-
const function_record *overloads = (function_record *) PyCapsule_GetPointer(self, nullptr),
680+
const function_record *overloads = (function_record *) PyCapsule_GetPointer(self, function_capsule_name()),
674681
*it = overloads;
675682

676683
/* Need to know how many arguments + keyword arguments there are to pick the right
@@ -1882,7 +1889,7 @@ class class_ : public detail::generic_type {
18821889
return nullptr;
18831890
}
18841891
auto cap = reinterpret_borrow<capsule>(PyCFunction_GET_SELF(h.ptr()));
1885-
if (cap.name() != nullptr) {
1892+
if (cap.name() != detail::function_capsule_name()) {
18861893
return nullptr;
18871894
}
18881895
return static_cast<detail::function_record *>(cap);

0 commit comments

Comments
 (0)