[issue11920] ctypes: Strange bitfield structure sizing issue

Steve Thompson report at bugs.python.org
Mon Apr 25 23:21:58 CEST 2011


Steve Thompson <steve.f.thompson at 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 <report at bugs.python.org>wrote:

>
> Santoso Wijaya <santoso.wijaya at 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 <report at bugs.python.org>
> <http://bugs.python.org/issue11920>
> _______________________________________
>

----------
Added file: http://bugs.python.org/file21777/unnamed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11920>
_______________________________________
-------------- next part --------------
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>
<br><div class="gmail_quote">On Mon, Apr 25, 2011 at 2:49 PM, Santoso Wijaya <span dir="ltr">&lt;<a href="mailto:report at bugs.python.org">report at bugs.python.org</a>&gt;</span> wrote:<br><blockquote 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.wijaya at gmail.com">santoso.wijaya at gmail.com</a>&gt; 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_WIN32<br>
        /* MSVC, GCC with -mms-bitfields */<br>
        &amp;&amp; dict-&gt;size * 8 == *pfield_size<br>
#else<br>
        /* GCC */<br>
        &amp;&amp; dict-&gt;size * 8 &lt;= *pfield_size<br>
#endif<br>
        &amp;&amp; (*pbitofs + bitsize) &lt;= *pfield_size) {<br>
        /* continue bit field */<br>
        fieldtype = CONT_BITFIELD;<br>
#ifndef MS_WIN32<br>
    } 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_size<br>
        &amp;&amp; (*pbitofs + bitsize) &lt;= dict-&gt;size * 8) {<br>
        /* expand bit field */<br>
        fieldtype = EXPAND_BITFIELD;<br>
#endif<br>
<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 _struct1<br>
{<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>
<div><div></div><div class="h5"><br>
_______________________________________<br>
Python tracker &lt;<a href="mailto:report at bugs.python.org">report at bugs.python.org</a>&gt;<br>
&lt;<a href="http://bugs.python.org/issue11920" target="_blank">http://bugs.python.org/issue11920</a>&gt;<br>
_______________________________________<br>
</div></div></blockquote></div><br>


More information about the Python-bugs-list mailing list