Add possibility to create Failure from exception

In some cases it's great to have possibility to create
misc.Failure object from an exception directly. This
commit adds the 'from_exception' class method to create
misc.Failure in such way.

Change-Id: I44ae6720196b7bbe1e4ad538018487f0761a1af5
This commit is contained in:
Stanislav Kudriashev
2014-01-16 15:57:11 +02:00
parent 799aae0ffb
commit cf80ab1b12
2 changed files with 11 additions and 0 deletions

View File

@@ -99,6 +99,13 @@ class ReCreatedFailureTestCase(test.TestCase, GeneralFailureObjTestsMixin):
self.assertIs(exc.check(RuntimeError), RuntimeError)
class FromExceptionTestCase(test.TestCase, GeneralFailureObjTestsMixin):
def setUp(self):
super(FromExceptionTestCase, self).setUp()
self.fail_obj = misc.Failure.from_exception(RuntimeError('Woot!'))
class FailureObjectTestCase(test.TestCase):
def test_dont_catch_base_exception(self):

View File

@@ -416,6 +416,10 @@ class Failure(object):
'Failure.__init__ got unexpected keyword argument(s): %s'
% ', '.join(six.iterkeys(kwargs)))
@classmethod
def from_exception(cls, exception):
return cls((type(exception), exception, None))
def _matches(self, other):
if self is other:
return True