]> git.apps.os.sepia.ceph.com Git - ceph.git/commit
ceph-volume: terminal: encode unicode when writing to stdout
authorKefu Chai <kchai@redhat.com>
Sun, 24 Mar 2019 09:11:31 +0000 (17:11 +0800)
committerJan Fajerski <jfajerski@suse.com>
Mon, 12 Aug 2019 12:20:49 +0000 (14:20 +0200)
commit8b09f2bcdb02f9cd9cd7c789690c889b16a66fb3
tree93a72e63d846fbeca7963aa066607e5548d85d68
parent4d9848f739fe0e129ced9f3ba198b079446d2806
ceph-volume: terminal: encode unicode when writing to stdout

python determins the encoding of stdout and stderr based on the LC_CTYPE
and PYTHONIOENCODING env variable, by default, python3's sys.stdout uses
'utf-8' as its encoding, so it will be able to write unicode string even
the stdout is not attached to a tty device. but when it comes to
python2, it will default to ascii if neither of these variabls is set.
so, if we are writing unicode using `_Write` in an environment where
LC_CTYPE and/or PYTHONIOENCODING are using non UTF-8 encoding, it chokes
by raising `UnicodeEncodeError` exception.

in this change, we add a wrapper around `_Write._writer` so it is able
to write unicode string in such a non-unicode-friendly environment.

for more info related the encoding of stdout and stderr, see
https://docs.python.org/3/using/cmdline.html#envvar-PYTHONIOENCODING .

Signed-off-by: Alfredo Deza <adeza@redhat.com>
Signed-off-by: Kefu Chai <kchai@redhat.com>
(cherry picked from commit 77912c0c71874a23785d3bdd109ad61e4f4c0e28)
src/ceph-volume/ceph_volume/terminal.py
src/ceph-volume/ceph_volume/tests/test_terminal.py