Python 3 replacement for PyFile_AsFile

Question:

The following code works in 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))

But in Python 3, there is no PyFile_AsFile in pythonapi.

The code is an except from testPixelOps.py.

Asked By: Janus Troelsen

||

Answers:

I didn’t find a real answer to the problem, but I found out that if you don’t need to convert the Python file object to a FILE* (i.e., you don’t need to “share” the opened file), you can just use ctypes to call fopen from libc and get the FILE* like that.

Answered By: Janus Troelsen

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

You are out of luck. That was possible in Python 2.x, but is not possible in Python 3.x. The documentation explains why not:

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.

If you want a FILE* you are going to have to make one yourself, using the C standard library directly.

Answered By: David Heffernan

py3c_PyFile_AsFileWithMode can be referred.The PyFile API Documentation states it as

a quick-and-dirty replacement for PyFile_AsFile()

Answered By: Prakhar
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.