New submission from STINNER Victor <vstin...@python.org>:

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 <ge...@python.org>
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.

----------
components: Interpreter Core
messages: 377308
nosy: vstinner
priority: normal
severity: normal
status: open
title: PyType_FromSpec() should accept tp_doc=NULL
versions: Python 3.10

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue41832>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to