Get Cython Pointer Value

I am writing a function that builds an malloc'd unsigned char * array, and then reconfigures the pointer. In pure Keaton or C it is easy. All you have to do is set the return type to the function and return a pointer to the array. Done. However, I have reached the point where I need to return a pointer to an array created in Cython in Python. I know that a pointer is just a memory address. Is there a way that I can return a Cython pointer to Python as a python object (e.g. int or hex, since the memory address is essentially a number), so I can then basically control pointers in python?

I tried to return the pointer value as follows:

 cdef unsigned char array[8] def return_pointer(): return &array 

This, of course, does not work, because it is impossible to perform the conversion. Keaton complains about Cannot convert 'unsigned char (*)[8]' to Python object . Any suggestions?

EDIT:

I do not need to access the value in the memory address pointed to by the pointer in Python, but only pass the pointer. Then I plan to use a pointer to a Python object and call c functions with it as an argument.

+6
source share
1 answer

You can overlay the corresponding type C, which must then be converted to an integer in Python Cython. The correct type is C uintptr_t from <stddef.h> , which in Cython will be available through from libc.stdint cimport uintptr_t . The statement, of course, is written by <uintptr_t>array_or_pointer .

+12
source

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


All Articles