FILE object in Python3.0 extension modules

"Martin v. Löwis" martin at v.loewis.de
Wed Jun 3 08:55:42 CEST 2009


> The purpose is to dump the contents of a Python extension type to disk
> as binary data using C's fwrite() function.

This isn't really possible anymore - the Python IO library has stopped
using stdio. There are a couple of alternatives:

1. don't use fwrite(3) to write the binary data, but instead use
   PyObject_CallMethod to call .write on the file object.
2. don't use fwrite(3), but write(2). To do so, fetch the file
   descriptor from the Python file object, and use that. Make sure
   you flush the stream before writing to it, or else you may get
   the data in the wrong order.
3. use fdopen to obtain a FILE*; the comments for 2) apply.
   In addition, make sure to flush and discard the FILE* before
   letting Python continue to write to the file.

Regards,
Martin



More information about the Python-list mailing list