gpt4 book ai didi

PyFile_AsFile 的 Python 3 替代品

转载 作者:太空狗 更新时间:2023-10-29 21:26:56 26 4
gpt4 key购买 nike

以下代码适用于 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))

但是在 Python 3 中,pythonapi 中没有 PyFile_AsFile。

代码是testPixelOps.py 中的一个异常(exception).

最佳答案

I just needed a way to convert a file object to a ctypes FILE* so that I can pass it to GD.

你运气不好。这在 Python 2.x 中是可能的,但在 Python 3.x 中是不可能的。 documentation解释为什么不:

These APIs are a minimal emulation of the Python 2 C API for built-in file objects, which used to rely on the buffered I/O (FILE*) support from the C standard library. In Python 3, files and streams use the new io module, which defines several layers over the low-level unbuffered I/O of the operating system.

如果您想要一个 FILE*,您将不得不自己制作一个,直接使用 C 标准库。

关于PyFile_AsFile 的 Python 3 替代品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16130268/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com