From 8a0f5678485076fbe0ea1f318c7fd49bac3f1df8 Mon Sep 17 00:00:00 2001 From: "Jay S. Bryant" Date: Mon, 3 Mar 2014 16:52:54 -0600 Subject: [PATCH] Remove str() from LOG.* and exceptions gettextutils is expecting to receive unicode strings rather than basestrings. A basestring can cause an unhandled exception in the logging code. To help avoid such issues we should remove str() from LOG.* messages and exceptions. We have verified that the %s formatting code properly handle getting strings to unicode where necessary. This patch also fixes one case where a message object was being concatenated with '+' . This, like using str() will cause logging to fail and needs to be fixed. See bug https://bugs.launchpad.net/cinder/+bug/1274245 for the original discussion of this problem. Fix for oslo.messaging: https://review.openstack.org/90577 Change-Id: Iad7c2284c6b21322b96dc881a82bbbab4ebb208e Closes-bug: 1286306 --- openstack/common/log.py | 2 +- openstack/common/processutils.py | 2 +- openstack/common/quota.py | 2 +- openstack/common/rpc/amqp.py | 2 +- openstack/common/rpc/impl_kombu.py | 10 +++++----- openstack/common/rpc/impl_qpid.py | 8 ++++---- openstack/common/strutils.py | 2 +- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/openstack/common/log.py b/openstack/common/log.py index 2807eb79a..193b79b4d 100644 --- a/openstack/common/log.py +++ b/openstack/common/log.py @@ -451,7 +451,7 @@ def _load_log_config(log_config_append): logging.config.fileConfig(log_config_append, disable_existing_loggers=False) except moves.configparser.Error as exc: - raise LogConfigError(log_config_append, str(exc)) + raise LogConfigError(log_config_append, six.text_type(exc)) def setup(product_name, version='unknown'): diff --git a/openstack/common/processutils.py b/openstack/common/processutils.py index f7d0e7937..4e7836fd8 100644 --- a/openstack/common/processutils.py +++ b/openstack/common/processutils.py @@ -224,7 +224,7 @@ def trycmd(*args, **kwargs): out, err = execute(*args, **kwargs) failed = False except ProcessExecutionError as exn: - out, err = '', str(exn) + out, err = '', six.text_type(exn) failed = True if not failed and discard_warnings and err: diff --git a/openstack/common/quota.py b/openstack/common/quota.py index 509d7a100..59a490a77 100644 --- a/openstack/common/quota.py +++ b/openstack/common/quota.py @@ -84,7 +84,7 @@ class QuotaException(Exception): class QuotaError(QuotaException): - msg_fmt = _("Quota exceeded") + ": code=%(code)s" + msg_fmt = _("Quota exceeded: code=%(code)s") code = 413 headers = {'Retry-After': 0} safe = True diff --git a/openstack/common/rpc/amqp.py b/openstack/common/rpc/amqp.py index ff69398da..de6cb3da6 100644 --- a/openstack/common/rpc/amqp.py +++ b/openstack/common/rpc/amqp.py @@ -202,7 +202,7 @@ class ReplyProxy(ConnectionContext): LOG.warn(_('No calling threads waiting for msg_id : %(msg_id)s' ', message : %(data)s'), {'msg_id': msg_id, 'data': message_data}) - LOG.warn(_('_call_waiters: %s') % str(self._call_waiters)) + LOG.warn(_('_call_waiters: %s') % self._call_waiters) else: waiter.put(message_data) diff --git a/openstack/common/rpc/impl_kombu.py b/openstack/common/rpc/impl_kombu.py index 2b23cf8a4..7ccd70314 100644 --- a/openstack/common/rpc/impl_kombu.py +++ b/openstack/common/rpc/impl_kombu.py @@ -549,7 +549,7 @@ class Connection(object): raise log_info = {} - log_info['err_str'] = str(e) + log_info['err_str'] = e log_info['max_retries'] = self.max_retries log_info.update(params) @@ -621,7 +621,7 @@ class Connection(object): """ def _connect_error(exc): - log_info = {'topic': topic, 'err_str': str(exc)} + log_info = {'topic': topic, 'err_str': exc} LOG.error(_LE("Failed to declare consumer for topic '%(topic)s': " "%(err_str)s") % log_info) @@ -641,11 +641,11 @@ class Connection(object): def _error_callback(exc): if isinstance(exc, socket.timeout): LOG.debug('Timed out waiting for RPC response: %s' % - str(exc)) + exc) raise rpc_common.Timeout() else: LOG.exception(_LE('Failed to consume message from queue: %s') % - str(exc)) + exc) info['do_consume'] = True def _consume(): @@ -682,7 +682,7 @@ class Connection(object): """Send to a publisher based on the publisher class.""" def _error_callback(exc): - log_info = {'topic': topic, 'err_str': str(exc)} + log_info = {'topic': topic, 'err_str': exc} LOG.exception(_LE("Failed to publish message to topic " "'%(topic)s': %(err_str)s") % log_info) diff --git a/openstack/common/rpc/impl_qpid.py b/openstack/common/rpc/impl_qpid.py index dc47661de..3920b9341 100644 --- a/openstack/common/rpc/impl_qpid.py +++ b/openstack/common/rpc/impl_qpid.py @@ -571,7 +571,7 @@ class Connection(object): add it to our list of consumers """ def _connect_error(exc): - log_info = {'topic': topic, 'err_str': str(exc)} + log_info = {'topic': topic, 'err_str': exc} LOG.error(_LE("Failed to declare consumer for topic '%(topic)s': " "%(err_str)s") % log_info) @@ -588,11 +588,11 @@ class Connection(object): def _error_callback(exc): if isinstance(exc, qpid_exceptions.Empty): LOG.debug('Timed out waiting for RPC response: %s' % - str(exc)) + exc) raise rpc_common.Timeout() else: LOG.exception(_LE('Failed to consume message from queue: %s') % - str(exc)) + exc) def _consume(): nxt_receiver = self.session.next_receiver(timeout=timeout) @@ -625,7 +625,7 @@ class Connection(object): """Send to a publisher based on the publisher class.""" def _connect_error(exc): - log_info = {'topic': topic, 'err_str': str(exc)} + log_info = {'topic': topic, 'err_str': exc} LOG.exception(_LE("Failed to publish message to topic " "'%(topic)s': %(err_str)s") % log_info) diff --git a/openstack/common/strutils.py b/openstack/common/strutils.py index 29cd57282..b49184e32 100644 --- a/openstack/common/strutils.py +++ b/openstack/common/strutils.py @@ -78,7 +78,7 @@ def bool_from_string(subject, strict=False, default=False): Strings yielding False are 'f', 'false', 'off', 'n', 'no', or '0'. """ if not isinstance(subject, six.string_types): - subject = str(subject) + subject = six.text_type(subject) lowered = subject.strip().lower()