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.

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

Created on 2012-07-26 18:35 by theller, last changed 2022-04-11 14:57 by admin.

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 (4)
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.
msg348756 - (view) Author: Tommy Sparber (tsparber) Date: 2019-07-30 16:24
This bug is still present using Python 3.7.
I'm also not sure how to fix the leakage of the internal bitfield size encoding.
History
Date User Action Args
2022-04-11 14:57:33adminsetgithub: 59664
2019-07-30 16:24:58tsparbersetnosy: + tsparber

messages: + msg348756
versions: + Python 3.5, Python 3.6, Python 3.7
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