Message377308
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 032400b2d83ba1c2e4ee1cd33f51e9a598b2cf6c
Author: Georg Brandl <[email protected]>
Date: Sat Feb 19 21:47:02 2011 +0000
#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. |
|
| Date |
User |
Action |
Args |
| 2020-09-22 10:29:32 | vstinner | set | recipients:
+ vstinner |
| 2020-09-22 10:29:31 | vstinner | set | messageid: <[email protected]> |
| 2020-09-22 10:29:31 | vstinner | link | issue41832 messages |
| 2020-09-22 10:29:31 | vstinner | create | |
|