Skip to content

Commit 75490c7

Browse files
committed
gh-135385: Fix memory regression for classes with both __slots__ and __dict__
1 parent 49d7236 commit 75490c7

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Objects/typeobject.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4621,7 +4621,19 @@ type_new_descriptors(const type_new_ctx *ctx, PyTypeObject *type)
46214621
}
46224622

46234623
type->tp_basicsize = slotoffset;
4624-
type->tp_itemsize = ctx->base->tp_itemsize;
4624+
4625+
// Only inherit tp_itemsize if this type defines its own __slots__
4626+
// Classes that don't define __slots__ but inherit from __slots__ classes
4627+
// should not inherit tp_itemsize as they don't use variable-size items
4628+
if (et->ht_slots != NULL && PyTuple_GET_SIZE(et->ht_slots) > 0) {
4629+
// This type defines its own __slots__, inherit tp_itemsize
4630+
type->tp_itemsize = ctx->base->tp_itemsize;
4631+
}
4632+
else {
4633+
// This type doesn't define __slots__, don't inherit tp_itemsize
4634+
type->tp_itemsize = 0;
4635+
}
4636+
46254637
type->tp_members = _PyHeapType_GET_MEMBERS(et);
46264638
return 0;
46274639
}

0 commit comments

Comments
 (0)