Python ctypes:Python文件对象<-> C文件* [英] Python ctypes: Python file object <-> C FILE *
问题描述
我正在使用ctypes用Python包装一个C库(我可以控制)。我想用声明包装一个C函数:
int fread_int(FILE * stream);
现在;我想在python中打开文件,然后使用python文件对象(以某种方式使用?)来访问底层的FILE *对象并将其传递给C函数:
#Python
fileH = open(file, r)
value = ctypes_function_fread_int(?????)
fileH.close()
Python文件<-> FILE *是否完全可能映射? / p>
Joakim
我遇到了同样的问题。
看看这个文件:
http://svn.python.org/projects/ctypes/trunk/ctypeslib/ctypeslib/contrib/pythonhdr.py
您可以从中使用PyFile_AsFile。
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
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.
这篇关于Python ctypes:Python文件对象<-> C文件*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!