From 98231f6ccdbbf248d479efef09b88f7abee49383 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Tue, 12 Nov 2013 22:23:49 +0800 Subject: [PATCH] Encode the text before print it to console Closes-Bug: #1250490 Change-Id: I516806a0f8a136bb66c64dcdcd07cee6d297e619 --- keystoneclient/tests/test_utils.py | 51 ++++++++++++++++++++++++++++++ keystoneclient/utils.py | 5 +-- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/keystoneclient/tests/test_utils.py b/keystoneclient/tests/test_utils.py index 65c6a1a3f..18554c127 100644 --- a/keystoneclient/tests/test_utils.py +++ b/keystoneclient/tests/test_utils.py @@ -12,6 +12,11 @@ # License for the specific language governing permissions and limitations # under the License. +import sys + +import mock +import six + from keystoneclient import exceptions from keystoneclient.tests import utils as test_utils from keystoneclient import utils @@ -91,3 +96,49 @@ class FindResourceTestCase(test_utils.TestCase): utils.find_resource, self.manager, 9999) + + +class FakeObject(object): + def __init__(self, name): + self.name = name + + +class PrintTestCase(test_utils.TestCase): + def setUp(self): + super(PrintTestCase, self).setUp() + self.old_stdout = sys.stdout + self.stdout = six.moves.cStringIO() + sys.stdout = self.stdout + + def tearDown(self): + super(PrintTestCase, self).tearDown() + sys.stdout = self.old_stdout + self.stdout = None + + def test_print_list_unicode(self): + name = u'\u540d\u5b57' + objs = [FakeObject(name)] + # NOTE(Jeffrey4l) If the text's encode is proper, this method will not + # raise UnicodeEncodeError exceptions + utils.print_list(objs, ['name']) + self.assertIn(name, self.stdout.getvalue().decode('utf8')) + + @mock.patch('keystoneclient.openstack.common.strutils.safe_encode') + def test_print_list_unicode_without_encode(self, safe_encode_mock): + safe_encode_mock.side_effect = lambda x, *args, **kwargs: x + + name = u'\u540d\u5b57' + objs = [FakeObject(name)] + self.assertRaises(UnicodeEncodeError, utils.print_list, objs, ['name']) + + def test_print_dict_unicode(self): + name = u'\u540d\u5b57' + utils.print_dict({'name': name}) + self.assertIn(name, self.stdout.getvalue().decode('utf8')) + + @mock.patch('keystoneclient.openstack.common.strutils.safe_encode') + def test_print_dict_unicode_without_encode(self, safe_encode_mock): + safe_encode_mock.side_effect = lambda x, *args, **kwargs: x + + name = u'\u540d\u5b57' + self.assertRaises(UnicodeEncodeError, utils.print_dict, {'name': name}) diff --git a/keystoneclient/utils.py b/keystoneclient/utils.py index 6902ed301..400c401f5 100644 --- a/keystoneclient/utils.py +++ b/keystoneclient/utils.py @@ -20,6 +20,7 @@ import prettytable import six from keystoneclient import exceptions +from keystoneclient.openstack.common import strutils # Decorator for cli-args @@ -56,7 +57,7 @@ def print_list(objs, fields, formatters={}, order_by=None): if order_by is None: order_by = fields[0] - print(pt.get_string(sortby=order_by)) + print(strutils.safe_encode(pt.get_string(sortby=order_by))) def _word_wrap(string, max_length=0): @@ -80,7 +81,7 @@ def print_dict(d, wrap=0): value = '' value = _word_wrap(value, max_length=wrap) pt.add_row([prop, value]) - print(pt.get_string(sortby='Property')) + print(strutils.safe_encode(pt.get_string(sortby='Property'))) def find_resource(manager, name_or_id):