[Python-Dev] Removing the implicit str() call from printing API

M.-A. Lemburg mal@lemburg.com
Fri, 09 Feb 2001 14:59:54 +0100


There was some discussion about this subject before, but nothing
much happened, so here we go again...

Printing in Python is a rather complicated task. It involves many
different APIs, flags, etc. Deep down in the printing machinery
there is a hidden call to str() which converts the to be printed
object into a string object. 

This is fine for non-string objects like numbers, but causes trouble 
when it comes to printing Unicode objects due to the auto-conversions 
this causes.

There is a patch on SF which tries to remedy this, but it introduces
a special attribute to maintain backward compatibility:

http://sourceforge.net/patch/?func=detailpatch&patch_id=103685&group_id=5470

I don't really like the idea to add such an attribute to the
file object. Instead, I think that we should simply pass along
Unicode objects as-is to the file object's .write() method and
have the method take care of the conversion. 

This will break some code, since not all file-like objects expect 
non-strings as input to the .write() method, but I think this small 
code breakage is worth it as it allows us to redirect printing
to streams which convert Unicode input into a specific output
encoding.

Thoughts ?

-- 
Marc-Andre Lemburg
______________________________________________________________________
Company:                                        http://www.egenix.com/
Consulting:                                    http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/