Part of twisted.python.failure.Failure View Source
This allows you to trap a Failure in an error callback. It will be automatically re-raised if it is not a type that you expect.
The reason for having this particular API is because it's very useful in Deferred errback chains:
def _ebFoo(self, failure): r = failure.trap(Spam, Eggs) print 'The Failure is due to either Spam or Eggs!' if r == Spam: print 'Spam did it!' elif r == Eggs: print 'Eggs did it!'
If the failure is not a Spam or an Eggs, then the Failure will be 'passed on' to the next errback. In Python 2 the Failure will be raised; in Python 3 the underlying exception will be re-raised.