[docs] [issue12880] ctypes: clearly document how structure bit fields are allocated

Vlad Riscutia report at bugs.python.org
Tue Oct 4 17:21:46 CEST 2011


Vlad Riscutia <riscutiavlad at gmail.com> added the comment:

Thanks for the "make patchcheck" tip, I didn't know about that. I will update the patch soon.

In the mean time, I want to point out a couple of things:
First, I'm saying "toying with the underlying buffer" because none of the bugs are actual issues of the form "I created this bitfield structure with Python, passed it to C function but C structure was different". That would be a bitfield bug. All of these bugs are people setting raw memory to some bytes, then looking at bitfield members and not seeing what they expect. 

Since this is platform dependent, they shouldn't worry about the raw memory as long as C interop works fine. Bitfield layout is complex as it involves both allocation algorithm and structure packing and same Python code will work differently on Windows and Unix.

My point is that documenting all this low-level stuff will encourage people to work with the raw memory which will open the door for other issues. I believe it would be better to encourage users to stick to declaring members and accessing them by name as raw memory WILL be different for the same code on different OSes.

Second, one of your review comments is: "GCC is used for most Unix systems and Microsoft VC++ is used on Windows.". This is not how ctypes works. Ctypes implements the bitfield allocation algorithm itself, it doesn't use the compiler with which it is built. Basically it says #ifdef WIN32 - allocate like VC++ - #else - allocate like GCC. So it doesn't really matter with which compiler you are building Python. It will still do GCC style allocation on Solaris.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12880>
_______________________________________


More information about the docs mailing list