开发者

wrapping library functions with multiple outputs in cython

开发者 https://www.devze.com 2023-02-17 22:24 出处:网络
I\'m trying to wrap a dummy C libray using Cython. Here is the .h file of the library... void say_hello(char *name);

I'm trying to wrap a dummy C libray using Cython.

Here is the .h file of the library...

void say_hello(char *name);
double multiply(double x, double y);
int divide(double x, double y, double *answer);

I have no problem wrapping the first 2 functions with cytho开发者_StackOverflow社区n but am having trouble wrapping the 3rd.

The third function divides x by y and returns the answer in the third pointer to a double argument. The function itself returns either success or fail.

The actual library that I'd like to wrap is full of these kinds of situations where it returns a status code and the actual output of the functions are returned via pointers.

What is the proper way to wrap such functions?

Thanks, ~Eric


you have to allocate answer on the stack. Cython will take care of the rest.

def divide(x, y):
  cdef double answer
  cdef int res
  res = c_divide( x , y, &answer) 
  if res != 0:
      throw ValueError("c_divide_error")
  return answer
0

精彩评论

暂无评论...
验证码 换一张
取 消