Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 127 additions & 4 deletions include/pybind11/detail/function_record_pyobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ inline function_record_PyObject *function_record_data(PyObject *obj) {
return type_data<function_record_PyObject>(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<function_record **>(PyObject_GetTypeData(obj, Py_TYPE(obj)));
# else
return *reinterpret_cast<function_record **>(reinterpret_cast<char *>(obj)
+ PyModule_Type.tp_basicsize);
# endif
}
#endif

PYBIND11_NAMESPACE_BEGIN(function_record_PyTypeObject_methods)

PyObject *tp_new_impl(PyTypeObject *type, PyObject *args, PyObject *kwds);
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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<void *>(function_record_PyTypeObject_methods::tp_dealloc_impl)},
{Py_tp_methods,
reinterpret_cast<void *>(function_record_PyTypeObject_methods::tp_methods_impl)},
// The module `tp_getattro` needs the module `__dict__`.
{Py_tp_getattro, reinterpret_cast<void *>(PyObject_GenericGetAttr)},
{0, nullptr}};
PyType_Spec spec = {function_record_PyTypeObject_methods::tp_module_qualname_impl,
# if PY_VERSION_HEX >= 0x030C0000
-static_cast<int>(sizeof(function_record *)),
# else
static_cast<int>(PyModule_Type.tp_basicsize
+ static_cast<Py_ssize_t>(sizeof(function_record *))),
# endif
0,
Py_TPFLAGS_DEFAULT,
slots};
// Python < 3.10 needs a tuple for the bases.
auto bases = reinterpret_steal<object>(
PyTuple_Pack(1, reinterpret_cast<PyObject *>(&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<PyTypeObject *>(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<object>(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;
Expand All @@ -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
Expand All @@ -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<object>(
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<object>(py_func_rec);
}

Expand Down
1 change: 1 addition & 0 deletions include/pybind11/detail/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ struct local_internals {

std::forward_list<ExceptionTranslator> registered_exception_translators;
PyTypeObject *function_record_py_type = nullptr;
PyTypeObject *function_record_module_py_type = nullptr;
};

enum class holder_enum_t : uint8_t {
Expand Down
49 changes: 44 additions & 5 deletions include/pybind11/pybind11-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<destructor>(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;
Expand Down Expand Up @@ -667,9 +681,15 @@ PYBIND11_INLINE void cpp_function::initialize_generic(unique_function_record &&u
= reinterpret_cast<PyCFunction>(reinterpret_cast<void (*)()>(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<PyCFunction>(
reinterpret_cast<void (*)()>(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());
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 */
Expand Down
8 changes: 8 additions & 0 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion tests/test_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,16 +525,20 @@ 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_<NestBase> base(m, "NestBase");
base.def(py::init<>());
py::class_<Nested>(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");
Expand Down
9 changes: 9 additions & 0 deletions tests/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
== """
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pickling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<module '?'>`, or, for a method:
# `<pybind11_detail_function_record_v1__gcc_libstdcpp_cxxabi1018 object at 0x...>`
# It is considered to be as much an implementation detail as the
# `pybind11::detail::function_record` C++ type is.
Expand Down
1 change: 1 addition & 0 deletions tools/check_inl_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading