10000 bpo-34784: Posixmodule: Heap StructSequence by eduardo-elizondo · Pull Request #9665 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-34784: Posixmodule: Heap StructSequence #9665

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

Merged
merged 12 commits into from
Nov 13, 2018
Prev Previous commit
Next Next commit
Addressed encukou comments
  • Loading branch information
eduardo-elizondo committed Nov 6, 2018
commit 8ac5c68bf912c984f47c51eb6fd232f905bf2172
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
Fix the implementation of PyStructSequence_NewType in order to create heap
allocated StructSequences.

Please refer to the following for the reasoning and motivation for this change:
https://mail.python.org/pipermail/python-dev/2018-September/155069.html
15 changes: 13 additions & 2 deletions Objects/structseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,12 @@ initialize_members(PyStructSequence_Desc *desc, PyMemberDef* members,
Py_ssize_t i, k;

for (i = k = 0; i < n_members; ++i) {
if (desc->fields[i].name == PyStructSequence_UnnamedField)
if (desc->fields[i].name == PyStructSequence_UnnamedField) {
continue;
}

/* The names and docstrings in these MemberDefs are statically */
/* allocated so it is expected that they'll outlive the MemberDef */
members[k].name = desc->fields[i].name;
members[k].type = T_OBJECT;
members[k].offset = offsetof(PyStructSequence, ob_item)
Expand All @@ -354,7 +358,12 @@ PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc)
}
#endif

Py_REFCNT(type) = 0;
/* PyTypeObject has already been initialized */
if (Py_REFCNT(type) != 0) {
PyErr_BadInternalCall();
return -1;
}

type->tp_name = desc->name;
type->tp_basicsize = sizeof(PyStructSequence) - sizeof(PyObject *);
type->tp_itemsize = sizeof(PyObject *);
Expand Down Expand Up @@ -426,6 +435,8 @@ PyStructSequence_NewType(PyStructSequence_Desc *desc)
slots[6] = (PyType_Slot){0, 0};

/* Initialize Spec */
/* The name in this PyType_Spec is statically allocated so it is */
/* expected that it'll outlive the PyType_Spec */
spec.name = desc->name;
spec.basicsize = sizeof(PyStructSequence) - sizeof(PyObject *);
spec.itemsize = sizeof(PyObject *);
Expand Down
20 changes: 12 additions & 8 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2852,15 +2852,19 @@ PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
PyTypeObject *type, *base;

PyType_Slot *slot;
Py_ssize_t nslots;
Py_ssize_t nmembers;
char *s, *res_start;

nslots = 0;
for (slot = spec->slots; slot->slot; slot++)
if (slot->slot == Py_tp_members)
for (memb = slot->pfunc; memb->name != NULL; memb++)
nslots++;
res = (PyHeapTypeObject*)PyType_GenericAlloc(&PyType_Type, nslots);
nmembers = 0;
for (slot = spec->slots; slot->slot; slot++) {
if (slot->slot == Py_tp_members) {
for (memb = slot->pfunc; memb->name != NULL; memb++) {
nmembers++;
}
}
}

res = (PyHeapTypeObject*)PyType_GenericAlloc(&PyType_Type, nmembers);
if (res == NULL)
return NULL;
res_start = (char*)res;
Expand Down Expand Up @@ -2962,7 +2966,7 @@ PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)

/* Move the slots to the heap type itself */
if (slot->slot == Py_tp_members) {
size_t len = Py_TYPE(type)->tp_itemsize * nslots;
size_t len = Py_TYPE(type)->tp_itemsize * nmembers;
memcpy(PyHeapType_GET_MEMBERS(res), slot->pfunc, len);
type->tp_members = PyHeapType_GET_MEMBERS(res);
}
Expand Down
0