<s> """<STR_LIT>""" <EOL> import sys <EOL> import traceback <EOL> import unittest <EOL> from StringIO import StringIO <EOL> from unittest2 import util <EOL> from unittest2 . compatibility import wraps <EOL> __unittest = True <EOL> def failfast ( method ) : <EOL> @ wraps ( method ) <EOL> def inner ( self , * args , ** kw ) : <EOL> if getattr ( self , '<STR_LIT>' , False ) : <EOL> self . stop ( ) <EOL> return method ( self , * args , ** kw ) <EOL> return inner <EOL> STDOUT_LINE = '<STR_LIT>' <EOL> STDERR_LINE = '<STR_LIT>' <EOL> class TestResult ( unittest . TestResult ) : <EOL> """<STR_LIT>""" <EOL> _previousTestClass = None <EOL> _moduleSetUpFailed = False <EOL> def __init__ ( self ) : <EOL> self . failfast = False <EOL> self . failures = [ ] <EOL> self . errors = [ ] <EOL> self . testsRun = <NUM_LIT:0> <EOL> self . skipped = [ ] <EOL> self . expectedFailures = [ ] <EOL> self . unexpectedSuccesses = [ ] <EOL> self . shouldStop = False <EOL> self . buffer = False <EOL> self . _stdout_buffer = None <EOL> self . _stderr_buffer = None <EOL> self . _original_stdout = sys . stdout <EOL> self . _original_stderr = sys . stderr <EOL> self . _mirrorOutput = False <EOL> def startTest ( self , test ) : <EOL> "<STR_LIT>" <EOL> self . testsRun += <NUM_LIT:1> <EOL> self . _mirrorOutput = False <EOL> if self . buffer : <EOL> if self . _stderr_buffer is None : <EOL> self . _stderr_buffer = StringIO ( ) <EOL> self . _stdout_buffer = StringIO ( ) <EOL> sys . stdout = self . _stdout_buffer <EOL> sys . stderr = self . _stderr_buffer <EOL> def startTestRun ( self ) : <EOL> """<STR_LIT>""" <EOL> def stopTest ( self , test ) : <EOL> """<STR_LIT>""" <EOL> if self . buffer : <EOL> if self . _mirrorOutput : <EOL> output = sys . stdout . getvalue ( ) <EOL> error = sys . stderr . getvalue ( ) <EOL> if output : <EOL> if not output . endswith ( '<STR_LIT:\n>' ) : <EOL> output += '<STR_LIT:\n>' <EOL> self . _original_stdout . write ( STDOUT_LINE % output ) <EOL> if error : <EOL> if not error . endswith ( '<STR_LIT:\n>' ) : <EOL> error += '<STR_LIT:\n>' <EOL> self . _original_stderr . write ( STDERR_LINE % error ) <EOL> sys . stdout = self . _original_stdout <EOL> sys . stderr = self . _original_stderr <EOL> self . _stdout_buffer . seek ( <NUM_LIT:0> ) <EOL> self . _stdout_buffer . truncate ( ) <EOL> self . _stderr_buffer . seek ( <NUM_LIT:0> ) <EOL> self . _stderr_buffer . truncate ( ) <EOL> self . _mirrorOutput = False <EOL> def stopTestRun ( self ) : <EOL> """<STR_LIT>""" <EOL> @ failfast <EOL> def addError ( self , test , err ) : <EOL> """<STR_LIT>""" <EOL> self . errors . append ( ( test , self . _exc_info_to_string ( err , test ) ) ) <EOL> self . _mirrorOutput = True <EOL> @ failfast <EOL> def addFailure ( self , test , err ) : <EOL> """<STR_LIT>""" <EOL> self . failures . append ( ( test , self . _exc_info_to_string ( err , test ) ) ) <EOL> self . _mirrorOutput = True <EOL> def addSuccess ( self , test ) : <EOL> "<STR_LIT>" <EOL> pass <EOL> def addSkip ( self , test , reason ) : <EOL> """<STR_LIT>""" <EOL> self . skipped . append ( ( test , reason ) ) <EOL> def addExpectedFailure ( self , test , err ) : <EOL> """<STR_LIT>""" <EOL> self . expectedFailures . append ( <EOL> ( test , self . _exc_info_to_string ( err , test ) ) ) <EOL> @ failfast <EOL> def addUnexpectedSuccess ( self , test ) : <EOL> """<STR_LIT>""" <EOL> self . unexpectedSuccesses . append ( test ) <EOL> def wasSuccessful ( self ) : <EOL> "<STR_LIT>" <EOL> return ( len ( self . failures ) + len ( self . errors ) == <NUM_LIT:0> ) <EOL> def stop ( self ) : <EOL> "<STR_LIT>" <EOL> self . shouldStop = True <EOL> def _exc_info_to_string ( self , err , test ) : <EOL> """<STR_LIT>""" <EOL> exctype , value , tb = err <EOL> while tb and self . _is_relevant_tb_level ( tb ) : <EOL> tb = tb . tb_next <EOL> if exctype is test . failureException : <EOL> length = self . _count_relevant_tb_levels ( tb ) <EOL> msgLines = traceback . format_exception ( exctype , value , tb , length ) <EOL> else : <EOL> msgLines = traceback . format_exception ( exctype , value , tb ) <EOL> if self . buffer : <EOL> output = sys . stdout . getvalue ( ) <EOL> error = sys . stderr . getvalue ( ) <EOL> if output : <EOL> if not output . endswith ( '<STR_LIT:\n>' ) : <EOL> output += '<STR_LIT:\n>' <EOL> msgLines . append ( STDOUT_LINE % output ) <EOL> if error : <EOL> if not error . endswith ( '<STR_LIT:\n>' ) : <EOL> error += '<STR_LIT:\n>' <EOL> msgLines . append ( STDERR_LINE % error ) <EOL> return '<STR_LIT>' . join ( msgLines ) <EOL> def _is_relevant_tb_level ( self , tb ) : <EOL> return '<STR_LIT>' in tb . tb_frame . f_globals <EOL> def _count_relevant_tb_levels ( self , tb ) : <EOL> length = <NUM_LIT:0> <EOL> while tb and not self . _is_relevant_tb_level ( tb ) : <EOL> length += <NUM_LIT:1> <EOL> tb = tb . tb_next <EOL> return length <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( util . strclass ( self . __class__ ) , self . testsRun , len ( self . errors ) , <EOL> len ( self . failures ) ) </s>
