bpo-41832: PyType_FromModuleAndSpec() can accept the NULL tp_doc slot. by shihai1991 · Pull Request #23123 · 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
6 changes: 4 additions & 2 deletions Doc/c-api/type.rst
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,10 @@ New Features
reference count of an object and return the object.
(Contributed by Victor Stinner in :issue:`42262`.)

* The :c:func:`PyType_FromModuleAndSpec` function now accepts NULL ``tp_doc``
slot.
Comment thread
shihai1991 marked this conversation as resolved.
(Contributed by Hai Shi in :issue:`41832`.)


Porting to Python 3.10
----------------------
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_capi.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,9 @@ def test_heap_ctype_doc_and_text_signature(self):
self.assertEqual(_testcapi.HeapDocCType.__doc__, "somedoc")
self.assertEqual(_testcapi.HeapDocCType.__text_signature__, "(arg1, arg2)")

def test_null_type_doc(self):
self.assertEqual(_testcapi.NullTpDocType.__doc__, None)

def test_subclass_of_heap_gc_ctype_with_tpdealloc_decrefs_once(self):
class HeapGcCTypeSubclass(_testcapi.HeapGcCType):
def __init__(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The :c:func:`PyType_FromModuleAndSpec` function now accepts NULL ``tp_doc``
slot.
4 changes: 2 additions & 2 deletions Modules/_lsprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,15 +489,15 @@ static PyStructSequence_Field profiler_subentry_fields[] = {

static PyStructSequence_Desc profiler_entry_desc = {
.name = "_lsprof.profiler_entry",
Comment thread
shihai1991 marked this conversation as resolved.
.doc = "",
.fields = profiler_entry_fields,
.doc = NULL,
.n_in_sequence = 6
};

static PyStructSequence_Desc profiler_subentry_desc = {
.name = "_lsprof.profiler_subentry",
.doc = "",
.fields = profiler_subentry_fields,
.doc = NULL,
.n_in_sequence = 5
};

Expand Down
25 changes: 25 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -6508,6 +6508,23 @@ static PyType_Spec HeapDocCType_spec = {
HeapDocCType_slots
};

typedef struct {
PyObject_HEAD
} NullTpDocTypeObject;

static PyType_Slot NullTpDocType_slots[] = {
{Py_tp_doc, NULL},
{0, 0},
};

static PyType_Spec NullTpDocType_spec = {
"_testcapi.NullTpDocType",
sizeof(NullTpDocTypeObject),
0,
Py_TPFLAGS_DEFAULT,
NullTpDocType_slots
};


PyDoc_STRVAR(heapgctype__doc__,
"A heap type with GC, and with overridden dealloc.\n\n"
Expand Down Expand Up @@ -7183,6 +7200,14 @@ PyInit__testcapi(void)
}
PyModule_AddObject(m, "HeapDocCType", HeapDocCType);

/* bpo-41832: Add a new type to test PyType_FromSpec()
now can accept a NULL tp_doc slot. */
PyObject *NullTpDocType = PyType_FromSpec(&NullTpDocType_spec);
Comment thread
shihai1991 marked this conversation as resolved.
if (NullTpDocType == NULL) {
return NULL;
}
PyModule_AddObject(m, "NullTpDocType", NullTpDocType);

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