memoryview(ctypes.Structure) does not produce the correct format string

Create issue
Issue #2930 resolved
mattip created an issue

The following passes on CPython, fails on PyPy (mv.format is B)

class Struct(Structure):
    _fields_ = [('a', c_int16)]

Struct3 = 3 * Struct

c_array = (2 * Struct3)(
    Struct3(Struct(a=1), Struct(a=2), Struct(a=3)),
    Struct3(Struct(a=4), Struct(a=5), Struct(a=6))
)

mv = memoryview(c_array)
assert mv.format == 'T{<h:a:}'

The memoryview call goes to _CData.__buffer__ in lib_pypy._ctypes.basics.py, which should not just call buffer(self._buffer)

Comments (5)

  1. Eric Wieser

    Watch out for the fact that it also produces incorrect format strings on CPython, just in different ways. The case you give above is correct though.

  2. mattip reporter

    In python3 there is memoyobject.cast. We should add it to python2 memoryview (W_MemoryView.descr_cast), and use it from app-level to fixup mv

  3. mattip reporter

    Change the plan since cpython3 does not support casting to complex format. So instead we will have a __pypy__.newmemoryview(buffer, format, *, shape, strides) approximately - a mix of the constructor API and cast API

  4. Log in to comment