RETIRED, further work has moved to Debian project infrastructure
Go to file
John Dennis 50d67088ea Avoid ASCII encoding errors when output is redirected
Unicode must be encoded/decoded for text I/O streams, the
correct encoding for the stream must be selected and it must
be capable of handling the set of characters in the stream
or Python will raise a codec error. The correct codec is
selected based on the locale. Python2 uses the locales
encoding but only when the I/O stream is attached to a
terminal (TTY) otherwise it uses the default ASCII
encoding. The effect is internationalized text written to
the terminal works as expected but if command line output is
redirected (file or pipe) the ASCII codec is used and the
program aborts with a codec error.

The default I/O streams stdin, stdout and stderr can be
wrapped in a codec based on the locale thus assuring the
users desired encoding is always used no matter the I/O
destination. Python3 does this by default.

If the caller supplies an I/O stream we use it unmodified on
the assumption the caller has taken all responsibility for
the stream.  But with Python2 if the caller allows us to
default the I/O streams to sys.stdin, sys.stdout and
sys.stderr we apply the locales encoding just as Python3
would do. We also check to make sure the main Python program
has not already already wrapped sys.stdin, sys.stdout and
sys.stderr as this is a common recommendation.

Note, the unit test enhancement included in this patch is limited to
assuring the I/O streams passed to the App constructor are wrapped or
not wrapped as expected. No attempt is made to assure codec errors
will not occur in a terminal because of the difficulty of constructing
such a test. Properly testing this probably would require spawing a
subshell but one can't use Python's subcommand because the I/O streams
are not attached to a TTY. One could set up a ptty (pseudo-terminal)
but experience has shown ptty's to be non-portable across operating
systems and environments, getting a ptty to work correctly in all
testing environments would be more work and hassle than is justified.

Change-Id: I8e85936dff0d5db29a66337c9ce60c70a8dee10a
Closes-Bug: 1603210
Signed-off-by: John Dennis <jdennis@redhat.com>
2016-08-05 18:24:49 +00:00
cliff Avoid ASCII encoding errors when output is redirected 2016-08-05 18:24:49 +00:00
demoapp Fix cliff URLs in doc and demoapp 2016-07-28 16:08:47 +09:00
doc Fix cliff URLs in doc and demoapp 2016-07-28 16:08:47 +09:00
integration-tests Fix stable integration tests 2014-08-26 10:52:38 -04:00
.gitignore Sort the fuzzy matches 2015-04-14 08:17:04 -06:00
.gitreview Update .gitreview after repo rename 2014-02-07 20:38:59 +00:00
.travis.yml add pypy test env configuration 2013-09-23 15:45:31 -04:00
CONTRIBUTING.rst Workflow documentation is now in infra-manual 2014-12-05 03:30:36 +00:00
LICENSE change to apache license 2012-04-20 10:06:58 -07:00
MANIFEST.in remove the other traces of distribute 2013-06-11 14:01:31 -04:00
Makefile Correct path to docs 2015-11-12 13:52:05 +00:00
README.rst Fixed broken link 2016-07-06 20:06:40 +00:00
openstack-common.conf remove openstack-common.conf 2016-01-15 12:53:44 +08:00
requirements.txt Updated from global requirements 2016-07-17 23:52:03 +00:00
setup.cfg setup: fix Python versions classifiers 2015-12-31 10:58:04 +01:00
setup.py Updated from global requirements 2015-11-19 15:44:19 +00:00
test-requirements.txt Updated from global requirements 2016-06-21 17:58:13 +00:00
tox.ini Drop Python 2.6 support 2016-02-11 01:55:34 -05:00

README.rst

cliff -- Command Line Interface Formulation Framework

cliff is a framework for building command line programs. It uses setuptools entry points to provide subcommands, output formatters, and other extensions.