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

noreply@sourceforge.net noreply@sourceforge.net
Tue, 12 Nov 2002 08:45:38 -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:45

Message:
Logged In: YES 
user_id=612691

Usually it does not have __str__, so __repr__ is getting used.
In other cases, the class does have a __str__, and it gets
used, but even then I think it shouldn't if there's a
__unicode__

All this presumes there's a way to diagnose whether files
are wide or narrow -- I'm not familiar enough with the
implementation details here to know if this makes sense or not.

My original message made the point as follows:

If you call str(object), and object's class has a __str__
method, the
  value is the value of the __str__ method;

If you print an object to a normal stream, and object's
class has a
  __str__ method, what appears is the result of the __str__
method.

If you call unicode(object), and object's class has a
__unicode__ method, the
  value is the value of the __unicode__ method;

So far so good, but read on . . .

If you print an object to a unicode stream, and object's
class has a
  __unicode__ method, what appears is the result of . . .

_not_ the __unicode__ method, but the __str__ method, if
there is one,
otherwise the usual default

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

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

Message:
Logged In: YES 
user_id=21627

So your class does have an __str__? Or is it the __repr__
that is being used?

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

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