Python ctypes:Python文件对象<-> C文件* [英] Python ctypes: Python file object <-> C FILE *

查看:119
本文介绍了Python ctypes:Python文件对象<-> C文件*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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文件对象&lt;-&gt; C文件*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆