[New-bugs-announce] [issue15119] Bug in ctypes bitfield layout?

Mark Dickinson report at bugs.python.org
Thu Jun 21 11:13:35 CEST 2012


New submission from Mark Dickinson <dickinsm at gmail.com>:

It looks as though there's a bug in the ctypes bitfield layout algorithm.  After:

>>> from ctypes import Structure, c_int, c_short
>>> class BITS(Structure):
...     _fields_ = [("A", c_int, 17), ("M", c_short, 1)]
... 

I get:

>>> BITS.M
<Field type=c_short, ofs=2:17, bits=1>

which doesn't make a lot of sense (17th bit of a short?)  This causes a negative shift operation when trying to access the .M field of an instance of this structure (see issue 9530 and in particular msg163303).

On this machine (OS X 10.6, 64-bit build of Python using the system gcc (4.2) with no special compiler flags), the corresponding struct in a simple C test program has size 4:

    #include <stdio.h>

    struct {
      int A : 17;
      short B: 1;
    } flags;

    int main(void) {
      printf("sizeof flags is: %ld\n", sizeof(flags));
      return 0;
    }

So it looks like everything gets packed into that first int.  At a guess, BITS.M should therefore look like <Field type=c_int, ofs=0:17, bits=1> instead.



System info:

Python 3.3.0a4+ (default:2035c5ad4239+, Jun 21 2012, 08:30:36) 
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

----------
components: ctypes
messages: 163315
nosy: mark.dickinson, meador.inge
priority: normal
severity: normal
status: open
title: Bug in ctypes bitfield layout?
type: behavior
versions: Python 3.3

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


More information about the New-bugs-announce mailing list