Skip to content

Commit 85ea07e

Browse files
encukouDinoV
authored andcommitted
bpo-37012: Clean up special cases in PyType_FromSpecWithBases slot assignments (pythonGH-13496)
The main slot assignment loop is now if-else if ladder, making the control flow clearer. Based on suggestion by Victor Stinner in: python#10304
1 parent 8ef3745 commit 85ea07e

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

Objects/typeobject.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2941,14 +2941,13 @@ PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
29412941
PyErr_SetString(PyExc_RuntimeError, "invalid slot offset");
29422942
goto fail;
29432943
}
2944-
if (slot->slot == Py_tp_base || slot->slot == Py_tp_bases)
2944+
else if (slot->slot == Py_tp_base || slot->slot == Py_tp_bases) {
29452945
/* Processed above */
29462946
continue;
2947-
*(void**)(res_start + slotoffsets[slot->slot]) = slot->pfunc;
2948-
2949-
/* need to make a copy of the docstring slot, which usually
2950-
points to a static string literal */
2951-
if (slot->slot == Py_tp_doc) {
2947+
}
2948+
else if (slot->slot == Py_tp_doc) {
2949+
/* For the docstring slot, which usually points to a static string
2950+
literal, we need to make a copy */
29522951
const char *old_doc = _PyType_DocWithoutSignature(type->tp_name, slot->pfunc);
29532952
size_t len = strlen(old_doc)+1;
29542953
char *tp_doc = PyObject_MALLOC(len);
@@ -2960,13 +2959,16 @@ PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
29602959
memcpy(tp_doc, old_doc, len);
29612960
type->tp_doc = tp_doc;
29622961
}
2963-
2964-
/* Move the slots to the heap type itself */
2965-
if (slot->slot == Py_tp_members) {
2962+
else if (slot->slot == Py_tp_members) {
2963+
/* Move the slots to the heap type itself */
29662964
size_t len = Py_TYPE(type)->tp_itemsize * nmembers;
29672965
memcpy(PyHeapType_GET_MEMBERS(res), slot->pfunc, len);
29682966
type->tp_members = PyHeapType_GET_MEMBERS(res);
29692967
}
2968+
else {
2969+
/* Copy other slots directly */
2970+
*(void**)(res_start + slotoffsets[slot->slot]) = slot->pfunc;
2971+
}
29702972
}
29712973
if (type->tp_dealloc == NULL) {
29722974
/* It's a heap type, so needs the heap types' dealloc.

0 commit comments

Comments
 (0)