.. versionadded:: 3.9
+ .. versionchanged:: 3.10
+
+ The function now accepts NULL ``tp_doc`` slot.
+
.. c:function:: PyObject* PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
Equivalent to ``PyType_FromModuleAndSpec(NULL, spec, bases)``.
The desired value of the slot. In most cases, this is a pointer
to a function.
-
- May not be ``NULL``.
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.
+ (Contributed by Hai Shi in :issue:`41832`.)
+
Porting to Python 3.10
----------------------
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):
--- /dev/null
+The :c:func:`PyType_FromModuleAndSpec` function now accepts NULL ``tp_doc``
+slot.
static PyStructSequence_Desc profiler_entry_desc = {
.name = "_lsprof.profiler_entry",
- .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
};
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"
}
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);
+ if (NullTpDocType == NULL) {
+ return NULL;
+ }
+ PyModule_AddObject(m, "NullTpDocType", NullTpDocType);
+
PyObject *HeapGcCType = PyType_FromSpec(&HeapGcCType_spec);
if (HeapGcCType == NULL) {
return NULL;
else if (slot->slot == Py_tp_doc) {
/* For the docstring slot, which usually points to a static string
literal, we need to make a copy */
+ if (slot->pfunc == NULL) {
+ type->tp_doc = NULL;
+ continue;
+ }
size_t len = strlen(slot->pfunc)+1;
char *tp_doc = PyObject_MALLOC(len);
if (tp_doc == NULL) {