Take the following simple structure:
class Foo(ctypes.Structure):
_fields_ = [('f', ctypes.uint32_t)]
And construct some arrays with it:
def get_array_view(N):
return memoryview((Foo * N)())
In most cases, this works as expected, returning the size of one item:
>>> get_array_view(10).itemsize
4
>>> get_array_view(1).itemsize
4
But when N=0, it returns the wrong result
>>> get_array_view(0).itemsize
0
Which contradicts its `.format`, which still describes a 4-byte struct
>>> get_array_view(0).format
'T{>I:one:}'
This causes a downstream problem in numpy:
>>> np.array(get_array_view(0))
RuntimeWarning: Item size computed from the PEP 3118 buffer format string does not match the actual item size. |