This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author karlding
Recipients Charles Machalow, amaury.forgeotdarc, belopolsky, eryksun, karlding, meador.inge, mleroy003, ztane
Date 2019-05-30.20:01:37
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1559246497.54.0.37296200927.issue29753@roundup.psfhosted.org>
In-reply-to
Content
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
History
Date User Action Args
2019-05-30 20:01:37karldingsetrecipients: + karlding, amaury.forgeotdarc, belopolsky, meador.inge, eryksun, ztane, Charles Machalow, mleroy003
2019-05-30 20:01:37karldingsetmessageid: <1559246497.54.0.37296200927.issue29753@roundup.psfhosted.org>
2019-05-30 20:01:37karldinglinkissue29753 messages
2019-05-30 20:01:37karldingcreate