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

Qiangning Hong hongqn at
Fri Dec 26 06:44:11 CET 2008

>>> u = u'\u554a'
>>> print u
>>> sys.stdout.write(u)
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)
>>> type(sys.stdout)
<type 'file'>
>>> sys.stdout.encoding

Quote from file object's documentation:

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?

More information about the Python-list mailing list