closes bpo-41689: Preserve text signature from tp_doc in C heap type creation. by benjaminp · Pull Request #22058 · python/cpython · GitHub
Skip to content
Merged
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
4 changes: 4 additions & 0 deletions Lib/test/test_capi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Types created with :c:func:`PyType_FromSpec` now make any signature in their
``tp_doc`` slot accessible from ``__text_signature__``.
30 changes: 30 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -6462,6 +6462,30 @@ static PyTypeObject MethodDescriptor2_Type = {
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_VECTORCALL,
};

PyDoc_STRVAR(heapdocctype__doc__,
"HeapDocCType(arg1, arg2)\n"
"--\n"
"\n"
"somedoc");

typedef struct {
PyObject_HEAD
} HeapDocCTypeObject;

static PyType_Slot HeapDocCType_slots[] = {
{Py_tp_doc, (char*)heapdocctype__doc__},
{0},
};

static PyType_Spec HeapDocCType_spec = {
"_testcapi.HeapDocCType",
sizeof(HeapDocCTypeObject),
0,
Py_TPFLAGS_DEFAULT,
HeapDocCType_slots
};


PyDoc_STRVAR(heapgctype__doc__,
"A heap type with GC, and with overridden dealloc.\n\n"
"The 'value' attribute is set to 10 in __init__.");
Expand Down Expand Up @@ -7130,6 +7154,12 @@ PyInit__testcapi(void)
Py_INCREF(TestError);
PyModule_AddObject(m, "error", TestError);

PyObject *HeapDocCType = PyType_FromSpec(&HeapDocCType_spec);
if (HeapDocCType == NULL) {
return NULL;
}
PyModule_AddObject(m, "HeapDocCType", HeapDocCType);

PyObject *HeapGcCType = PyType_FromSpec(&HeapGcCType_spec);
if (HeapGcCType == NULL) {
return NULL;
Expand Down
15 changes: 12 additions & 3 deletions Objects/typeobject.c