[ python-Feature Requests-637094 ] print to unicode stream should
__unicode
SourceForge.net
noreply at sourceforge.net
Thu Dec 2 16:46:04 CET 2004
Feature Requests item #637094, was opened at 2002-11-12 13:24
Message generated for change (Settings changed) made by lemburg
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=637094&group_id=5470
>Category: Unicode
>Group: None
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: M.-A. Lemburg (lemburg)
Date: 2004-12-02 16:45
Message:
Logged In: YES
user_id=38388
Please leave this open: it's a reminder to start working on
an overhaul of the printing sub-system and file.write() in
particular.
Thanks.
----------------------------------------------------------------------
Comment By: Facundo Batista (facundobatista)
Date: 2004-12-02 01:03
Message:
Logged In: YES
user_id=752496
Please, could you verify if this problem persists in Python 2.3.4
or 2.4?
If yes, in which version? Can you provide a test case?
If the problem is solved, from which version?
Note that if you fail to answer in one month, I'll close this bug
as "Won't fix".
Thank you!
. Facundo
----------------------------------------------------------------------
Comment By: Henry S Thompson (hthompson)
Date: 2002-11-12 17: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 17: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 17: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 16: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 16: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 15: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=355470&aid=637094&group_id=5470
More information about the Python-bugs-list
mailing list