user422005 user422005 - 23 days ago 6
Python Question

Python ctypes: Python file object <-> C FILE *

I am using ctypes to wrap a C-library (which I have control over) with Python. I want to wrap a C-function with declaration:

int fread_int( FILE * stream );


Now; I would like to open file in python, and then use the Python file-object (in some way??) to get access to the underlying FILE * object and pass that to the C-function:

# Python
fileH = open( file , "r")
value = ctypes_function_fread_int( ????? )
fileH.close()


Is the Python file <-> FILE * mapping at all possible?

Joakim

Answer

I've encountered the same problem.

Take a look at this file:

http://svn.python.org/projects/ctypes/trunk/ctypeslib/ctypeslib/contrib/pythonhdr.py

You can use PyFile_AsFile from it.