<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import gc <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> try : <EOL> MAXFD = os . sysconf ( '<STR_LIT>' ) <EOL> except ( AttributeError , ValueError ) : <EOL> MAXFD = <NUM_LIT> <EOL> _active = [ ] <EOL> def _cleanup ( ) : <EOL> for inst in _active [ : ] : <EOL> if inst . poll ( _deadstate = sys . maxint ) >= <NUM_LIT:0> : <EOL> try : <EOL> _active . remove ( inst ) <EOL> except ValueError : <EOL> pass <EOL> class Popen3 : <EOL> """<STR_LIT>""" <EOL> sts = - <NUM_LIT:1> <EOL> def __init__ ( self , cmd , capturestderr = False , bufsize = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> _cleanup ( ) <EOL> self . cmd = cmd <EOL> p2cread , p2cwrite = os . pipe ( ) <EOL> c2pread , c2pwrite = os . pipe ( ) <EOL> if capturestderr : <EOL> errout , errin = os . pipe ( ) <EOL> gc . disable_finalizers ( ) <EOL> try : <EOL> self . pid = os . fork ( ) <EOL> if self . pid == <NUM_LIT:0> : <EOL> os . dup2 ( p2cread , <NUM_LIT:0> ) <EOL> os . dup2 ( c2pwrite , <NUM_LIT:1> ) <EOL> if capturestderr : <EOL> os . dup2 ( errin , <NUM_LIT:2> ) <EOL> self . _run_child ( cmd ) <EOL> finally : <EOL> gc . enable_finalizers ( ) <EOL> os . close ( p2cread ) <EOL> self . tochild = os . fdopen ( p2cwrite , '<STR_LIT:w>' , bufsize ) <EOL> os . close ( c2pwrite ) <EOL> self . fromchild = os . fdopen ( c2pread , '<STR_LIT:r>' , bufsize ) <EOL> if capturestderr : <EOL> os . close ( errin ) <EOL> self . childerr = os . fdopen ( errout , '<STR_LIT:r>' , bufsize ) <EOL> else : <EOL> self . childerr = None <EOL> def __del__ ( self ) : <EOL> self . poll ( _deadstate = sys . maxint ) <EOL> if self . sts < <NUM_LIT:0> : <EOL> if _active is not None : <EOL> _active . append ( self ) <EOL> def _run_child ( self , cmd ) : <EOL> if isinstance ( cmd , basestring ) : <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT:-c>' , cmd ] <EOL> for i in xrange ( <NUM_LIT:3> , MAXFD ) : <EOL> try : <EOL> os . close ( i ) <EOL> except OSError : <EOL> pass <EOL> try : <EOL> os . execvp ( cmd [ <NUM_LIT:0> ] , cmd ) <EOL> finally : <EOL> os . _exit ( <NUM_LIT:1> ) <EOL> def poll ( self , _deadstate = None ) : <EOL> """<STR_LIT>""" <EOL> if self . sts < <NUM_LIT:0> : <EOL> try : <EOL> pid , sts = os . waitpid ( self . pid , os . WNOHANG ) <EOL> if pid == self . pid : <EOL> self . sts = sts <EOL> except os . error : <EOL> if _deadstate is not None : <EOL> self . sts = _deadstate <EOL> return self . sts <EOL> def wait ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . sts < <NUM_LIT:0> : <EOL> pid , sts = os . waitpid ( self . pid , <NUM_LIT:0> ) <EOL> assert pid == self . pid <EOL> self . sts = sts <EOL> return self . sts <EOL> class Popen4 ( Popen3 ) : <EOL> childerr = None <EOL> def __init__ ( self , cmd , bufsize = - <NUM_LIT:1> ) : <EOL> _cleanup ( ) <EOL> self . cmd = cmd <EOL> p2cread , p2cwrite = os . pipe ( ) <EOL> c2pread , c2pwrite = os . pipe ( ) <EOL> gc . disable_finalizers ( ) <EOL> try : <EOL> self . pid = os . fork ( ) <EOL> if self . pid == <NUM_LIT:0> : <EOL> os . dup2 ( p2cread , <NUM_LIT:0> ) <EOL> os . dup2 ( c2pwrite , <NUM_LIT:1> ) <EOL> os . dup2 ( c2pwrite , <NUM_LIT:2> ) <EOL> self . _run_child ( cmd ) <EOL> finally : <EOL> gc . enable_finalizers ( ) <EOL> os . close ( p2cread ) <EOL> self . tochild = os . fdopen ( p2cwrite , '<STR_LIT:w>' , bufsize ) <EOL> os . close ( c2pwrite ) <EOL> self . fromchild = os . fdopen ( c2pread , '<STR_LIT:r>' , bufsize ) <EOL> if sys . platform [ : <NUM_LIT:3> ] == "<STR_LIT>" or sys . platform == "<STR_LIT>" : <EOL> del Popen3 , Popen4 <EOL> def popen2 ( cmd , bufsize = - <NUM_LIT:1> , mode = '<STR_LIT:t>' ) : <EOL> """<STR_LIT>""" <EOL> w , r = os . popen2 ( cmd , mode , bufsize ) <EOL> return r , w <EOL> def popen3 ( cmd , bufsize = - <NUM_LIT:1> , mode = '<STR_LIT:t>' ) : <EOL> """<STR_LIT>""" <EOL> w , r , e = os . popen3 ( cmd , mode , bufsize ) <EOL> return r , w , e <EOL> def popen4 ( cmd , bufsize = - <NUM_LIT:1> , mode = '<STR_LIT:t>' ) : <EOL> """<STR_LIT>""" <EOL> w , r = os . popen4 ( cmd , mode , bufsize ) <EOL> return r , w <EOL> else : <EOL> def popen2 ( cmd , bufsize = - <NUM_LIT:1> , mode = '<STR_LIT:t>' ) : <EOL> """<STR_LIT>""" <EOL> inst = Popen3 ( cmd , False , bufsize ) <EOL> return inst . fromchild , inst . tochild <EOL> def popen3 ( cmd , bufsize = - <NUM_LIT:1> , mode = '<STR_LIT:t>' ) : <EOL> """<STR_LIT>""" <EOL> inst = Popen3 ( cmd , True , bufsize ) <EOL> return inst . fromchild , inst . tochild , inst . childerr <EOL> def popen4 ( cmd , bufsize = - <NUM_LIT:1> , mode = '<STR_LIT:t>' ) : <EOL> """<STR_LIT>""" <EOL> inst = Popen4 ( cmd , bufsize ) <EOL> return inst . fromchild , inst . tochild <EOL> __all__ . extend ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> def _test ( ) : <EOL> _cleanup ( ) <EOL> assert not _active , "<STR_LIT>" + repr ( [ c . cmd for c in _active ] ) <EOL> cmd = "<STR_LIT>" <EOL> teststr = "<STR_LIT>" <EOL> if os . name == "<STR_LIT>" : <EOL> cmd = "<STR_LIT>" <EOL> expected = teststr . strip ( ) <EOL> print "<STR_LIT>" <EOL> r , w = popen2 ( cmd ) <EOL> w . write ( teststr ) <EOL> w . close ( ) <EOL> got = r . read ( ) <EOL> if got . strip ( ) != expected : <EOL> raise ValueError ( "<STR_LIT>" % ( teststr , got ) ) <EOL> print "<STR_LIT>" <EOL> try : <EOL> r , w , e = popen3 ( [ cmd ] ) <EOL> except : <EOL> r , w , e = popen3 ( cmd ) <EOL> w . write ( teststr ) <EOL> w . close ( ) <EOL> got = r . read ( ) <EOL> if got . strip ( ) != expected : <EOL> raise ValueError ( "<STR_LIT>" % ( teststr , got ) ) <EOL> got = e . read ( ) <EOL> if got : <EOL> raise ValueError ( "<STR_LIT>" % ( got , ) ) <EOL> for inst in _active [ : ] : <EOL> inst . wait ( ) <EOL> _cleanup ( ) <EOL> if _active : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> print "<STR_LIT>" <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> _test ( ) </s>
