This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Behavior of Structure inconsistent with BigEndianStructure when using __slots__
Type: behavior Stage: resolved
Components: ctypes Versions: Python 3.5
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Claudiu.Popa, Florian.Dold, amaury.forgeotdarc, belopolsky, eryksun, meador.inge, pitrou, python-dev
Priority: normal Keywords: patch

Created on 2014-07-28 21:58 by Florian.Dold, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit Florian.Dold, 2014-07-28 21:58 test case
issue22098.patch Claudiu.Popa, 2014-08-25 09:39 review
Messages (7)
msg224195 - (view) Author: Florian Dold (Florian.Dold) Date: 2014-07-28 21:58
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.
msg225658 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2014-08-22 11:07
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.
msg225674 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2014-08-22 14:58
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.
msg225675 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-08-22 15:02
> 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?
msg225870 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2014-08-25 09:39
Here's a short patch.
msg225872 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-08-25 13:56
Thanks Claudiu, the patch looks good to me.
msg226096 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-08-29 22:39
New changeset c499cc2c4a06 by Antoine Pitrou in branch 'default':
Issue #22098: ctypes' BigEndianStructure and LittleEndianStructure now define an empty __slots__ so that subclasses don't always get an instance dict.
Date User Action Args
2022-04-11 14:58:06adminsetgithub: 66296
2021-10-23 21:13:08yaubisetnosy: - yaubi
2014-08-29 22:39:40pitrousetstatus: open -> closed
resolution: fixed
stage: commit review -> resolved
2014-08-29 22:39:10python-devsetnosy: + python-dev
messages: + msg226096
2014-08-29 05:23:15Claudiu.Popasetstage: patch review -> commit review
2014-08-25 13:56:29pitrousetmessages: + msg225872
2014-08-25 09:39:18Claudiu.Popasetfiles: + issue22098.patch
keywords: + patch
messages: + msg225870

stage: needs patch -> patch review
2014-08-22 15:02:42pitrousetversions: + Python 3.5, - Python 3.4
nosy: + pitrou, belopolsky, amaury.forgeotdarc, meador.inge

messages: + msg225675

stage: needs patch
2014-08-22 14:58:41eryksunsetnosy: + eryksun
messages: + msg225674
2014-08-22 11:07:51Claudiu.Popasetnosy: + Claudiu.Popa
messages: + msg225658
2014-07-29 02:51:43yaubisetnosy: + yaubi
2014-07-28 21:59:24Florian.Doldsettype: behavior
2014-07-28 21:58:29Florian.Doldcreate