Avoid UnicodeEncodeError on python 2
Python2 has default encoding as ascii which means that is likely that some print() commands would fails with UnicodeEncodeError. This hack changes default encoding in order to avoid such errors. Change-Id: I4e21e6e32d4bb815693b7d6ce35efb6a5cca2fc2
This commit is contained in:
parent
c243d80af9
commit
138e9a25e3
@ -1,3 +1,4 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import print_function
|
||||
|
||||
COPYRIGHT = """\
|
||||
@ -1700,6 +1701,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:
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user