classification
Title: Make __sizeof__ for builtin types more subclass friendly
Type: behavior Stage: resolved
Components: Extension Modules, Interpreter Core Versions: Python 3.6, Python 3.5, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: pitrou, python-dev, r.david.murray, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2015-10-16 16:42 by serhiy.storchaka, last changed 2015-12-20 07:27 by serhiy.storchaka. This issue is now closed.

Files
File name Uploaded Description Edit
sizeof_dynamic.patch serhiy.storchaka, 2015-10-16 16:41 review
Messages (6)
msg253074 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-10-16 16:41
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.
msg256734 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-12-19 18:08
New changeset 71d6755145ae by Serhiy Storchaka in branch '3.5':
Issue #25421: __sizeof__ methods of builtin types now use dynamic basic size.
https://hg.python.org/cpython/rev/71d6755145ae

New changeset 4a47e998c40a by Serhiy Storchaka in branch 'default':
Issue #25421: __sizeof__ methods of builtin types now use dynamic basic size.
https://hg.python.org/cpython/rev/4a47e998c40a

New changeset be3998aed1e7 by Serhiy Storchaka in branch '2.7':
Issue #25421: __sizeof__ methods of builtin types now use dynamic basic size.
https://hg.python.org/cpython/rev/be3998aed1e7
msg256735 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-12-19 18:13
Committed with improved tests.
msg256737 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-12-19 19:08
At least some of the buildbots aren't happy.

http://buildbot.python.org/all/builders/ARMv7%20Ubuntu%203.5/builds/479/steps/test/logs/stdio

http://buildbot.python.org/all/builders/ARMv7%20Ubuntu%202.7/builds/1077/steps/test/logs/stdio

http://buildbot.python.org/all/builders/AMD64%20FreeBSD%209.x%202.7/builds/1098/steps/test/logs/stdio
msg256738 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-12-19 20:51
New changeset a1388dbdd293 by Serhiy Storchaka in branch '3.5':
Use correct PyGC_Head size in tests for issue #25421.
https://hg.python.org/cpython/rev/a1388dbdd293

New changeset ab97ff838e11 by Serhiy Storchaka in branch '2.7':
Use correct PyGC_Head size in tests for issue #25421.
https://hg.python.org/cpython/rev/ab97ff838e11

New changeset 5115f39cc59f by Serhiy Storchaka in branch 'default':
Use correct PyGC_Head size in tests for issue #25421.
https://hg.python.org/cpython/rev/5115f39cc59f
msg256739 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-12-19 20:52
Thank you David. Looks related to alignment. Hope this will help.
History
Date User Action Args
2015-12-20 07:27:55serhiy.storchakasetstatus: open -> closed
2015-12-19 20:52:33serhiy.storchakasetmessages: + msg256739
2015-12-19 20:51:26python-devsetmessages: + msg256738
2015-12-19 19:08:32r.david.murraysetstatus: closed -> open
nosy: + r.david.murray
messages: + msg256737

2015-12-19 18:13:45serhiy.storchakasetstatus: open -> closed
versions: - Python 3.4
messages: + msg256735

assignee: serhiy.storchaka
resolution: fixed
stage: patch review -> resolved
2015-12-19 18:08:30python-devsetnosy: + python-dev
messages: + msg256734
2015-10-17 03:20:04rhettingersetnosy: + pitrou
2015-10-16 16:42:00serhiy.storchakacreate