Ka-Ping Yee email@example.com wrote:
I know this is not a small change, but i'm pretty convinced the right answer here is that the print hook should call a *method* on sys.stdout, whatever sys.stdout happens to be. The details are described in the other long message i wrote ("Printing objects on files").
Here is an addendum that might actually make that proposal feasible enough (compatibility-wise) to fly in the short term:
try: sys.stdout.printout(x) except AttributeError: sys.stdout.write(str(x)) sys.stdout.write("\n")
The rest can then be added, and the change in 'print x' will work nicely for any file objects, but will not break on file-like substitutes that don't define a 'printout' method.
another approach is (simplified):
try: sys.stdout.write(x.encode(sys.stdout.encoding)) except AttributeError: sys.stdout.write(str(x))
or, if str is changed to return any kind of string:
x = str(x) try: x = x.encode(sys.stdout.encoding) except AttributeError: pass sys.stdout.write(x)