0

I have a C function in write.h:

int write(int fd, void* arr, int size);

I have written write.pxd file to define it in Cython:

cdef extern from "write.h":
  int write(int fd, void* arr, int size)

Now, how do I write the write.pyx for its implementation. I tried this:

def write(int fd, void* buf, int size):
  return write(fd, buf, size)

But it complains that:

Cannot convert Python object argument to type 'void *'

How should I define so that I can send a list from Python and C gets the starting address of that list. For example, I want to call:

my_list = [1, 2, 3]
write(fd, my_list, 3 * 4)

as sizeof(int) is 4.

soham
  • 1,508
  • 6
  • 30
  • 47
  • 1
    The Python list is not an array of C ints. What you try to do cannot be done. But see https://cython.readthedocs.io/en/latest/src/tutorial/array.html – Antti Haapala -- Слава Україні Jan 05 '21 at 13:37
  • The duplicate I suggested isn't quite exact (given @AnttiHaapala's comment) but it is how you solve this problem. Use a proper array (numpy or `array.array`) rather than a Python list – DavidW Jan 05 '21 at 13:38

0 Answers0