Skip to content
Merged
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
Allocate & copy the docstring early
  • Loading branch information
encukou committed Jun 10, 2022
commit 87b28aa319e0ee2b0c925e4efdc21400a2b9477b
55 changes: 30 additions & 25 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3406,6 +3406,7 @@ PyType_FromMetaclass(PyTypeObject *metaclass, PyObject *module,
PyObject *modname = NULL;
Comment thread
encukou marked this conversation as resolved.
Outdated
PyTypeObject *type;
PyObject *bases = NULL;
char *tp_doc = NULL;
int r;

const PyType_Slot *slot;
Expand Down Expand Up @@ -3451,6 +3452,29 @@ PyType_FromMetaclass(PyTypeObject *metaclass, PyObject *module,
}
}
break;
case Py_tp_doc:
Comment thread
erlend-aasland marked this conversation as resolved.
/* For the docstring slot, which usually points to a static string
literal, we need to make a copy */
if (tp_doc != NULL) {
PyErr_SetString(
PyExc_SystemError,
"Multiple Py_tp_doc slots are not supported.");
return NULL;
}
if (slot->pfunc == NULL) {
PyObject_Free(tp_doc);
tp_doc = NULL;
}
else {
size_t len = strlen(slot->pfunc)+1;
tp_doc = PyObject_Malloc(len);
if (tp_doc == NULL) {
PyErr_NoMemory();
goto finally;
}
memcpy(tp_doc, slot->pfunc, len);
}
break;
}
}

Expand Down Expand Up @@ -3555,43 +3579,23 @@ PyType_FromMetaclass(PyTypeObject *metaclass, PyObject *module,
type->tp_bases = bases;
bases = NULL; // We give our reference to bases to the type

type->tp_doc = tp_doc;
tp_doc = NULL; // Give ownership of the allocated memory to the type

/* Copy the sizes */
type->tp_basicsize = spec->basicsize;
type->tp_itemsize = spec->itemsize;

for (slot = spec->slots; slot->slot; slot++) {
size_t len;
switch (slot->slot) {
case Py_tp_base:
case Py_tp_bases:
/* Processed above */
break;
case Py_tp_doc:
/* For the docstring slot, which usually points to a static string
literal, we need to make a copy */
if (type->tp_doc != NULL) {
PyErr_SetString(
PyExc_SystemError,
"Multiple Py_tp_doc slots are not supported.");
return NULL;
}
if (slot->pfunc == NULL) {
type->tp_doc = NULL;
break;
}
len = strlen(slot->pfunc)+1;
char *tp_doc = PyObject_Malloc(len);
if (tp_doc == NULL) {
type->tp_doc = NULL;
PyErr_NoMemory();
goto finally;
}
memcpy(tp_doc, slot->pfunc, len);
type->tp_doc = tp_doc;
/* Processed above */
break;
case Py_tp_members:
/* Move the slots to the heap type itself */
len = Py_TYPE(type)->tp_itemsize * nmembers;
size_t len = Py_TYPE(type)->tp_itemsize * nmembers;
memcpy(_PyHeapType_GET_MEMBERS(res), slot->pfunc, len);
type->tp_members = _PyHeapType_GET_MEMBERS(res);
break;
Expand Down Expand Up @@ -3687,6 +3691,7 @@ PyType_FromMetaclass(PyTypeObject *metaclass, PyObject *module,
}
Py_XDECREF(bases);
Py_XDECREF(modname);
PyObject_Free(tp_doc);
return (PyObject*)res;
}

Expand Down