classification
Title: ctypes Structures with subclassed types in the _fields_ list
Type: behavior Stage: patch review
Components: ctypes Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Arfrever, amaury.forgeotdarc, belopolsky, meador.inge, theller
Priority: normal Keywords: needs review, patch

Created on 2012-07-26 18:35 by theller, last changed 2012-08-03 02:58 by meador.inge.

Files
File name Uploaded Description Edit
showbug.py theller, 2012-07-26 18:35 demonstrates the bitfield bug
bitfields.patch theller, 2012-07-26 18:38 review
Messages (3)
msg166496 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2012-07-26 18:35
When a ctypes Structure uses a subclass of c_int, c_short, c_byte in the _fields_ list to define bitfields, the results are wrong.

The attached script showbug.py demonstrates this behaviour.
msg166497 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2012-07-26 18:38
The attached patch for branch default fixes this issue.  It also contains a unittest.
msg167285 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2012-08-03 02:58
OK, I finally had some time to look at this.  The attached patch does fix the issue.  However, it has a nasty side effect in that the encoded bitfield size stays with the field member object:

   >>> class SubByte(c_ubyte): pass
   ... 
   >>> class A(Structure):
   ...    _fields_ = [("x", SubByte, 5)]
   ... 
   >>> sizeof(A().x)
   327680

Before the patch things look like:

   >>> class SubByte(c_ubyte): pass
   ... 
   >>> class A(Structure):
   ...    _fields_ = [("x", SubByte, 5)]
   ... 
   >>> sizeof(A().x)
   1

Leaking the internal bitfield size encoding seems like a deal breaker to me.
History
Date User Action Args
2012-08-03 02:58:45meador.ingesettype: behavior
messages: + msg167285
2012-07-26 21:16:51eric.araujosetnosy: + amaury.forgeotdarc, belopolsky, meador.inge
stage: patch review

versions: + Python 3.2, - Python 3.4
2012-07-26 19:10:08Arfreversetnosy: + Arfrever
2012-07-26 18:38:47thellersetfiles: + bitfields.patch

messages: + msg166497
2012-07-26 18:35:05thellercreate