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) *  |
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) *  |
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. http://hg.python.org/cpython/file/c0e311e010fc/Lib/ctypes/_endian.py#l46 |
|
|
msg225675 - (view) |
Author: Antoine Pitrou (pitrou) *  |
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) *  |
Date: 2014-08-25 09:39 |
Here's a short patch. |
|
|
msg225872 - (view) |
Author: Antoine Pitrou (pitrou) *  |
Date: 2014-08-25 13:56 |
Thanks Claudiu, the patch looks good to me. |
|
|
msg226096 - (view) |
Author: Roundup Robot (python-dev)  |
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. http://hg.python.org/cpython/rev/c499cc2c4a06 |
|
|