3

I am writing a Cython wrapper around a C library we are maintaining. I am getting the following error message:

analog.pyx:6:66: Cannot convert 'unsigned short (*)' to Python object

Here's the code I am trying to write:

cimport company as lib

def get_value(device, channel, index):
    cdef unsigned short aValue

    err = library_get_data_val(device, channel, index, &aValue) # line 6

    # Ignore the err return value for StackOverflow.

    return aValue

The prototype of the C function I am trying to use is:

unsigned long library_get_data_val(unsigned long device, int channel,
    int index, unsigned short *pValue);

The library function returns the requested value in the aValue parameter. It's just an unsigned short primitive. What's the expected way of returning primitives (i.e. not struct) from these type of functions? I am new to Cython so the answer may be quite simple but I didn't see anything obvious through Google.

Stephen Rasku
  • 2,554
  • 7
  • 29
  • 51

2 Answers2

0

I think the problem is that you haven't defined library_get_data_val properly, so Cython thinks it's a Python type function you're calling, and doesn't know what to do with the pointer to aValue

Try:

cdef extern from "header_containing_library_get_data_val.h":
    # I've taken a guess at the signature of library_get_data_val
    # Update it to match reality
    int library_get_data_val(int device, int channel, int index, int* value)

That way Cython knows it's a C-function that expects a pointer, and will be happy.

(Edited to be significantly changed from my original answer, where I misunderstood the problem!)

DavidW
  • 29,336
  • 6
  • 55
  • 86
0

I found out what my problem was. You can probably tell what it is now that I've edited the question. There's a company.pxd file that's cimported by the .pyx file. Once I copied the C prototype into company.pxd it worked.

I also needed to use the lib prefix in my call:

err = lib.library_get_data_val(device, channel, index, &aValue) # line 6
Stephen Rasku
  • 2,554
  • 7
  • 29
  • 51