<s> """<STR_LIT>""" <EOL> import os , sys <EOL> class Capture : <EOL> def __init__ ( self , mixed_out_err = False ) : <EOL> "<STR_LIT>" <EOL> if ( not hasattr ( os , '<STR_LIT>' ) or <EOL> not hasattr ( os , '<STR_LIT>' ) or <EOL> not hasattr ( os , '<STR_LIT>' ) or <EOL> not hasattr ( os , '<STR_LIT>' ) ) : <EOL> self . dummy = <NUM_LIT:1> <EOL> else : <EOL> self . dummy = <NUM_LIT:0> <EOL> self . localoutfd = os . dup ( <NUM_LIT:1> ) <EOL> self . localerrfd = os . dup ( <NUM_LIT:2> ) <EOL> if hasattr ( sys . stdout , '<STR_LIT>' ) and sys . stdout . fileno ( ) == <NUM_LIT:1> : <EOL> self . saved_stdout = sys . stdout <EOL> sys . stdout = os . fdopen ( self . localoutfd , '<STR_LIT:w>' , <NUM_LIT:1> ) <EOL> else : <EOL> self . saved_stdout = None <EOL> if hasattr ( sys . stderr , '<STR_LIT>' ) and sys . stderr . fileno ( ) == <NUM_LIT:2> : <EOL> self . saved_stderr = sys . stderr <EOL> sys . stderr = os . fdopen ( self . localerrfd , '<STR_LIT:w>' , <NUM_LIT:0> ) <EOL> else : <EOL> self . saved_stderr = None <EOL> self . tmpout = os . tmpfile ( ) <EOL> if mixed_out_err : <EOL> self . tmperr = self . tmpout <EOL> else : <EOL> self . tmperr = os . tmpfile ( ) <EOL> os . dup2 ( self . tmpout . fileno ( ) , <NUM_LIT:1> ) <EOL> os . dup2 ( self . tmperr . fileno ( ) , <NUM_LIT:2> ) <EOL> def done ( self ) : <EOL> "<STR_LIT>" <EOL> if self . dummy : <EOL> import cStringIO <EOL> return cStringIO . StringIO ( ) , cStringIO . StringIO ( ) <EOL> else : <EOL> os . dup2 ( self . localoutfd , <NUM_LIT:1> ) <EOL> os . dup2 ( self . localerrfd , <NUM_LIT:2> ) <EOL> if self . saved_stdout is not None : <EOL> f = sys . stdout <EOL> sys . stdout = self . saved_stdout <EOL> f . close ( ) <EOL> else : <EOL> os . close ( self . localoutfd ) <EOL> if self . saved_stderr is not None : <EOL> f = sys . stderr <EOL> sys . stderr = self . saved_stderr <EOL> f . close ( ) <EOL> else : <EOL> os . close ( self . localerrfd ) <EOL> self . tmpout . seek ( <NUM_LIT:0> ) <EOL> self . tmperr . seek ( <NUM_LIT:0> ) <EOL> return self . tmpout , self . tmperr <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> c = Capture ( ) <EOL> try : <EOL> os . system ( '<STR_LIT>' ) <EOL> finally : <EOL> fout , ferr = c . done ( ) <EOL> print '<STR_LIT>' , ` fout . read ( ) ` <EOL> print '<STR_LIT>' , ` ferr . read ( ) ` </s>
