[Python-bugs-list] [ python-Bugs-637094 ] print to unicode stream should __unicode

noreply@sourceforge.net noreply@sourceforge.net
Tue, 12 Nov 2002 08:34:50 -0800


Bugs item #637094, was opened at 2002-11-12 12:24
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=637094&group_id=5470

Category: Unicode
Group: Python 2.2.1
Status: Open
Resolution: None
Priority: 5
Submitted By: Henry S Thompson (hthompson)
Assigned to: M.-A. Lemburg (lemburg)
Summary: print to unicode stream should __unicode

Initial Comment:
To make __unicode__ parallel to __str__ in what seems
like the right way, print >>f,x should check for
__unicode__ if f is a unicode-enabled stream
 See
http://mail.python.org/pipermail/python-list/2002-November/129859.html
for more details

----------------------------------------------------------------------

>Comment By: Henry S Thompson (hthompson)
Date: 2002-11-12 16:34

Message:
Logged In: YES 
user_id=612691

As MvL said, I'm looking at a case such as the following:

x=X()
f=codecs.getwriter('utf8')(open("/tmp/out","w"))
print >>f,x

where X has a __unicode__ method.

It seems wrong to me that __str__ gets used in this case,
not __unicode__

----------------------------------------------------------------------

Comment By: M.-A. Lemburg (lemburg)
Date: 2002-11-12 15:53

Message:
Logged In: YES 
user_id=38388

Hmm, in those cases, passing Unicode objects to .write()
should work (and thus printing too). I think he's trying
to print some user-defined instances to such a stream...
that's where __str__ is called instead of __unicode__
by PyFile_WriteObject(). 

The question then becomes: how should PyFile_WriteObject()
know whether to look for __unicode__ or not ?



----------------------------------------------------------------------

Comment By: Martin v. L÷wis (loewis)
Date: 2002-11-12 15:20

Message:
Logged In: YES 
user_id=21627

Henry is talking about the objects returned from
codecs.open, or instantiating the classes returned from
codecs.get_writer.

----------------------------------------------------------------------

Comment By: M.-A. Lemburg (lemburg)
Date: 2002-11-12 14:58

Message:
Logged In: YES 
user_id=38388

I'm not sure I understand: what is a "unicode stream".

All streams in Python are considered byte streams and
(currently) have no encoding attached. Changing that
would require a lot of work, some of which is under way.

Still, the best way to deal with this is to first encode Unicode
to a string using a known stream encoding and then sending
off the 8-bit data from the encoding process to the stream.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=637094&group_id=5470