Issue1807

classification
Title: Failed to set UTF-8 encoding for output redirected to file.
Type: behaviour Severity: normal
Components: Core Versions: Jython 2.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: amak, fwierzbicki, irmen, ssoldatenko
Priority: normal Keywords:

Created on 2011-10-11.06:42:29 by ssoldatenko, last changed 2013-03-05.22:37:02 by amak.

Messages
msg6667 (view) Author: Sam (ssoldatenko) Date: 2011-10-11.06:42:28
Failed to set UTF-8 encoding for output redirected to file.

--------x.py--------
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import sys

print sys.stdout.encoding
print sys.getdefaultencoding()

print(u"Ф")
--------x.py--------

=== Output to console works fine ===

$ java -jar jython.jar -Dpython.console.encoding=UTF-8 x.py
UTF-8
ascii
Ф

=== Output to file does not work ===

$ java -jar jython.jar -Dpython.console.encoding=UTF-8 x.py > tmp.txt 2>tmp2.txt ; cat tmp.txt
$ cat tmp.txt
None
ascii
$ cat tmp2.txt
Traceback (most recent call last):
  File "x.py", line 9, in <module>
    print(u"Ф")
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0424' in position 0: ordinal not in range(128)
msg6711 (view) Author: Irmen de Jong (irmen) Date: 2011-11-06.22:55:33
in PySystemState.java there is this code fragment in initEncoding:

            if (stdStream.isatty()) {
                stdStream.encoding = encoding;
            }

So the encoding is only applied if the stream is a tty. 
When the check was removed, your code example works fine.
I'm not sure why this check is there?
msg6717 (view) Author: Sam (ssoldatenko) Date: 2011-11-07.06:20:11
I think it because of property name '-Dpython.console.encoding=UTF-8'.
It is CONSOLE encoding, not output encoding...

Can we add properties python.stdout.encoding and python.stderr.encoding?
Can we then change the code of initialization of the streams?

python.console.encoding - applied when stdout of stderr is a terminal.
python.stdout.encoding - applied when stdout is not a terminal, or when it is terminal but python.console.encoding is not set.
python.stderr.encoding - same as python.stdout.encoding.
History
Date User Action Args
2013-03-05 22:37:02amaksetkeywords: - console
2013-02-25 22:02:28amaksetkeywords: + console
2013-02-25 20:29:15fwierzbickisetpriority: normal
nosy: + fwierzbicki
versions: + Jython 2.5, - 2.5.2
2012-03-19 18:44:32amaksetnosy: + amak
2011-11-07 06:20:11ssoldatenkosetmessages: + msg6717
2011-11-06 22:55:33irmensetnosy: + irmen
messages: + msg6711
2011-10-11 06:42:29ssoldatenkocreate