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

Behavior of Structure inconsistent with BigEndianStructure when using __slots__ #66296

Closed
FlorianDold mannequin opened this issue Jul 28, 2014 · 7 comments
Closed

Behavior of Structure inconsistent with BigEndianStructure when using __slots__ #66296

FlorianDold mannequin opened this issue Jul 28, 2014 · 7 comments
Labels
topic-ctypes type-bug An unexpected behavior, bug, or error

Comments

@FlorianDold
Copy link
Mannequin

FlorianDold mannequin commented Jul 28, 2014

BPO 22098
Nosy @amauryfa, @abalkin, @pitrou, @meadori, @PCManticore, @eryksun
Files
  • bug-ctypes-slots.py: test case
  • issue22098.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 = None
    closed_at = <Date 2014-08-29.22:39:40.065>
    created_at = <Date 2014-07-28.21:58:29.214>
    labels = ['ctypes', 'type-bug']
    title = 'Behavior of Structure inconsistent with BigEndianStructure when using __slots__'
    updated_at = <Date 2021-10-23.21:13:08.265>
    user = 'https://bugs.python.org/FlorianDold'

    bugs.python.org fields:

    activity = <Date 2021-10-23.21:13:08.265>
    actor = 'yaubi'
    assignee = 'none'
    closed = True
    closed_date = <Date 2014-08-29.22:39:40.065>
    closer = 'pitrou'
    components = ['ctypes']
    creation = <Date 2014-07-28.21:58:29.214>
    creator = 'Florian.Dold'
    dependencies = []
    files = ['36146', '36467']
    hgrepos = []
    issue_num = 22098
    keywords = ['patch']
    message_count = 7.0
    messages = ['224195', '225658', '225674', '225675', '225870', '225872', '226096']
    nosy_count = 8.0
    nosy_names = ['amaury.forgeotdarc', 'belopolsky', 'pitrou', 'meador.inge', 'Claudiu.Popa', 'python-dev', 'eryksun', 'Florian.Dold']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue22098'
    versions = ['Python 3.5']

    @FlorianDold
    Copy link
    Mannequin Author

    FlorianDold mannequin commented Jul 28, 2014

    Habimg __slots__ = [] on a class inheriting from ctypes.Structure prevents undefined fields from being set, as expected.

    When inheriting from ctypes.BigEndianStructure, however, it is possible to set undefined fields.

    See the attached file for small test case for the behavior.

    @FlorianDold FlorianDold mannequin added topic-ctypes type-bug An unexpected behavior, bug, or error labels Jul 28, 2014
    @PCManticore
    Copy link
    Mannequin

    PCManticore mannequin commented Aug 22, 2014

    That makes sense. Quoting from the data model: "When inheriting from a class without __slots__, the __dict__ attribute of that class will always be accessible, so a __slots__ definition in the subclass is meaningless".
    In the current case, for the little-endian systems, which I presume you have, BigEndianStructure is a subclass of Structure, but it doesn't have a definition of __slots__ in the body, leading to your results, according to the specificaton from the data model. In this case, it makes sense for Point1 to have __slots__, but not for Point2.

    @eryksun
    Copy link
    Contributor

    eryksun commented Aug 22, 2014

    Since BigEndianStructure doesn't explicitly define the instance slots, they get the default slots for __dict__ and __weakref__. It wouldn't make sense to exclude these slots from instances of the Point2 subclass, so there's no mechanism for that. You can only add slots.

    That said, the BigEndianStructure and LittleEndianStructure subclasses should define __slots__ = (). That removes the inconsistency.

    http://hg.python.org/cpython/file/c0e311e010fc/Lib/ctypes/_endian.py#l46

    @pitrou
    Copy link
    Member

    pitrou commented Aug 22, 2014

    That said, the BigEndianStructure and LittleEndianStructure subclasses should define __slots__ = (). That removes the inconsistency.

    That would probably be reasonable indeed. Anyone wants to write a patch?

    @PCManticore
    Copy link
    Mannequin

    PCManticore mannequin commented Aug 25, 2014

    Here's a short patch.

    @pitrou
    Copy link
    Member

    pitrou commented Aug 25, 2014

    Thanks Claudiu, the patch looks good to me.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Aug 29, 2014

    New changeset c499cc2c4a06 by Antoine Pitrou in branch 'default':
    Issue bpo-22098: ctypes' BigEndianStructure and LittleEndianStructure now define an empty __slots__ so that subclasses don't always get an instance dict.
    http://hg.python.org/cpython/rev/c499cc2c4a06

    @pitrou pitrou closed this as completed Aug 29, 2014
    @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
    topic-ctypes type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants