<s> """<STR_LIT>""" <EOL> from pypy . interpreter . baseobjspace import Wrappable , UnpackValueError <EOL> from pypy . interpreter . argument import Arguments <EOL> from pypy . interpreter . typedef import GetSetProperty , TypeDef <EOL> from pypy . interpreter . typedef import interp_attrproperty , interp_attrproperty_w <EOL> from pypy . interpreter . gateway import interp2app , ObjSpace , W_Root <EOL> from pypy . interpreter . error import OperationError <EOL> from pypy . interpreter . function import StaticMethod <EOL> from pypy . module . _stackless . stackless_flags import StacklessFlags <EOL> from pypy . rlib . rcoroutine import Coroutine , BaseCoState , AbstractThunk <EOL> from pypy . rlib import rstack <EOL> from pypy . tool import stdlib_opcode as pythonopcode <EOL> class _AppThunk ( AbstractThunk ) : <EOL> def __init__ ( self , space , costate , w_obj , args ) : <EOL> self . space = space <EOL> self . costate = costate <EOL> if not space . is_true ( space . callable ( w_obj ) ) : <EOL> raise OperationError ( <EOL> space . w_TypeError , <EOL> space . mod ( space . wrap ( '<STR_LIT>' ) , <EOL> space . newtuple ( [ w_obj ] ) ) ) <EOL> self . w_func = w_obj <EOL> self . args = args <EOL> def call ( self ) : <EOL> costate = self . costate <EOL> w_result = self . space . call_args ( self . w_func , self . args ) <EOL> rstack . resume_point ( "<STR_LIT>" , costate , returns = w_result ) <EOL> costate . w_tempval = w_result <EOL> class AppCoroutine ( Coroutine ) : <EOL> def __init__ ( self , space , state = None ) : <EOL> self . space = space <EOL> if state is None : <EOL> state = AppCoroutine . _get_state ( space ) <EOL> Coroutine . __init__ ( self , state ) <EOL> self . flags = <NUM_LIT:0> <EOL> self . newsubctx ( ) <EOL> def newsubctx ( self ) : <EOL> ec = self . space . getexecutioncontext ( ) <EOL> self . subctx = ec . Subcontext ( ) <EOL> def descr_method__new__ ( space , w_subtype ) : <EOL> co = space . allocate_instance ( AppCoroutine , w_subtype ) <EOL> AppCoroutine . __init__ ( co , space ) <EOL> return space . wrap ( co ) <EOL> def _get_state ( space ) : <EOL> return space . fromcache ( AppCoState ) <EOL> _get_state = staticmethod ( _get_state ) <EOL> def w_bind ( self , w_func , __args__ ) : <EOL> space = self . space <EOL> if self . frame is not None : <EOL> raise OperationError ( space . w_ValueError , space . wrap ( <EOL> "<STR_LIT>" ) ) <EOL> state = self . costate <EOL> thunk = _AppThunk ( space , state , w_func , __args__ ) <EOL> self . bind ( thunk ) <EOL> def w_switch ( self ) : <EOL> space = self . space <EOL> if self . frame is None : <EOL> raise OperationError ( space . w_ValueError , space . wrap ( <EOL> "<STR_LIT>" ) ) <EOL> state = self . costate <EOL> self . switch ( ) <EOL> rstack . resume_point ( "<STR_LIT>" , state , space ) <EOL> w_ret , state . w_tempval = state . w_tempval , space . w_None <EOL> return w_ret <EOL> def w_finished ( self , w_excinfo ) : <EOL> pass <EOL> def finish ( self , operror = None ) : <EOL> space = self . space <EOL> if isinstance ( operror , OperationError ) : <EOL> w_exctype = operror . w_type <EOL> w_excvalue = operror . w_value <EOL> w_exctraceback = operror . application_traceback <EOL> w_excinfo = space . newtuple ( [ w_exctype , w_excvalue , w_exctraceback ] ) <EOL> else : <EOL> w_N = space . w_None <EOL> w_excinfo = space . newtuple ( [ w_N , w_N , w_N ] ) <EOL> return space . call_method ( space . wrap ( self ) , '<STR_LIT>' , w_excinfo ) <EOL> def hello ( self ) : <EOL> ec = self . space . getexecutioncontext ( ) <EOL> self . subctx . enter ( ec ) <EOL> def goodbye ( self ) : <EOL> ec = self . space . getexecutioncontext ( ) <EOL> self . subctx . leave ( ec ) <EOL> def w_kill ( self ) : <EOL> self . kill ( ) <EOL> def _userdel ( self ) : <EOL> if self . get_is_zombie ( ) : <EOL> return <EOL> self . set_is_zombie ( True ) <EOL> self . space . userdel ( self . space . wrap ( self ) ) <EOL> def w_getcurrent ( space ) : <EOL> return space . wrap ( AppCoroutine . _get_state ( space ) . current ) <EOL> w_getcurrent = staticmethod ( w_getcurrent ) <EOL> def w_getmain ( space ) : <EOL> return space . wrap ( AppCoroutine . _get_state ( space ) . main ) <EOL> w_getmain = staticmethod ( w_getmain ) <EOL> def descr__reduce__ ( self , space ) : <EOL> from pypy . interpreter . mixedmodule import MixedModule <EOL> w_mod = space . getbuiltinmodule ( '<STR_LIT>' ) <EOL> mod = space . interp_w ( MixedModule , w_mod ) <EOL> w_mod2 = space . getbuiltinmodule ( '<STR_LIT>' ) <EOL> mod2 = space . interp_w ( MixedModule , w_mod2 ) <EOL> w_new_inst = mod . get ( '<STR_LIT>' ) <EOL> w = space . wrap <EOL> nt = space . newtuple <EOL> ec = self . space . getexecutioncontext ( ) <EOL> if self is self . costate . main : <EOL> return nt ( [ mod . get ( '<STR_LIT>' ) , nt ( [ ] ) ] ) <EOL> thunk = self . thunk <EOL> if isinstance ( thunk , _AppThunk ) : <EOL> w_args , w_kwds = thunk . args . topacked ( ) <EOL> w_thunk = nt ( [ thunk . w_func , w_args , w_kwds ] ) <EOL> else : <EOL> w_thunk = space . w_None <EOL> tup_base = [ <EOL> ] <EOL> tup_state = [ <EOL> w ( self . flags ) , <EOL> self . subctx . getstate ( space ) , <EOL> w_thunk , <EOL> w ( self . parent ) , <EOL> ] <EOL> return nt ( [ w_new_inst , nt ( tup_base ) , nt ( tup_state ) ] ) <EOL> def descr__setstate__ ( self , space , w_args ) : <EOL> try : <EOL> w_flags , w_state , w_thunk , w_parent = space . unpackiterable ( w_args , <EOL> expected_length = <NUM_LIT:4> ) <EOL> except UnpackValueError , e : <EOL> raise OperationError ( space . w_ValueError , space . wrap ( e . msg ) ) <EOL> self . flags = space . int_w ( w_flags ) <EOL> if space . is_w ( w_parent , space . w_None ) : <EOL> w_parent = self . w_getmain ( space ) <EOL> self . parent = space . interp_w ( AppCoroutine , w_parent ) <EOL> ec = self . space . getexecutioncontext ( ) <EOL> self . subctx . setstate ( space , w_state ) <EOL> self . reconstruct_framechain ( ) <EOL> if space . is_w ( w_thunk , space . w_None ) : <EOL> self . thunk = None <EOL> else : <EOL> try : <EOL> w_func , w_args , w_kwds = space . unpackiterable ( w_thunk , <EOL> expected_length = <NUM_LIT:3> ) <EOL> except UnpackValueError , e : <EOL> raise OperationError ( space . w_ValueError , space . wrap ( e . msg ) ) <EOL> args = Arguments . frompacked ( space , w_args , w_kwds ) <EOL> self . bind ( _AppThunk ( space , self . costate , w_func , args ) ) <EOL> def reconstruct_framechain ( self ) : <EOL> from pypy . interpreter . pyframe import PyFrame <EOL> from pypy . rlib . rstack import resume_state_create <EOL> if self . subctx . topframe is None : <EOL> self . frame = None <EOL> return <EOL> space = self . space <EOL> ec = space . getexecutioncontext ( ) <EOL> costate = self . costate <EOL> bottom = resume_state_create ( None , "<STR_LIT>" ) <EOL> _bind_frame = resume_state_create ( bottom , "<STR_LIT>" , costate ) <EOL> appthunk_frame = resume_state_create ( _bind_frame , "<STR_LIT>" , costate ) <EOL> chain = appthunk_frame <EOL> for frame in self . subctx . getframestack ( ) : <EOL> assert isinstance ( frame , PyFrame ) <EOL> chain = resume_state_create ( chain , "<STR_LIT>" , frame , ec ) <EOL> code = frame . pycode . co_code <EOL> chain = resume_state_create ( chain , "<STR_LIT>" , frame , code , ec ) <EOL> chain = resume_state_create ( chain , "<STR_LIT>" , frame , code , <EOL> ec ) <EOL> instr = frame . last_instr <EOL> opcode = ord ( code [ instr ] ) <EOL> map = pythonopcode . opmap <EOL> call_ops = [ map [ '<STR_LIT>' ] , map [ '<STR_LIT>' ] , map [ '<STR_LIT>' ] , <EOL> map [ '<STR_LIT>' ] , map [ '<STR_LIT>' ] ] <EOL> assert opcode in call_ops <EOL> chain = resume_state_create ( chain , "<STR_LIT>" , frame , code , <EOL> instr + <NUM_LIT:3> , ec ) <EOL> instr += <NUM_LIT:1> <EOL> oparg = ord ( code [ instr ] ) | ord ( code [ instr + <NUM_LIT:1> ] ) << <NUM_LIT:8> <EOL> nargs = oparg & <NUM_LIT> <EOL> if space . config . objspace . opcodes . CALL_METHOD and opcode == map [ '<STR_LIT>' ] : <EOL> chain = resume_state_create ( chain , '<STR_LIT>' , frame , <EOL> nargs ) <EOL> elif opcode == map [ '<STR_LIT>' ] and ( oparg >> <NUM_LIT:8> ) & <NUM_LIT> == <NUM_LIT:0> : <EOL> chain = resume_state_create ( chain , '<STR_LIT>' , frame , <EOL> nargs ) <EOL> else : <EOL> chain = resume_state_create ( chain , '<STR_LIT>' , frame ) <EOL> w_switch_frame = resume_state_create ( chain , '<STR_LIT>' , costate , space ) <EOL> switch_frame = resume_state_create ( w_switch_frame , "<STR_LIT>" , costate ) <EOL> self . frame = switch_frame <EOL> for methname in StacklessFlags . __dict__ : <EOL> meth = getattr ( StacklessFlags , methname ) <EOL> if hasattr ( meth , '<STR_LIT>' ) : <EOL> setattr ( AppCoroutine , meth . __name__ , meth . im_func ) <EOL> del meth , methname <EOL> def w_get_is_zombie ( space , self ) : <EOL> return space . wrap ( self . get_is_zombie ( ) ) <EOL> AppCoroutine . w_get_is_zombie = w_get_is_zombie <EOL> def w_get_is_alive ( space , self ) : <EOL> return space . wrap ( self . is_alive ( ) ) <EOL> AppCoroutine . w_get_is_alive = w_get_is_alive <EOL> def w_descr__framestack ( space , self ) : <EOL> assert isinstance ( self , AppCoroutine ) <EOL> index = self . subctx . framestackdepth <EOL> if not index : <EOL> return space . newtuple ( [ ] ) <EOL> items = [ None ] * index <EOL> f = self . subctx . topframe <EOL> f . force_f_back ( ) <EOL> while index > <NUM_LIT:0> : <EOL> index -= <NUM_LIT:1> <EOL> items [ index ] = space . wrap ( f ) <EOL> f = f . f_back ( ) <EOL> assert f is None <EOL> return space . newtuple ( items ) <EOL> def makeStaticMethod ( module , classname , funcname ) : <EOL> space = module . space <EOL> w_klass = space . getattr ( space . wrap ( module ) , space . wrap ( classname ) ) <EOL> from pypy . objspace . std . typeobject import _HEAPTYPE , W_TypeObject <EOL> assert isinstance ( w_klass , W_TypeObject ) <EOL> old_flags = w_klass . __flags__ <EOL> w_klass . __flags__ |= _HEAPTYPE <EOL> space . appexec ( [ w_klass , space . wrap ( funcname ) ] , """<STR_LIT>""" ) <EOL> w_klass . __flags__ = old_flags <EOL> def post_install ( module ) : <EOL> makeStaticMethod ( module , '<STR_LIT>' , '<STR_LIT>' ) <EOL> makeStaticMethod ( module , '<STR_LIT>' , '<STR_LIT>' ) <EOL> space = module . space <EOL> AppCoroutine . _get_state ( space ) . post_install ( ) <EOL> AppCoroutine . typedef = TypeDef ( "<STR_LIT>" , <EOL> __new__ = interp2app ( AppCoroutine . descr_method__new__ . im_func ) , <EOL> bind = interp2app ( AppCoroutine . w_bind , <EOL> unwrap_spec = [ '<STR_LIT>' , W_Root , Arguments ] ) , <EOL> switch = interp2app ( AppCoroutine . w_switch ) , <EOL> kill = interp2app ( AppCoroutine . w_kill ) , <EOL> finished = interp2app ( AppCoroutine . w_finished ) , <EOL> is_alive = GetSetProperty ( AppCoroutine . w_get_is_alive ) , <EOL> is_zombie = GetSetProperty ( AppCoroutine . w_get_is_zombie , <EOL> doc = AppCoroutine . get_is_zombie . __doc__ ) , <EOL> _framestack = GetSetProperty ( w_descr__framestack ) , <EOL> getcurrent = interp2app ( AppCoroutine . w_getcurrent ) , <EOL> getmain = interp2app ( AppCoroutine . w_getmain ) , <EOL> __reduce__ = interp2app ( AppCoroutine . descr__reduce__ , <EOL> unwrap_spec = [ '<STR_LIT>' , ObjSpace ] ) , <EOL> __setstate__ = interp2app ( AppCoroutine . descr__setstate__ , <EOL> unwrap_spec = [ '<STR_LIT>' , ObjSpace , W_Root ] ) , <EOL> __module__ = '<STR_LIT>' , <EOL> ) <EOL> class AppCoState ( BaseCoState ) : <EOL> def __init__ ( self , space ) : <EOL> BaseCoState . __init__ ( self ) <EOL> self . w_tempval = space . w_None <EOL> self . space = space <EOL> def post_install ( self ) : <EOL> self . current = self . main = AppCoroutine ( self . space , state = self ) <EOL> self . main . subctx . clear_framestack ( ) <EOL> def return_main ( space ) : <EOL> return AppCoroutine . _get_state ( space ) . main <EOL> return_main . unwrap_spec = [ ObjSpace ] <EOL> def get_stack_depth_limit ( space ) : <EOL> return space . wrap ( rstack . get_stack_depth_limit ( ) ) <EOL> get_stack_depth_limit . unwrap_spec = [ ObjSpace ] <EOL> def set_stack_depth_limit ( space , limit ) : <EOL> rstack . set_stack_depth_limit ( limit ) <EOL> set_stack_depth_limit . unwrap_spec = [ ObjSpace , int ] </s>
