Merge "Add excutils.save_and_reraise_exception force_reraise + capture"

This commit is contained in:
Jenkins 2016-01-25 16:50:54 +00:00 committed by Gerrit Code Review
commit 866ce629bc
2 changed files with 52 additions and 3 deletions

View File

@ -187,10 +187,26 @@ class save_and_reraise_exception(object):
if logger is None:
logger = logging.getLogger()
self.logger = logger
self.type_, self.value, self.tb = (None, None, None)
def force_reraise(self):
if self.type_ is None and self.value is None:
raise RuntimeError("There is no (currently) captured exception"
" to force the reraising of")
six.reraise(self.type_, self.value, self.tb)
def capture(self, check=True):
(type_, value, tb) = sys.exc_info()
if check and type_ is None and value is None:
raise RuntimeError("There is no active exception to capture")
self.type_, self.value, self.tb = (type_, value, tb)
return self
def __enter__(self):
self.type_, self.value, self.tb, = sys.exc_info()
return self
# TODO(harlowja): perhaps someday in the future turn check here
# to true, because that is likely the desired intention, and doing
# so ensures that people are actually using this correctly.
return self.capture(check=False)
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type is not None:
@ -201,7 +217,7 @@ class save_and_reraise_exception(object):
self.tb))
return False
if self.reraise:
six.reraise(self.type_, self.value, self.tb)
self.force_reraise()
def forever_retry_uncaught_exceptions(*args, **kwargs):

View File

@ -63,6 +63,39 @@ class CausedByTest(test_base.BaseTestCase):
class SaveAndReraiseTest(test_base.BaseTestCase):
def test_save_and_reraise_exception_forced(self):
def _force_reraise():
try:
raise IOError("I broke")
except Exception:
with excutils.save_and_reraise_exception() as e:
e.reraise = False
e.force_reraise()
self.assertRaises(IOError, _force_reraise)
def test_save_and_reraise_exception_capture_reraise(self):
def _force_reraise():
try:
raise IOError("I broke")
except Exception:
excutils.save_and_reraise_exception().capture().force_reraise()
self.assertRaises(IOError, _force_reraise)
def test_save_and_reraise_exception_capture_not_active(self):
e = excutils.save_and_reraise_exception()
self.assertRaises(RuntimeError, e.capture, check=True)
def test_save_and_reraise_exception_forced_not_active(self):
e = excutils.save_and_reraise_exception()
self.assertRaises(RuntimeError, e.force_reraise)
e = excutils.save_and_reraise_exception()
e.capture(check=False)
self.assertRaises(RuntimeError, e.force_reraise)
def test_save_and_reraise_exception(self):
e = None
msg = 'foo'