classification
Title: Remove "isatty" check when setting sys.std[in|out|err] encoding to that of locale
Type: behavior Stage: test needed
Components: Unicode Versions: Python 2.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: dmalcolm, pitrou
Priority: normal Keywords: needs review, patch

Created on 2010-01-20 23:04 by dmalcolm, last changed 2010-01-21 19:09 by brian.curtin. This issue is now closed.

Files
File name Uploaded Description Edit
remove-isatty-check-from-sys-stream-encodings.patch dmalcolm, 2010-01-20 23:04
Messages (2)
msg98086 - (view) Author: Dave Malcolm (dmalcolm) (Python committer) Date: 2010-01-20 23:04
Currently sys.std[in|out|err] are set to the locale encoding but only if they are directly connected to a tty (and PYTHONIOENCODING is not set in the environment to override things).

The conditionality on "isatty" leads to lots of unexpected changes when scripts are added to shell pipelines, or run from cronjobs.

For example:
[david@brick ~]$ python -c 'print u"\u03b1\u03b2\u03b3"'
αβγ

[david@brick ~]$ python -c 'print u"\u03b1\u03b2\u03b3"' | less
Traceback (most recent call last):
  File "<string>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2:
ordinal not in range(128)  

("less" will respect the locale encoding).

The attached patch removes the check to see if these streams are connected to a tty, so that the encoding is always set (either to the locale value, or to the "override" value from PYTHONIOENCODING)

With this patch:
$ ./python -c 'print u"\u03b1\u03b2\u03b3"'| less

has output:
αβγ
[17298 refs]
msg98090 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-01-21 01:24
As discussed on python-dev, this patch should probably be rejected.
History
Date User Action Args
2010-01-21 19:09:28brian.curtinsetresolution: rejected
2010-01-21 19:03:37dmalcolmsetstatus: open -> closed
2010-01-21 01:24:33pitrousetnosy: + pitrou
messages: + msg98090
2010-01-21 00:55:23brian.curtinsetpriority: normal
keywords: + needs review
type: behavior
stage: test needed
2010-01-20 23:04:09dmalcolmcreate