Note that there are some explanatory texts on larger screens.

plurals
  1. POCython - converting pointers to arrays into Python objects
    primarykey
    data
    text
    <p>Alright, I am so close to finishing this I can taste it. Over the past few week or so, I've been attempting to create a Python extension to interface with a library written in C++ via Cython. With a little help from the guys here and a couple of friends, I have managed to get what feels like 98% of the way there. Only thing remaining is this: I can't for the life of me figure out how to turn a pointer to an array of unsigned shorts into a python object (Preferably a list).</p> <p>A little background, I am trying to interface with a part of the library that sets a callback function, which I have successfully done with this:</p> <pre><code>global callbackfunc ctypedef unsigned short const_ushort "const uint16_t" ctypedef void (*Function1)(const_ushort *data, unsigned width, unsigned height) cdef extern from "lib.hpp": void SetCallback(Function1) cdef void cSetCallback(Function1 function): SetCallback(function) cdef void callcallback(const_ushort *data, unsigned width, unsigned height): global callbackfunc callbackfunc(data,width,height) cSetCallback(callcallback) def PySetCallback(callbackFunc): global callbackfunc callbackfunc = callbackFunc </code></pre> <p>The problem occurs within the function "callcallback", where I get the error: "Cannot convert 'const_ushort *' to Python object". My first attempt around this was to create a new python list, and loop through to get each element of the array into a python list, like this:</p> <pre><code>datalist = [] for i in range(width*height): datalist += data[i] </code></pre> <p>Which, sadly, nets me with the compiled cython code trying to define a type as a "const const unsigned short", which is obviously a problem.</p> <p>Then I tried this:</p> <pre><code>datalist = [] for i in data: datalist += i </code></pre> <p>Which gives me "C array iteration requires known end index". Note that I know very little C/C++, so most of this doesn't make much sense to me.</p> <p>So, anyways, is there any effective way of translating a pointer like that into a python object (Preferably faster than looping through the array, since it's usually about 57344 items, and this is quite time sensitive)</p> <p><strong>Edit</strong>: A little more clarification, as I mentioned, I'm working with callbacks, and the C++ function within the library that calls this sends a pointer to an array of "const uint_16"s, which is why I defined const_ushort that way, because otherwise the types don't unify. I cannot modify the library in any way.</p> <p><strong>Edit2</strong>: Looks like I got it. What I ended up having to do was explicitly cast the array as an array of unsigned shorts rather than an array of const unsigned shorts to I could index them with a non constant. To achieve this, I created another C++ function like this (Someone else wrote it for me, I barely know C++):</p> <pre><code>unsigned short *convert_short(const unsigned short *test){ return const_cast&lt;unsigned short *&gt;(test); } </code></pre> <p>and that allowed me to create the "<strong>getindex</strong>" function within my class and return the correct values based on the function. So yeah, Python seems to be reading the arrays correctly and whatnot, so this case seems closed. Thanks a lot.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload