Reputation: 13
I am using Cython to write a Python API to a C project.
In the C project there is the following relevant code in the header file:
#ifndef EN_API_FLOAT_TYPE
#define EN_API_FLOAT_TYPE float
#endif
int DLLEXPORT ENgetlinkvalue(int index, int property, EN_API_FLOAT_TYPE *value);
In my .pyx file I have:
cdef extern from "epanet2.h":
int ENgetlinkvalue(int, int, float *)
def engetlinkvalue(int index, int proprty, float *value):
return ENgetlinkvalue(index, proprty, value)
When I run setup.py I get the following error:
src\cyepanet.pyx:48:43: Cannot convert Python object argument to type 'float *'
I have been banging my head against the wall trying to figure this one out (oh, and also searching SO, Cython documentation, a book on Cython, ChatGPT, and Copilot)
I am sure this is an easy one for someone more experienced in C coding. For me, 99% of my experience is in Python. Thanks in advance.
Upvotes: 1
Views: 279
Reputation: 30925
If (as you say in the comments) the float is being used as a return value, then you'd do something like
def engetlinkvalue(int index, int proprty):
cdef float value
errCode = ENgetlinkvalue(index, proprty, &value)
if errCode:
... # raise an exception
return value
That converts it to a nicer Python interface - you use return
to send back a value (as you would in Python) and you use Python exceptions to indicate a failure.
It's often a good idea in these cases to think about the Python interface you'd like rather than trying to match the C interface. (This is pretty much what @user2357112 also says in the comments)
Upvotes: 3