[Pyrex] Mapping a stdio-FILE to a Python-file

Joachim Saul saul at gfz-potsdam.de
Mon Nov 17 17:54:44 CET 2003


Hi there,

I have been using Pyrex for a while, but have got stuck with a
probably rather simple problem: What is the best way to map a
stdio-FILE to a Python-file object in Pyrex?

I have a C function:

cdef extern int ms_read_record_raw(FILE *fp, char *buf, int nbuf, int *size)

which I want to call using a Python-file object, i.e. something like

class _MSeedPacket(object):
    ...

    def fromfile(self, file f):
        cdef char buf[4096]
        cdef int err, size
        err = ms_read_record_raw(f.f_fp, buf, 4096, &size)
    ...

The above additionally requires

cdef extern from "fileobject.h":
    ctypedef class __builtin__.file [object PyFileObject]:
        cdef FILE *f_fp

This way it works fine, but I find it rather "ugly". Ideally, I
would like Pyrex to directly map the file object to a FILE, since
both are standard types in their respective languages. I suspect
there is probably a much more "beautiful" way to achieve the
mapping than explicitly referring to the 'f_fp'. Did I perhaps
just overlook something really trivial?

Cheers,
Joachim




More information about the Pyrex mailing list