8000 bpo-41832: PyType_FromModuleAndSpec() can accept the NULL tp_doc slot. by shihai1991 · Pull Request #23123 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-41832: PyType_FromModuleAndSpec() can accept the NULL tp_doc slot. #23123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
apply victor's comment
  • Loading branch information
shihai1991 committed Nov 4, 2020
commit 1f5eec340388667c536fba92518fbbfda6aac530
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
@@ -1 +1,2 @@
:c:func:`PyType_FromModuleAndSpec` can accept tp_doc=NULL.
The :c:func:`PyType_FromModuleAndSpec` function now accepts NULL ``tp_doc``
slot.
2 changes: 2 additions & 0 deletions Modules/_lsprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,12 +490,14 @@ static PyStructSequence_Field profiler_subentry_fields[] = {
static PyStructSequence_Desc profiler_entry_desc = {
.name = "_lsprof.profiler_entry",
.fields = profiler_entry_fields,
.doc = NULL,
.n_in_sequence = 6
};

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

Expand Down
39 changes: 23 additions & 16 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3990,21 +3990,6 @@ test_structseq_newtype_doesnt_leak(PyObject *Py_UNUSED(self),
Py_RETURN_NONE;
}

static PyType_Spec HeapDocCType_spec;

static PyObject *
test_PyType_FromSpec(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
{
void *tp_doc = HeapDocCType_spec.slots[0].pfunc;
HeapDocCType_spec.slots[0].pfunc = NULL;
PyObject *HeapDocCType = PyType_FromSpec(&HeapDocCType_spec);
assert(HeapDocCType != NULL);
HeapDocCType_spec.slots[0].pfunc = tp_doc;
Py_DECREF(HeapDocCType);

Py_RETURN_NONE;
}

static PyObject *
test_incref_decref_API(PyObject *ob, PyObject *Py_UNUSED(ignored))
{
Expand Down Expand Up @@ -5616,7 +5601,6 @@ static PyMethodDef TestMethods[] = {
{"test_decref_doesnt_leak", test_decref_doesnt_leak, METH_NOARGS},
{"test_structseq_newtype_doesnt_leak",
test_structseq_newtype_doesnt_leak, METH_NOARGS},
{"test_PyType_FromSpec", test_PyType_FromSpec, METH_NOARGS},
{"test_incref_decref_API", test_incref_decref_API, METH_NOARGS},
{"test_long_and_overflow", test_long_and_overflow, METH_NOARGS},
{"test_long_as_double", test_long_as_double, METH_NOARGS},
Expand Down Expand Up @@ -6524,6 +6508,23 @@ static PyType_Spec HeapDocCType_spec = {
HeapDocCType_slots
};

typedef struct {
PyObject_HEAD
} NullTpDocTypeObject;

static PyType_Slot NullTpDocType_slots[] = {
8000 {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 @@ -7199,6 +7200,12 @@ PyInit__testcapi(void)
}
PyModule_AddObject(m, "HeapDocCType", HeapDocCType);

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;
Expand Down
0