From d8c07006dd85ad64a4a4a4cb47558c0a19e42e24 Mon Sep 17 00:00:00 2001 From: bhagyashris Date: Tue, 5 Jul 2016 19:24:21 +0530 Subject: [PATCH] Fix 'UnicodeEncodeError' for unicode values Used '%' instead of format() because "%" formatting operation returns a unicode string for both unicode and non-unicode format strings. Encoded the exception error message in novaclient.shell.main and novaclient.utils.do_action_on_many method to avoid "UnicodeEncodeError" and print valid message. NOTE: Not used u'{0}.format() because in python 3 everything is unicode so no need to add explicit 'u' to format string. Closes-Bug: #1403379 Change-Id: I13f1ef2e1b41299b417cad3759a5cda241890df7 --- novaclient/shell.py | 8 ++++++-- novaclient/utils.py | 2 +- novaclient/v2/hosts.py | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/novaclient/shell.py b/novaclient/shell.py index 1f277485d..3a3f28529 100644 --- a/novaclient/shell.py +++ b/novaclient/shell.py @@ -982,9 +982,13 @@ def main(): OpenStackComputeShell().main(argv) except Exception as exc: logger.debug(exc, exc_info=1) - print(_("ERROR (%(type)s): %(msg)s") % { + if six.PY2: + message = encodeutils.safe_encode(six.text_type(exc)) + else: + message = encodeutils.exception_to_unicode(exc) + print("ERROR (%(type)s): %(msg)s" % { 'type': exc.__class__.__name__, - 'msg': encodeutils.exception_to_unicode(exc)}, + 'msg': message}, file=sys.stderr) sys.exit(1) except KeyboardInterrupt: diff --git a/novaclient/utils.py b/novaclient/utils.py index 093bc9619..e6f3fd48c 100644 --- a/novaclient/utils.py +++ b/novaclient/utils.py @@ -407,7 +407,7 @@ def do_action_on_many(action, resources, success_msg, error_msg): print(success_msg % resource) except Exception as e: failure_flag = True - print(e) + print(encodeutils.safe_encode(six.text_type(e))) if failure_flag: raise exceptions.CommandError(error_msg) diff --git a/novaclient/v2/hosts.py b/novaclient/v2/hosts.py index 88789e242..f97818a0d 100644 --- a/novaclient/v2/hosts.py +++ b/novaclient/v2/hosts.py @@ -76,7 +76,7 @@ class HostManager(base.ManagerWithFind): :param action: The action to perform returns: An instance of novaclient.base.TupleWithMeta """ - url = '/os-hosts/{0}/{1}'.format(host, action) + url = '/os-hosts/%s/%s' % (host, action) resp, body = self.api.client.get(url) return base.TupleWithMeta((resp, body), resp)