On 12/6/2009 12:56 PM, Gnarlodious wrote:
On Dec 5, 3:54 am, Lie Ryan wrote:

Because of the switch to unicode str, a simple print('晉') should've
worked flawlessly if your terminal can accept the character, but the
problem is your terminal does not.

There is nothing wrong with Terminal, Mac OSX supports Unicode from
one end to the other.
The problem is that your code works normally in Terminal but not in a
browser.

#!/usr/bin/python
import sys, io
print("Content-type:text/plain;charset=utf-8\n\n")
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
print("晉")

The browser shows "Server error", Apache 2 reports error:

[error] [client 127.0.0.1] malformed header from script. Bad header=
\xe6\x99\x89: test.py

I've already posted before for some reason it is not possible to mix writing using print() and sys.stdout.buffer. On my machine, the output got mixed up:

--------------------------
晉
Content-type:text/plain;charset=utf-8
<BLANKLINE>
<BLANKLINE>
--------------------------

notice that the chinese character is on top of the header. I guess this is due to the buffering from print.

So far every way to print Unicode to a browser looks very un-Pythonic.
I am just wondering if I have a bug or am missing the right way
entirely.

My *guess* is Apache does not request a utf-8 stdout. When run on the Terminal, the Terminal requested utf-8 stdout from python and the script runs correctly. I'm not too familiar with Apache's internal nor how python 3 figured its stdout's encoding, you might want to find Apache's mailing list if they have any similar case.

PS: You might also want to look at this: http://stackoverflow.com/questions/984014/python-3-is-using-sys-stdout-buffer-write-good-style

it says to try setting your PYTHONIOENCODING environment variable to "utf8"
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to