Passing FILE * types using ctypes

Francesco Bochicchio bieffe62 at gmail.com
Thu Mar 4 08:45:40 EST 2010


On Mar 4, 12:50 am, Zeeshan Quireshi <zeeshan.quire... at gmail.com>
wrote:
> Hello, I'm using ctypes to wrap a library i wrote. I am trying to pass
> it a FILE *pointer, how do i open a file in Python and convert it to a
> FILE *pointer. Or do i have to call the C library using ctypes first,
> get the pointer and then pass it to my function.
>
> Also, is there any automated way to convert c struct and enum
> definitions to ctypes data types.
>
> Zeeshan

Python file objects have a method fileno() whic returns the 'C file
descriptor', i.e. the number used by low level IO in python as well as
in C.
I would use this as interface between python and C and then in the C
function using fdopen to get a FILE * for an already open file for
which you have a file descriptor.

If you don't want change the C interface, you could try using fdopen
in python by loading the standard C library ang using ctypes
to call the function. (I tried briefly but always get 0 from fdopen ).

But if you can change the C code, why not to pass the file name? The
idea of opening the file in python and manage it in C feels a bit
icky ...

Ciao
----
FB




More information about the Python-list mailing list