redirect python output + extension type with own print method = FAILS

Donn Cave donn at drizzle.com
Sat Aug 11 08:35:25 CEST 2001


Quoth doug_bartholomew99 at yahoo.com (doug):

| BOTTOMLINE: Why when python's output is redirected by overriding
| sys.stdout, would a user-defined type's print method not be
| accessible?

BOTTOMLINE ANSWER:  If you want to say "print xxx" and have the
output go through your stdout - you don't really want a "print"
method, you want a "repr" method.  No I/O, just return a string
(as in PyString_FromString().)  The "print" statement does all the I/O.

SIDE NOTE:  The effect of replacing stdout is purely at the Python
level, affecting only those Python functions that look up sys.stdout
and deal with it as a file object.  C library I/O, to "stdout" or
wherever, does not take notice of this and will still go where it
went before.

There are other ways to redirect output that do affect output at all
levels, at least on UNIX and similar platforms, but that uses a lower
level abstraction in the operating system's concept of a device, so
your choice of redirections is naturally limited to devices.

	Donn Cave, donn at drizzle.com



More information about the Python-list mailing list