日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python ctypes教程_Python ctypes: Python file object - C FILE * | 易学教程

發(fā)布時(shí)間:2025/4/16 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python ctypes教程_Python ctypes: Python file object - C FILE * | 易学教程 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

可以將文章內(nèi)容翻譯成中文,廣告屏蔽插件可能會(huì)導(dǎo)致該功能失效(如失效,請(qǐng)關(guān)閉廣告屏蔽插件后再試):

問(wèn)題:

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

回答1:

I've encountered the same problem.

Take a look at this file:

You can use PyFile_AsFile from it.

回答2:

A Python file object does not necessarily have an underlying C-level FILE * -- at least, not unless you're willing to tie your code to extremely specific Python versions and platforms.

What I would recommend instead is using the Python file object's fileno to get a file descriptor, then use ctypes to call the C runtime library's fdopen to make a FILE * out of that. This is a very portable approach (and you can go the other way, too). The big issue is that buffering will be separate for the two objects opened on the same file descriptor (the Python file object, and the C FILE *), so be sure to flush said objects as often as needed (or, open both as unbuffered, if that's more convenient and compatible with your intended use).

回答3:

Well;

I tried the fileno based solution, but was quite uncomfortable with opening the file twice; It was also not clear to me how to avoid the return value from fdopen() to leak.

In the end I wrote a microscopic C-extension: static PyObject cfile(PyObject * self, PyObject * args) { PyObject * pyfile; if (PyArg_ParseTuple( 'O' , &pyfile)) { FILE * cfile = PyFile_AsFile( pyfile ); return Py_BuildValue( "l" , cfile ); else return Py_BuildValue( "" ); }

which uses PyFile_AsFile and subsequently returns the FILE * pointer as a pure pointer value to Python which passes this back to C function expecting FILE * input. It works at least.

Joakim

回答4:

If you want to use stdout/stdin/stderr, you can import those variables from the standard C library. libc = ctypes.cdll.LoadLibrary('libc.so.6') cstdout = ctypes.c_void_p.in_dll(libc, 'stdout')

Or, if you want to avoid using void* for some reason: class FILE(ctypes.Structure): pass FILE_p = ctypes.POINTER(FILE) libc = ctypes.cdll.LoadLibrary('libc.so.6') cstdout = FILE_p.in_dll(libc, 'stdout')

回答5:

Adapted from svplayer import sys from ctypes import POINTER, Structure, py_object, pythonapi class File(Structure): pass if sys.version_info[0] > 2: convert_file = pythonapi.PyObject_AsFileDescriptor convert_file.restype = c_int else: convert_file = pythonapi.PyFile_AsFile convert_file.restype = POINTER(File) convert_file.argtypes = [py_object] fp = open('path').fp c_file = convert_file(fp)

回答6:

Tried this: #if PY_MAJOR_VERSION >= 3 if (PyObject_HasAttrString(pyfile, "fileno")) { int fd = (int)PyLong_AsLong(PyObject_CallMethod(pyfile, "fileno", NULL)); if (PyObject_HasAttrString(pyfile, "mode")) { char *mode = PyUnicode_AsUTF8AndSize( PyObject_CallMethod(pyfile, "mode", NULL), NULL); fp = fdopen(fd, mode); } else { return PyErr_Format(PyExc_ValueError, "File doesn’t have mode attribute!"); } } else { return PyErr_Format(PyExc_ValueError, "File doesn’t have fileno method!"); } #else fp = PyFile_AsFile(pyfile); #endif

It looks like it might be working.

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的python ctypes教程_Python ctypes: Python file object - C FILE * | 易学教程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。