How to receive a FILE* from Python under MinGW?

John Pye john.pye at
Wed Mar 21 06:48:47 CET 2007

On Mar 21, 4:04 pm, Ross Ridge <rri... at>
> Gabriel Genellina <gagsl-... at> wrote:
> >You can get the file descriptor from the Python file object using its
> >fileno() method. The file descriptor lives at the OS level, so it's safe
> >to pass around.
> Not under Windows.  Windows doesn't have Unix-like descriptors, so the
> C runtime emulates them.

I am trying the following... any thoughts?

%typemap(in) FILE * {
    if (!PyFile_Check($input)) {
        PyErr_SetString(PyExc_TypeError, "Need a file!");
        return NULL;
%#ifdef __MINGW32__
	PyFileObject *fo = (PyFileObject *)$input;
	$1 = fdopen(_fileno(fo->f_fp),PyString_AsString(fo->f_mode));
    $1 = PyFile_AsFile($input);

More information about the Python-list mailing list