From 7bfd4439fe48e861675e3930486b55f8badd072e Mon Sep 17 00:00:00 2001 From: Mark McLoughlin Date: Mon, 1 Jul 2013 10:43:37 +0100 Subject: Avoid shadowing Exception 'message' attribute The Exception class has a 'message' attribute: >>> Exception('foo').message 'foo' however, we follow a pattern that results in the attribute being shadowed by a class attribute: >>> class MyException(Exception): ... message = 'Foo %s' ... def __init__(self, bar): ... message = self.message % bar ... super(MyException, self).__init__(message) ... >>> MyException('bar').message 'Foo %s' whereas, we obviously want behaviour like this: >>> class MyException(Exception): ... msg_fmt = 'Foo %s' ... def __init__(self, bar): ... message = self.msg_fmt % bar ... super(MyException, self).__init__(message) ... >>> MyException('bar').message 'Foo bar' Avoid this shadowing by using 'msg_fmt' as the name of the class attribute. Add a test which fails without this fix. Change-Id: I304f31edec20b1293cf6c8de931415590fde6752 --- openstack/common/rpc/common.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'openstack/common') diff --git a/openstack/common/rpc/common.py b/openstack/common/rpc/common.py index 5eacd32..9cfb942 100644 --- a/openstack/common/rpc/common.py +++ b/openstack/common/rpc/common.py @@ -74,14 +74,14 @@ _REMOTE_POSTFIX = '_Remote' class RPCException(Exception): - message = _("An unknown RPC related exception occurred.") + msg_fmt = _("An unknown RPC related exception occurred.") def __init__(self, message=None, **kwargs): self.kwargs = kwargs if not message: try: - message = self.message % kwargs + message = self.msg_fmt % kwargs except Exception: # kwargs doesn't match a variable in the message @@ -90,7 +90,7 @@ class RPCException(Exception): for name, value in kwargs.iteritems(): LOG.error("%s: %s" % (name, value)) # at least get the core message out if something happened - message = self.message + message = self.msg_fmt super(RPCException, self).__init__(message) @@ -104,7 +104,7 @@ class RemoteError(RPCException): contains all of the relevant info. """ - message = _("Remote error: %(exc_type)s %(value)s\n%(traceback)s.") + msg_fmt = _("Remote error: %(exc_type)s %(value)s\n%(traceback)s.") def __init__(self, exc_type=None, value=None, traceback=None): self.exc_type = exc_type @@ -121,7 +121,7 @@ class Timeout(RPCException): This exception is raised if the rpc_response_timeout is reached while waiting for a response from the remote side. """ - message = _('Timeout while waiting on RPC response - ' + msg_fmt = _('Timeout while waiting on RPC response - ' 'topic: "%(topic)s", RPC method: "%(method)s" ' 'info: "%(info)s"') @@ -144,25 +144,25 @@ class Timeout(RPCException): class DuplicateMessageError(RPCException): - message = _("Found duplicate message(%(msg_id)s). Skipping it.") + msg_fmt = _("Found duplicate message(%(msg_id)s). Skipping it.") class InvalidRPCConnectionReuse(RPCException): - message = _("Invalid reuse of an RPC connection.") + msg_fmt = _("Invalid reuse of an RPC connection.") class UnsupportedRpcVersion(RPCException): - message = _("Specified RPC version, %(version)s, not supported by " + msg_fmt = _("Specified RPC version, %(version)s, not supported by " "this endpoint.") class UnsupportedRpcEnvelopeVersion(RPCException): - message = _("Specified RPC envelope version, %(version)s, " + msg_fmt = _("Specified RPC envelope version, %(version)s, " "not supported by this endpoint.") class RpcVersionCapError(RPCException): - message = _("Specified RPC version cap, %(version_cap)s, is too low") + msg_fmt = _("Specified RPC version cap, %(version_cap)s, is too low") class Connection(object): -- cgit