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 Eric.Wieser
Recipients Eric.Wieser
Date 2018-02-06.05:27:59
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1517894879.39.0.467229070634.issue32780@psf.upfronthosting.co.za>
In-reply-to
Content
Discovered [here](https://github.com/numpy/numpy/issues/10528)

Consider the following structure, and a memoryview created from it:

    class foo(ctypes.Structure):
        _fields_ = [('one', ctypes.c_uint8),
                    ('two', ctypes.c_uint32)]
    f = foo()
    mf = memoryview(f)

We'd expect this to insert padding, and it does:

>>> mf.itemsize
8

But that padding doesn't show up in the format string:

>>> mf.format
'T{<B:one:<I:two:}'

That format string describes the _packed_ version of the struct, with the `two` field starting at offset 1, based on the `struct` documentation on how `<` should be interpreted:

> No padding is added when using non-native size and alignment, e.g. with ‘<’, ‘>’

But ctypes doesn't even get it right for packed structs:


    class foop(ctypes.Structure):
        _fields_ = [('one', ctypes.c_uint8),
                    ('two', ctypes.c_uint32)]
        _pack_ = 1
    f = foo()
    mf = memoryview(f)

The size is what we'd expect:

>>> mf.itemsize
5

But the format is garbage:

>>> mf.format
'B'  # sizeof(byte) == 5!?
History
Date User Action Args
2018-02-06 05:27:59Eric.Wiesersetrecipients: + Eric.Wieser
2018-02-06 05:27:59Eric.Wiesersetmessageid: <1517894879.39.0.467229070634.issue32780@psf.upfronthosting.co.za>
2018-02-06 05:27:59Eric.Wieserlinkissue32780 messages
2018-02-06 05:27:59Eric.Wiesercreate