diff --git a/include/pybind11/detail/function_record_pyobject.h b/include/pybind11/detail/function_record_pyobject.h index cb1fe341a6..9b29dab6ba 100644 --- a/include/pybind11/detail/function_record_pyobject.h +++ b/include/pybind11/detail/function_record_pyobject.h @@ -30,6 +30,26 @@ inline function_record_PyObject *function_record_data(PyObject *obj) { return type_data(obj); } +// Python computes the `__qualname__` of a builtin function from its `__self__`, which is the +// function record. A module gives no prefix, so the record of a function that is not in a class +// is a module subclass. The record type for the functions in a class has the `__qualname__` of +// that class. +#if !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON) +# define PYBIND11_FUNCTION_RECORD_QUALNAME +#endif + +#if defined(PYBIND11_FUNCTION_RECORD_QUALNAME) +/// The record pointer of a function record that is a module subclass. +inline function_record *&function_record_module_data(PyObject *obj) { +# if PY_VERSION_HEX >= 0x030C0000 + return *static_cast(PyObject_GetTypeData(obj, Py_TYPE(obj))); +# else + return *reinterpret_cast(reinterpret_cast(obj) + + PyModule_Type.tp_basicsize); +# endif +} +#endif + PYBIND11_NAMESPACE_BEGIN(function_record_PyTypeObject_methods) PyObject *tp_new_impl(PyTypeObject *type, PyObject *args, PyObject *kwds); @@ -83,6 +103,8 @@ static PyMethodDef tp_methods_impl[] constexpr char tp_plainname_impl[] = PYBIND11_DETAIL_FUNCTION_RECORD_TP_PLAINNAME; constexpr char tp_qualname_impl[] = PYBIND11_DUMMY_MODULE_NAME "." PYBIND11_DETAIL_FUNCTION_RECORD_TP_PLAINNAME; +constexpr char tp_module_qualname_impl[] + = PYBIND11_DUMMY_MODULE_NAME "." PYBIND11_DETAIL_FUNCTION_RECORD_TP_PLAINNAME "_module"; PYBIND11_NAMESPACE_END(function_record_PyTypeObject_methods) @@ -123,6 +145,69 @@ inline bool function_record_PyTypeObject_name_matches(PyTypeObject *obj_type) { || tp_name_equals(obj_type, function_record_PyTypeObject_methods::tp_plainname_impl); } +#if defined(PYBIND11_FUNCTION_RECORD_QUALNAME) +inline PyTypeObject *get_function_record_module_PyTypeObject() { + PYBIND11_LOCK_INTERNALS(get_internals()); + PyTypeObject *&py_type_obj = detail::get_local_internals().function_record_module_py_type; + if (!py_type_obj) { + static PyType_Slot slots[] + = {{Py_tp_dealloc, + reinterpret_cast(function_record_PyTypeObject_methods::tp_dealloc_impl)}, + {Py_tp_methods, + reinterpret_cast(function_record_PyTypeObject_methods::tp_methods_impl)}, + // The module `tp_getattro` needs the module `__dict__`. + {Py_tp_getattro, reinterpret_cast(PyObject_GenericGetAttr)}, + {0, nullptr}}; + PyType_Spec spec = {function_record_PyTypeObject_methods::tp_module_qualname_impl, +# if PY_VERSION_HEX >= 0x030C0000 + -static_cast(sizeof(function_record *)), +# else + static_cast(PyModule_Type.tp_basicsize + + static_cast(sizeof(function_record *))), +# endif + 0, + Py_TPFLAGS_DEFAULT, + slots}; + // Python < 3.10 needs a tuple for the bases. + auto bases = reinterpret_steal( + PyTuple_Pack(1, reinterpret_cast(&PyModule_Type))); + if (!bases) { + throw error_already_set(); + } + PyObject *py_obj = PyType_FromSpecWithBases(&spec, bases.ptr()); + if (py_obj == nullptr) { + throw error_already_set(); + } + py_type_obj = reinterpret_cast(py_obj); + } + return py_type_obj; +} + +inline bool is_function_record_module_PyObject(PyObject *obj) { + PyTypeObject *obj_type = Py_TYPE(obj); + return obj_type == get_function_record_module_PyTypeObject() + || tp_name_equals(obj_type, + function_record_PyTypeObject_methods::tp_module_qualname_impl); +} + +// The function record type for the functions in the class `scope`. It is in the class +// `__dict__`, so that all extension modules use the same one. +inline object get_function_record_PyTypeObject(handle scope) { + constexpr const char *key = "__pybind11_function_record_type__"; + object scope_dict = scope.attr("__dict__"); + if (scope_dict.contains(key)) { + return scope_dict[key]; + } + auto py_type_obj = reinterpret_steal(PyType_FromSpec(&function_record_PyType_Spec)); + if (!py_type_obj) { + throw error_already_set(); + } + setattr(py_type_obj, "__qualname__", scope.attr("__qualname__")); + setattr(scope, key, py_type_obj); + return py_type_obj; +} +#endif + inline bool is_function_record_PyObject(PyObject *obj) { if (PyType_Check(obj) != 0) { return false; @@ -138,13 +223,24 @@ inline bool is_function_record_PyObject(PyObject *obj) { return function_record_PyTypeObject_name_matches(obj_type); } -inline function_record *function_record_ptr_from_PyObject(PyObject *obj) { +/// The location of the record pointer in a function record object of any layout, or nullptr. +inline function_record **function_record_ptr_location(PyObject *obj) { +#if defined(PYBIND11_FUNCTION_RECORD_QUALNAME) + if (is_function_record_module_PyObject(obj)) { + return &function_record_module_data(obj); + } +#endif if (is_function_record_PyObject(obj)) { - return function_record_data(obj)->cpp_func_rec; + return &function_record_data(obj)->cpp_func_rec; } return nullptr; } +inline function_record *function_record_ptr_from_PyObject(PyObject *obj) { + function_record **location = function_record_ptr_location(obj); + return location != nullptr ? *location : nullptr; +} + // The `self` of cpp_function::dispatcher() is always the function_record_PyObject that was // created for that dispatcher in cpp_function::initialize_generic(), so the type check can be // skipped. is_function_record_PyObject() is deliberately not used here: it calls @@ -156,12 +252,39 @@ inline function_record *function_record_ptr_from_dispatcher_self(PyObject *self) return function_record_data(self)->cpp_func_rec; } -inline object function_record_PyObject_New() { +#if defined(PYBIND11_FUNCTION_RECORD_QUALNAME) +// Same as function_record_ptr_from_dispatcher_self(), for a record that is a module subclass. +inline function_record *function_record_ptr_from_dispatcher_module_self(PyObject *self) { + assert(self != nullptr); + assert(PyModule_Check(self) != 0); + return function_record_module_data(self); +} +#endif + +/// A new function record object for a function in `scope`, which holds `rec`. +inline object function_record_PyObject_New(handle scope, function_record *rec) { +#if defined(PYBIND11_FUNCTION_RECORD_QUALNAME) + if (!scope || PyType_Check(scope.ptr()) == 0) { + // Not the module `tp_new`: it adds a `__dict__`, and the free-threaded build then + // defers the deallocation to the garbage collector. + auto py_func_rec = reinterpret_steal( + PyType_GenericAlloc(get_function_record_module_PyTypeObject(), 0)); + if (!py_func_rec) { + throw error_already_set(); + } + function_record_module_data(py_func_rec.ptr()) = rec; + return py_func_rec; + } + object py_type_obj = get_function_record_PyTypeObject(scope); + PyObject *py_func_rec = PyObject_New(PyObject, (PyTypeObject *) py_type_obj.ptr()); +#else + (void) scope; PyObject *py_func_rec = PyObject_New(PyObject, get_function_record_PyTypeObject()); +#endif if (py_func_rec == nullptr) { throw error_already_set(); } - function_record_data(py_func_rec)->cpp_func_rec = nullptr; // For clarity. Redundant. + function_record_data(py_func_rec)->cpp_func_rec = rec; return reinterpret_steal(py_func_rec); } diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index 4fbdf93614..0256239421 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -507,6 +507,7 @@ struct local_internals { std::forward_list registered_exception_translators; PyTypeObject *function_record_py_type = nullptr; + PyTypeObject *function_record_module_py_type = nullptr; }; enum class holder_enum_t : uint8_t { diff --git a/include/pybind11/pybind11-inl.h b/include/pybind11/pybind11-inl.h index 60967d5847..1758fdabbe 100644 --- a/include/pybind11/pybind11-inl.h +++ b/include/pybind11/pybind11-inl.h @@ -199,6 +199,20 @@ PYBIND11_NAMESPACE_BEGIN(function_record_PyTypeObject_methods) PYBIND11_INLINE void tp_dealloc_impl(PyObject *self) { // Save type before PyObject_Free invalidates self. auto *type = Py_TYPE(self); +#if defined(PYBIND11_FUNCTION_RECORD_QUALNAME) + if (PyModule_Check(self) != 0) { + cpp_function::destruct(function_record_module_data(self)); + function_record_module_data(self) = nullptr; +# if defined(Py_LIMITED_API) + auto dealloc = reinterpret_cast(PyType_GetSlot(&PyModule_Type, Py_tp_dealloc)); +# else + auto dealloc = PyModule_Type.tp_dealloc; +# endif + dealloc(self); + Py_DECREF(type); + return; + } +#endif auto *py_func_rec = function_record_data(self); cpp_function::destruct(py_func_rec->cpp_func_rec); py_func_rec->cpp_func_rec = nullptr; @@ -667,9 +681,15 @@ PYBIND11_INLINE void cpp_function::initialize_generic(unique_function_record &&u = reinterpret_cast(reinterpret_cast(dispatcher)); rec->def->ml_flags = METH_FASTCALL | METH_KEYWORDS; - object py_func_rec = detail::function_record_PyObject_New(); - detail::function_record_data(py_func_rec.ptr())->cpp_func_rec = unique_rec.release(); + object py_func_rec = detail::function_record_PyObject_New(rec->scope, rec); + unique_rec.release(); // NOLINT(bugprone-unused-return-value): owned by py_func_rec guarded_strdup.release(); +#if defined(PYBIND11_FUNCTION_RECORD_QUALNAME) + if (PyModule_Check(py_func_rec.ptr()) != 0) { + rec->def->ml_meth = reinterpret_cast( + reinterpret_cast(dispatcher_module_self)); + } +#endif object scope_module = detail::get_scope_module(rec->scope); m_ptr = PyCFunction_NewEx(rec->def, py_func_rec.ptr(), scope_module.ptr()); @@ -701,8 +721,8 @@ PYBIND11_INLINE void cpp_function::initialize_generic(unique_function_record &&u // chain. chain_start = rec; rec->next = chain; - auto *py_func_rec = detail::function_record_data(PYBIND11_PYCFUNCTION_GET_SELF(m_ptr)); - py_func_rec->cpp_func_rec = unique_rec.release(); + *detail::function_record_ptr_location(PYBIND11_PYCFUNCTION_GET_SELF(m_ptr)) + = unique_rec.release(); guarded_strdup.release(); } else { // Or end of chain (normal behavior) @@ -839,8 +859,27 @@ PYBIND11_INLINE PyObject *cpp_function::dispatcher(PyObject *self, PyObject *const *args_in_arr, size_t nargsf, PyObject *kwnames_in) { + return dispatch( + detail::function_record_ptr_from_dispatcher_self(self), args_in_arr, nargsf, kwnames_in); +} + +#if defined(PYBIND11_FUNCTION_RECORD_QUALNAME) +PYBIND11_INLINE PyObject *cpp_function::dispatcher_module_self(PyObject *self, + PyObject *const *args_in_arr, + size_t nargsf, + PyObject *kwnames_in) { + return dispatch(detail::function_record_ptr_from_dispatcher_module_self(self), + args_in_arr, + nargsf, + kwnames_in); +} +#endif + +PYBIND11_INLINE PyObject *cpp_function::dispatch(const detail::function_record *overloads, + PyObject *const *args_in_arr, + size_t nargsf, + PyObject *kwnames_in) { using namespace detail; - const function_record *overloads = function_record_ptr_from_dispatcher_self(self); assert(overloads != nullptr); /* Iterator over the list of potentially admissible overloads */ diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 2459a12f5f..e3e0e55d66 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -556,6 +556,14 @@ class cpp_function : public function { /// Main dispatch logic for calls to functions bound using pybind11 static PyObject * dispatcher(PyObject *self, PyObject *const *args_in_arr, size_t nargsf, PyObject *kwnames_in); + static PyObject *dispatcher_module_self(PyObject *self, + PyObject *const *args_in_arr, + size_t nargsf, + PyObject *kwnames_in); + static PyObject *dispatch(const detail::function_record *overloads, + PyObject *const *args_in_arr, + size_t nargsf, + PyObject *kwnames_in); static ssize_t keyword_index(PyObject *haystack, char const *needle) { /* kwargs is usually very small (<= 5 entries). The arg strings are typically interned. diff --git a/tests/test_class.cpp b/tests/test_class.cpp index e76d2413da..b316eb6b7a 100644 --- a/tests/test_class.cpp +++ b/tests/test_class.cpp @@ -525,6 +525,7 @@ TEST_SUBMODULE(class_, m) { // test_qualname // #1166: nested class docstring doesn't show nested name // Also related: tests that __qualname__ is set properly + m.def("module_func", []() {}); struct NestBase {}; struct Nested {}; py::class_ base(m, "NestBase"); @@ -532,9 +533,12 @@ TEST_SUBMODULE(class_, m) { py::class_(base, "Nested") .def(py::init<>()) .def("fn", [](Nested &, int, NestBase &, Nested &) {}) - .def("fa", [](Nested &, int, NestBase &, Nested &) {}, "a"_a, "b"_a, "c"_a); + .def( + "fa", [](Nested &, int, NestBase &, Nested &) {}, "a"_a, "b"_a, "c"_a) + .def_static("static_func", []() {}); base.def("g", [](NestBase &, Nested &) {}); base.def("h", []() { return NestBase(); }); + base.def_static("static_func", []() {}); // test_class_names_match_type auto names = m.def_submodule("class_names"); diff --git a/tests/test_class.py b/tests/test_class.py index 8785dc529b..ff2dd971b3 100644 --- a/tests/test_class.py +++ b/tests/test_class.py @@ -178,6 +178,15 @@ def test_qualname(doc): assert m.NestBase.__qualname__ == "NestBase" assert m.NestBase.Nested.__qualname__ == "NestBase.Nested" + if env.CPYTHON: + assert m.module_func.__qualname__ == "module_func" + assert m.NestBase.g.__qualname__ == "NestBase.g" + assert m.NestBase.static_func.__qualname__ == "NestBase.static_func" + assert m.NestBase.Nested.fn.__qualname__ == "NestBase.Nested.fn" + assert ( + m.NestBase.Nested.static_func.__qualname__ == "NestBase.Nested.static_func" + ) + assert ( doc(m.NestBase.__init__) == """ diff --git a/tests/test_pickling.py b/tests/test_pickling.py index 9febc3f6a8..d6301cd574 100644 --- a/tests/test_pickling.py +++ b/tests/test_pickling.py @@ -34,7 +34,7 @@ def test_pickle_simple_callable(protocol): # This is not expected to create issues because the only purpose of # `m.simple_callable.__self__` is to enable pickling: the only method it has is # `__reduce_ex__`. Direct access for any other purpose is not supported. - # Note that `repr(m.simple_callable.__self__)` shows, e.g.: + # Note that `repr(m.simple_callable.__self__)` shows ``, or, for a method: # `` # It is considered to be as much an implementation detail as the # `pybind11::detail::function_record` C++ type is. diff --git a/tools/check_inl_headers.py b/tools/check_inl_headers.py index e9399e0797..d2417619bd 100755 --- a/tools/check_inl_headers.py +++ b/tools/check_inl_headers.py @@ -21,6 +21,7 @@ "GRAALVM_PYTHON", "NDEBUG", "PYBIND11_BUILTIN_QUALNAME", + "PYBIND11_FUNCTION_RECORD_QUALNAME", # not PyPy, not GraalPy "PYBIND11_HAS_CXXABI_H", "PYBIND11_HAS_STRING_VIEW", "PYBIND11_PRECOMPILED",