<s> """<STR_LIT>""" <EOL> from pypy . objspace . proxy import patch_space_in_place <EOL> from pypy . interpreter import gateway , baseobjspace , argument <EOL> from pypy . interpreter . error import OperationError <EOL> from pypy . interpreter . function import Method <EOL> baseobjspace . W_Root . w_thunkalias = None <EOL> baseobjspace . W_Root . __slots__ += ( '<STR_LIT>' , ) <EOL> class W_Thunk ( baseobjspace . W_Root , object ) : <EOL> def __init__ ( w_self , w_callable , args ) : <EOL> w_self . w_callable = w_callable <EOL> w_self . args = args <EOL> w_self . operr = None <EOL> w_NOT_COMPUTED_THUNK = W_Thunk ( None , None ) <EOL> W_Thunk . w_thunkalias = w_NOT_COMPUTED_THUNK <EOL> def _force ( space , w_self ) : <EOL> w_alias = w_self . w_thunkalias <EOL> while w_alias is not None : <EOL> if w_alias is w_NOT_COMPUTED_THUNK : <EOL> assert isinstance ( w_self , W_Thunk ) <EOL> if w_self . operr is not None : <EOL> raise w_self . operr <EOL> w_callable = w_self . w_callable <EOL> args = w_self . args <EOL> if w_callable is None or args is None : <EOL> raise OperationError ( space . w_RuntimeError , <EOL> space . wrap ( "<STR_LIT>" ) ) <EOL> w_self . w_callable = None <EOL> w_self . args = None <EOL> try : <EOL> w_alias = space . call_args ( w_callable , args ) <EOL> except OperationError , operr : <EOL> w_self . operr = operr <EOL> raise <EOL> if _is_circular ( w_self , w_alias ) : <EOL> operr = OperationError ( space . w_RuntimeError , <EOL> space . wrap ( "<STR_LIT>" ) ) <EOL> w_self . operr = operr <EOL> raise operr <EOL> w_self . w_thunkalias = w_alias <EOL> w_self = w_alias <EOL> w_alias = w_self . w_thunkalias <EOL> return w_self <EOL> def _is_circular ( w_obj , w_alias ) : <EOL> assert ( w_obj . w_thunkalias is None or <EOL> w_obj . w_thunkalias is w_NOT_COMPUTED_THUNK ) <EOL> while <NUM_LIT:1> : <EOL> if w_obj is w_alias : <EOL> return True <EOL> w_next = w_alias . w_thunkalias <EOL> if w_next is None : <EOL> return False <EOL> if w_next is w_NOT_COMPUTED_THUNK : <EOL> return False <EOL> w_alias = w_next <EOL> def force ( space , w_self ) : <EOL> if w_self . w_thunkalias is not None : <EOL> w_self = _force ( space , w_self ) <EOL> return w_self <EOL> def thunk ( w_callable , __args__ ) : <EOL> """<STR_LIT>""" <EOL> return W_Thunk ( w_callable , __args__ ) <EOL> app_thunk = gateway . interp2app ( thunk , unwrap_spec = [ baseobjspace . W_Root , <EOL> argument . Arguments ] ) <EOL> def is_thunk ( space , w_obj ) : <EOL> """<STR_LIT>""" <EOL> while <NUM_LIT:1> : <EOL> w_alias = w_obj . w_thunkalias <EOL> if w_alias is None : <EOL> return space . w_False <EOL> if w_alias is w_NOT_COMPUTED_THUNK : <EOL> return space . w_True <EOL> w_obj = w_alias <EOL> app_is_thunk = gateway . interp2app ( is_thunk ) <EOL> def become ( space , w_target , w_source ) : <EOL> """<STR_LIT>""" <EOL> w_target = force ( space , w_target ) <EOL> if not _is_circular ( w_target , w_source ) : <EOL> w_target . w_thunkalias = w_source <EOL> return space . w_None <EOL> app_become = gateway . interp2app ( become ) <EOL> def lazy ( space , w_callable ) : <EOL> """<STR_LIT>""" <EOL> meth = Method ( space , space . w_fn_thunk , <EOL> w_callable , space . type ( w_callable ) ) <EOL> return space . wrap ( meth ) <EOL> app_lazy = gateway . interp2app ( lazy ) <EOL> nb_forcing_args = { } <EOL> def setup ( ) : <EOL> nb_forcing_args . update ( { <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> } ) <EOL> for opname , _ , arity , _ in baseobjspace . ObjSpace . MethodTable : <EOL> nb_forcing_args . setdefault ( opname , arity ) <EOL> for opname in baseobjspace . ObjSpace . IrregularOpTable : <EOL> assert opname in nb_forcing_args , "<STR_LIT>" % opname <EOL> setup ( ) <EOL> del setup <EOL> def proxymaker ( space , opname , parentfn ) : <EOL> nb_args = nb_forcing_args [ opname ] <EOL> if nb_args == <NUM_LIT:0> : <EOL> proxy = None <EOL> elif nb_args == <NUM_LIT:1> : <EOL> def proxy ( w1 , * extra ) : <EOL> w1 = force ( space , w1 ) <EOL> return parentfn ( w1 , * extra ) <EOL> elif nb_args == <NUM_LIT:2> : <EOL> def proxy ( w1 , w2 , * extra ) : <EOL> w1 = force ( space , w1 ) <EOL> w2 = force ( space , w2 ) <EOL> return parentfn ( w1 , w2 , * extra ) <EOL> elif nb_args == <NUM_LIT:3> : <EOL> def proxy ( w1 , w2 , w3 , * extra ) : <EOL> w1 = force ( space , w1 ) <EOL> w2 = force ( space , w2 ) <EOL> w3 = force ( space , w3 ) <EOL> return parentfn ( w1 , w2 , w3 , * extra ) <EOL> elif nb_args == <NUM_LIT:4> : <EOL> def proxy ( w1 , w2 , w3 , w4 , * extra ) : <EOL> w1 = force ( space , w1 ) <EOL> w2 = force ( space , w2 ) <EOL> w3 = force ( space , w3 ) <EOL> w4 = force ( space , w4 ) <EOL> return parentfn ( w1 , w2 , w3 , w4 , * extra ) <EOL> else : <EOL> raise NotImplementedError ( "<STR_LIT>" % <EOL> ( opname , nb_args ) ) <EOL> return proxy <EOL> def Space ( * args , ** kwds ) : <EOL> from pypy . objspace import std <EOL> space = std . Space ( * args , ** kwds ) <EOL> patch_space_in_place ( space , '<STR_LIT>' , proxymaker ) <EOL> space . resolve_target = lambda w_arg : _force ( space , w_arg ) <EOL> w___pypy__ = space . getbuiltinmodule ( "<STR_LIT>" ) <EOL> space . w_fn_thunk = space . wrap ( app_thunk ) <EOL> space . setattr ( w___pypy__ , space . wrap ( '<STR_LIT>' ) , <EOL> space . w_fn_thunk ) <EOL> space . setattr ( w___pypy__ , space . wrap ( '<STR_LIT>' ) , <EOL> space . wrap ( app_is_thunk ) ) <EOL> space . setattr ( w___pypy__ , space . wrap ( '<STR_LIT>' ) , <EOL> space . wrap ( app_become ) ) <EOL> space . setattr ( w___pypy__ , space . wrap ( '<STR_LIT>' ) , <EOL> space . wrap ( app_lazy ) ) <EOL> return space </s>
