Karl Ding <karl.jw.d...@gmail.com> added the comment:

I believe the example can be simplified to the following:

        class MyStructure(ctypes.Structure):
            _pack_ = 1
            _fields_ = [('A', ctypes.c_uint32, 8)]

        assert ctypes.sizeof(MyStructure) == 1

Here, ctypes.sizeof returns 4 on my machine (64-bit Ubuntu 18.04 LTS), while I 
would expect it to return 1 like GCC. This is using a tree checked out at 
33ce3f012f249782507df896824b045b34f765aa, if it makes a difference.

If we compile the equivalent C code (on 64-bit Ubuntu 18.04 LTS):

        #include <stdio.h>
        #include <stdint.h>
        #include <stdalign.h>

        struct my_structure_uint32 {
          uint32_t a : 8;
        } __attribute__((packed));

        int main(int argc, char *argv[]) {
          printf("sizeof(struct my_structure_uint32): %d\n", sizeof(struct 
my_structure_uint32));
          printf("alignof(struct my_structure_uint32): %d\n", alignof(struct 
my_structure_uint32));
          return 0;
        }

Using the following GCC invocation:

        gcc temp.c -o test && ./test

We get the following result:

        sizeof(struct my_structure_uint32): 1
        alignof(struct my_structure_uint32): 1

However, if I compile with MSVC bitfields:

        gcc -mms-bitfields test.c -o test && ./test

We get the following result:

        sizeof(struct my_structure_uint32): 4
        alignof(struct my_structure_uint32): 1

Similarly, adding a second and third 8-bit wide bitfield member fails and 
returns 4, instead of the expected 2 and 3.

This is not just c_uint32, c_uint16 and c_int also exhibit the same behaviour:

        class MyStructure(ctypes.Structure):
            _pack_ = 1
            _fields_ = [('A', ctypes.c_uint16, 8)]

        assert ctypes.sizeof(MyStructure) == 1

----------
nosy: +karlding

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue29753>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to