bizarre behaviour

I've been trying to get utf-8 output working properly in python (2.5.1) and I've encountered some strangely inconsistent behavious. It seems like a bug, but I'm probably just getting something wrong.

Basically print works, but sys.stdout.write of the same thing doesn't.

Any ideas?

Python 2.5.1 (r251:54863, Apr 15 2008, 22:57:26) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
> print unichr(0xe9)
é
> sys.stdout.write(unichr(0xe9))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0: ordinal not in range(128)
> class Test:
    def write(self, x):
        sys.stdout.write(x)

> print >> Test(), unichr(0xe9)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in write
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0: ordinal not in range(128)
> print >> sys.stdout, unichr(0xe9)
é
> sys.stdout.encoding
'UTF-8'
Written in WikklyText.

Reply

The content of this field is kept private and will not be shown publicly.