
    <Yf              
          d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z		 ddl
mZ ddlmZ  G d de      Zg dZej$                  ZdZdZi ag d	Zdd
dddd
dddd	Zddd
dddd
ddd	ZdddddZdZdZdddZdZdZedediZ 	  e	jB                  d      jD                  Z#d Z&d Z'd Z(d Z)d Z*d Z+dSd Z,d! Z-d" Z.d# Z/	 d$ Z0dTd%Z1 G d& d'      Z2 G d( d)e3      Z4 G d* d+e4      Z5 G d, d-e5      Z6 G d. d/e4      Z7 G d0 d1e4      Z8 G d2 d3      Z9 G d4 d5e9      Z: G d6 d7e:      Z; G d8 d9e:      Z< G d: d;e9      Z= G d< d=e=      Z> G d> d?e=      Z?d@ Z@dUdAZAdVdBZBdC ZCdD ZDdE ZEedUdF       ZFdG ZGdH ZHdI ZIdJ ZJdK ZKdL ZLdM ZMdN ZNdO ZOdP ZP ePd       dQ ZQeRdRk(  r eQ        yy# e$ r
 ddlmZ Y w xY w# e$$ r!  e	jB                  d      jJ                  Z#Y ?w xY w)Wz(
yappi.py - Yet Another Python Profiler
    N)	get_ident)contextmanagerc                       e Zd Zy)
YappiErrorN)__name__
__module____qualname__     E/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/yappi.pyr   r      s    r   r   )startstopget_func_statsget_thread_statsclear_stats
is_runningget_clock_timeget_clock_typeset_clock_typeget_clock_infoget_mem_usageset_context_backend   )namencallttottsubtavg            )	r   	callcount	totaltimesubtimeavgtimer   r   r   r   
            )	ascendingasc
descendingdescr$   r.   )WALLCPUNATIVE_THREADGREENLET)r   c                 L    | j                         } | |vrt        d|  d      | S )NzInvalid SortType parameter: ''lowerr   )	sort_typelists     r   _validate_sorttyper9   R   s1    !I81EFFr   c                 T    | j                         } | t        vrt        d|  d      | S )NzInvalid SortOrder parameter: 'r4   )r6   SORT_ORDERSr   )
sort_orders    r   _validate_sortorderr=   Y   s3    !!#J$9*QGHHr   c                 J    | j                         } | |vrt        d|  d      y )NzInvalid Column name: 'r4   r5   )r   r8   s     r   _validate_columnsr?   `   s/    ::<D41$q9:: r   c                      	 t         j                  t                  } | j                  j                  S # t
        $ r Y yw xY w)z
    We don't use threading.current_thread() because it will deadlock if
    called when profiling threading._active_limbo_lock.acquire().
    See: #Issue48.
    N)	threading_activer   	__class__r   KeyError)current_threads    r   _ctx_name_callbackrF   f   s=    "**9;7''000 s   03 	??c                 2    t        j                  | ||       y)z
    _profile_thread_callback will only be called once per-thread. _yappi will detect
    the new thread and changes the profilefunc param of the ThreadState
    structure. This is an internal function please don't mess with it.
    N)_yappi_profile_event)frameeventargs      r   _profile_thread_callbackrM   t   s     %,r   c                  ~    	 ddl m fd}fd}||fS # t        $ r} t        dt	        |              d} ~ ww xY w)as  
    Returns two functions:
    - one that can identify unique greenlets. Identity of a greenlet
      cannot be reused once a greenlet dies. 'id(greenlet)' cannot be used because
      'id' returns an identifier that can be reused once a greenlet object is garbage
      collected.
    - one that can return the name of the greenlet class used to spawn the greenlet
    r   
getcurrentz'greenlet' import failed with: Nc                  V            } t        | dd       }|t               }|| _        |S )N
_yappi_tid)getattrGREENLET_COUNTERrR   )curr_greenletid_rP   s     r   _get_greenlet_idz4_create_greenlet_callbacks.<locals>._get_greenlet_id   s2    "m\48;"$C'*M$
r   c                  :             j                   j                  S N)rC   r   rO   s   r   _get_greenlet_namez6_create_greenlet_callbacks.<locals>._get_greenlet_name   s    |%%...r   )greenletrP   ImportErrorr   repr)excrW   rZ   rP   s      @r   _create_greenlet_callbacksr_   }   sO    H'/ ///  H:49+FGGHs    	<7<c                 l    d}|dkD  r,dd|z  z   dz   }|| z  }t        |      |k  r	 |S |dz  }|dkD  r,S )z5
    function to prettify time columns in stats.
    r    r   z%0.z%dfr(   )len)xCOL_SIZE_rprecision_fmtss        r   _fftrh      s]     K?t{++c1AJq6XH 	q ? Hr   c                 $    | r| d| S d|||fz  S )N.z%s:%d %sr
   )builtinmodulelinenor   s       r   _func_fullnamern      s(    4&!!VVT222r   c                    t        | t              st        d|  d      t        |t              st        d| d      t	        |      st        d      | j
                  t        vryt        |      }|D ]+  }t        |t        j                        rt        d| d       t        j                  t        | j
                           |v S )N*Argument 'stat' shall be a YStat object. ()z*Argument 'modules' is not a list object. (z#Argument 'modules' cannot be empty.FzNon-module item in 'modules'. ()
isinstanceYStatr   r8   rb   	full_name_fn_descriptor_dictsettypes
ModuleTypeinspect	getmodule)statmodulesrl   s      r   module_matchesr}      s    dE"8a@
 	
 gt$8	C
 	
 w<>??~~00'lG J&%"2"23>vhaHIIJ 0@AWLLr   c                    t        | t              st        d|  d      t        |t              st        d| d      t	        |      st        d      | j
                  t        vryt        |      }|j                         D ]E  }t        |      st        d| d      t        |dd      s+|j                  |j                         G 	 t        | j
                     |v S # t        $ r Y yw xY w)	aY  
    This function will not work with stats that are saved and loaded. That is 
    because current API of loading stats is as following:
    yappi.get_func_stats(filter_callback=_filter).add('dummy.ys').print_all()

    funcs: is an iterable that selects functions via method descriptor/bound method
        or function object. selector type depends on the function object: If function
        is a builtin method, you can use method_descriptor. If it is a builtin function
        you can select it like e.g: `time.sleep`. For other cases you could use anything 
        that has a code object.
    rp   rq   z(Argument 'funcs' is not a list object. (z!Argument 'funcs' cannot be empty.FzNon-callable item in 'funcs'. (__code__N)rr   rs   r   r8   rb   rt   ru   rv   copycallablerS   addr   	TypeError)r{   funcsfuncs      r   func_matchesr      s     dE"8a@
 	
 eT"6ugQ?
 	
 u:<==~~00JE

 %~>tfAFGG 4T*IIdmm$%"4>>2e;;  s   C! !	C-,C-c                    ddl m} 	 t        | t              st	        d      dd l} G d d      }d }i } |t              }| D ]M  }|j                  D ]<  }|j                  |j                  |j                  |j                  f||    ||      <   > O | D ]=  }|j                  |j                  |j                  |j                  ||   f| ||      <   ? |j                   ||            S )Nr   )defaultdictz-Source stats must be derived from YFuncStats.c                       e Zd Zd Zd Zy)$convert2pstats.<locals>._PStatHolderc                     || _         y rY   )stats)selfds     r   __init__z-convert2pstats.<locals>._PStatHolder.__init__,  s	    DJr   c                      y rY   r
   r   s    r   create_statsz1convert2pstats.<locals>._PStatHolder.create_stats/  s    r   N)r   r   r	   r   r   r
   r   r   _PStatHolderr   *  s    		r   r   c                 H    | j                   | j                  | j                  fS rY   )rl   rm   r   )fss    r   pstat_idz convert2pstats.<locals>.pstat_id2  s    		299bgg..r   )collectionsr   rr   
YFuncStatsr   pstatsdictchildrenr   nactualcallr   r   Stats)	r   r   r   r   r   _pdict_callersr   cts	            r   convert2pstatsr     s    ' eZ(HII / F 4 H J++ 	JB hhI RL" 	JJ  
HHNNGGGGRL 
x|
 <<V,--r   c                       fd}|S )a  
    A profile decorator that can be used to profile a single call.

    We need to clear_stats() on entry/exit of the function unfortunately.
    As yappi is a per-interpreter resource, we cannot simply resume profiling
    session upon exit of the function, that is because we _may_ simply change
    start() params which may differ from the paused session that may cause instable
    results. So, if you use a decorator, then global profiling may return bogus
    results or no results at all.
    c                 (      fd}d _         |S )Nc            
         j                   dk(  r"t                t               t        d       xj                   dz  c_         	  | i |xj                   dz  c_         j                   dk(  r	 t	                t
        j                  j                  t               t
        j                  j                  dj                  t        t               d   j                        j                                      t
        j                  j                  t               t               j                          n t                      t                S S # t                w xY w# xj                   dz  c_         j                   dk(  r	 t	                t
        j                  j                  t               t
        j                  j                  dj                  t        t               d   j                        j                                      t
        j                  j                  t               t               j                          n t                      t                w # t                w xY ww xY w)Nr   F)profile_threadsr(   zExecuted in {} {} clock seconds)
_rec_levelr   r   r   r   sysstdoutwriteLINESEPformatrh   r   r   upperr   	print_all)argskwargs
clock_typer   profile_builtinsreturn_callbacks     r   wrapperz.profile.<locals>._profile_dec.<locals>.wrapperY  s   !#z*&>OOq O&T,V,1$ ??a'&*2JJ,,W5JJ,, A H H$()9);A)>)C)C %+,6,<,<,>!"  JJ,,W5*,668+D.2BC#! (  $) 1$ ??a'&*2JJ,,W5JJ,, A H H$()9);A)>)C)C %+,6,<,<,>!"  JJ,,W5*,668+D.2BC#! (s2   	E' 5CEE$'%I?CI/$I?/I;;I?r   )r   )r   r   r   r   r   s   ` r   _profile_deczprofile.<locals>._profile_decW  s    	&> r   r
   )r   r   r   r   s   ``` r   profiler   K  s    "H r   c                   4    e Zd ZdZdZdZdZd Zd Zd Z	d Z
y	)

StatStringz9
    Class to prettify/trim a profile result column.
    z..r(   r   c                 $    t        |      | _        y rY   )str_s)r   rg   s     r   r   zStatString.__init__  s    a&r   c                    t        | j                        |kD  r|| j                  k(  rD| j                  | d  | _        | j                  | j                  t        | j                        d  z   S || j                  k(  rD| j                  d | | _        | j                  d t        | j                          | j                  z   S | j                  d|t        | j                        z
  z  z   S )N )rb   r   _LEFT
_TRAIL_DOT_RIGHT)r   length	directions      r   _trimzStatString._trim  s    L6!DJJ&''6'(+T__1E1F)GGGdkk)'''6*ww5T__!5 56HHww##dgg,!6788r   c                 :    | j                  || j                        S rY   )r   r   r   r   s     r   ltrimzStatString.ltrim  s    zz&$**--r   c                 :    | j                  || j                        S rY   )r   r   r   s     r   rtrimzStatString.rtrim  s    zz&$++..r   N)r   r   r	   __doc__r   r   r   r   r   r   r   r
   r   r   r   r   ~  s,     JEF9./r   r   c                   0     e Zd ZdZi Z fdZ fdZ xZS )rs   z
    Class to hold a profile result line in a dict object, which all items can also be accessed as
    instance attributes where their attribute name is the given key. Mimicked NamedTuples.
    c                     t         |           | j                  j                         D ]  \  }}t	        | |||           y rY   )superr   _KEYSitemssetattr)r   valueskeyirC   s       r   r   zYStat.__init__  s>    jj&&( 	*FCD#vay)	*r   c                 J    || | j                   |   <   t        | 	  ||       y rY   )r   r   __setattr__)r   r   valuerC   s      r   r   zYStat.__setattr__  s%    !&TZZD%(r   )r   r   r	   r   r   r   r   __classcell__rC   s   @r   rs   rs     s     E*) )r   rs   c                   ^    e Zd ZdZdddddddd	d
dddddddZd Zd Zd Zd Zd Z	d Z
d Zy)	YFuncStatz7
    Class holding information for function stats.
    r   r(   r   r   r)   r*   r    r!      	   r'         r"      )r   rl   rm   r   r   rk   r   r   indexr   ctx_idctx_nametagr   rt   c                 :    |y| j                   |j                   k(  S NF)rt   r   others     r   __eq__zYFuncStat.__eq__  s    =~~00r   c                     | |k(   S rY   r
   r   s     r   __ne__zYFuncStat.__ne__      5=  r   c                    | |u r| S | xj                   |j                   z  c_         | xj                  |j                  z  c_        | xj                  |j                  z  c_        | xj                  |j                  z  c_        | j                  | j                   z  | _        |j
                  D ]  }| j
                  j                  |        | S rY   )r   r   r   r   r   r   append)r   r   other_child_stats      r   __add__zYFuncStat.__add__  s     5=K

ekk!
E---		UZZ			UZZ	II

*	 % 	3MM  !12	3 r   c                 ,    t        | j                        S rY   )hashrt   r   s    r   __hash__zYFuncStat.__hash__  s    DNN##r   c                 T    | j                   dk(  ry| j                  | j                   k7  S )Nr   F)r   r   r   s    r   is_recursivezYFuncStat.is_recursive  s*     q zzT----r   c                     t         j                  j                  | j                        | _        t	        | j
                  | j                  | j                  | j                        | _        | S rY   )	ospathbasenamerl   rn   rk   rm   r   rt   r   s    r   
strip_dirszYFuncStat.strip_dirs  sH    gg&&t{{3'LL$++t{{DII
 r   c           	      b   t        |j                               D ]  }||   \  }}|dk(  rL|j                  t        | j                        j                  |             |j                  dt        z         ]|dk(  r| j                         rC|j                  t        d| j                  | j                  fz        j                  |             n3|j                  t        | j                        j                  |             |j                  dt        z         |dk(  rW|j                  t        t        | j                  |            j                  |             |j                  dt        z         ^|dk(  rW|j                  t        t        | j                  |            j                  |             |j                  dt        z         |dk(  s|j                  t        t        | j                  |            j                  |               |j                  t               y )Nr   r   r   z%d/%dr   r   r   )sortedkeysr   r   rt   r   
COLUMN_GAPr   r   r   r   rh   r   r   r   r   r   outcolumnsrc   titlesizes         r   _printzYFuncStat._print  s   ' 	IA!!*KE4		*T^^4::4@A		#
*+'!$$&II"7djj$:J:J-K#K %%*U4[
 IIj4::4@A		#
*+&		*T$))T%:;AA$GH		#
*+&		*T$))T%:;AA$GH		#
*+&		*T$))T%:;AA$GH+	I, 			'r   N)r   r   r	   r   r   r   r   r   r   r   r   r  r
   r   r   r   r     s`     E$1
!"$.r   r   c                   2    e Zd ZdZdddddddd	d
dddZd Zy)YChildFuncStatz@
    Class holding information for children function stats.
    r   r(   r   r   r)   r*   r    r!   r   r   r'   )r   r   r   r   r   r   rk   rt   rl   rm   r   c                 B   || S | xj                   |j                   z  c_         | xj                  |j                  z  c_        | xj                  |j                  z  c_        | xj                  |j                  z  c_        | j                  | j                  z  | _        | S rY   )r   r   r   r   r   r   s     r   r   zYChildFuncStat.__add__  sr    =KE---

ekk!
		UZZ			UZZ	II

*	r   N)r   r   r	   r   r   r   r
   r   r   r  r    s6     Er   r  c                   8    e Zd ZdZddddddZd Zd	 Zd
 Zd Zy)YThreadStat5
    Class holding information for thread stats.
    r   r(   r   r   r)   )r   idtidr   sched_countc                 :    |y| j                   |j                   k(  S r   r  r   s     r   r   zYThreadStat.__eq__0      =ww%((""r   c                     | |k(   S rY   r
   r   s     r   r   zYThreadStat.__ne__5  r   r   c                 ,    t        | j                        S rY   r   r  r   r   r   s      r   r   zYThreadStat.__hash__8      DGG}r   c           	         t        |j                               D ]  }||   \  }}|dk(  rL|j                  t        | j                        j                  |             |j                  dt        z         ]|dk(  rL|j                  t        | j                        j                  |             |j                  dt        z         |dk(  rL|j                  t        | j                        j                  |             |j                  dt        z         |dk(  rW|j                  t        t        | j                  |            j                  |             |j                  dt        z         [|dk(  sb|j                  t        | j                        j                  |              |j                  t               y )Nr   r   r  r	  r   scnt)r   r   r   r   r   r   r   r  r   r	  rh   r   r
  r   r   s         r   r  zYThreadStat._print;  sI   ' 	DA!!*KE4		*TYY/55d;<		#
*+$		*TWW-33D9:		#
*+%		*TXX.44T:;		#
*+&		*T$))T%:;AA$GH		#
*+&		*T%5%56<<TBC	D  			'r   N	r   r   r	   r   r   r   r   r   r  r
   r   r   r  r  $  s3     E#
!r   r  c                   6    e Zd ZdZdddddZd Zd Zd	 Zd
 Zy)YGreenletStatr  r   r(   r   r)   )r   r  r   r
  c                 :    |y| j                   |j                   k(  S r   r  r   s     r   r   zYGreenletStat.__eq__Z  r  r   c                     | |k(   S rY   r
   r   s     r   r   zYGreenletStat.__ne___  r   r   c                 ,    t        | j                        S rY   r  r  s      r   r   zYGreenletStat.__hash__b  r  r   c           	         t        |j                               D ]C  }||   \  }}|dk(  rL|j                  t        | j                        j                  |             |j                  dt        z         ]|dk(  rL|j                  t        | j                        j                  |             |j                  dt        z         |dk(  rW|j                  t        t        | j                  |            j                  |             |j                  dt        z         
|dk(  s|j                  t        | j                        j                  |             F |j                  t               y )Nr   r   r  r   r  )r   r   r   r   r   r   r   r  r   rh   r   r
  r   r   s         r   r  zYGreenletStat._printe  s   ' 	DA!!*KE4		*TYY/55d;<		#
*+$		*TWW-33D9:		#
*+&		*T$))T%:;AA$GH		#
*+&		*T%5%56<<TBC	D 			'r   Nr  r
   r   r   r  r  O  s0     	E#
!r   r  c                   ^    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zy)YStatszc
    Main Stats class where we collect the information from _yappi and apply the user filters.
    c                 .    d | _         i | _        g | _        y rY   )_clock_type_as_dict_as_listr   s    r   r   zYStats.__init__{  s    r   c                 l    t        j                         | _        | j                  t        t
               | S rY   )rH   r   r  sortDEFAULT_SORT_TYPEDEFAULT_SORT_ORDERr   s    r   getz
YStats.get  s(    !002		#%78r   c                 \    | j                   j                  fd|t        d   k(         | S )Nc                 Z    t        |    t              r|    j                         S |    S rY   )rr   r   r6   )r{   r7   s    r   <lambda>zYStats.sort.<locals>.<lambda>  s1    !$y/37 ")_224 =A)_ r   r.   )r   reverse)r!  r#  r;   )r   r7   r<   s    ` r   r#  zYStats.sort  s4    M;v#66 	 	

 r   c                 V    | j                   d d = | j                  j                          y rY   )r!  r   clearr   s    r   r,  zYStats.clear  s    MM!r   c                 2    t        | j                        dk(  S Nr   rb   r!  r   s    r   emptyzYStats.empty  s    DMM"a'(r   c                 @    	 | j                   |   S # t        $ r Y y w xY wrY   )r!  
IndexError)r   r   s     r   __getitem__zYStats.__getitem__  s(    	==%% 		s    	c                 8    | j                   j                  |      S rY   )r!  countr   items     r   r5  zYStats.count  s    }}""4((r   c                 ,    t        | j                        S rY   )iterr!  r   s    r   __iter__zYStats.__iter__  s    DMM""r   c                 ,    t        | j                        S rY   r/  r   s    r   __len__zYStats.__len__  s    4==!!r   c                 T    | j                   j                         }| j                  |= |S rY   )r!  popr   r6  s     r   r>  z
YStats.pop  s%    }}  "MM$r   c                     | j                   j                  |      }|r||z  }y | j                  j                  |       || j                   |<   y rY   )r   r&  r!  r   )r   r7  existings      r   r   zYStats.append  sH     ==$$T*HT""dr   c                 &   t        |j                               D ]`  }||   \  }}t        |      |kD  rt        d||fz        |j	                  |       |j	                  dt
        |z   t        |      z
  z         b |j	                  t               y )Nz,Column title exceeds available length[%s:%d]r   )r   r   rb   r   r   r   r   r   s         r   _print_headerzYStats._print_header  s    ' 	>A!!*KE45zD  !ODM"" # #IIeIIcZ$.U;<=	> 			'r   c                 >    | D ]  }| j                  |      dkD  s y y)zY
        Check for basic sanity errors in stats. e.g: Check for duplicate stats.
        r(   FT)r5  )r   rc   s     r   _debug_check_sanityzYStats._debug_check_sanity  s+      	Azz!}q 	 r   N)r   r   r	   r   r   r&  r#  r,  r0  r3  r5  r:  r<  r>  r   rB  rD  r
   r   r   r  r  v  sH    

))#"
#r   r  c                   F     e Zd Z fdZ fdZ fdZ fdZ fdZ xZS )YStatsIndexablec                 0    t         |           i | _        y rY   )r   r   _additional_indexingr   rC   s    r   r   zYStatsIndexable.__init__  s    $&!r   c                 V    t         |           | j                  j                          y rY   )r   r,  rH  rI  s    r   r,  zYStatsIndexable.clear  s    !!'')r   c                     t         |          }| j                  j                  |j                  d        | j                  j                  |j                  d        |S rY   )r   r>  rH  r   rt   r   r7  rC   s     r   r>  zYStatsIndexable.pop  sG    w{}!!%%djj$7!!%%dnnd;r   c                     t         |   |       | j                  j                  |j                  |       | j                  j                  |j
                  |       y rY   )r   r   rH  
setdefaultr   rt   rL  s     r   r   zYStatsIndexable.append  sC    t!!,,TZZ>!!,,T^^TBr   c                 ^   t        |t              r| j                  j                  |d       S t        |t              r| j                  j                  |d       S t        |t
              st        |t              r&| j                  j                  |j                  d       S t        | %  |      S rY   )
rr   intrH  r&  r   r   r  r   r   r3  )r   r   rC   s     r   r3  zYStatsIndexable.__getitem__  s    c3,,00d;;S!,,00d;;Y':c>+J,,00DAAw"3''r   )	r   r   r	   r   r,  r>  r   r3  r   r   s   @r   rF  rF    s"    '*C
( 
(r   rF  c                   R     e Zd Zd
 fd	Zej
                  ddddddfdZd	 Z xZS )YChildFuncStatsc                 v    t        |t              }t        |      }t        |   t        |   t
        |         S rY   )r9   SORT_TYPES_CHILDFUNCSTATSr=   r   r#  r;   r   r7   r<   rC   s      r   r#  zYChildFuncStats.sort  s:    &y2KL	(4
w|%i0+j2I
 	
r   r   $   r   r*   r   r   r   r   r   r   r   r(   r   r   r)   c                    | j                         st        |      dk(  ry|j                         D ]  \  }}t        |d   t                |j                  t               | j                  ||       | D ]  }|j                  ||        y)zh
        Prints all of the child function profiler results to a given file. (stdout by default)
        r   N)	r0  rb   r   r?   COLUMNS_FUNCSTATSr   r   rB  r  r   r   r   _colr{   s         r   r   zYChildFuncStats.print_all  s~     ::<3w<1,mmo 	9FAsc!f&78	9 			'3( 	&DKKW%	&r   c                 4    | D ]  }|j                           | S rY   )r   r   r{   s     r   r   zYChildFuncStats.strip_dirs  s      	DOO	r   r.   )	r   r   r	   r#  r   r   r   r   r   r   s   @r   rR  rR    s/    
 JJ
&2r   rR  c                        e Zd ZdZdZdZdgZg dZg f fd	Zd Z	i df fd	Z
d Zd	 Zd
 Zd Zd ZddZddZej&                  ddddddfdZd fd	Zd Z xZS )r   r   NYSTAT)rf  	CALLGRINDPSTATc                 R    t         |           | j                  |       d | _        y rY   )r   r   r   _filter_callback)r   filesrC   s     r   r   zYFuncStats.__init__  s!     $r   c                 h    | D ],  }|j                          |j                  j                          . | S rY   )r   r   rc  s     r   r   zYFuncStats.strip_dirs!  s2     	'DOOMM$$&	' r   c                 l   t        j                          | j                          	 || _        t        j                  | j
                  |       d | _        | D ]  }t               }|j                  D ]}  }| |d      }||j                  |j                  z  }t        |||j                  |j                  |j                  |j                  |j                  fz         }|j!                  |        ||_         t"        
| I         }	t        j&                          |	S # t        j&                          w xY wr.  )rH   _pauser,  rj  enum_func_stats_enumeratorrR  r   r   r   r  rk   rt   rl   rm   r   r   r   r&  _resume)r   filterfilter_callbackr{   _childs	child_tplrstatr   cfstatresultrC   s             r   r&  zYFuncStats.get'  s   

 	$3D!""4#3#3V<$(D!  ()+!% +I 1.E
 }  ::3D+! !MM!OO!LL!LL!JJ% 	F NN6*)+* !(/(0 W[]FNN NNs   C D D3c                    |\  }}}}}}}}	}
}}}}}t        t        |      |||      }||z  }t        |||fz         }|t        |<   t        j
                  j                  |j                        dk(  s|j                  dk(  ry t        |j                        |_        | j                  r| j                  |      sy | j                  |       | j                  |j                  k  r|j                  | _        y y )Nzyappi.pyrH   )rn   boolr   ru   r   r   r   rl   rk   rj  r   _idx_maxr   )r   
stat_entryfnamefmoduleflinenofncallfnactualcallfbuiltinfttotftsubfindex	fchildrenfctxidfctxnameftagffn_descriptor
ffull_nameftavgfstats                      r   rp  zYFuncStats._enumeratorM  s     IS	FwxIvx~ $DNGWeL
*z'::;*8J' 77LL
 <<83U]]+  ((/E ==5;;&!KKDM 'r   c                 6   	 t        j                  |      \  }}| j	                         s7| j
                  |k7  r(| j
                  t        d| j
                  d|d      || _        |D ]>  }|| vs| xj                  dz  c_        | j                  |_        | j                  |       @ |D ]1  }|j                  D ]   }| |j                     j                  |_        " 3 |D ]  }| |j                     }||z  } y #  t        d|j                   d      xY w)Nz2Unable to load the saved profile information from rj   zAClock type mismatch between current and saved profiler sessions.[,]r(   )pickleloadr   r   r0  r  r{  r   r   r   rt   )r   filesaved_statssaved_clock_type
saved_statsaved_child_statsaved_stat_in_currs          r   _add_from_YSTATzYFuncStats._add_from_YSTATk  s@   	,2KK,=)K) zz|#338H8H8T %%'7"9 : : , & 	(J%"#'==
 J'		( & 	PJ$.$7$7 P  *..>.H.H)I)O)O &	P	P & 	-J!%j&:&:!;*,	-;	DTYYKqQ s   C= =Dc                     t        |d      5 }t        j                  | | j                  f|t               d d d        y # 1 sw Y   y xY w)Nwb)openr  dumpr  YPICKLE_PROTOCOL)r   r   ra   s      r   _save_as_YSTATzYFuncStats._save_as_YSTAT  sC    $ 	GKKt//0!5EF	G 	G 	Gs	   (>Ac                 <    t        |       }|j                  |       y)z:
        Save the profiling information as PSTAT.
        N)r   
dump_stats)r   r   _statss      r   _save_as_PSTATzYFuncStats._save_as_PSTAT  s      %$r   c                    ddt        j                         dj                  t        j                        fz  }|g}dg}dg}g }| D ]  }|d|j
                  |j                  fz  gz  }|d|j
                  |j                  |j                  |j                  fz  gz  }|j                  |j
                         |j                  D ]  }|j
                  |v r|d|j
                  |j                  fz  gz  }|d|j
                  |j                  |j                  |j                  fz  gz  }|j                  |j
                           |||z   z  }| D ]  }dd|j
                  z  d|j
                  z  g}	|	|j                   dt        |j                  d	z         gz  }	|j                  D ]K  }|	d
|j
                  z  d|j
                  z  d|j                  z  dt        |j                  d	z        z  gz  }	M ||	z  } t        |d      5 }
|
j!                  dj                  |             ddd       y# 1 sw Y   yxY w)zz
        Writes all the function stats in a callgrind-style format to the given
        file. (stdout by default)
        z>version: 1
creator: %s
pid: %d
cmd:  %s
part: 1

events: Ticksyappir    z
fl=(%d) %szfn=(%d) %s %s:%szfl=(%d)zfn=(%d)g    .Azcfl=(%d)zcfn=(%d)z
calls=%d 0z0 %dw
N)r   getpidjoinr   argvr   rl   r   rm   r   r   rP  r   r   r   r  r   )r   r   headerlinesfile_idsfunc_idsfunc_idx_list	func_statchild
func_statsra   s              r   _save_as_CALLGRINDzYFuncStats._save_as_CALLGRIND  ss   
 \biik388CHH#567  44 	2I):J:J(KKLLH"OOY^^Y5E5E$$&  H   1 #++ 2;;-/\U[[%,,,GGHH&UZZ*   $$U[[12	20 	H$$  	 IIOO+IOO+J
 ##$Ac)..3*>&?%@A J
 #++ ,, 5;;.Sc!122	 
 ZE%	 ( $_ 	&GGDIIe$%	& 	& 	&s   !H88Ic                 .   |j                         }|| j                  vrt        d      t        |t              r|g}|D ]/  }t        |d      5 }t        | d|       } ||       d d d        1 | j                  t        t              S # 1 sw Y   UxY w)Nz3Loading from (%s) format is not possible currently.rb
_add_from_)r  )
r   _SUPPORTED_LOAD_FORMATSNotImplementedErrorrr   r   r  rS   r#  r$  r%  )r   rk  typefdra   add_funcs         r   r   zYFuncStats.add  s    zz|t333%E  eS!E  	!Bb$ !1"4:dV)<=a ! !	!
 yy*,>??	! !s   BB	c                     |j                         }|| j                  vrt        d| d      t        | d|       } ||       y )NzSaving in "z#" format is not possible currently.	_save_as_)r   )r   _SUPPORTED_SAVE_FORMATSr  rS   )r   r   r  	save_funcs       r   savezYFuncStats.save  sR    zz|t333%dV#FG  DIdV"45	tr   rV  rX  rY  rZ  r[  r\  c                 (   | j                         ry|j                         D ]  \  }}t        |d   t                |j	                  t
               |j	                  d| j                  j                                 |j	                  t
               |j	                  d| j                   d| j                          |j	                  t
               |j	                  t
               | j                  ||       | D ]  }|j                  ||        y)zb
        Prints all of the function profiler results to a given file. (stdout by default)
        Nr   zClock type: zOrdered by: z, )r0  r   r?   r^  r   r   r  r   
_sort_type_sort_orderrB  r  r_  s         r   r   zYFuncStats.print_all  s     ::<mmo 	9FAsc!f&78	9 			'		L!1!1!7!7!9 :;<		'		L 1D4D4D3EFG		'		'3( 	&DKKW%	&r   c                     t        |t              }t        |      }|| _        || _        t
        |   t        |   t        |         S rY   )r9   SORT_TYPES_FUNCSTATSr=   r  r  r   r#  r;   rU  s      r   r#  zYFuncStats.sort  sI    &y2FG	(4
#%w| +[-D
 	
r   c                 ^   | j                         ry t        j                  }d}| D ]  }|j                  d|j                  z         |j                  t
               |j                  d|j                          |j                  t
               |j                  d|j                  |j                  fz         |j                  t
               |j                  dt        |j                                |j                  t
               |j                  dt        |j                                |j                  t
               |j                  d       |j                  t
               |j                  D ]  }|j                  t
               |j                  d|z         |j                  d|j                  z         |j                  t
               |j                  d|z         |j                  d	|j                          |j                  t
               |j                  d|z         |j                  d|j                  |j                  fz         |j                  t
               |j                  d|z         |j                  dt        |j                                |j                  t
               |j                  d|z         |j                  dt        |j                                |j                  t
                |j                  t
                y )
Nr*   z	index: %dzfull_name: zncall: %d/%dzttot: ztsub: z
children: r   zchild_full_name: )r0  r   r   r   r   r   rt   r   r   rh   r   r   r   )r   consoleCHILD_STATS_LEFT_MARGINr{   
child_stats        r   debug_printzYFuncStats.debug_print  s\   ::<**"#  	#DMM+

23MM'"MMK'789MM'"MM.DJJ8H8H+IIJMM'"MMF4		?"345MM'"MMF4		?"345MM'"MM,'MM'""mm '
g&c$;;<kJ,<,<<=g&c$;;< 1*2F2F1GHIg&c$;;<"j&6&6
8N8N%OO g&c$;;<tJOO'<&=>?g&c$;;<tJOO'<&=>?g&%'& MM'"A 	#r   )ystatrd  )r   r   r	   r{  r  r  r  r  r   r   r&  rp  r  r  r  r  r   r  r   r   r   r#  r  r   r   s   @r   r   r     s    HJK&i= % T $L(<"-HG >&@@" JJ
&>	
&#r   r   c                   j     e Zd ZdZdZdZdZdZ fdZd Z	d fd	Z
ej                  dfdZd Z xZS )_YContextStatsNc                    t        j                         }| j                  |k7  r8t        d| j                  j	                         d|j	                         d      t        j
                          | j                          	 t        j                  | j                         t        | )         }t        j                          |S # t        j                          w xY w)NzCannot retrieve stats for 'z' when backend is set as 'r4   )rH   get_context_backend_BACKENDr   r6   rn  r,  enum_context_statsrp  r   r&  rq  )r   backendrx  rC   s      r   r&  z_YContextStats.getP  s    ,,.==G#$$&9 
 	

	%%d&6&67W[]FNN NNs   -C Cc                 H    | j                  |      }| j                  |       y rY   )_STAT_CLASSr   )r   r|  tstats      r   rp  z_YContextStats._enumeratorb  s      ,Er   c                     t        || j                        }t        |      }t        |   | j                  |   t
        |         S rY   )r9   _SORT_TYPESr=   r   r#  r;   rU  s      r   r#  z_YContextStats.sortf  sE    &y$2B2BC	(4
w|Y'Z)@
 	
r   c                 $   || j                   }| j                         ry|j                         D ]  \  }}t        |d   | j                           |j                  t               | j                  ||       | D ]  }|j                  ||        y)z`
        Prints all of the thread profiler results to a given file. (stdout by default)
        Nr   )	_DEFAULT_PRINT_COLUMNSr0  r   r?   _ALL_COLUMNSr   r   rB  r  r_  s         r   r   z_YContextStats.print_alln  s    
 ?11G::<mmo 	9FAsc!fd&7&78	9 			'3( 	&DKKW%	&r   c                      y rY   r
   r   s    r   r   z_YContextStats.strip_dirs  s    r   rd  )r   r   r	   r  r  r  r  r  r&  rp  r#  r   r   r   r   r   r   s   @r   r  r  H  sA    HKK!L$
  JJ &&r   r  c                   @    e Zd ZeZeZddddddddZddd	d
ddZg dZ	y)YThreadStatsr   r(   r   r   r)   )r   r  r	  r$   
schedcountr   r  r      r  r*   )r	  r   rZ  r  r'   r\  )r   r  r	  r   r  N)
r   r   r	   r1   r  r  r  r  r  r  r
   r   r   r  r    sH    HKK  9Lr   r  c                   <    e Zd ZeZeZdddddddZdddd	d
Zg dZ	y)YGreenletStatsr   r(   r   r)   )r   r  r$   r  r   r  r  r  rZ  r  )r   r(   r   r   )r   r  r   r  N)
r   r   r	   r2   r  r  r  r  r  r  r
   r   r   r  r    sB    HKK 	 2Lr   r  c                  <    t        t        j                               S )zC
    Returns true if the profiler is running, false otherwise.
    )rz  rH   r   r
   r   r   r   r     s     !!#$$r   c                     t        j                         }|xr	 |t        k(  xs |xr	 |t        k(  }|rt	        j
                  t               t        j                  | |       y)a  
    Start profiler.

    profile_threads: Set to True to profile multiple threads. Set to false
    to profile only the invoking thread. This argument is only respected when
    context backend is 'native_thread' and ignored otherwise.

    profile_greenlets: Set to True to to profile multiple greenlets. Set to
    False to profile only the invoking greenlet. This argument is only respected
    when context backend is 'greenlet' and ignored otherwise.
    N)rH   r  r1   r2   rA   
setprofilerM   r   )builtinsr   profile_greenletsr  profile_contextss        r   r   r     sY     ((*G		5W5 	75'X"5  56
LL+,r   c                     |si }| r| |d<   |r||d<   t        j                          	 t               j                  ||      }t        j                          |S # t        j                          w xY w)a  
    Gets the function profiler results with given filters and returns an iterable.

    filter: is here mainly for backward compat. we will not document it anymore.
    tag, ctx_id: select given tag and ctx_id related stats in C side.
    filter_callback: we could do it like: get_func_stats().filter(). The problem
    with this approach is YFuncStats has an internal list which complicates:
        - delete() operation because list deletions are O(n)
        - sort() and pop() operations currently work on sorted list and they hold the
          list as sorted.
    To preserve above behaviour and have a delete() method, we can use an OrderedDict()
    maybe, but simply that is not worth the effort for an extra filter() call. Maybe
    in the future.
    r   r   )rr  rs  )rH   rn  r   r&  rq  )r   r   rr  rs  r   s        r   r   r     si     
u!x MMO   PL 	s   A A/c                  2    t               j                         S )zV
    Gets the thread profiler results with given filters and returns an iterable.
    )r  r&  r
   r   r   r   r     s     >r   c                  2    t               j                         S )z:
    Gets the greenlet stats captured by the profiler
    )r  r&  r
   r   r   get_greenlet_statsr    s     !!r   c                  V    t        j                          t        j                  d       y)z
    Stop profiler.
    N)rH   r   rA   r  r
   r   r   r   r     s     KKMr   c              #   d   K   t        | ||       	 d t                y# t                w xY ww)a  
    Context manger for profiling block of code.

    Starts profiling before entering the context, and stop profilying when
    exiting from the context.

    Usage:

        with yappi.run():
            print("this call is profiled")

    Warning: don't use this recursively, the inner context will stop profiling
    when exited:

        with yappi.run():
            with yappi.run():
                print("this call will be profiled")
            print("this call will *not* be profiled")
    r  r   r  N)r   r   r  s      r   runr    s+     * 
'+
s   0! 0-0c                      t        j                          	 t        j                          t        j                          y# t        j                          w xY w)z,
    Clears all of the profile results.
    N)rH   rn  r   rq  r
   r   r   r   r   #  s3     MMOs	   ? Ac                  *    t        j                         S )zK
    Returns the current clock time with regard to current clock type.
    )rH   r   r
   r   r   r   r   .         ""r   c                  *    t        j                         S )z+
    Returns the underlying clock type
    )rH   r   r
   r   r   r   r   5  r  r   c                  *    t        j                         S )zj
    Returns a dict containing the OS API used for timing, the precision of the
    underlying clock.
    )rH   r   r
   r   r   r   r   <  s    
   ""r   c                     | j                         } | t        vrt        d|        t        j                  t        |           y)z
    Sets the internal clock type for timing. Profiler shall not have any previous stats.
    Otherwise an exception is thrown.
    zInvalid clock type:N)r   CLOCK_TYPESr   rH   r   )r  s    r   r   r   D  s=    
 ::<D;.tf566
+d+,r   c                  *    t        j                         S )zC
    Returns the internal memory usage of the profiler itself.
    )rH   r   r
   r   r   r   r   P  s     !!r   c                 ,    t        j                  |       S )zx
    Every stat. entry will have a specific tag field and users might be able
    to filter on stats via tag field.
    )rH   set_tag_callback)cbks    r   r  r  W  s    
 ""3''r   c                 6   | j                         } | t        vrt        d|        | t        k(  r.t	               \  }}t        j                  |       t        |       n t        j                  d       t        d       t        j                  t        |           y)a  
    Sets the internal context backend used to track execution context.

    type must be one of 'greenlet' or 'native_thread'. For example:

    >>> import greenlet, yappi
    >>> yappi.set_context_backend("greenlet")

    Setting the context backend will reset any callbacks configured via:
      - set_context_id_callback
      - set_context_name_callback

    The default callbacks for the backend provided will be installed instead.
    Configure the callbacks each time after setting context backend.
    zInvalid backend type: N)	r   BACKEND_TYPESr   r2   r_   rH   set_context_id_callbackset_context_name_callbackr   )r  id_cbkname_cbks      r   r   r   _  s~      ::<D= 1$899x57&&v.!(+&&t,!$'
}T23r   c                 ,    t        j                  |       S )a  
    Use a number other than thread_id to determine the current context.

    The callback must take no arguments and return an integer. For example:

    >>> import greenlet, yappi
    >>> yappi.set_context_id_callback(lambda: id(greenlet.getcurrent()))
    )rH   r  callbacks    r   r  r  ~  s     ))(33r   c                 b    | t        j                  t              S t        j                  |       S )a  
    Set the callback to retrieve current context's name.

    The callback must take no arguments and return a string. For example:

    >>> import greenlet, yappi
    >>> yappi.set_context_name_callback(
    ...     lambda: greenlet.getcurrent().__class__.__name__)

    If the callback cannot return the name at this time but may be able to
    return it later, it should return None.
    )rH   r  rF   r  s    r   r  r    s-     //0BCC++H55r   c            	         ddl m}  dt        j                  j	                  t
        j                  d         z  } | |      }d|_        |j                  dddt        d	 t        D              d
d       |j                  dddddd       |j                  dddd       |j                  dddddd       |j                  dddd dd!       t
        j                  d"d  s%|j                          t        j                  d#       |j                         \  }}|t
        j                  d d  t        t
        j                        dkD  rt
        j                  j                  dt        j                  j!                  t
        j                  d                t#        |j$                         t'        |j(                  |j*                          	 t-        t/        t1        t
        j                  d         j3                         t
        j                  d   d$      t        j4                  d"      j6                  t        j4                  d"      j8                         t;                |j<                  r1t?               }|jA                  |j<                  |jB                         y t?               jE                          tG               jE                          y |j                          y # t;                |j<                  r1t?               }|jA                  |j<                  |jB                         w t?               jE                          tG               jE                          w xY w)%Nr   )OptionParserzV%s [-b] [-c clock_type] [-o output_file] [-f output_format] [-s] [scriptfile] args ...)usageFz-cz--clock-typecpuc              3   <   K   | ]  }|j                           y wrY   )r6   ).0cs     r   	<genexpr>zmain.<locals>.<genexpr>  s     6Qqwwy6s   r   zFClock type to use during profiling("cpu" or "wall", default is "cpu").)defaultchoicesmetavarhelpz-bz
--builtins
store_truer   z5Profiles builtin functions when set. [default: False])actiondestr  r  z-oz--output-fileoutput_filezWrite stats to output_file.)r  r  z-fz--output-formatpstat)r  	callgrindr  output_formatzYWrite stats in the specifiedformat ("pstat", "callgrind" or "ystat", default is "pstat").z-sz--single_threadprofile_single_threadz=Profiles only the thread that calls start(). [default: False]r(   r   exec)$optparser
  r   r   r   r   r  allow_interspersed_args
add_optionr   r  print_usageexit
parse_argsrb   insertdirnamer   r   r   r   r  r  compiler  read	_getframe	f_globalsf_localsr   r  r   r  r  r   r   )r
  r  parseroptionsr   r   s         r   mainr-    s   %dgigngngwgwh E &F%*F"
6+665   D   *	   /  	 $L   88AB<'')OWdCHHQKCHH277??388A;78w))*g&&G,I,I(IJ	/SXXa[)..0#((1+vFa **a )) F""&(

7..0E0EF  **, ",,. F""&(

7..0E0EF  **, ",,.s   BK# #A9M__main__)r   )r  FN)FTT)NNNN)Sr   r   r   rH   r  rA   warningsrw   ry   	itertoolsthreadr   r\   
contextlibr   	Exceptionr   __all__linesepr   r   r  ru   r^  r  rT  r;   r$  r%  r  r1   r2   r   r5  nextrT   AttributeError__next__r9   r=   r?   rF   rM   r_   rh   rn   r}   r   r   r   r   r   rs   r   r  r  r  r  rF  rR  r   r  r  r  r   r   r   r   r  r   r  r   r   r   r   r   r   r  r   r  r  r-  r   r
   r   r   <module>r9     s   
 
       $  &	 	 **
   = 
  
  aq!D  #8Q/9&yQ/44
;-083M2,^&R).X0f/ /8)D )$X XvY :(% (V$E $NN Nb (f  (F&o &Rr# r#j	:V :z9> 9,2^ 2(%-,B "  >###	-"(4>	46& $ K\ zF .  $##$~  9&yQ/889s#   F1 G 1G ?G "G)(G)