From 1c37ea475ee1a0973dffb5fa1e93040c11d831c5 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Wed, 18 Sep 2013 16:52:03 +0200 Subject: [PATCH] Remove various usage of sys.exc_info() Change-Id: I7454264b82e808c68dd182e7ce43db756651a00b --- wsme/root.py | 9 +++------ wsme/tests/protocol.py | 19 +++++-------------- wsme/tests/test_api.py | 4 +--- wsme/tests/test_types.py | 4 +--- wsme/types.py | 3 +-- wsmeext/tests/test_soap.py | 4 +--- 6 files changed, 12 insertions(+), 31 deletions(-) diff --git a/wsme/root.py b/wsme/root.py index 7eaa7b7..03561af 100644 --- a/wsme/root.py +++ b/wsme/root.py @@ -192,8 +192,7 @@ class WSRoot(object): # TODO make sure result type == a._wsme_definition.return_type return protocol.encode_result(context, result) - except Exception: - e = sys.exc_info()[1] + except Exception as e: infos = wsme.api.format_exception(sys.exc_info(), self._debug) if isinstance(e, ClientSideError): request.client_errorcount += 1 @@ -233,8 +232,7 @@ class WSRoot(object): try: msg = None protocol = self._select_protocol(request) - except Exception: - e = sys.exc_info()[1] + except Exception as e: msg = ("Error while selecting protocol: %s" % str(e)) log.exception(msg) protocol = None @@ -339,8 +337,7 @@ class WSRoot(object): return html_body % dict( css=formatter.get_style_defs(), content=highlight(content, lexer, formatter).encode('utf8')) - except Exception: - e = sys.exc_info()[1] + except Exception as e: log.warning( "Could not pygment the content because of the following " "error :\n%s" % e) diff --git a/wsme/tests/protocol.py b/wsme/tests/protocol.py index c2974dc..98f52f2 100644 --- a/wsme/tests/protocol.py +++ b/wsme/tests/protocol.py @@ -4,7 +4,6 @@ import unittest import warnings import datetime import decimal -import sys import six from six import u, b @@ -22,8 +21,7 @@ binarysample = b('\x00\xff\x43') try: 1 / 0 -except ZeroDivisionError: - e = sys.exc_info()[1] +except ZeroDivisionError as e: zerodivisionerrormsg = str(e) @@ -379,8 +377,7 @@ class ProtocolTestCase(unittest.TestCase): res = self.call('invalid_function') print(res) assert "No error raised" - except CallException: - e = sys.exc_info()[1] + except CallException as e: self.assertEquals(e.faultcode, 'Client') self.assertEquals(e.faultstring.lower(), u('unknown function name: invalid_function')) @@ -390,9 +387,7 @@ class ProtocolTestCase(unittest.TestCase): res = self.call('witherrors/divide_by_zero') print(res) assert "No error raised" - except CallException: - e = sys.exc_info()[1] - print(e) + except CallException as e: self.assertEquals(e.faultcode, 'Server') self.assertEquals(e.faultstring, zerodivisionerrormsg) assert e.debuginfo is not None @@ -403,9 +398,7 @@ class ProtocolTestCase(unittest.TestCase): res = self.call('witherrors/divide_by_zero') print(res) assert "No error raised" - except CallException: - e = sys.exc_info()[1] - print(e) + except CallException as e: self.assertEquals(e.faultcode, 'Server') self.assertEquals(e.faultstring, zerodivisionerrormsg) assert e.debuginfo is None @@ -649,9 +642,7 @@ class ProtocolTestCase(unittest.TestCase): r = self.call('argtypes/setdatetime') print(r) assert "No error raised" - except CallException: - e = sys.exc_info()[1] - print(e) + except CallException as e: self.assertEquals(e.faultcode, 'Client') self.assertEquals(e.faultstring, u('Missing argument: "value"')) diff --git a/wsme/tests/test_api.py b/wsme/tests/test_api.py index d42e1ff..d2afed0 100644 --- a/wsme/tests/test_api.py +++ b/wsme/tests/test_api.py @@ -1,7 +1,6 @@ # encoding=utf8 from six import b -import sys import unittest import webtest @@ -107,8 +106,7 @@ class TestController(unittest.TestCase): try: list(scan_api(r)) assert False, "ValueError not raised" - except ValueError: - e = sys.exc_info()[1] + except ValueError as e: assert str(e).startswith("Path is too long") def test_handle_request(self): diff --git a/wsme/tests/test_types.py b/wsme/tests/test_types.py index 31892fd..c878513 100644 --- a/wsme/tests/test_types.py +++ b/wsme/tests/test_types.py @@ -1,5 +1,4 @@ import unittest -import sys import six from wsme import types @@ -180,8 +179,7 @@ class TestTypes(unittest.TestCase): try: obj.a = 'v3' assert False, 'ValueError was not raised' - except ValueError: - e = sys.exc_info()[1] + except ValueError as e: assert str(e) == \ "a: Value 'v3' is invalid (should be one of: v1, v2)", e diff --git a/wsme/types.py b/wsme/types.py index 1856a83..41570e1 100644 --- a/wsme/types.py +++ b/wsme/types.py @@ -340,8 +340,7 @@ class wsattr(object): def __set__(self, instance, value): try: value = validate_value(self.datatype, value) - except ValueError: - e = sys.exc_info()[1] + except ValueError as e: raise ValueError("%s: %s" % (self.name, e)) dataholder = self._get_dataholder(instance) if value is Unset: diff --git a/wsmeext/tests/test_soap.py b/wsmeext/tests/test_soap.py index cb3ea6e..34dafcb 100644 --- a/wsmeext/tests/test_soap.py +++ b/wsmeext/tests/test_soap.py @@ -1,7 +1,6 @@ import decimal import datetime import base64 -import sys import six @@ -318,8 +317,7 @@ class TestSOAP(wsme.tests.protocol.ProtocolTestCase): print(kw) try: return fromsuds(_rt, m(**kw)) - except suds.WebFault: - exc = sys.exc_info()[1] + except suds.WebFault as exc: raise wsme.tests.protocol.CallException( exc.fault.faultcode, exc.fault.faultstring,