[issue11920] ctypes: Strange bitfield structure sizing issue
Meador Inge mead...@gmail.com added the comment: Hi Steve, There is currently no way to deal with this situation. Vlad has opened issue12528 with a proposal to make the allocation strategy configurable from the 'ctypes' API. Please follow that issue if you are still interested. I am closing this issue. -- nosy: +meadori resolution: - duplicate stage: - committed/rejected status: open - closed superseder: - Implement configurable bitfield allocation strategy ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue11920 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue11920] ctypes: Strange bitfield structure sizing issue
Vlad Riscutia riscutiav...@gmail.com added the comment: Opened http://bugs.python.org/issue12528 to address this. -- nosy: +vladris ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue11920 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue11920] ctypes: Strange bitfield structure sizing issue
New submission from Steve Thompson steve.f.thomp...@gmail.com: Consider the following: import ctypes class struct1( ctypes.Structure ): _pack_ = 1 _fields_ = [ ( first, ctypes.c_uint8, 1 ), ( second, ctypes.c_uint8, 1 ), ( third, ctypes.c_uint8, 1 ), ( fourth, ctypes.c_uint8, 1 ), ( fifth, ctypes.c_uint8, 1 ), ( pad,ctypes.c_uint16, 11 ), ] s1 = struct1() print ctypes.sizeof( s1 ) class struct2( ctypes.Structure ): _pack_ = 1 _fields_ = [ ( first, ctypes.c_uint16, 1 ), ( second, ctypes.c_uint16, 1 ), ( third, ctypes.c_uint16, 1 ), ( fourth, ctypes.c_uint16, 1 ), ( fifth, ctypes.c_uint16, 1 ), ( pad,ctypes.c_uint16, 11 ), ] s2 = struct2() print ctypes.sizeof( s2 ) The output is: 3 2 I'm generating python code from real c code. The compiler I'm using for the real c code packs both of these structures into two bytes. I need a way to make the first example work in python like the compiler without having to modify the source code. Is this possible? -- components: ctypes messages: 134393 nosy: Steve.Thompson priority: normal severity: normal status: open title: ctypes: Strange bitfield structure sizing issue versions: Python 2.6 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue11920 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue11920] ctypes: Strange bitfield structure sizing issue
Changes by Santoso Wijaya santoso.wij...@gmail.com: -- nosy: +santa4nt type: - behavior versions: +Python 2.7, Python 3.1, Python 3.2, Python 3.3 -Python 2.6 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue11920 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue11920] ctypes: Strange bitfield structure sizing issue
Amaury Forgeot d'Arc amaur...@gmail.com added the comment: The output is: 3 2 on windows. It is 2 2 on the linux 64bit I tried. This is consistent with what the usual compilers do on these platforms: MSVC on windows, and gcc on linux. The specification of the C language specifies that If an adjacent bitfield will not fit into the remainder of the unit, the implementation defines whether bitfields are allowed to span units or whether another unit is allocated for the second bitfield Are you using gcc on windows? In this case I suggest to always use the largest type to declare the bitfields. -- nosy: +amaury.forgeotdarc Added file: http://bugs.python.org/file21776/bitfields.c ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue11920 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue11920] ctypes: Strange bitfield structure sizing issue
Santoso Wijaya santoso.wij...@gmail.com added the comment: What compilers were used to build your Python distro and the native structure? I found out in _ctypes/cfield.c (lns. 76-95): if (bitsize /* this is a bitfield request */ *pfield_size /* we have a bitfield open */ #ifdef MS_WIN32 /* MSVC, GCC with -mms-bitfields */ dict-size * 8 == *pfield_size #else /* GCC */ dict-size * 8 = *pfield_size #endif (*pbitofs + bitsize) = *pfield_size) { /* continue bit field */ fieldtype = CONT_BITFIELD; #ifndef MS_WIN32 } else if (bitsize /* this is a bitfield request */ *pfield_size /* we have a bitfield open */ dict-size * 8 = *pfield_size (*pbitofs + bitsize) = dict-size * 8) { /* expand bit field */ fieldtype = EXPAND_BITFIELD; #endif So the allocation of the extra byte for the structure seems to depend on Python's compiler. To make sure, I compiled a native structure using MSVC: #pragma pack(1) typedef struct _struct1 { UINT8 first : 1; UINT8 second : 1; UINT8 third : 1; UINT8 fourth : 1; UINT8 fifth : 1; UINT16 pad: 11; } struct1; And I got the same value (sizeof == 3). -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue11920 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue11920] ctypes: Strange bitfield structure sizing issue
Steve Thompson steve.f.thomp...@gmail.com added the comment: So, knowing there's a potential cross platform inconsistency here, is there a proposed way to deal with this that doesn't involve modifying the real c code I'm interfacing with? That's not always an option. On Mon, Apr 25, 2011 at 2:49 PM, Santoso Wijaya rep...@bugs.python.orgwrote: Santoso Wijaya santoso.wij...@gmail.com added the comment: What compilers were used to build your Python distro and the native structure? I found out in _ctypes/cfield.c (lns. 76-95): if (bitsize /* this is a bitfield request */ *pfield_size /* we have a bitfield open */ #ifdef MS_WIN32 /* MSVC, GCC with -mms-bitfields */ dict-size * 8 == *pfield_size #else /* GCC */ dict-size * 8 = *pfield_size #endif (*pbitofs + bitsize) = *pfield_size) { /* continue bit field */ fieldtype = CONT_BITFIELD; #ifndef MS_WIN32 } else if (bitsize /* this is a bitfield request */ *pfield_size /* we have a bitfield open */ dict-size * 8 = *pfield_size (*pbitofs + bitsize) = dict-size * 8) { /* expand bit field */ fieldtype = EXPAND_BITFIELD; #endif So the allocation of the extra byte for the structure seems to depend on Python's compiler. To make sure, I compiled a native structure using MSVC: #pragma pack(1) typedef struct _struct1 { UINT8 first : 1; UINT8 second : 1; UINT8 third : 1; UINT8 fourth : 1; UINT8 fifth : 1; UINT16 pad: 11; } struct1; And I got the same value (sizeof == 3). -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue11920 ___ -- Added file: http://bugs.python.org/file21777/unnamed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue11920 ___So, knowing there#39;s a potential cross platform inconsistency here, is there a proposed way to deal with this that doesn#39;t involve modifying the real c code I#39;m interfacing with?  That#39;s not always an option.br brdiv class=gmail_quoteOn Mon, Apr 25, 2011 at 2:49 PM, Santoso Wijaya span dir=ltrlt;a href=mailto:rep...@bugs.python.org;rep...@bugs.python.org/agt;/span wrote:brblockquote class=gmail_quote style=margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex; br Santoso Wijaya lt;a href=mailto:santoso.wij...@gmail.com;santoso.wij...@gmail.com/agt; added the comment:br br What compilers were used to build your Python distro and the native structure?br br I found out in _ctypes/cfield.c (lns. 76-95):br br   if (bitsize /* this is a bitfield request */br     amp;amp; *pfield_size /* we have a bitfield open */br #ifdef MS_WIN32br     /* MSVC, GCC with -mms-bitfields */br     amp;amp; dict-gt;size * 8 == *pfield_sizebr #elsebr     /* GCC */br     amp;amp; dict-gt;size * 8 lt;= *pfield_sizebr #endifbr     amp;amp; (*pbitofs + bitsize) lt;= *pfield_size) {br     /* continue bit field */br     fieldtype = CONT_BITFIELD;br #ifndef MS_WIN32br   } else if (bitsize /* this is a bitfield request */br     amp;amp; *pfield_size /* we have a bitfield open */br     amp;amp; dict-gt;size * 8 gt;= *pfield_sizebr     amp;amp; (*pbitofs + bitsize) lt;= dict-gt;size * 8) {br     /* expand bit field */br     fieldtype = EXPAND_BITFIELD;br #endifbr br So the allocation of the extra byte for the structure seems to depend on Python#39;s compiler. To make sure, I compiled a native structure using MSVC:br br #pragma pack(1)br typedef struct _struct1br {br   UINT8 first  : 1;br   UINT8 second  : 1;br   UINT8 third  : 1;br   UINT8 fourth  : 1;br   UINT8 fifth  : 1;br   UINT16 pad   : 11;br } struct1;br br And I got the same value (sizeof == 3).br br --br divdiv/divdiv class=h5br ___br Python tracker lt;a href=mailto:rep...@bugs.python.org;rep...@bugs.python.org/agt;br lt;a href=http://bugs.python.org/issue11920; target=_blankhttp://bugs.python.org/issue11920/agt;br ___br /div/div/blockquote/divbr ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com