Skip to content

gh-135385: Fix memory regression for classes with both __slots__ and __dict__ #135389

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a memory regression for classes that have both :attr:`~object.__slots__` and
:attr:`~object.__dict__`.
14 changes: 13 additions & 1 deletion Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4621,7 +4621,19 @@ type_new_descriptors(const type_new_ctx *ctx, PyTypeObject *type)
}

type->tp_basicsize = slotoffset;
type->tp_itemsize = ctx->base->tp_itemsize;

// Only inherit tp_itemsize if this type defines its own __slots__
// Classes that don't define __slots__ but inherit from __slots__ classes
// should not inherit tp_itemsize as they don't use variable-size items
if (et->ht_slots != NULL && PyTuple_GET_SIZE(et->ht_slots) > 0) {
// This type defines its own __slots__, inherit tp_itemsize
type->tp_itemsize = ctx->base->tp_itemsize;
}
else {
// This type doesn't define __slots__, don't inherit tp_itemsize
type->tp_itemsize = 0;
}

type->tp_members = _PyHeapType_GET_MEMBERS(et);
return 0;
}
Expand Down
Loading