From b46248c98fd3dcafb6dba24cf17103d4e644e2e4 Mon Sep 17 00:00:00 2001 From: Kui Shi Date: Mon, 21 Oct 2013 16:58:14 +0800 Subject: [PATCH] Sync up exceptions.py from oslo oslo change: I169b13546bcd0d87353dbca5a9b64229b704fe66 Define _code_map in an expression. Partial implements: blueprint py33-support Change-Id: I4b9f5bc083ebe62739a33674d26728d4a8f1a2d6 --- .../openstack/common/apiclient/exceptions.py | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/troveclient/openstack/common/apiclient/exceptions.py b/troveclient/openstack/common/apiclient/exceptions.py index b03def77..fec8ade1 100644 --- a/troveclient/openstack/common/apiclient/exceptions.py +++ b/troveclient/openstack/common/apiclient/exceptions.py @@ -22,8 +22,11 @@ Exception definitions. """ +import inspect import sys +import six + class ClientException(Exception): """The base exception class for all exceptions this library raises. @@ -387,20 +390,12 @@ class HttpVersionNotSupported(HttpServerError): message = "HTTP Version Not Supported" -# In Python 2.4 Exception is old-style and thus doesn't have a __subclasses__() -# so we can do this: -# _code_map = dict((c.http_status, c) -# for c in HttpError.__subclasses__()) -_code_map = {} -for obj in sys.modules[__name__].__dict__.values(): - if isinstance(obj, type): - try: - http_status = obj.http_status - except AttributeError: - pass - else: - if http_status: - _code_map[http_status] = obj +# _code_map contains all the classes that have http_status attribute. +_code_map = dict( + (getattr(obj, 'http_status', None), obj) + for name, obj in six.iteritems(vars(sys.modules[__name__])) + if inspect.isclass(obj) and getattr(obj, 'http_status', False) +) def from_response(response, method, url):