You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PyType_FromSpec() should accept tp_doc=NULL #85998
When a Python type is defined in a C extension module as a static type, its documentation can be a NULL string (NULL pointer). But PyType_FromSpec() does crash if tp_doc is NULL, since this change:
bpo-11249: in PyType_FromSpec, copy tp_doc slot since it usually will point to a static string literal which should not be deallocated together with the type.
(...)
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index e9c7591b81..b1fe44ebe4 100644
--- a/Objects/typeobject.c+++ b/Objects/typeobject.c@@ -2347,6 +2347,17 @@ PyObject* PyType_FromSpec(PyType_Spec *spec)
goto fail;
}
*(void**)(res_start + slotoffsets[slot->slot]) = slot->pfunc;
++ /* need to make a copy of the docstring slot, which usually+ points to a static string literal */+ if (slot->slot == Py_tp_doc) {+ ssize_t len = strlen(slot->pfunc)+1;+ char *tp_doc = PyObject_MALLOC(len);+ if (tp_doc == NULL)+ goto fail;+ memcpy(tp_doc, slot->pfunc, len);+ res->ht_type.tp_doc = tp_doc;+ }
}
return (PyObject*)res;
I propose to accept tp_doc=NULL in PyType_FromSpec(), as we do in static types.
I noticed this difference while reviewing PR 22220 which convert _lsprof extension static types to heap types.
If you change this, why do it only for tp_doc, but for all the slots? NULL should *always* mean that the slot is set to NULL instead of inherited. (Except maybe in cases where this is dangerous; then it should result in an error?)
See: https://bugs.python.org/issue26979
If you want to only change this for tp_doc, please also update the PyType_Slot documentation to cover the exception.
If you change this, why do it only for tp_doc, but for all the slots? NULL should *always* mean that the slot is set to NULL instead of inherited. (Except maybe in cases where this is dangerous; then it should result in an error?
IMO, it's a proper user case.
From bpo-26978: "I'll also try making an explicit {Py_tp_dealloc, NULL} override the inherited value, as per Serhiy's suggestion."
Petr, do you have a PR about it?
If you want to only change this for tp_doc, please also update the PyType_Slot documentation to cover the exception.
The PR entirely removed the note that a slot's value "May not be NULL".
In fact, only tp_doc may be NULL. AFAIK, the restriction is still there for other slots.
Can that note be added back?
The PR entirely removed the note that a slot's value "May not be NULL".
In fact, only tp_doc may be NULL. AFAIK, the restriction is still there > for other slots.
Can that note be added back?
May not be NULL means everthing is possible, so I decided removed it:(
Before, I updated an old commit in PR23123 to describe this fact in cca7bc7.
No, it does not. It only means a slot's value may not be NULL, which is an important difference between slots and entries in the PyTypeObject structure.
It's OK that it's mentioned elsewhere, but can we please put it back in the docs of PyType_Slot?
Activity
vstinner commentedon Sep 22, 2020
When a Python type is defined in a C extension module as a static type, its documentation can be a NULL string (NULL pointer). But PyType_FromSpec() does crash if tp_doc is NULL, since this change:
commit 032400b
Author: Georg Brandl <georg@python.org>
Date: Sat Feb 19 21:47:02 2011 +0000
I propose to accept tp_doc=NULL in PyType_FromSpec(), as we do in static types.
I noticed this difference while reviewing PR 22220 which convert _lsprof extension static types to heap types.
shihai1991 commentedon Nov 2, 2020
I will take a look in this weekend :)
encukou commentedon Nov 3, 2020
The PyType_Slot documentation says that the pointer may not be NULL: https://docs.python.org/3/c-api/type.html?highlight=pytype_fromspec#c.PyType_Slot.PyType_Slot.pfunc
If you change this, why do it only for tp_doc, but for all the slots? NULL should *always* mean that the slot is set to NULL instead of inherited. (Except maybe in cases where this is dangerous; then it should result in an error?)
See: https://bugs.python.org/issue26979
If you want to only change this for tp_doc, please also update the PyType_Slot documentation to cover the exception.
shihai1991 commentedon Nov 4, 2020
IMO, it's a proper user case.
From bpo-26978: "I'll also try making an explicit
{Py_tp_dealloc, NULL}override the inherited value, as per Serhiy's suggestion."Petr, do you have a PR about it?
Copy that, I will update it soon.
vstinner commentedon Nov 6, 2020
New changeset 88c2cfd by Hai Shi in branch 'master':
bpo-41832: PyType_FromModuleAndSpec() now accepts NULL tp_doc (GH-23123)
88c2cfd
vstinner commentedon Nov 6, 2020
Thanks Hai Shi.
shihai1991 commentedon Nov 7, 2020
Thank you all:)
encukou commentedon Nov 10, 2020
The PR entirely removed the note that a slot's value "May not be NULL".
In fact, only tp_doc may be NULL. AFAIK, the restriction is still there for other slots.
Can that note be added back?
shihai1991 commentedon Nov 11, 2020
May not be NULLmeans everthing is possible, so I decided removed it:(Before, I updated an old commit in PR23123 to describe this fact in cca7bc7.
encukou commentedon Nov 11, 2020
No, it does not. It only means a slot's value may not be NULL, which is an important difference between slots and entries in the PyTypeObject structure.
It's OK that it's mentioned elsewhere, but can we please put it back in the docs of PyType_Slot?
encukou commentedon Nov 14, 2020
New changeset 2b39da4 by Hai Shi in branch 'master':
bpo-41832: Restore note about NULL in PyType_Slot.pfunc (GH-23243)
2b39da4
shihai1991 commentedon Nov 15, 2020
Hi, petr. If there is no other doubt about this bpo, I suggest close this bpo.
vstinner commentedon Nov 15, 2020
Sorry Petr, I didn't notice that the note was fully removed in the first change. Thanks for restoring the note!