How to convert ctypes pointer to ctypes array without copying data. (Without pointing to a pointer)

I have a python callback function that takes 2 arguments: a pointer to a piece of memory and the length of a memory block.

def my_callback( buf, len):
    """ buf is of type POINTER(c_ubyte), len is of type c_uint"""

Inside the callback function, I would like to first convert a piece of memory into a ctypes array. I know that this can be done by creating a new ctypes array and copying all the data from the memory block.

def my_callback( buf, len):
    """ buf is of type POINTER(c_ubyte), len is of type c_uint"""
    temp_array = (c_ubyte * len)(*[buf[n] for n in range(len)])

But thus a copy of the memory is needed. My question is, is there any way to just point the ctypes pointer to an array without copying the memory?

Thanks in advance for any answers or tips.

+4
source share

No one has answered this question yet.

Source: https://habr.com/ru/post/1607958/


All Articles