<s> from pypy . interpreter . baseobjspace import Wrappable <EOL> from pypy . interpreter . error import OperationError <EOL> from pypy . interpreter . typedef import TypeDef , make_weakref_descr <EOL> from pypy . interpreter . gateway import interp2app , ObjSpace , W_Root <EOL> from pypy . rlib . rarithmetic import ovfcheck <EOL> class W_Count ( Wrappable ) : <EOL> def __init__ ( self , space , firstval ) : <EOL> self . space = space <EOL> self . c = firstval <EOL> def iter_w ( self ) : <EOL> return self . space . wrap ( self ) <EOL> def next_w ( self ) : <EOL> c = self . c <EOL> try : <EOL> self . c = ovfcheck ( self . c + <NUM_LIT:1> ) <EOL> except OverflowError : <EOL> raise OperationError ( self . space . w_OverflowError , <EOL> self . space . wrap ( "<STR_LIT>" ) ) <EOL> return self . space . wrap ( c ) <EOL> def repr_w ( self ) : <EOL> s = '<STR_LIT>' % ( self . c , ) <EOL> return self . space . wrap ( s ) <EOL> def W_Count___new__ ( space , w_subtype , firstval = <NUM_LIT:0> ) : <EOL> return space . wrap ( W_Count ( space , firstval ) ) <EOL> W_Count . typedef = TypeDef ( <EOL> '<STR_LIT:count>' , <EOL> __new__ = interp2app ( W_Count___new__ , unwrap_spec = [ ObjSpace , W_Root , int ] ) , <EOL> __iter__ = interp2app ( W_Count . iter_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> next = interp2app ( W_Count . next_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> __repr__ = interp2app ( W_Count . repr_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> __doc__ = """<STR_LIT>""" ) <EOL> class W_Repeat ( Wrappable ) : <EOL> def __init__ ( self , space , w_obj , w_times ) : <EOL> self . space = space <EOL> self . w_obj = w_obj <EOL> if space . is_w ( w_times , space . w_None ) : <EOL> self . counting = False <EOL> self . count = <NUM_LIT:0> <EOL> else : <EOL> self . counting = True <EOL> self . count = self . space . int_w ( w_times ) <EOL> def next_w ( self ) : <EOL> if self . counting : <EOL> if self . count <= <NUM_LIT:0> : <EOL> raise OperationError ( self . space . w_StopIteration , self . space . w_None ) <EOL> self . count -= <NUM_LIT:1> <EOL> return self . w_obj <EOL> def iter_w ( self ) : <EOL> return self . space . wrap ( self ) <EOL> def repr_w ( self ) : <EOL> objrepr = self . space . str_w ( self . space . repr ( self . w_obj ) ) <EOL> if self . counting : <EOL> s = '<STR_LIT>' % ( objrepr , self . count ) <EOL> else : <EOL> s = '<STR_LIT>' % ( objrepr , ) <EOL> return self . space . wrap ( s ) <EOL> def W_Repeat___new__ ( space , w_subtype , w_obj , w_times = None ) : <EOL> return space . wrap ( W_Repeat ( space , w_obj , w_times ) ) <EOL> W_Repeat . typedef = TypeDef ( <EOL> '<STR_LIT>' , <EOL> __new__ = interp2app ( W_Repeat___new__ , unwrap_spec = [ ObjSpace , W_Root , W_Root , W_Root ] ) , <EOL> __iter__ = interp2app ( W_Repeat . iter_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> next = interp2app ( W_Repeat . next_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> __repr__ = interp2app ( W_Repeat . repr_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> __doc__ = """<STR_LIT>""" ) <EOL> class W_TakeWhile ( Wrappable ) : <EOL> def __init__ ( self , space , w_predicate , w_iterable ) : <EOL> self . space = space <EOL> self . w_predicate = w_predicate <EOL> self . iterable = space . iter ( w_iterable ) <EOL> self . stopped = False <EOL> def iter_w ( self ) : <EOL> return self . space . wrap ( self ) <EOL> def next_w ( self ) : <EOL> if self . stopped : <EOL> raise OperationError ( self . space . w_StopIteration , self . space . w_None ) <EOL> w_obj = self . space . next ( self . iterable ) <EOL> w_bool = self . space . call_function ( self . w_predicate , w_obj ) <EOL> if not self . space . is_true ( w_bool ) : <EOL> self . stopped = True <EOL> raise OperationError ( self . space . w_StopIteration , self . space . w_None ) <EOL> return w_obj <EOL> def W_TakeWhile___new__ ( space , w_subtype , w_predicate , w_iterable ) : <EOL> return space . wrap ( W_TakeWhile ( space , w_predicate , w_iterable ) ) <EOL> W_TakeWhile . typedef = TypeDef ( <EOL> '<STR_LIT>' , <EOL> __new__ = interp2app ( W_TakeWhile___new__ , unwrap_spec = [ ObjSpace , W_Root , W_Root , W_Root ] ) , <EOL> __iter__ = interp2app ( W_TakeWhile . iter_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> next = interp2app ( W_TakeWhile . next_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> __doc__ = """<STR_LIT>""" ) <EOL> class W_DropWhile ( Wrappable ) : <EOL> def __init__ ( self , space , w_predicate , w_iterable ) : <EOL> self . space = space <EOL> self . w_predicate = w_predicate <EOL> self . iterable = space . iter ( w_iterable ) <EOL> self . started = False <EOL> def iter_w ( self ) : <EOL> return self . space . wrap ( self ) <EOL> def next_w ( self ) : <EOL> if self . started : <EOL> w_obj = self . space . next ( self . iterable ) <EOL> else : <EOL> while True : <EOL> w_obj = self . space . next ( self . iterable ) <EOL> w_bool = self . space . call_function ( self . w_predicate , w_obj ) <EOL> if not self . space . is_true ( w_bool ) : <EOL> self . started = True <EOL> break <EOL> return w_obj <EOL> def W_DropWhile___new__ ( space , w_subtype , w_predicate , w_iterable ) : <EOL> return space . wrap ( W_DropWhile ( space , w_predicate , w_iterable ) ) <EOL> W_DropWhile . typedef = TypeDef ( <EOL> '<STR_LIT>' , <EOL> __new__ = interp2app ( W_DropWhile___new__ , unwrap_spec = [ ObjSpace , W_Root , W_Root , W_Root ] ) , <EOL> __iter__ = interp2app ( W_DropWhile . iter_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> next = interp2app ( W_DropWhile . next_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> __doc__ = """<STR_LIT>""" ) <EOL> class _IFilterBase ( Wrappable ) : <EOL> def __init__ ( self , space , w_predicate , w_iterable ) : <EOL> self . space = space <EOL> if space . is_w ( w_predicate , space . w_None ) : <EOL> self . no_predicate = True <EOL> else : <EOL> self . no_predicate = False <EOL> self . w_predicate = w_predicate <EOL> self . iterable = space . iter ( w_iterable ) <EOL> def iter_w ( self ) : <EOL> return self . space . wrap ( self ) <EOL> def next_w ( self ) : <EOL> while True : <EOL> w_obj = self . space . next ( self . iterable ) <EOL> if self . no_predicate : <EOL> pred = self . space . is_true ( w_obj ) <EOL> else : <EOL> w_pred = self . space . call_function ( self . w_predicate , w_obj ) <EOL> pred = self . space . is_true ( w_pred ) <EOL> if pred ^ self . reverse : <EOL> return w_obj <EOL> class W_IFilter ( _IFilterBase ) : <EOL> reverse = False <EOL> def W_IFilter___new__ ( space , w_subtype , w_predicate , w_iterable ) : <EOL> return space . wrap ( W_IFilter ( space , w_predicate , w_iterable ) ) <EOL> W_IFilter . typedef = TypeDef ( <EOL> '<STR_LIT>' , <EOL> __new__ = interp2app ( W_IFilter___new__ , unwrap_spec = [ ObjSpace , W_Root , W_Root , W_Root ] ) , <EOL> __iter__ = interp2app ( W_IFilter . iter_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> next = interp2app ( W_IFilter . next_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> __doc__ = """<STR_LIT>""" ) <EOL> class W_IFilterFalse ( _IFilterBase ) : <EOL> reverse = True <EOL> def W_IFilterFalse___new__ ( space , w_subtype , w_predicate , w_iterable ) : <EOL> return space . wrap ( W_IFilterFalse ( space , w_predicate , w_iterable ) ) <EOL> W_IFilterFalse . typedef = TypeDef ( <EOL> '<STR_LIT>' , <EOL> __new__ = interp2app ( W_IFilterFalse___new__ , unwrap_spec = [ ObjSpace , W_Root , W_Root , W_Root ] ) , <EOL> __iter__ = interp2app ( W_IFilterFalse . iter_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> next = interp2app ( W_IFilterFalse . next_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> __doc__ = """<STR_LIT>""" ) <EOL> class W_ISlice ( Wrappable ) : <EOL> def __init__ ( self , space , w_iterable , w_startstop , args_w ) : <EOL> self . iterable = space . iter ( w_iterable ) <EOL> self . space = space <EOL> num_args = len ( args_w ) <EOL> if num_args == <NUM_LIT:0> : <EOL> start = <NUM_LIT:0> <EOL> w_stop = w_startstop <EOL> elif num_args <= <NUM_LIT:2> : <EOL> if space . is_w ( w_startstop , space . w_None ) : <EOL> start = <NUM_LIT:0> <EOL> else : <EOL> start = space . int_w ( w_startstop ) <EOL> w_stop = args_w [ <NUM_LIT:0> ] <EOL> else : <EOL> raise OperationError ( space . w_TypeError , space . wrap ( "<STR_LIT>" + str ( num_args ) + "<STR_LIT>" ) ) <EOL> if space . is_w ( w_stop , space . w_None ) : <EOL> stop = - <NUM_LIT:1> <EOL> stoppable = False <EOL> else : <EOL> stop = space . int_w ( w_stop ) <EOL> stoppable = True <EOL> if num_args == <NUM_LIT:2> : <EOL> w_step = args_w [ <NUM_LIT:1> ] <EOL> if space . is_w ( w_step , space . w_None ) : <EOL> step = <NUM_LIT:1> <EOL> else : <EOL> step = space . int_w ( w_step ) <EOL> else : <EOL> step = <NUM_LIT:1> <EOL> if start < <NUM_LIT:0> : <EOL> raise OperationError ( space . w_ValueError , space . wrap ( "<STR_LIT>" ) ) <EOL> if stoppable and stop < <NUM_LIT:0> : <EOL> raise OperationError ( space . w_ValueError , space . wrap ( "<STR_LIT>" ) ) <EOL> if step < <NUM_LIT:1> : <EOL> raise OperationError ( space . w_ValueError , space . wrap ( "<STR_LIT>" ) ) <EOL> self . start = start <EOL> self . stop = stop <EOL> self . step = step <EOL> def iter_w ( self ) : <EOL> return self . space . wrap ( self ) <EOL> def next_w ( self ) : <EOL> if self . start >= <NUM_LIT:0> : <EOL> consume = self . start + <NUM_LIT:1> <EOL> self . start = - <NUM_LIT:1> <EOL> else : <EOL> consume = self . step <EOL> if self . stop >= <NUM_LIT:0> : <EOL> if self . stop < consume : <EOL> raise OperationError ( self . space . w_StopIteration , <EOL> self . space . w_None ) <EOL> self . stop -= consume <EOL> while True : <EOL> w_obj = self . space . next ( self . iterable ) <EOL> consume -= <NUM_LIT:1> <EOL> if consume <= <NUM_LIT:0> : <EOL> return w_obj <EOL> def W_ISlice___new__ ( space , w_subtype , w_iterable , w_startstop , args_w ) : <EOL> return space . wrap ( W_ISlice ( space , w_iterable , w_startstop , args_w ) ) <EOL> W_ISlice . typedef = TypeDef ( <EOL> '<STR_LIT>' , <EOL> __new__ = interp2app ( W_ISlice___new__ , unwrap_spec = [ ObjSpace , W_Root , W_Root , W_Root , '<STR_LIT>' ] ) , <EOL> __iter__ = interp2app ( W_ISlice . iter_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> next = interp2app ( W_ISlice . next_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> __doc__ = """<STR_LIT>""" ) <EOL> class W_Chain ( Wrappable ) : <EOL> def __init__ ( self , space , args_w ) : <EOL> self . space = space <EOL> iterators_w = [ ] <EOL> i = <NUM_LIT:0> <EOL> for iterable_w in args_w : <EOL> try : <EOL> iterator_w = space . iter ( iterable_w ) <EOL> except OperationError , e : <EOL> if e . match ( self . space , self . space . w_TypeError ) : <EOL> raise OperationError ( space . w_TypeError , space . wrap ( "<STR_LIT>" + str ( i + <NUM_LIT:1> ) + "<STR_LIT>" ) ) <EOL> else : <EOL> raise <EOL> else : <EOL> iterators_w . append ( iterator_w ) <EOL> i += <NUM_LIT:1> <EOL> self . iterators_w = iterators_w <EOL> self . current_iterator = <NUM_LIT:0> <EOL> self . num_iterators = len ( iterators_w ) <EOL> self . started = False <EOL> def iter_w ( self ) : <EOL> return self . space . wrap ( self ) <EOL> def next_w ( self ) : <EOL> if self . current_iterator >= self . num_iterators : <EOL> raise OperationError ( self . space . w_StopIteration , self . space . w_None ) <EOL> if not self . started : <EOL> self . current_iterator = <NUM_LIT:0> <EOL> self . w_it = self . iterators_w [ self . current_iterator ] <EOL> self . started = True <EOL> while True : <EOL> try : <EOL> w_obj = self . space . next ( self . w_it ) <EOL> except OperationError , e : <EOL> if e . match ( self . space , self . space . w_StopIteration ) : <EOL> self . current_iterator += <NUM_LIT:1> <EOL> if self . current_iterator >= self . num_iterators : <EOL> raise OperationError ( self . space . w_StopIteration , self . space . w_None ) <EOL> else : <EOL> self . w_it = self . iterators_w [ self . current_iterator ] <EOL> else : <EOL> raise <EOL> else : <EOL> break <EOL> return w_obj <EOL> def W_Chain___new__ ( space , w_subtype , args_w ) : <EOL> return space . wrap ( W_Chain ( space , args_w ) ) <EOL> W_Chain . typedef = TypeDef ( <EOL> '<STR_LIT>' , <EOL> __new__ = interp2app ( W_Chain___new__ , unwrap_spec = [ ObjSpace , W_Root , '<STR_LIT>' ] ) , <EOL> __iter__ = interp2app ( W_Chain . iter_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> next = interp2app ( W_Chain . next_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> __doc__ = """<STR_LIT>""" ) <EOL> class W_IMap ( Wrappable ) : <EOL> _error_name = "<STR_LIT>" <EOL> def __init__ ( self , space , w_fun , args_w ) : <EOL> self . space = space <EOL> self . identity_fun = ( self . space . is_w ( w_fun , space . w_None ) ) <EOL> self . w_fun = w_fun <EOL> iterators_w = [ ] <EOL> i = <NUM_LIT:0> <EOL> for iterable_w in args_w : <EOL> try : <EOL> iterator_w = space . iter ( iterable_w ) <EOL> except OperationError , e : <EOL> if e . match ( self . space , self . space . w_TypeError ) : <EOL> raise OperationError ( space . w_TypeError , space . wrap ( self . _error_name + "<STR_LIT>" + str ( i + <NUM_LIT:1> ) + "<STR_LIT>" ) ) <EOL> else : <EOL> raise <EOL> else : <EOL> iterators_w . append ( iterator_w ) <EOL> i += <NUM_LIT:1> <EOL> self . iterators_w = iterators_w <EOL> def iter_w ( self ) : <EOL> return self . space . wrap ( self ) <EOL> def next_w ( self ) : <EOL> w_objects = self . space . newtuple ( [ self . space . next ( w_it ) for w_it in self . iterators_w ] ) <EOL> if self . identity_fun : <EOL> return w_objects <EOL> else : <EOL> return self . space . call ( self . w_fun , w_objects ) <EOL> def W_IMap___new__ ( space , w_subtype , w_fun , args_w ) : <EOL> if len ( args_w ) == <NUM_LIT:0> : <EOL> raise OperationError ( space . w_TypeError , <EOL> space . wrap ( "<STR_LIT>" ) ) <EOL> return space . wrap ( W_IMap ( space , w_fun , args_w ) ) <EOL> W_IMap . typedef = TypeDef ( <EOL> '<STR_LIT>' , <EOL> __new__ = interp2app ( W_IMap___new__ , unwrap_spec = [ ObjSpace , W_Root , W_Root , '<STR_LIT>' ] ) , <EOL> __iter__ = interp2app ( W_IMap . iter_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> next = interp2app ( W_IMap . next_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> __doc__ = """<STR_LIT>""" ) <EOL> class W_IZip ( W_IMap ) : <EOL> _error_name = "<STR_LIT>" <EOL> def next_w ( self ) : <EOL> if len ( self . iterators_w ) == <NUM_LIT:0> : <EOL> raise OperationError ( self . space . w_StopIteration , self . space . w_None ) <EOL> return W_IMap . next_w ( self ) <EOL> def W_IZip___new__ ( space , w_subtype , args_w ) : <EOL> return space . wrap ( W_IZip ( space , space . w_None , args_w ) ) <EOL> W_IZip . typedef = TypeDef ( <EOL> '<STR_LIT>' , <EOL> __new__ = interp2app ( W_IZip___new__ , unwrap_spec = [ ObjSpace , W_Root , '<STR_LIT>' ] ) , <EOL> __iter__ = interp2app ( W_IZip . iter_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> next = interp2app ( W_IZip . next_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> __doc__ = """<STR_LIT>""" ) <EOL> class W_Cycle ( Wrappable ) : <EOL> def __init__ ( self , space , w_iterable ) : <EOL> self . space = space <EOL> self . saved_w = [ ] <EOL> self . w_iterable = space . iter ( w_iterable ) <EOL> self . index = <NUM_LIT:0> <EOL> self . exhausted = False <EOL> def iter_w ( self ) : <EOL> return self . space . wrap ( self ) <EOL> def next_w ( self ) : <EOL> if self . exhausted : <EOL> if not self . saved_w : <EOL> raise OperationError ( self . space . w_StopIteration , self . space . w_None ) <EOL> try : <EOL> w_obj = self . saved_w [ self . index ] <EOL> except IndexError : <EOL> self . index = <NUM_LIT:1> <EOL> w_obj = self . saved_w [ <NUM_LIT:0> ] <EOL> else : <EOL> self . index += <NUM_LIT:1> <EOL> else : <EOL> try : <EOL> w_obj = self . space . next ( self . w_iterable ) <EOL> except OperationError , e : <EOL> if e . match ( self . space , self . space . w_StopIteration ) : <EOL> self . exhausted = True <EOL> if not self . saved_w : <EOL> raise <EOL> self . index = <NUM_LIT:1> <EOL> w_obj = self . saved_w [ <NUM_LIT:0> ] <EOL> else : <EOL> raise <EOL> else : <EOL> self . index += <NUM_LIT:1> <EOL> self . saved_w . append ( w_obj ) <EOL> return w_obj <EOL> def W_Cycle___new__ ( space , w_subtype , w_iterable ) : <EOL> return space . wrap ( W_Cycle ( space , w_iterable ) ) <EOL> W_Cycle . typedef = TypeDef ( <EOL> '<STR_LIT>' , <EOL> __new__ = interp2app ( W_Cycle___new__ , unwrap_spec = [ ObjSpace , W_Root , W_Root ] ) , <EOL> __iter__ = interp2app ( W_Cycle . iter_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> next = interp2app ( W_Cycle . next_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> __doc__ = """<STR_LIT>""" ) <EOL> class W_StarMap ( Wrappable ) : <EOL> def __init__ ( self , space , w_fun , w_iterable ) : <EOL> self . space = space <EOL> self . w_fun = w_fun <EOL> self . w_iterable = self . space . iter ( w_iterable ) <EOL> def iter_w ( self ) : <EOL> return self . space . wrap ( self ) <EOL> def next_w ( self ) : <EOL> w_obj = self . space . next ( self . w_iterable ) <EOL> if not self . space . is_true ( self . space . isinstance ( w_obj , self . space . w_tuple ) ) : <EOL> raise OperationError ( self . space . w_TypeError , self . space . wrap ( "<STR_LIT>" ) ) <EOL> return self . space . call ( self . w_fun , w_obj ) <EOL> def W_StarMap___new__ ( space , w_subtype , w_fun , w_iterable ) : <EOL> return space . wrap ( W_StarMap ( space , w_fun , w_iterable ) ) <EOL> W_StarMap . typedef = TypeDef ( <EOL> '<STR_LIT>' , <EOL> __new__ = interp2app ( W_StarMap___new__ , unwrap_spec = [ ObjSpace , W_Root , W_Root , W_Root ] ) , <EOL> __iter__ = interp2app ( W_StarMap . iter_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> next = interp2app ( W_StarMap . next_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> __doc__ = """<STR_LIT>""" ) <EOL> def tee ( space , w_iterable , n = <NUM_LIT:2> ) : <EOL> """<STR_LIT>""" <EOL> if n < <NUM_LIT:0> : <EOL> raise OperationError ( space . w_ValueError , space . wrap ( "<STR_LIT>" ) ) <EOL> myiter = space . interpclass_w ( w_iterable ) <EOL> if isinstance ( myiter , W_TeeIterable ) : <EOL> tee_state = myiter . tee_state <EOL> iterators_w = [ w_iterable ] * n <EOL> for i in range ( <NUM_LIT:1> , n ) : <EOL> iterators_w [ i ] = space . wrap ( W_TeeIterable ( space , tee_state ) ) <EOL> else : <EOL> tee_state = TeeState ( space , w_iterable ) <EOL> iterators_w = [ space . wrap ( W_TeeIterable ( space , tee_state ) ) for x in range ( n ) ] <EOL> return space . newtuple ( iterators_w ) <EOL> tee . unwrap_spec = [ ObjSpace , W_Root , int ] <EOL> class TeeState ( object ) : <EOL> def __init__ ( self , space , w_iterable ) : <EOL> self . space = space <EOL> self . w_iterable = self . space . iter ( w_iterable ) <EOL> self . num_saved = <NUM_LIT:0> <EOL> self . saved_w = [ ] <EOL> def get_next ( self , index ) : <EOL> if index >= self . num_saved : <EOL> w_obj = self . space . next ( self . w_iterable ) <EOL> self . saved_w . append ( w_obj ) <EOL> self . num_saved += <NUM_LIT:1> <EOL> return w_obj <EOL> else : <EOL> return self . saved_w [ index ] <EOL> class W_TeeIterable ( Wrappable ) : <EOL> def __init__ ( self , space , tee_state ) : <EOL> self . space = space <EOL> self . tee_state = tee_state <EOL> self . index = <NUM_LIT:0> <EOL> def iter_w ( self ) : <EOL> return self . space . wrap ( self ) <EOL> def next_w ( self ) : <EOL> try : <EOL> w_obj = self . tee_state . get_next ( self . index ) <EOL> return w_obj <EOL> finally : <EOL> self . index += <NUM_LIT:1> <EOL> def W_TeeIterable___new__ ( space , w_subtype , w_iterable ) : <EOL> myiter = space . interp_w ( W_TeeIterable , w_iterable ) <EOL> tee_state = myiter . tee_state <EOL> return space . wrap ( W_TeeIterable ( space , tee_state ) ) <EOL> W_TeeIterable . typedef = TypeDef ( <EOL> '<STR_LIT>' , <EOL> __new__ = interp2app ( W_TeeIterable___new__ , unwrap_spec = [ ObjSpace , <EOL> W_Root , <EOL> W_Root ] ) , <EOL> __iter__ = interp2app ( W_TeeIterable . iter_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> next = interp2app ( W_TeeIterable . next_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> __weakref__ = make_weakref_descr ( W_TeeIterable ) , <EOL> ) <EOL> W_TeeIterable . typedef . acceptable_as_base_class = False <EOL> class W_GroupBy ( Wrappable ) : <EOL> def __init__ ( self , space , w_iterable , w_fun ) : <EOL> self . space = space <EOL> self . w_iterable = self . space . iter ( w_iterable ) <EOL> self . identity_fun = self . space . is_w ( w_fun , self . space . w_None ) <EOL> self . w_fun = w_fun <EOL> self . index = <NUM_LIT:0> <EOL> self . lookahead = False <EOL> self . exhausted = False <EOL> self . started = False <EOL> self . new_group = True <EOL> self . w_lookahead = self . space . w_None <EOL> self . w_key = self . space . w_None <EOL> def iter_w ( self ) : <EOL> return self . space . wrap ( self ) <EOL> def next_w ( self ) : <EOL> if self . exhausted : <EOL> raise OperationError ( self . space . w_StopIteration , self . space . w_None ) <EOL> if not self . new_group : <EOL> try : <EOL> while True : <EOL> self . group_next ( self . index ) <EOL> except StopIteration : <EOL> pass <EOL> if self . exhausted : <EOL> raise OperationError ( self . space . w_StopIteration , self . space . w_None ) <EOL> if not self . started : <EOL> self . started = True <EOL> try : <EOL> w_obj = self . space . next ( self . w_iterable ) <EOL> except OperationError , e : <EOL> if e . match ( self . space , self . space . w_StopIteration ) : <EOL> self . exhausted = True <EOL> raise <EOL> else : <EOL> self . w_lookahead = w_obj <EOL> if self . identity_fun : <EOL> self . w_key = w_obj <EOL> else : <EOL> self . w_key = self . space . call_function ( self . w_fun , w_obj ) <EOL> self . lookahead = True <EOL> self . new_group = False <EOL> w_iterator = self . space . wrap ( W_GroupByIterator ( self . space , self . index , self ) ) <EOL> return self . space . newtuple ( [ self . w_key , w_iterator ] ) <EOL> def group_next ( self , group_index ) : <EOL> if group_index < self . index : <EOL> raise StopIteration <EOL> else : <EOL> if self . lookahead : <EOL> self . lookahead = False <EOL> return self . w_lookahead <EOL> try : <EOL> w_obj = self . space . next ( self . w_iterable ) <EOL> except OperationError , e : <EOL> if e . match ( self . space , self . space . w_StopIteration ) : <EOL> self . exhausted = True <EOL> raise StopIteration <EOL> else : <EOL> raise <EOL> else : <EOL> if self . identity_fun : <EOL> w_new_key = w_obj <EOL> else : <EOL> w_new_key = self . space . call_function ( self . w_fun , w_obj ) <EOL> if self . space . eq_w ( self . w_key , w_new_key ) : <EOL> return w_obj <EOL> else : <EOL> self . index += <NUM_LIT:1> <EOL> self . w_lookahead = w_obj <EOL> self . w_key = w_new_key <EOL> self . lookahead = True <EOL> self . new_group = True <EOL> raise StopIteration <EOL> def W_GroupBy___new__ ( space , w_subtype , w_iterable , w_key = None ) : <EOL> return space . wrap ( W_GroupBy ( space , w_iterable , w_key ) ) <EOL> W_GroupBy . typedef = TypeDef ( <EOL> '<STR_LIT>' , <EOL> __new__ = interp2app ( W_GroupBy___new__ , unwrap_spec = [ ObjSpace , W_Root , W_Root , W_Root ] ) , <EOL> __iter__ = interp2app ( W_GroupBy . iter_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> next = interp2app ( W_GroupBy . next_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> __doc__ = """<STR_LIT>""" ) <EOL> class W_GroupByIterator ( Wrappable ) : <EOL> def __init__ ( self , space , index , groupby ) : <EOL> self . space = space <EOL> self . index = index <EOL> self . groupby = groupby <EOL> self . exhausted = False <EOL> def iter_w ( self ) : <EOL> return self . space . wrap ( self ) <EOL> def next_w ( self ) : <EOL> if self . exhausted : <EOL> raise OperationError ( self . space . w_StopIteration , self . space . w_None ) <EOL> try : <EOL> w_obj = self . groupby . group_next ( self . index ) <EOL> except StopIteration : <EOL> self . exhausted = True <EOL> raise OperationError ( self . space . w_StopIteration , self . space . w_None ) <EOL> else : <EOL> return w_obj <EOL> W_GroupByIterator . typedef = TypeDef ( <EOL> '<STR_LIT>' , <EOL> __iter__ = interp2app ( W_GroupByIterator . iter_w , unwrap_spec = [ '<STR_LIT>' ] ) , <EOL> next = interp2app ( W_GroupByIterator . next_w , unwrap_spec = [ '<STR_LIT>' ] ) ) <EOL> W_GroupByIterator . typedef . acceptable_as_base_class = False </s>
