cliff/cliff
John Dennis 35f3820855 Fix codec error when format=csv
The Py2 version of the csv module cannot accept unicode text. In Py2
we replaced the csv module with the unicodecsv module which encodes
unicode text prior to calling the standard Py2 csv module. Thus when
the cvs formatted output is emitted on stdout it is presented as a
encoded byte stream. But stdout has been replaced with a StreamWriter
which encodes to the desired encoding. The problem is the StreamWriter
attempts to encode all objects passed to it's write function,
including str objects. Instead it should only encode unicode text objects
and allow bytes to pass through unmodified.

This patch adds an override of the codecs.getwriter function which
only encodes unicode text objects. In addtion we pass the encoding
value obtained from the stream to the unicodecsv writer.

The patch fixes the codec error when outputing csv formated text that
contains a non-ASCII character. The unicodecsv implmentation will emit
byte encoded str objects to the stream. When the core StreamWriter
attempts to encode a str object Python will first promote the str
object to a unicode object. The promotion of str to unicode requires
the str bytes to be decoded. However the encoding associated with the
str object is not known therefore Python applies the default-encoding
which is ASCII. In the case where the str object contains utf-8
encoded non-ASCII characters a decoding error is raised. By not
attempting to encode a byte stream we avoid this error.

A more complete discussion of the above issues can be found here:

https://github.com/fedora-infra/kitchen/blob/develop/kitchen2/docs/unicode-frustrations.rst#frustration-4-now-it-doesnt-take-byte-strings

Change-Id: I22b5ad8bf0e227ec75a2a36986f0487191f7cbc2
Closes-Bug: 1720115
Signed-off-by: John Dennis <jdennis@redhat.com>
(cherry picked from commit c61cc30060)
2017-12-06 13:53:41 +00:00
..
formatters Fix codec error when format=csv 2017-12-06 13:53:41 +00:00
tests Fix codec error when format=csv 2017-12-06 13:53:41 +00:00
__init__.py add setup.py and package directory 2012-04-20 09:52:55 -07:00
_argparse.py Ensure python standard argparse module is loaded 2017-05-20 02:16:34 +09:00
app.py Fix codec error when format=csv 2017-12-06 13:53:41 +00:00
columns.py Add Apache 2.0 license to source file 2016-10-10 13:43:09 +07:00
command.py Run hooks for DisplayCommandBase 2017-07-06 15:39:39 -07:00
commandmanager.py Merge "Fix command order" 2017-01-03 19:00:05 +00:00
complete.py add cmd_name argument to CompleteCommand 2017-05-25 18:38:58 -04:00
display.py Run hooks for DisplayCommandBase 2017-07-06 15:39:39 -07:00
help.py pass the command name from HelpCommand 2017-06-29 22:29:01 -07:00
hooks.py add before and after hooks 2017-06-30 09:50:21 -04:00
interactive.py Adjust completenames tests for cmd2 0.7.3+ 2017-06-28 07:45:44 +02:00
lister.py Add Apache 2.0 license to source file 2016-10-10 13:43:09 +07:00
show.py Add Apache 2.0 license to source file 2016-10-10 13:43:09 +07:00
sphinxext.py sphinxext: Add 'application' option to the autoprogram directive 2017-07-03 09:17:41 +09:00
utils.py Fix codec error when format=csv 2017-12-06 13:53:41 +00:00