@@ -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