How to pass a file opened in Python to a C function for writing?

196 views
Skip to first unread message

Joel Daniels

unread,
Feb 28, 2014, 1:25:01 PM2/28/14
to cython...@googlegroups.com
Hi there, first time posting here.  I'm kind of a noob with Cython, so bear with me.  :)

I've written a function in C to write to a file, but I need to pass it a file I've already opened in Python.  I assume this is possible, but I can't figure out what the syntax should look like, and Googling hasn't turned anything up.

Thanks in advance!

Jérôme Kieffer

unread,
Feb 28, 2014, 3:36:24 PM2/28/14
to cython...@googlegroups.com
In python2 that's pretty easy: there is a function for that:
http://docs.python.org/2/c-api/file.html
FILE* PyFile_AsFile(PyObject *p)

Return the file object associated with p as a FILE*.

If the caller will ever use the returned FILE* object while the GIL is released it must also call the PyFile_IncUseCount() and PyFile_DecUseCount() functions described below as appropriate.

This stops working with python3

Cheers,
--
Jérôme Kieffer <goo...@terre-adelie.org>

Joel Daniels

unread,
Feb 28, 2014, 3:39:24 PM2/28/14
to cython...@googlegroups.com
Ah, then I should have mentioned I have to use Python 3.3 :(  Dang, that's a shame.

Stefan Behnel

unread,
Feb 28, 2014, 3:45:33 PM2/28/14
to cython...@googlegroups.com
Jérôme Kieffer, 28.02.2014 21:36:
It's a bit more complex than that. In both Py2 and Py3, you can get at the
file descriptor (an integer value). However, you most like don't want to
use that because it doesn't take Python level buffering into account.

Even in Py2, you have to double check that reading from the underlying C
FILE* starts at exactly the same byte as reading from the file object
(seeking, buffering, ...). And this really only works for files opened
directly from the file system, not for arbitrary file-like objects
(BytesIO, urlopen, ...).

So, even in Py2, the value of this C-API is fairly limited, which is why it
was so easily dropped in Py3.

Stefan

Reply all
Reply to author
Forward
0 new messages