Merge "Avoid UnicodeEncodeError on python 2"

This commit is contained in:
Zuul 2018-12-05 11:15:49 +00:00 committed by Gerrit Code Review
commit e9fede805a
2 changed files with 22 additions and 0 deletions

View File

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import print_function
COPYRIGHT = """\
@ -1689,6 +1690,16 @@ def _main():
def main():
# workaround for avoiding UnicodeEncodeError on print() with older python
if sys.version_info[0] < 3:
# without reload print would fail even if sys.stdin.encoding
# would report utf-8
# see: https://stackoverflow.com/a/23847316/99834
stdin, stdout, stderr = sys.stdin, sys.stdout, sys.stderr
reload(sys)
sys.stdin, sys.stdout, sys.stderr = stdin, stdout, stderr
sys.setdefaultencoding(os.environ.get('PYTHONIOENCODING', 'utf-8'))
try:
_main()
except GitReviewException as e:

View File

@ -104,6 +104,17 @@ class GitReviewConsole(testtools.TestCase, fixtures.TestWithFixtures):
self.run_cmd_patcher.stop()
super(GitReviewConsole, self).tearDown()
@mock.patch('git_review.cmd.get_version',
side_effect=cmd.GitReviewException(u"simple-toπ㌀c"))
def test_print_exception_with_unicode(self, exists_mock):
try:
with mock.patch('sys.argv', ['git-review', '--version']):
with self.assertRaisesRegexp(SystemExit, '1'):
cmd.main()
except Exception as e:
self.fail('Exception not expected: %s' % e)
@mock.patch('git_review.cmd.query_reviews')
@mock.patch('git_review.cmd.get_remote_url', mock.MagicMock)
@mock.patch('git_review.cmd._has_color', False)