以下代码可以在Python 2中运行:
from ctypes import *
## Setup python file -> c 'FILE *' conversion :
class FILE(Structure):
pass
FILE_P = POINTER(FILE)
PyFile_AsFile = pythonapi.PyFile_AsFile # problem here
PyFile_AsFile.argtypes = [py_object]
PyFile_AsFile.restype = FILE_P
fp = open(filename,'wb')
gd.gdImagePng(img, PyFile_AsFile(fp))但是在Python3中,pythonapi中没有PyFile_AsFile。
代码是来自testPixelOps.py的except。
发布于 2013-04-21 20:51:12
我没有找到这个问题的真正答案,但我发现如果不需要将Python file对象转换为FILE* (即,您不需要“共享”打开的文件),则只需使用ctypes从libc调用fopen并获得类似的FILE*即可。
https://stackoverflow.com/questions/16130268
复制相似问题