-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
PyType_FromSpec() should accept tp_doc=NULL #85998
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
Comments
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
(...)
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. |
I will take a look in this weekend :) |
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?) If you want to only change this for tp_doc, please also update the PyType_Slot documentation to cover the exception. |
IMO, it's a proper user case.
Copy that, I will update it soon. |
Thanks Hai Shi. |
Thank you all:) |
The PR entirely removed the note that a slot's value "May not be NULL". |
|
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? |
Hi, petr. If there is no other doubt about this bpo, I suggest close this bpo. |
Sorry Petr, I didn't notice that the note was fully removed in the first change. Thanks for restoring the note! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: