
ёRMc        	   @   s	  d  d k  Z  d  d k l Z d  d k l Z d  d k l Z d  d k l Z d  d k	 l
 Z
 d Z d   Z d	 e f d
     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d S(   iN(   t   Stack(   t   OperationError(   t   LONG_BIT(   t   unrolling_iterable(   t   jitid   c         C   s,   |  i  | |  i |  |  i |  |  d  S(   N(   t   call_functiont   wrap(   t   spacet
   w_callablet   framet   eventt   w_arg(    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   app_profile_call
   s    	t   ExecutionContextc           B   s,  e  Z d  Z d   Z d   Z d   Z e d    Z d   Z d   Z	 d e
 f d     YZ d d	  Z d d
  Z d   Z d   Z d   Z d   Z e d  Z e e _ d   Z e e _ d   Z d   Z d   Z d   Z d   Z d   Z e d  Z d   Z  d d  Z! d   Z" d   Z# RS(   sY   An ExecutionContext holds the state of an execution thread
    in the Python interpreter.c         C   sL   | |  _  t i |  _ d  |  _ d |  _ | i   |  _ d  |  _	 d  |  _
 d  S(   Ni    (   R   R   t	   vref_Nonet   topframereft   Nonet   w_tracefunct
   is_tracingt   createcompilert   compilert   profilefunct   w_profilefuncarg(   t   selfR   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   __init__   s    				c         C   s
   |  i    S(   N(   R   (   R   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   gettopframe'   s    c         C   s8   |  i    } x% | o | i   o | i   } q W| S(   N(   R   t   hidet	   f_backref(   R   R	   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   gettopframe_nohidden*   s
     c         C   s8   |  i    }  x% |  o |  i   o |  i    }  q W|  S(   N(   R   R   (   R	   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   getnextframe_nohidden0   s
     c         C   s"   |  i  | _ t i |  |  _  d  S(   N(   R   R   R   t   virtual_ref(   R   R	   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   enter7   s    c         C   s~   z+ |  i  o |  i | d |  i i  n Wd  | i |  _ t i |  X|  i d  j	 o" | i
   o |  i i i   n d  S(   Nt
   leaveframe(   R   t   _traceR   t   w_NoneR   R   R   t   virtual_ref_finishR   R   R   t   frame_trace_actiont   fire(   R   R	   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   leave;   s    
!t
   Subcontextc           B   sG   e  Z d    Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   c         C   s1   d  |  _ d  |  _ d  |  _ d  |  _ d |  _ d  S(   Ni    (   R   t   topframeR   R   R   R   (   R   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR   L   s
    				c         C   sY   t  i |  i  | _ |  i | _ |  i | _ |  i | _ |  i | _ | i i	 i
   d  S(   N(   R   t   non_virtual_refR(   R   R   R   R   R   R   R$   R%   (   R   t   ec(    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR   S   s    c         C   sC   | i    |  _ | i |  _ | i |  _ | i |  _ | i |  _ d  S(   N(   R   R(   R   R   R   R   (   R   R*   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR&   [   s
    c         C   s   d  |  _ d  S(   N(   R   R(   (   R   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   clear_framestackb   s    c         C   s=   g  } |  i    D] } | | i |  q ~ } | i |  S(   N(   t   getframestackR   t   newtuple(   R   R   t   _[1]t   ft   items(    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   getstatef   s    0c         C   s\   d d k  l } | i |  } t |  d j o | i | | d  |  _ n
 d  |  _ d  S(   Ni(   t   PyFramei    (   t   pypy.interpreter.pyframeR2   t   unpackiterablet   lent   interp_wR(   R   (   R   R   t   w_stateR2   t   frames_w(    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   setstatel   s
    c         C   sK   g  } |  i  } x+ | d  j	 o | i |  | i   } q W| i   | S(   N(   R(   R   t   appendR   t   reverse(   R   t   lstR/   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR,   t   s    	 
(	   t   __name__t
   __module__R   R   R&   R+   R1   R9   R,   (    (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR'   I   s   						c         C   s   |  i  | | | d  d S(   s&   Profile the call of a builtin functiont   c_callN(   t   _c_call_return_trace(   R   R	   t   w_funct   args(    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   c_call_trace~   s    c         C   s   |  i  | | | d  d S(   s*   Profile the return from a builtin functiont   c_returnN(   R@   (   R   R	   RA   RB   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   c_return_trace   s    c         C   s   |  i  d  j o t | _ n d d k l } t | |  ob | d  j	 oU | i   } | d  j	 o8 d d k l } | |  i	 | | |  i	 i
 |   } q n |  i | | |  d  S(   Ni(   t   FunctionWithFixedCode(   t   descr_function_get(   R   R   t   Falset   is_being_profiledt   pypy.interpreter.functionRF   t
   isinstancet   firstargRG   R   t   typeR!   (   R   R	   RA   RB   R
   RF   t
   w_firstargRG   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR@      s    c         C   s4   |  i  d j o t | _ n |  i | d |  d S(   s,   Profile function called upon OperationError.t   c_exceptionN(   R   R   RH   RI   R!   (   R   R	   t   w_exc(    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   c_exception_trace   s    c         C   sX   |  i  d j	 p |  i d j	 o4 |  i | d |  i i  |  i o t | _ qT n d S(   s   Trace the call of a functiont   callN(   R   R   R   R!   R   R"   t   TrueRI   (   R   R	   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt
   call_trace   s     
c         C   s+   |  i  d j	 o |  i | d |  n d S(   s    Trace the return from a functiont   returnN(   R   R   R!   (   R   R	   t   w_retval(    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   return_trace   s    c         C   s:   |  i  i } | i |  d j  o | i |  |  n d S(   s+   Trace function called before each bytecode.i    N(   R   t
   actionflagt   decrement_tickert   action_dispatcher(   R   R	   t   decr_byRX   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   bytecode_trace   s    c         C   s7   |  i  i } | i   d j  o | i |  |  n d S(   s9   Like bytecode_trace(), but without increasing the ticker.i    N(   R   RX   t
   get_tickerRZ   (   R   R	   RX   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   bytecode_trace_after_exception   s    c         C   s8   | i    |  i d j	 o |  i | d d |  n d S(   s*   Trace function called upon OperationError.t	   exceptionN(   t   record_interpreter_tracebackR   R   R!   (   R   R	   t   operationerr(    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   exception_trace   s    
c         C   sI   |  i    } x6 | o. | i d j	 o | i Sn |  i |  } q Wd S(   sM   Implements sys.exc_info().
        Return an OperationError instance or None.N(   R   t   last_exceptionR   R   (   R   R	   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   sys_exc_info   s     c         C   sP   |  i  i | |  i  i  o d |  _ n$ |  i   | |  _ |  i  i i   d S(   s   Set the global trace function.N(   R   t   is_wR"   R   R   t   force_all_framesR$   R%   (   R   RA   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   settrace   s
    
	c         C   sF   |  i  i | |  i  i  o d |  _ d |  _ n |  i t |  d S(   s   Set the global trace function.N(   R   Re   R"   R   R   R   t   setllprofileR   (   R   RA   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt
   setprofile   s    	c         C   s   |  i  S(   N(   R   (   R   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt
   getprofile   s    c         C   sT   | d  j	 o1 | d  j o t d   n |  i d t  n | |  _ | |  _ d  S(   Ns'   Cannot call setllprofile with real NoneRI   (   R   t
   ValueErrorRf   RS   R   R   (   R   t   funcR   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyRh      s    	c         C   sB   |  i    } x/ | o' | o t | _ n |  i |  } q Wd  S(   N(   R   RS   RI   R   (   R   RI   R	   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyRf      s    	 c         C   sJ   |  i  } d |  _  z' |  i i i   |  i i | |  SWd  | |  _  Xd  S(   Ni    (   R   R   R$   R%   RR   (   R   RA   t   w_argsR   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   call_tracing   s    		c   
      C   sI  |  i  p | i   o d  Sn |  i } | d j o |  i } n
 | i } | d  j	 o| d j o| d  j	 o: | i |  } | i | i | | i	 | i
  g  } n | i   |  i  d 7_  z yZ | i | | i	 |  | i	 |  |  } | i | | i  o d  | _ n
 | | _ Wn# |  i | i  d  | _   n XWd  |  i  d 8_  | i   | i i   Xn |  i d  j	 o | d	 j o d  Sn | i }	 | d j o
 d } n |  i  d j p t  |  i  d 7_  zC y  |  i | |  i | | |  Wn d  |  _ d  |  _   n XWd  |	 | _ |  i  d 8_  Xn d  S(
   NRR   R    i   R?   RD   RO   RU   i    (   s
   leaveframes   calls   c_calls   c_returns   c_exception(   R   R   R   R   t	   w_f_traceR   t   get_w_valueR-   t   w_typeR   t   application_tracebackt   fast2localsR   Re   R"   Rg   t   locals2fastR$   R%   R   Rc   t   AssertionErrorR   (
   R   R	   R
   R   t   operrR   t
   w_callbackt   w_valuet   w_resultRc   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR!      sZ    		
*	

	
			c         C   s1   |  i  i d j	 o |  i  i i |  d  n d S(   s   Similar to PyErr_CheckSignals().  If called in the main thread,
        and if signals are pending for the process, deliver them now
        (i.e. call the signal handlers).N(   R   t   check_signal_actionR   t   perform(   R   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   checksignals<  s    c         C   s   t  d   d  S(   Ns   ExecutionContext instances should not be seen during translation.  Now is a good time to inspect the traceback and see where this one comes from :-)(   t	   Exception(   R   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   _freeze_C  s    N($   R=   R>   t   __doc__R   R   R   t   staticmethodR   R   R&   t   objectR'   R   RC   RE   R@   RQ   RT   RW   t   TICK_COUNTER_STEPR\   RS   t   _always_inline_R^   Rb   Rd   Rg   Ri   Rj   Rh   RH   Rf   Rn   R!   R|   R~   (    (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR      s:   
					5									
							=	t   AbstractActionFlagc           B   sD   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   s
  This holds in an integer the 'ticker'.  If threads are enabled,
    it is decremented at each bytecode; when it reaches zero, we release
    the GIL.  And whether we have threads or not, it is forced to zero
    whenever we fire any of the asynchronous actions.
    c         C   s?   g  |  _  g  |  _ t |  _ d  |  _ d t |  _ |  i   d  S(   Nid   (	   t   _periodic_actionst   _nonperiodic_actionsRH   t   has_bytecode_counterR   t   fired_actionsR   t   checkinterval_scaledt   _rebuild_action_dispatcher(   R   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR   O  s    				c         C   sU   | i  pG t | _  |  i d j o g  |  _ n |  i i |  |  i d  n d S(   s8   Request for the action to be run before the next opcode.iN(   t   _firedRS   R   R   R:   t   reset_ticker(   R   t   action(    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR%   W  s    
	c         C   sI   t  | t  p t  |  i i |  | o t |  _ n |  i   d S(   s  NOT_RPYTHON:
        Register the PeriodicAsyncAction action to be called whenever the
        tick counter becomes smaller than 0.  If 'use_bytecode_counter' is
        True, make sure that we decrease the tick counter at every bytecode.
        This is needed for threads.  Note that 'use_bytecode_counter' can be
        False for signal handling, because whenever the process receives a
        signal, the tick counter is set to -1 by C code in signals.h.
        N(   RK   t   PeriodicAsyncActionRu   R   R:   RS   R   R   (   R   R   t   use_bytecode_counter(    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   register_periodic_actionb  s
    	c         C   s   |  i  t S(   N(   R   R   (   R   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   getcheckintervalq  s    c         C   sL   t  i t } | d j  o
 d } n | | j o
 | } n | t |  _ d  S(   Ni   (   t   syst   maxintR   R   (   R   t   intervalt   MAX(    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   setcheckintervalt  s    

c            s@   t    i   t i    f d    } t | _ |   _ d  S(   Nc            s    i   i  x   D] } | i |  |  q W i } | d  j	 o7 d   _ x+ | D] } t | _ | i |  |  qW Wn d  S(   N(   R   R   R{   R   R   RH   R   (   R*   R	   R   t   list(   t   periodic_actionsR   (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyRZ     s     		 	(   R   R   R   t   dont_look_insideRS   t   _dont_inline_RZ   (   R   RZ   (    (   R   R   s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR   |  s    	(	   R=   R>   R   R   R%   R   R   R   R   (    (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR   I  s   					t
   ActionFlagc           B   s/   e  Z d  Z d Z d   Z d   Z d   Z RS(   sW   The normal class for space.actionflag.  The signal module provides
    a different one.i    c         C   s   |  i  S(   N(   t   _ticker(   R   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR]     s    c         C   s   | |  _  d  S(   N(   R   (   R   t   value(    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR     s    c         C   s.   |  i  } |  i o | | 8} | |  _  n | S(   N(   R   R   (   R   t   byR   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyRY     s
    	

(   R=   R>   R   R   R]   R   RY   (    (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR     s
   		t   AsyncActionc           B   s8   e  Z d  Z e Z d   Z d   Z d   Z d   Z RS(   s   Abstract base class for actions that must be performed
    asynchronously with regular bytecode execution, but that still need
    to occur between two opcodes, not at a completely random time.
    c         C   s   | |  _  d  S(   N(   R   (   R   R   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR     s    c         C   s   |  i  i i |   d S(   s   Request for the action to be run before the next opcode.
        The action must have been registered at space initalization time.N(   R   RX   R%   (   R   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR%     s    c         C   s   d d k  l } |  | _ d S(   s+  Bit of a hack: fire() the action but only the next time the GIL
        is released and re-acquired (i.e. after a potential thread switch).
        Don't call this if threads are not enabled.  Currently limited to
        one action (i.e. reserved for CheckSignalAction from module/signal).
        i(   t
   spacestateN(   t   pypy.module.thread.gilR   t   action_after_thread_switch(   R   R   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   fire_after_thread_switch  s    c         C   s   d S(   s   To be overridden.N(    (   R   t   executioncontextR	   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR{     s    (	   R=   R>   R   RH   R   R   R%   R   R{   (    (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR     s   				R   c           B   s   e  Z d  Z RS(   sd   Abstract base class for actions that occur automatically
    every sys.checkinterval bytecodes.
    (   R=   R>   R   (    (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR     s   t   UserDelActionc           B   s2   e  Z d  Z d   Z d   Z d   Z d   Z RS(   sU  An action that invokes all pending app-level __del__() method.
    This is done as an action instead of immediately when the
    interp-level __del__() is invoked, because the latter can occur more
    or less anywhere in the middle of code that might not be happy with
    random app-level code mutating data structures under its feet.
    c         C   s/   t  i |  |  g  |  _ g  |  _ d |  _ d  S(   Ni    (   R   R   t   dying_objects_wt
   weakrefs_wt   finalizers_lock_count(   R   R   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR     s    		c         C   s   |  i  i |  |  i   d  S(   N(   R   R:   R%   (   R   t   w_obj(    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   register_dying_object  s    c         C   s   |  i  i |  |  i   d  S(   N(   R   R:   R%   (   R   t   w_ref(    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyt   register_weakref_callback  s    c   	      C   s   |  i  d j o d  Sn |  i } g  |  _ |  i } x t t |   D]m } | | } d  | | <y | i |  Wn5 t j
 o) } | i | d |  | i	 |  n X| i
   qF W|  i } g  |  _ x. t t |   D] } | | } | i   q Wd  S(   Ni    s   method __del__ of (   R   R   R   t   rangeR5   R   t   userdelR   t   write_unraisablet   cleart   _call_builtin_destructorR   t   activate_callback(	   R   R   R	   t	   pending_wR   t   iR   t   eR   (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR{     s*    			 

		 
(   R=   R>   R   R   R   R   R{   (    (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR     s
   			t   FrameTraceActionc           B   s   e  Z d  Z d   Z RS(   s;   An action that calls the local trace functions (w_f_trace).c   
      C   s!  | i  d  j p
 | i o d  Sn | i } | i | i j o | i j  n o4 | i | i j o | i | d |  i	 i
  qn|t | i  d } d } | i } d } | i } x | d j ow t | |  }	 | |	 | i j o Pn | |	 7} |	 o | | _ n | t | | d  7} | d 7} | d 8} q W| d j op x` t oX | d 8} | d j  o Pn | t | |  7} t | | d  o Pn | d 7} qOW| | _ n t i | _ | i | i j o& | | _ | i | d |  i	 i
  n | i | _ |  i	 i i   d  S(   Nt   linei   i    i   (   Ro   R   R   t   pycodet   instr_lbt
   last_instrt   instr_ubt
   instr_prevR!   R   R"   R5   t	   co_lnotabt   co_firstlinenot   ordRS   R   R   t   f_linenoR$   R%   (
   R   R   R	   t   codet   sizet   addrR   t   pt   linenot   c(    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR{     sN    	'!		 

 
	(   R=   R>   R   R{   (    (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pyR     s   (   R   t   pypy.interpreter.miscutilsR    t   pypy.interpreter.errorR   t   pypy.rlib.rarithmeticR   t   pypy.rlib.unrollR   t	   pypy.rlibR   R   R   R   R   R   R   R   R   R   R   (    (    (    s5   /64/home/arigo/u/pypy/interpreter/executioncontext.pys   <module>   s   	 ;I1
