sys.stdout.write()'s bug or doc bug?

Q

Qiangning Hong

u = u'\u554a'Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u554a' in
position 0: ordinal not in range(128)'UTF-8'

Quote from file object's documentation:

encoding
The encoding that this file uses. When Unicode strings are written to
a file, they will be converted to byte strings using this
encoding. .....

So, my question is, as sys.stdout IS a file object, why it does not
use its encoding attribute to convert the given unicode? An
implementation bug? A documenation bug?
 
Q

Qiangning Hong

Python 2.4.4 (#2, Oct 22 2008, 19:52:44)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode character u'\u554a' in
position 0: ordinal not in range(128)

Actually, I know relationship between unicode and str objects very
well. That's why I only quoted the unicode-related part of
file.encoding's documentation in my original post. Thank you.
hmm I always thought "sys.stdout" is a "file-like object" not that it IS a file.

In my original post, I have figured out that sys.stdout IS a file, by
using type() function. And isinstance() function tells the same:

Python 2.5.2 (r252:60911, Dec 18 2008, 12:39:19)
[GCC 4.2.1 (Apple Inc. build 5564)] on darwin
Type "help", "copyright", "credits" or "license" for more information.True

So, sys.stdout SHOULD do what the doc says, otherwise there is a bug
either in implementation of sys.stdout, or in the documentation of
file.
 
S

Steven D'Aprano

hmm I always thought "sys.stdout" is a "file-like object" not that it
IS a file.

In my original post, I have figured out that sys.stdout IS a file, by
using type() function. And isinstance() function tells the same:

Python 2.5.2 (r252:60911, Dec 18 2008, 12:39:19) [GCC 4.2.1 (Apple Inc.
build 5564)] on darwin Type "help", "copyright", "credits" or "license"
for more information.True

So, sys.stdout SHOULD do what the doc says, otherwise there is a bug
either in implementation of sys.stdout, or in the documentation of file.

The documentation says:

file.encoding
The encoding that this file uses. When Unicode strings are written to a
file, they will be converted to byte strings using this encoding. In
addition, when the file is connected to a terminal, the attribute gives
the encoding that the terminal is likely to use (that information might
be incorrect if the user has misconfigured the terminal). The attribute
is read-only and may not be present on all file-like objects. It may also
be None, in which case the file uses the system default encoding for
converting Unicode strings.
New in version 2.3.

http://docs.python.org/library/stdtypes.html#file.encoding


And I agree that sys.stdout is a file. Using Python 2.6:
<type 'file'>


I can confirm the behaviour you report:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u554a' in
position 0: ordinal not in range(128)

But if you explicitly convert the string, it works:
å•Š



I agree that this appears to be a bug, either of the write() method or
the documentation.
 
A

Aahz

So, my question is, as sys.stdout IS a file object, why it does not
use its encoding attribute to convert the given unicode? An
implementation bug? A documenation bug?

Please file a bug on bugs.python.org -- that's the only way this can be
tracked.
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it." --Brian W. Kernighan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top