[New-bugs-announce] [issue25421] Make __sizeof__ for builtin types more subclass friendly

Serhiy Storchaka report at bugs.python.org
Fri Oct 16 18:42:00 CEST 2015


New submission from Serhiy Storchaka:

Default __sizeof__() implementation uses tp_basicsize. This makes it to work correctly with most builtin types and types with __slots__ (using __slots__ increases tp_basicsize). But special implementations of __sizeof__() use static object size ('sizeof(XXXObject)'), and return incorrect result for subclasses that increase tp_basicsize. Proposed patch makes __sizeof__() for all subclassable builtin type that support changing object size (i.e. tp_itemsize == 0) to use dynamic size _PyObject_SIZE(Py_TYPE(self)).

Example (with patched code):
>>> class D(dict):
...     __slots__ = 'a', 'b', 'c'
... 
>>> sys.getsizeof({})
144
>>> sys.getsizeof(D())
156

In unpatched Python sys.getsizeof(D()) returns 144.

----------
components: Extension Modules, Interpreter Core
files: sizeof_dynamic.patch
keywords: patch
messages: 253074
nosy: serhiy.storchaka
priority: normal
severity: normal
stage: patch review
status: open
title: Make __sizeof__ for builtin types more subclass friendly
type: behavior
versions: Python 2.7, Python 3.4, Python 3.5, Python 3.6
Added file: http://bugs.python.org/file40795/sizeof_dynamic.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25421>
_______________________________________


More information about the New-bugs-announce mailing list