New submission from Eric Wieser <wieser.eric+py...@gmail.com>:

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.

----------
components: ctypes
messages: 311740
nosy: Eric.Wieser
priority: normal
severity: normal
status: open
title: ctypes: memoryview gives incorrect PEP3118 itemsize for array of length 
zero
type: behavior
versions: Python 3.6

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

Reply via email to