<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import sys <EOL> import traceback <EOL> from twisted . trial . unittest import TestCase <EOL> from twisted . python . failure import Failure <EOL> from twisted . internet import defer <EOL> def getDivisionFailure ( ) : <EOL> try : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> except : <EOL> f = Failure ( ) <EOL> return f <EOL> class TwoPointFiveFailureTests ( TestCase ) : <EOL> def test_inlineCallbacksTracebacks ( self ) : <EOL> """<STR_LIT>""" <EOL> f = getDivisionFailure ( ) <EOL> d = defer . Deferred ( ) <EOL> try : <EOL> f . raiseException ( ) <EOL> except : <EOL> d . errback ( ) <EOL> failures = [ ] <EOL> def collect_error ( result ) : <EOL> failures . append ( result ) <EOL> def ic ( d ) : <EOL> yield d <EOL> ic = defer . inlineCallbacks ( ic ) <EOL> ic ( d ) . addErrback ( collect_error ) <EOL> newFailure , = failures <EOL> self . assertEqual ( <EOL> traceback . extract_tb ( newFailure . getTracebackObject ( ) ) [ - <NUM_LIT:1> ] [ - <NUM_LIT:1> ] , <EOL> "<STR_LIT>" <EOL> ) <EOL> def _throwIntoGenerator ( self , f , g ) : <EOL> try : <EOL> f . throwExceptionIntoGenerator ( g ) <EOL> except StopIteration : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def test_throwExceptionIntoGenerator ( self ) : <EOL> """<STR_LIT>""" <EOL> stuff = [ ] <EOL> def generator ( ) : <EOL> try : <EOL> yield <EOL> except : <EOL> stuff . append ( sys . exc_info ( ) ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> g = generator ( ) <EOL> f = getDivisionFailure ( ) <EOL> g . next ( ) <EOL> self . _throwIntoGenerator ( f , g ) <EOL> self . assertEqual ( stuff [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , ZeroDivisionError ) <EOL> self . assertTrue ( isinstance ( stuff [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , ZeroDivisionError ) ) <EOL> self . assertEqual ( traceback . extract_tb ( stuff [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] ) [ - <NUM_LIT:1> ] [ - <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> def test_findFailureInGenerator ( self ) : <EOL> """<STR_LIT>""" <EOL> f = getDivisionFailure ( ) <EOL> f . cleanFailure ( ) <EOL> foundFailures = [ ] <EOL> def generator ( ) : <EOL> try : <EOL> yield <EOL> except : <EOL> foundFailures . append ( Failure . _findFailure ( ) ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> g = generator ( ) <EOL> g . next ( ) <EOL> self . _throwIntoGenerator ( f , g ) <EOL> self . assertEqual ( foundFailures , [ f ] ) <EOL> def test_failureConstructionFindsOriginalFailure ( self ) : <EOL> """<STR_LIT>""" <EOL> f = getDivisionFailure ( ) <EOL> f . cleanFailure ( ) <EOL> newFailures = [ ] <EOL> def generator ( ) : <EOL> try : <EOL> yield <EOL> except : <EOL> newFailures . append ( Failure ( ) ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> g = generator ( ) <EOL> g . next ( ) <EOL> self . _throwIntoGenerator ( f , g ) <EOL> self . assertEqual ( len ( newFailures ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( newFailures [ <NUM_LIT:0> ] . getTraceback ( ) , f . getTraceback ( ) ) <EOL> def test_ambiguousFailureInGenerator ( self ) : <EOL> """<STR_LIT>""" <EOL> def generator ( ) : <EOL> try : <EOL> try : <EOL> yield <EOL> except : <EOL> [ ] [ <NUM_LIT:1> ] <EOL> except : <EOL> self . assertIsInstance ( Failure ( ) . value , IndexError ) <EOL> g = generator ( ) <EOL> g . next ( ) <EOL> f = getDivisionFailure ( ) <EOL> self . _throwIntoGenerator ( f , g ) <EOL> def test_ambiguousFailureFromGenerator ( self ) : <EOL> """<STR_LIT>""" <EOL> def generator ( ) : <EOL> try : <EOL> yield <EOL> except : <EOL> [ ] [ <NUM_LIT:1> ] <EOL> g = generator ( ) <EOL> g . next ( ) <EOL> f = getDivisionFailure ( ) <EOL> try : <EOL> self . _throwIntoGenerator ( f , g ) <EOL> except : <EOL> self . assertIsInstance ( Failure ( ) . value , IndexError ) </s>
