Keith Hughitt, 11.05.2012 12:56:
> I'm using Cython 0.15.1. There is no error message indicating that the
> operation isn't working, but when I attempt to pass the array into a
> function that expects the unsigned char* array, it doesn't work.
That's what I meant when I wrote that "it's best to provide this
information". How does "it doesn't work" look exactly?
> The reason I suspect that it has to do with how I'm creating the array is
> that the function works (sometimes...) when I create the array using
> c-style syntax, e.g.:
>
> from libc.stdlib cimport calloc, malloc, free
>
> cdef unsigned char *data = NULL
> cdef int file_length
>
> cdef unsigned char* read_file(char* filename, int* file_length):
> cdef int length
> cdef fsrc = fopen(filename, "rb")
>
> # Determine length of file
> fseek(fsrc, 0, SEEK_END)
> length = ftell(fsrc)
> fseek(fsrc, 0, SEEK_SET)
Hmm, that seems a rather uncommon way to determine the file length. Why not
ask the file system?
> *file_length = &length
I'm sure you want to assign the value here, not the address.
> # Set aside memory
> data = <unsigned char *> malloc(length)
... which may return NULL ...
> # Read file in
> fread(data, 1, length, fsrc)
>
> fclose(fsrc)
>
> return data
>
> data = read_file('test.jpg', &file_length)
Let me express a very educated guess about the code that you did not show
us yet. It does not care about the length of the Python bytes object, does
it? And since you're dealing with binary data, it stops processing at the
first null byte...
Did you take a look at the string processing tutorial?
http://docs.cython.org/src/tutorial/strings.html
Stefan