Skip to content
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

Make __sizeof__ for builtin types more subclass friendly #69607

Closed
serhiy-storchaka opened this issue Oct 16, 2015 · 6 comments
Closed

Make __sizeof__ for builtin types more subclass friendly #69607

serhiy-storchaka opened this issue Oct 16, 2015 · 6 comments
Assignees
Labels
extension-modules C modules in the Modules dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@serhiy-storchaka
Copy link
Member

BPO 25421
Nosy @pitrou, @bitdancer, @serhiy-storchaka
Files
  • sizeof_dynamic.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2015-12-20.07:27:55.452>
    created_at = <Date 2015-10-16.16:42:00.760>
    labels = ['extension-modules', 'interpreter-core', 'type-bug']
    title = 'Make __sizeof__ for builtin types more subclass friendly'
    updated_at = <Date 2015-12-20.07:27:55.451>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2015-12-20.07:27:55.451>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2015-12-20.07:27:55.452>
    closer = 'serhiy.storchaka'
    components = ['Extension Modules', 'Interpreter Core']
    creation = <Date 2015-10-16.16:42:00.760>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = ['40795']
    hgrepos = []
    issue_num = 25421
    keywords = ['patch']
    message_count = 6.0
    messages = ['253074', '256734', '256735', '256737', '256738', '256739']
    nosy_count = 4.0
    nosy_names = ['pitrou', 'r.david.murray', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue25421'
    versions = ['Python 2.7', 'Python 3.5', 'Python 3.6']

    @serhiy-storchaka
    Copy link
    Member Author

    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.

    @serhiy-storchaka serhiy-storchaka added extension-modules C modules in the Modules dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Oct 16, 2015
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Dec 19, 2015

    New changeset 71d6755145ae by Serhiy Storchaka in branch '3.5':
    Issue bpo-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 bpo-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 bpo-25421: __sizeof__ methods of builtin types now use dynamic basic size.
    https://hg.python.org/cpython/rev/be3998aed1e7

    @serhiy-storchaka
    Copy link
    Member Author

    Committed with improved tests.

    @bitdancer bitdancer reopened this Dec 19, 2015
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Dec 19, 2015

    New changeset a1388dbdd293 by Serhiy Storchaka in branch '3.5':
    Use correct PyGC_Head size in tests for issue bpo-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 bpo-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 bpo-25421.
    https://hg.python.org/cpython/rev/5115f39cc59f

    @serhiy-storchaka
    Copy link
    Member Author

    Thank you David. Looks related to alignment. Hope this will help.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    extension-modules C modules in the Modules dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants