[New-bugs-announce] [issue7745] Remove "isatty" check when setting sys.std[in|out|err] encoding to that of locale

Dave Malcolm report at bugs.python.org
Thu Jan 21 00:04:10 CET 2010


New submission from Dave Malcolm <dmalcolm at redhat.com>:

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 at brick ~]$ python -c 'print u"\u03b1\u03b2\u03b3"'
αβγ

[david at 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]

----------
components: Unicode
files: remove-isatty-check-from-sys-stream-encodings.patch
keywords: patch
messages: 98086
nosy: dmalcolm
severity: normal
status: open
title: Remove "isatty" check when setting sys.std[in|out|err] encoding to that of locale
versions: Python 2.7
Added file: http://bugs.python.org/file15957/remove-isatty-check-from-sys-stream-encodings.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue7745>
_______________________________________


More information about the New-bugs-announce mailing list