Buffers and pointers (Py3.4)

eryk sun eryksun at gmail.com
Mon Apr 17 17:49:15 EDT 2017

On Mon, Apr 17, 2017 at 5:58 PM, Rob Gaddi
<rgaddi at highlandtechnology.invalid> wrote:
> buffertype = c_uint8 * size
> return addressof(buffertype.from_buffer(buf, offset))
> works but is inefficient and woefully inelegant.

A lot of the cost there is in creating buffertype, but why do you need
that? You can use c_char.from_buffer if all you need is the base

    import ctypes

    def addressof_buffer(buf):
        return ctypes.addressof(ctypes.c_char.from_buffer(buf))

    >>> b = bytearray(b'spam')
    >>> addressof_buffer(b)
    >>> ctypes.string_at(2619159025048, 5)

More information about the Python-list mailing list