Although I could not find a mention of this in elswhere, it seems that Python 2.7 will automatically convert the text to a terminal encoding, instead of throwing an error as expected.
Python 2.7:
> echo $LANG en_US.UTF-8 > python -c 'import sys; print sys.getdefaultencoding()' ascii > python -c 'import sys; sys.stdout.write(u"\u03A3")' Ξ£ > python -c 'import sys; sys.stdout.write(u"\u03A3".encode("utf8"))' Ξ£
Python 2.6 (in another window)
> echo $LANG en_US.UTF-8 > python -c 'import sys; print sys.getdefaultencoding()' ascii > python -c 'import sys; sys.stdout.write(u"\u03A3")' Traceback (most recent call last): File "<string>", line 1, in <module> UnicodeEncodeError: 'ascii' codec cant encode character u'\u03a3' in position 0: ordinal not in range(128) > python -c 'import sys; sys.stdout.write(u"\u03A3".encode("utf8"))' Ξ£
In any case, .encode ("utf8") for the data should be avoided before exiting.
source share