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: ctype with packed bitfields does not match native compiler
Type: Stage: resolved
Components: ctypes Versions: Python 2.7
Status: closed Resolution: duplicate
Dependencies: Superseder: [Linux] ctypes packs bitfields Incorrectly
View: 29753
Assigned To: Nosy List: Mike.Castle, mark.dickinson, meador.inge, terry.reedy
Priority: normal Keywords:

Created on 2012-07-26 07:24 by Mike.Castle, last changed 2022-04-11 14:57 by admin. This issue is now closed.

File name Uploaded Description Edit Mike.Castle, 2012-07-26 07:24
t.c Mike.Castle, 2012-07-26 07:25
Messages (4)
msg166462 - (view) Author: Mike Castle (Mike.Castle) Date: 2012-07-26 07:24
On debian/testing with python 2.7.3rc2 and gcc 4.7.1.

I was trying to use ctypeslib to wrap libdvdnav and running into some issues porting my test C code to Python, eventually tracking it down to this difference between how ctypes and gcc handles bitfields in packed structs (i.e., affects real world problems).

Basically, by default, gcc treats 8, 16 and 32 bit types in bit fields as 1 byte when using __attribute__ ((packed)) , while ctypes treats them each as 1, 2 and 4 bytes even when using _pack_ = 1.  (and libdvdnav using packed a lot)

Output from the attached programs:

$ ./a.out
$ ./

Removing the packed attribute or building with gcc -mms-bitfields both match the ctypes expectations (though -mms-bitfields is probably rarely used in Linux).
msg166463 - (view) Author: Mike Castle (Mike.Castle) Date: 2012-07-26 07:25
And the C version....
msg166621 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-07-28 00:00
If you have not, please check other ctypes bitfields issues, especially #15119 "ctypes mixed-types bitfield layout nonsensical; doesn't match compiler." to see if this is the same problem.
msg167078 - (view) Author: Mike Castle (Mike.Castle) Date: 2012-08-01 03:09
I did read through all of those before posting this one.

As far as I can remember, none of those addressed the use of pack, which is the sole problem I'm facing here.

Now maybe when all is said and done and all of these bitfield bugs are worked out, this one and that one will both be fixed.  But I wasn't sure if this example would clarify or cloud those issues.
Date User Action Args
2022-04-11 14:57:33adminsetgithub: 59658
2021-03-12 21:11:59eryksunsetstatus: open -> closed
superseder: [Linux] ctypes packs bitfields Incorrectly
resolution: duplicate
stage: resolved
2018-04-01 22:36:33martin.panterlinkissue22781 superseder
2012-08-01 03:09:32Mike.Castlesetmessages: + msg167078
2012-07-28 00:00:41terry.reedysetnosy: + terry.reedy

messages: + msg166621
versions: - Python 2.6
2012-07-26 07:42:18mark.dickinsonsetnosy: + mark.dickinson, meador.inge
2012-07-26 07:25:54Mike.Castlesetfiles: + t.c

messages: + msg166463
2012-07-26 07:24:57Mike.Castlecreate