<s> r"""<STR_LIT>""" <EOL> try : <EOL> import greenlet <EOL> except ImportError : <EOL> greenlet = None <EOL> class IterIO ( object ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , obj ) : <EOL> try : <EOL> iterator = iter ( obj ) <EOL> except TypeError : <EOL> return IterI ( obj ) <EOL> return IterO ( iterator ) <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def tell ( self ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return self . pos <EOL> def isatty ( self ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return False <EOL> def seek ( self , pos , mode = <NUM_LIT:0> ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> raise IOError ( <NUM_LIT:9> , '<STR_LIT>' ) <EOL> def truncate ( self , size = None ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> raise IOError ( <NUM_LIT:9> , '<STR_LIT>' ) <EOL> def write ( self , s ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> raise IOError ( <NUM_LIT:9> , '<STR_LIT>' ) <EOL> def writelines ( self , list ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> raise IOError ( <NUM_LIT:9> , '<STR_LIT>' ) <EOL> def read ( self , n = - <NUM_LIT:1> ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> raise IOError ( <NUM_LIT:9> , '<STR_LIT>' ) <EOL> def readlines ( self , sizehint = <NUM_LIT:0> ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> raise IOError ( <NUM_LIT:9> , '<STR_LIT>' ) <EOL> def readline ( self , length = None ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> raise IOError ( <NUM_LIT:9> , '<STR_LIT>' ) <EOL> def flush ( self ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> raise IOError ( <NUM_LIT:9> , '<STR_LIT>' ) <EOL> def next ( self ) : <EOL> if self . closed : <EOL> raise StopIteration ( ) <EOL> line = self . readline ( ) <EOL> if not line : <EOL> raise StopIteration ( ) <EOL> return line <EOL> class IterI ( IterIO ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , func ) : <EOL> if greenlet is None : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> stream = object . __new__ ( cls ) <EOL> stream . _parent = greenlet . getcurrent ( ) <EOL> stream . _buffer = [ ] <EOL> stream . closed = False <EOL> stream . pos = <NUM_LIT:0> <EOL> def run ( ) : <EOL> func ( stream ) <EOL> stream . flush ( ) <EOL> g = greenlet . greenlet ( run , stream . _parent ) <EOL> while <NUM_LIT:1> : <EOL> rv = g . switch ( ) <EOL> if not rv : <EOL> return <EOL> yield rv [ <NUM_LIT:0> ] <EOL> def close ( self ) : <EOL> if not self . closed : <EOL> self . closed = True <EOL> def write ( self , s ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . pos += len ( s ) <EOL> self . _buffer . append ( s ) <EOL> def writelines ( self , list ) : <EOL> self . write ( '<STR_LIT>' . join ( list ) ) <EOL> def flush ( self ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> data = '<STR_LIT>' . join ( self . _buffer ) <EOL> self . _buffer = [ ] <EOL> self . _parent . switch ( ( data , ) ) <EOL> class IterO ( IterIO ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , gen ) : <EOL> self = object . __new__ ( cls ) <EOL> self . _gen = gen <EOL> self . _buf = '<STR_LIT>' <EOL> self . closed = False <EOL> self . pos = <NUM_LIT:0> <EOL> return self <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def close ( self ) : <EOL> if not self . closed : <EOL> self . closed = True <EOL> if hasattr ( self . _gen , '<STR_LIT>' ) : <EOL> self . _gen . close ( ) <EOL> def seek ( self , pos , mode = <NUM_LIT:0> ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if mode == <NUM_LIT:1> : <EOL> pos += self . pos <EOL> elif mode == <NUM_LIT:2> : <EOL> self . read ( ) <EOL> self . pos = min ( self . pos , self . pos + pos ) <EOL> return <EOL> elif mode != <NUM_LIT:0> : <EOL> raise IOError ( '<STR_LIT>' ) <EOL> buf = [ ] <EOL> try : <EOL> tmp_end_pos = len ( self . _buf ) <EOL> while pos > tmp_end_pos : <EOL> item = self . _gen . next ( ) <EOL> tmp_end_pos += len ( item ) <EOL> buf . append ( item ) <EOL> except StopIteration : <EOL> pass <EOL> if buf : <EOL> self . _buf += '<STR_LIT>' . join ( buf ) <EOL> self . pos = max ( <NUM_LIT:0> , pos ) <EOL> def read ( self , n = - <NUM_LIT:1> ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if n < <NUM_LIT:0> : <EOL> self . _buf += '<STR_LIT>' . join ( self . _gen ) <EOL> result = self . _buf [ self . pos : ] <EOL> self . pos += len ( result ) <EOL> return result <EOL> new_pos = self . pos + n <EOL> buf = [ ] <EOL> try : <EOL> tmp_end_pos = len ( self . _buf ) <EOL> while new_pos > tmp_end_pos : <EOL> item = self . _gen . next ( ) <EOL> tmp_end_pos += len ( item ) <EOL> buf . append ( item ) <EOL> except StopIteration : <EOL> pass <EOL> if buf : <EOL> self . _buf += '<STR_LIT>' . join ( buf ) <EOL> new_pos = max ( <NUM_LIT:0> , new_pos ) <EOL> try : <EOL> return self . _buf [ self . pos : new_pos ] <EOL> finally : <EOL> self . pos = min ( new_pos , len ( self . _buf ) ) <EOL> def readline ( self , length = None ) : <EOL> if self . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> nl_pos = self . _buf . find ( '<STR_LIT:\n>' , self . pos ) <EOL> buf = [ ] <EOL> try : <EOL> pos = self . pos <EOL> while nl_pos < <NUM_LIT:0> : <EOL> item = self . _gen . next ( ) <EOL> local_pos = item . find ( '<STR_LIT:\n>' ) <EOL> buf . append ( item ) <EOL> if local_pos >= <NUM_LIT:0> : <EOL> nl_pos = pos + local_pos <EOL> break <EOL> pos += len ( item ) <EOL> except StopIteration : <EOL> pass <EOL> if buf : <EOL> self . _buf += '<STR_LIT>' . join ( buf ) <EOL> if nl_pos < <NUM_LIT:0> : <EOL> new_pos = len ( self . _buf ) <EOL> else : <EOL> new_pos = nl_pos + <NUM_LIT:1> <EOL> if length is not None and self . pos + length < new_pos : <EOL> new_pos = self . pos + length <EOL> try : <EOL> return self . _buf [ self . pos : new_pos ] <EOL> finally : <EOL> self . pos = min ( new_pos , len ( self . _buf ) ) <EOL> def readlines ( self , sizehint = <NUM_LIT:0> ) : <EOL> total = <NUM_LIT:0> <EOL> lines = [ ] <EOL> line = self . readline ( ) <EOL> while line : <EOL> lines . append ( line ) <EOL> total += len ( line ) <EOL> if <NUM_LIT:0> < sizehint <= total : <EOL> break <EOL> line = self . readline ( ) <EOL> return lines </s>
