<s> import sys <EOL> from pypy . interpreter . miscutils import Stack <EOL> from pypy . interpreter . error import OperationError <EOL> from pypy . rlib . rarithmetic import LONG_BIT <EOL> from pypy . rlib . unroll import unrolling_iterable <EOL> from pypy . rlib . jit import we_are_jitted <EOL> from pypy . rlib import jit <EOL> def app_profile_call ( space , w_callable , frame , event , w_arg ) : <EOL> space . call_function ( w_callable , <EOL> space . wrap ( frame ) , <EOL> space . wrap ( event ) , w_arg ) <EOL> class ExecutionContext ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , space ) : <EOL> self . space = space <EOL> self . _init_frame_chain ( ) <EOL> self . w_tracefunc = None <EOL> self . is_tracing = <NUM_LIT:0> <EOL> self . compiler = space . createcompiler ( ) <EOL> self . profilefunc = None <EOL> self . w_profilefuncarg = None <EOL> def gettopframe_nohidden ( self ) : <EOL> frame = self . gettopframe ( ) <EOL> while frame and frame . hide ( ) : <EOL> frame = frame . f_back ( ) <EOL> return frame <EOL> @ staticmethod <EOL> def getnextframe_nohidden ( frame ) : <EOL> frame = frame . f_back ( ) <EOL> while frame and frame . hide ( ) : <EOL> frame = frame . f_back ( ) <EOL> return frame <EOL> def enter ( self , frame ) : <EOL> if self . framestackdepth > self . space . sys . recursionlimit : <EOL> raise OperationError ( self . space . w_RuntimeError , <EOL> self . space . wrap ( "<STR_LIT>" ) ) <EOL> self . _chain ( frame ) <EOL> def leave ( self , frame ) : <EOL> if self . profilefunc : <EOL> self . _trace ( frame , '<STR_LIT>' , self . space . w_None ) <EOL> self . _unchain ( frame ) <EOL> if self . w_tracefunc is not None and not frame . hide ( ) : <EOL> self . space . frame_trace_action . fire ( ) <EOL> @ jit . unroll_safe <EOL> def gettopframe ( self ) : <EOL> frame = self . some_frame <EOL> if frame is not None : <EOL> while frame . f_forward is not None : <EOL> frame = frame . f_forward <EOL> return frame <EOL> def _init_frame_chain ( self ) : <EOL> self . some_frame = None <EOL> self . framestackdepth = <NUM_LIT:0> <EOL> @ staticmethod <EOL> def _init_chaining_attributes ( frame ) : <EOL> """<STR_LIT>""" <EOL> frame . f_back_some = None <EOL> frame . f_forward = None <EOL> frame . f_back_forced = False <EOL> def _chain ( self , frame ) : <EOL> self . framestackdepth += <NUM_LIT:1> <EOL> frame . f_back_some = self . some_frame <EOL> if self . _we_are_jitted ( ) : <EOL> curtopframe = self . gettopframe ( ) <EOL> assert curtopframe is not None <EOL> curtopframe . f_forward = frame <EOL> else : <EOL> self . some_frame = frame <EOL> def _unchain ( self , frame ) : <EOL> if self . some_frame is frame : <EOL> self . some_frame = frame . f_back_some <EOL> else : <EOL> f_back = frame . f_back ( ) <EOL> if f_back is not None : <EOL> f_back . f_forward = None <EOL> self . framestackdepth -= <NUM_LIT:1> <EOL> @ staticmethod <EOL> def _jit_rechain_frame ( ec , frame ) : <EOL> frame . f_back ( ) . f_forward = None <EOL> ec . some_frame = frame <EOL> @ staticmethod <EOL> @ jit . unroll_safe <EOL> def _extract_back_from_frame ( frame ) : <EOL> back_some = frame . f_back_some <EOL> if frame . f_back_forced : <EOL> return back_some <EOL> if back_some is None : <EOL> return None <EOL> while True : <EOL> f_forward = back_some . f_forward <EOL> if f_forward is frame or f_forward is None : <EOL> return back_some <EOL> back_some = f_forward <EOL> @ staticmethod <EOL> def _force_back_of_frame ( frame ) : <EOL> orig_frame = frame <EOL> while frame is not None and not frame . f_back_forced : <EOL> frame . f_back_some = f_back = ExecutionContext . _extract_back_from_frame ( frame ) <EOL> frame . f_back_forced = True <EOL> frame = f_back <EOL> return orig_frame . f_back_some <EOL> _we_are_jitted = staticmethod ( we_are_jitted ) <EOL> class Subcontext ( object ) : <EOL> def __init__ ( self ) : <EOL> self . topframe = None <EOL> self . framestackdepth = <NUM_LIT:0> <EOL> self . w_tracefunc = None <EOL> self . profilefunc = None <EOL> self . w_profilefuncarg = None <EOL> self . is_tracing = <NUM_LIT:0> <EOL> def enter ( self , ec ) : <EOL> ec . some_frame = self . topframe <EOL> ec . framestackdepth = self . framestackdepth <EOL> ec . w_tracefunc = self . w_tracefunc <EOL> ec . profilefunc = self . profilefunc <EOL> ec . w_profilefuncarg = self . w_profilefuncarg <EOL> ec . is_tracing = self . is_tracing <EOL> ec . space . frame_trace_action . fire ( ) <EOL> def leave ( self , ec ) : <EOL> self . topframe = ec . gettopframe ( ) <EOL> self . framestackdepth = ec . framestackdepth <EOL> self . w_tracefunc = ec . w_tracefunc <EOL> self . profilefunc = ec . profilefunc <EOL> self . w_profilefuncarg = ec . w_profilefuncarg <EOL> self . is_tracing = ec . is_tracing <EOL> def clear_framestack ( self ) : <EOL> self . topframe = None <EOL> self . framestackdepth = <NUM_LIT:0> <EOL> def getstate ( self , space ) : <EOL> items = [ space . wrap ( f ) for f in self . getframestack ( ) ] <EOL> return space . newtuple ( items ) <EOL> def setstate ( self , space , w_state ) : <EOL> from pypy . interpreter . pyframe import PyFrame <EOL> frames_w = space . unpackiterable ( w_state ) <EOL> if len ( frames_w ) > <NUM_LIT:0> : <EOL> self . topframe = space . interp_w ( PyFrame , frames_w [ - <NUM_LIT:1> ] ) <EOL> else : <EOL> self . topframe = None <EOL> self . framestackdepth = len ( frames_w ) <EOL> def getframestack ( self ) : <EOL> index = self . framestackdepth <EOL> lst = [ None ] * index <EOL> f = self . topframe <EOL> while index > <NUM_LIT:0> : <EOL> index -= <NUM_LIT:1> <EOL> lst [ index ] = f <EOL> f = f . f_back ( ) <EOL> assert f is None <EOL> return lst <EOL> def get_builtin ( self ) : <EOL> frame = self . gettopframe_nohidden ( ) <EOL> if frame is not None : <EOL> return frame . builtin <EOL> else : <EOL> return self . space . builtin <EOL> def make_standard_w_globals ( self ) : <EOL> "<STR_LIT>" <EOL> w_key = self . space . wrap ( "<STR_LIT>" ) <EOL> w_value = self . space . wrap ( self . get_builtin ( ) ) <EOL> w_globals = self . space . newdict ( ) <EOL> space . setitem ( w_globals , w_key , w_value ) <EOL> return w_globals <EOL> @ jit . dont_look_inside <EOL> def c_call_trace ( self , frame , w_func ) : <EOL> "<STR_LIT>" <EOL> if self . profilefunc is None : <EOL> frame . is_being_profiled = False <EOL> else : <EOL> self . _trace ( frame , '<STR_LIT>' , w_func ) <EOL> @ jit . dont_look_inside <EOL> def c_return_trace ( self , frame , w_retval ) : <EOL> "<STR_LIT>" <EOL> if self . profilefunc is None : <EOL> frame . is_being_profiled = False <EOL> else : <EOL> self . _trace ( frame , '<STR_LIT>' , w_retval ) <EOL> @ jit . dont_look_inside <EOL> def c_exception_trace ( self , frame , w_exc ) : <EOL> "<STR_LIT>" <EOL> if self . profilefunc is None : <EOL> frame . is_being_profiled = False <EOL> else : <EOL> self . _trace ( frame , '<STR_LIT>' , w_exc ) <EOL> @ jit . dont_look_inside <EOL> def call_trace ( self , frame ) : <EOL> "<STR_LIT>" <EOL> if self . w_tracefunc is not None or self . profilefunc is not None : <EOL> self . _trace ( frame , '<STR_LIT>' , self . space . w_None ) <EOL> if self . profilefunc : <EOL> frame . is_being_profiled = True <EOL> @ jit . dont_look_inside <EOL> def return_trace ( self , frame , w_retval ) : <EOL> "<STR_LIT>" <EOL> if self . w_tracefunc is not None : <EOL> self . _trace ( frame , '<STR_LIT>' , w_retval ) <EOL> def bytecode_trace ( self , frame ) : <EOL> "<STR_LIT>" <EOL> actionflag = self . space . actionflag <EOL> ticker = actionflag . get ( ) <EOL> if actionflag . has_bytecode_counter : <EOL> ticker += <NUM_LIT:1> <EOL> actionflag . set ( ticker ) <EOL> if ticker & actionflag . interesting_bits : <EOL> actionflag . action_dispatcher ( self , frame ) <EOL> bytecode_trace . _always_inline_ = True <EOL> @ jit . dont_look_inside <EOL> def exception_trace ( self , frame , operationerr ) : <EOL> "<STR_LIT>" <EOL> operationerr . record_interpreter_traceback ( ) <EOL> if self . w_tracefunc is not None : <EOL> self . _trace ( frame , '<STR_LIT>' , None , operationerr ) <EOL> def sys_exc_info ( self ) : <EOL> """<STR_LIT>""" <EOL> frame = self . gettopframe_nohidden ( ) <EOL> while frame : <EOL> if frame . last_exception is not None : <EOL> return frame . last_exception <EOL> frame = self . getnextframe_nohidden ( frame ) <EOL> return None <EOL> def settrace ( self , w_func ) : <EOL> """<STR_LIT>""" <EOL> if self . space . is_w ( w_func , self . space . w_None ) : <EOL> self . w_tracefunc = None <EOL> else : <EOL> self . w_tracefunc = w_func <EOL> self . space . frame_trace_action . fire ( ) <EOL> def setprofile ( self , w_func ) : <EOL> """<STR_LIT>""" <EOL> if self . space . is_w ( w_func , self . space . w_None ) : <EOL> self . profilefunc = None <EOL> self . w_profilefuncarg = None <EOL> else : <EOL> self . setllprofile ( app_profile_call , w_func ) <EOL> def setllprofile ( self , func , w_arg ) : <EOL> self . profilefunc = func <EOL> if func is not None : <EOL> if w_arg is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> frame = self . gettopframe_nohidden ( ) <EOL> while frame : <EOL> frame . is_being_profiled = True <EOL> frame = self . getnextframe_nohidden ( frame ) <EOL> self . w_profilefuncarg = w_arg <EOL> def call_tracing ( self , w_func , w_args ) : <EOL> is_tracing = self . is_tracing <EOL> self . is_tracing = <NUM_LIT:0> <EOL> try : <EOL> self . space . frame_trace_action . fire ( ) <EOL> return self . space . call ( w_func , w_args ) <EOL> finally : <EOL> self . is_tracing = is_tracing <EOL> def _trace ( self , frame , event , w_arg , operr = None ) : <EOL> if self . is_tracing or frame . hide ( ) : <EOL> return <EOL> space = self . space <EOL> if event == '<STR_LIT>' : <EOL> w_callback = self . w_tracefunc <EOL> else : <EOL> w_callback = frame . w_f_trace <EOL> if w_callback is not None and event != "<STR_LIT>" : <EOL> if operr is not None : <EOL> w_arg = space . newtuple ( [ operr . w_type , operr . w_value , <EOL> space . wrap ( operr . application_traceback ) ] ) <EOL> frame . fast2locals ( ) <EOL> self . is_tracing += <NUM_LIT:1> <EOL> try : <EOL> try : <EOL> w_result = space . call_function ( w_callback , space . wrap ( frame ) , space . wrap ( event ) , w_arg ) <EOL> if space . is_w ( w_result , space . w_None ) : <EOL> frame . w_f_trace = None <EOL> else : <EOL> frame . w_f_trace = w_result <EOL> except : <EOL> self . settrace ( space . w_None ) <EOL> frame . w_f_trace = None <EOL> raise <EOL> finally : <EOL> self . is_tracing -= <NUM_LIT:1> <EOL> frame . locals2fast ( ) <EOL> space . frame_trace_action . fire ( ) <EOL> if self . profilefunc is not None : <EOL> if event not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return <EOL> last_exception = None <EOL> if event == '<STR_LIT>' : <EOL> last_exception = frame . last_exception <EOL> event = '<STR_LIT>' <EOL> assert self . is_tracing == <NUM_LIT:0> <EOL> self . is_tracing += <NUM_LIT:1> <EOL> try : <EOL> try : <EOL> self . profilefunc ( space , self . w_profilefuncarg , <EOL> frame , event , w_arg ) <EOL> except : <EOL> self . profilefunc = None <EOL> self . w_profilefuncarg = None <EOL> raise <EOL> finally : <EOL> frame . last_exception = last_exception <EOL> self . is_tracing -= <NUM_LIT:1> <EOL> def _freeze_ ( self ) : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class AbstractActionFlag : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _periodic_actions = [ ] <EOL> self . _nonperiodic_actions = [ ] <EOL> self . unused_bits = self . FREE_BITS [ : ] <EOL> self . has_bytecode_counter = False <EOL> self . interesting_bits = <NUM_LIT:0> <EOL> self . _rebuild_action_dispatcher ( ) <EOL> def fire ( self , action ) : <EOL> """<STR_LIT>""" <EOL> ticker = self . get ( ) <EOL> self . set ( ticker | action . bitmask ) <EOL> def register_action ( self , action ) : <EOL> "<STR_LIT>" <EOL> assert isinstance ( action , AsyncAction ) <EOL> if action . bitmask == <NUM_LIT:0> : <EOL> while True : <EOL> action . bitmask = self . unused_bits . pop ( <NUM_LIT:0> ) <EOL> if not ( action . bitmask & self . interesting_bits ) : <EOL> break <EOL> self . interesting_bits |= action . bitmask <EOL> if action . bitmask & self . BYTECODE_COUNTER_OVERFLOW_BIT : <EOL> assert action . bitmask == self . BYTECODE_COUNTER_OVERFLOW_BIT <EOL> self . _periodic_actions . append ( action ) <EOL> self . has_bytecode_counter = True <EOL> self . force_tick_counter ( ) <EOL> else : <EOL> self . _nonperiodic_actions . append ( ( action , action . bitmask ) ) <EOL> self . _rebuild_action_dispatcher ( ) <EOL> def setcheckinterval ( self , space , interval ) : <EOL> if interval < self . CHECK_INTERVAL_MIN : <EOL> interval = self . CHECK_INTERVAL_MIN <EOL> elif interval > self . CHECK_INTERVAL_MAX : <EOL> interval = self . CHECK_INTERVAL_MAX <EOL> space . sys . checkinterval = interval <EOL> self . force_tick_counter ( ) <EOL> def force_tick_counter ( self ) : <EOL> ticker = self . get ( ) <EOL> ticker &= ~ self . BYTECODE_COUNTER_OVERFLOW_BIT <EOL> ticker |= self . BYTECODE_COUNTER_MASK <EOL> self . set ( ticker ) <EOL> def _rebuild_action_dispatcher ( self ) : <EOL> periodic_actions = unrolling_iterable ( self . _periodic_actions ) <EOL> nonperiodic_actions = unrolling_iterable ( self . _nonperiodic_actions ) <EOL> has_bytecode_counter = self . has_bytecode_counter <EOL> @ jit . dont_look_inside <EOL> def action_dispatcher ( ec , frame ) : <EOL> if has_bytecode_counter : <EOL> ticker = self . get ( ) <EOL> if ticker & self . BYTECODE_COUNTER_OVERFLOW_BIT : <EOL> ticker -= ec . space . sys . checkinterval <EOL> self . set ( ticker ) <EOL> for action in periodic_actions : <EOL> action . perform ( ec , frame ) <EOL> for action , bitmask in nonperiodic_actions : <EOL> ticker = self . get ( ) <EOL> if ticker & bitmask : <EOL> self . set ( ticker & ~ bitmask ) <EOL> action . perform ( ec , frame ) <EOL> action_dispatcher . _dont_inline_ = True <EOL> self . action_dispatcher = action_dispatcher <EOL> BYTECODE_COUNTER_MASK = ( <NUM_LIT:1> << <NUM_LIT:20> ) - <NUM_LIT:1> <EOL> BYTECODE_COUNTER_OVERFLOW_BIT = ( <NUM_LIT:1> << <NUM_LIT:20> ) <EOL> FREE_BITS = [ <NUM_LIT:1> << _b for _b in range ( <NUM_LIT> , LONG_BIT - <NUM_LIT:1> ) ] <EOL> CHECK_INTERVAL_MIN = <NUM_LIT:1> <EOL> CHECK_INTERVAL_MAX = BYTECODE_COUNTER_OVERFLOW_BIT <EOL> class ActionFlag ( AbstractActionFlag ) : <EOL> """<STR_LIT>""" <EOL> _flags = <NUM_LIT:0> <EOL> def get ( self ) : <EOL> return self . _flags <EOL> def set ( self , value ) : <EOL> self . _flags = value <EOL> class AsyncAction ( object ) : <EOL> """<STR_LIT>""" <EOL> bitmask = <NUM_LIT:0> <EOL> def __init__ ( self , space ) : <EOL> self . space = space <EOL> def fire ( self ) : <EOL> """<STR_LIT>""" <EOL> self . space . actionflag . fire ( self ) <EOL> def fire_after_thread_switch ( self ) : <EOL> """<STR_LIT>""" <EOL> from pypy . module . thread . gil import spacestate <EOL> spacestate . set_actionflag_bit_after_thread_switch |= self . bitmask <EOL> def perform ( self , executioncontext , frame ) : <EOL> """<STR_LIT>""" <EOL> class PeriodicAsyncAction ( AsyncAction ) : <EOL> """<STR_LIT>""" <EOL> bitmask = ActionFlag . BYTECODE_COUNTER_OVERFLOW_BIT <EOL> class UserDelAction ( AsyncAction ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , space ) : <EOL> AsyncAction . __init__ ( self , space ) <EOL> self . dying_objects_w = [ ] <EOL> self . finalizers_lock_count = <NUM_LIT:0> <EOL> def register_dying_object ( self , w_obj ) : <EOL> self . dying_objects_w . append ( w_obj ) <EOL> self . fire ( ) <EOL> def perform ( self , executioncontext , frame ) : <EOL> if self . finalizers_lock_count > <NUM_LIT:0> : <EOL> return <EOL> pending_w = self . dying_objects_w <EOL> self . dying_objects_w = [ ] <EOL> space = self . space <EOL> for w_obj in pending_w : <EOL> try : <EOL> space . userdel ( w_obj ) <EOL> except OperationError , e : <EOL> e . write_unraisable ( space , '<STR_LIT>' , w_obj ) <EOL> e . clear ( space ) <EOL> w_obj . _call_builtin_destructor ( ) <EOL> class FrameTraceAction ( AsyncAction ) : <EOL> """<STR_LIT>""" <EOL> def perform ( self , executioncontext , frame ) : <EOL> if frame . w_f_trace is None or executioncontext . is_tracing : <EOL> return <EOL> code = frame . pycode <EOL> if frame . instr_lb <= frame . last_instr < frame . instr_ub : <EOL> if frame . last_instr <= frame . instr_prev : <EOL> executioncontext . _trace ( frame , '<STR_LIT>' , self . space . w_None ) <EOL> else : <EOL> size = len ( code . co_lnotab ) / <NUM_LIT:2> <EOL> addr = <NUM_LIT:0> <EOL> line = code . co_firstlineno <EOL> p = <NUM_LIT:0> <EOL> lineno = code . co_lnotab <EOL> while size > <NUM_LIT:0> : <EOL> c = ord ( lineno [ p ] ) <EOL> if ( addr + c ) > frame . last_instr : <EOL> break <EOL> addr += c <EOL> if c : <EOL> frame . instr_lb = addr <EOL> line += ord ( lineno [ p + <NUM_LIT:1> ] ) <EOL> p += <NUM_LIT:2> <EOL> size -= <NUM_LIT:1> <EOL> if size > <NUM_LIT:0> : <EOL> while True : <EOL> size -= <NUM_LIT:1> <EOL> if size < <NUM_LIT:0> : <EOL> break <EOL> addr += ord ( lineno [ p ] ) <EOL> if ord ( lineno [ p + <NUM_LIT:1> ] ) : <EOL> break <EOL> p += <NUM_LIT:2> <EOL> frame . instr_ub = addr <EOL> else : <EOL> frame . instr_ub = sys . maxint <EOL> if frame . instr_lb == frame . last_instr : <EOL> frame . f_lineno = line <EOL> executioncontext . _trace ( frame , '<STR_LIT>' , self . space . w_None ) <EOL> frame . instr_prev = frame . last_instr <EOL> self . space . frame_trace_action . fire ( ) </s>
