Re: PROPOSAL: A Generic Python Object Interface for Python C Modules

Guido.van.Rossum@cwi.nl
Thu, 30 Mar 1995 09:48:29 +0200

> > int PyObject_Print(PyObject *o, FILE *fp, int flags)
> >
> > Print an object, o, on file, fp. Returns -1 on
> > error. The flags argument is used to enable certain printing
> > options. The only option currently supported is Py_Print_RAW.
> >
> > (What should be said about Py_Print_RAW?)
>
> It seems to me that 'fp' should be a PyObject* instead of a FILE*, pointing
> to an object that acted like a file. This has several advantages:
>
> 1) "Printing" can be to non-file objects, as long as the correct interface
> is supported. This would be useful, for instance, if the output needed
> to be encrypted or stored in a packet and sent over a socket or written
> to some sort of window object or ... . I very often wish that stdio
> allowed me to build FILE objects that could do more than work on file
> streams!
>
> The ability to actually print to a FILE* object is not lost, since a
> Python object can be built to support this.
>
> 2) If in the future it were decided that Python should not use stdio for
> its files, using a Python object here will lead to one less compatability
> problem. [One reason to avoid stdio might be to support buffered socket
> objects that need to interact with select() in a portable manner. Another
> reason might be to improve I/O efficiency, or to provide a more Posix-like
> interface to all 'file' objects...]
>
> PyFile_FromFile would probably need to be reexamined if anyone seriously
> thought about replacing stdio with some other I/O mechanism.

I hate to pick a fight over this, but I doubt that I will stray away
from using stdio for file objects -- because it's in the C standard,
it's just so much more portable than any other solution.

However, the idea to have a more general printing function is good --
in fact, the exact function you're proposing already exists, by the
(confusing!) name of PyFile_WriteObject(). Perhaps it can be renamed
to PyObject_Write() and added to Jim's proposal, with an added note
that using PyObject_Write() is preferred over PyObject_Print().

--Guido van Rossum, CWI, Amsterdam <mailto:Guido.van.Rossum@cwi.nl>
<http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>