1
# Copyright (c) 2001-2007 Twisted Matrix Laboratories.
2
# See LICENSE for details.
6
Python 2.5 test cases for failures thrown into generators.
12
from twisted.trial.unittest import TestCase
14
from twisted.python.failure import Failure
15
from twisted.test.test_failure import getDivisionFailure
16
from twisted.internet import defer
19
class TwoPointFiveFailureTests(TestCase):
21
def test_inlineCallbacksTracebacks(self):
23
inlineCallbacks that re-raise tracebacks into their deferred
24
should not lose their tracebacsk.
26
f = getDivisionFailure()
34
def collect_error(result):
35
failures.append(result)
39
ic = defer.inlineCallbacks(ic)
40
ic(d).addErrback(collect_error)
42
newFailure, = failures
44
traceback.extract_tb(newFailure.getTracebackObject())[-1][-1],
49
def _throwIntoGenerator(self, f, g):
51
f.throwExceptionIntoGenerator(g)
55
self.fail("throwExceptionIntoGenerator should have raised "
58
def test_throwExceptionIntoGenerator(self):
60
It should be possible to throw the exception that a Failure
61
represents into a generator.
68
stuff.append(sys.exc_info())
70
self.fail("Yield should have yielded exception.")
72
f = getDivisionFailure()
74
self._throwIntoGenerator(f, g)
76
self.assertEquals(stuff[0][0], ZeroDivisionError)
77
self.assertTrue(isinstance(stuff[0][1], ZeroDivisionError))
79
self.assertEquals(traceback.extract_tb(stuff[0][2])[-1][-1], "1/0")
82
def test_findFailureInGenerator(self):
84
Within an exception handler, it should be possible to find the
85
original Failure that caused the current exception (if it was
86
caused by throwExceptionIntoGenerator).
88
f = getDivisionFailure()
96
foundFailures.append(Failure._findFailure())
98
self.fail("No exception sent to generator")
102
self._throwIntoGenerator(f, g)
104
self.assertEqual(foundFailures, [f])
107
def test_failureConstructionFindsOriginalFailure(self):
109
When a Failure is constructed in the context of an exception
110
handler that is handling an exception raised by
111
throwExceptionIntoGenerator, the new Failure should be chained to that
114
f = getDivisionFailure()
123
newFailures.append(Failure())
125
self.fail("No exception sent to generator")
128
self._throwIntoGenerator(f, g)
130
self.assertEqual(len(newFailures), 1)
131
self.assertEqual(newFailures[0].getTraceback(), f.getTraceback())
133
def test_ambiguousFailureInGenerator(self):
135
When a generator reraises a different exception,
136
L{Failure._findFailure} inside the generator should find the reraised
137
exception rather than original one.
146
self.assertIsInstance(Failure().value, IndexError)
149
f = getDivisionFailure()
150
self._throwIntoGenerator(f, g)
152
def test_ambiguousFailureFromGenerator(self):
154
When a generator reraises a different exception,
155
L{Failure._findFailure} above the generator should find the reraised
156
exception rather than original one.
165
f = getDivisionFailure()
167
self._throwIntoGenerator(f, g)
169
self.assertIsInstance(Failure().value, IndexError)