-
-
Notifications
You must be signed in to change notification settings - Fork 29.2k
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
Comments
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. |
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". |
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 |
That would probably be reasonable indeed. Anyone wants to write a patch? |
Here's a short patch. |
Thanks Claudiu, the patch looks good to me. |
New changeset c499cc2c4a06 by Antoine Pitrou in branch 'default': |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: