[issue12945] ctypes works incorrectly with _swappedbytes_ = 1

Meador Inge report at bugs.python.org
Sun Sep 11 04:59:22 CEST 2011


Meador Inge <meadori at gmail.com> added the comment:

Yes I can.  This seems strange, but it is correct.  The little endian case look like:

 Little endian
 ---------------------------------------
 | unsigned short   | unsigned short   |
 ---------------------------------------
 | bbbbaaaa ....cccc dddddddd ....dddd |
 ---------------------------------------
 | 00010010 00110100 01010110 01111000 |
 ---------------------------------------

where the 'd' bits pack from left to right, so '1000 01010110'.
The big endian case look like:

 Big endian
 ---------------------------------------
 | unsigned short   | unsigned short   |
 ---------------------------------------
 | aaaabbbb cccc.... dddddddd dddd.... |
 ---------------------------------------
 | 00010010 00110100 01010110 01111000 |
 ---------------------------------------

where the 'd' bits pack from right to left, so '01010110 0111'.

The native case (Structure) can typically be verified using your host C compiler.  For example, the above code can be represented in C as:

#include <stdio.h>

struct T
{
  unsigned char  a : 4;
  unsigned char  b : 4;
  unsigned short c : 4;
  unsigned short d : 12;
};

int main (int argc, char **argv)
{
  unsigned char bytes[] = {0x12, 0x34, 0x56, 0x78};
  struct T *t = (struct T*)&bytes;

  printf ("%X\n", t->a);
  printf ("%X\n", t->b);
  printf ("%X\n", t->c);
  printf ("%X\n", t->d);
}

With respect to structure layout, ctypes typically behaves the same way as the native compiler used to build the interpreter.

----------

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


More information about the Python-bugs-list mailing list