a
    zer                     @   s  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 d dl	m
Z
 d dlmZ g dZG dd deeZe
d	d
G dd dZe
d	d
G dd dZG dd dZG dd dZdd Zdd Zdd Zdd Zdd Zdd Zdd  Zed!krd dlZzd dlZW n ey(   Y n0 G d"d# d#ejZ e!e j"d$krXe j"d$ Z#ndZ#zPe e#Z$e j"d%d D ]Z%e$&e% qte'd&e$j(d' e$)  e'd(e$j(d' W n e*y   Y n0 dS ))    N)Enum)
cmp_to_key)	dataclass)Dict)StatsSortKeyFunctionProfileStatsProfilec                   @   s8   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
d ZdS )r   )callsncalls)
cumulativecumtime)filenamemodulelinenamenflpcallsstdname)timetottimec                 G   s@   |d }t | |}||_|dd  D ]}|| j|< q&||_|S Nr      )str__new___value__value2member_map_Z_all_values)clsvaluesvalueobjZother_value r!   /usr/lib/python3.9/pstats.pyr   /   s    zSortKey.__new__N)__name__
__module____qualname__ZCALLSZ
CUMULATIVEZFILENAMEZLINENAMEZNFLZPCALLSZSTDNAMEZTIMEr   r!   r!   r!   r"   r   $   s   r   T)Zunsafe_hashc                   @   sF   e Zd ZU eed< eed< eed< eed< eed< eed< eed< dS )	r   r   r   percall_tottimer   percall_cumtime	file_nameline_numberN)r#   r$   r%   int__annotations__floatr   r!   r!   r!   r"   r   9   s   
r   c                   @   s&   e Zd ZU eed< eeef ed< dS )r	   total_ttfunc_profilesN)r#   r$   r%   r-   r,   r   r   r   r!   r!   r!   r"   r	   C   s   
r	   c                   @   s   e Zd ZddddZdd Zdd Zd	d
 Zdd Zdd ZddddddddddddddZ	dd Z
dd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd8d2d3Zd4d5 Zd6d7 ZdS )9r   N)streamc                G   sF   |pt j| _t|sd }n|d }|dd  }| | | j|  d S r   )sysstdoutr0   leninitadd)selfr0   argsargr!   r!   r"   __init__k   s    
zStats.__init__c                 C   s   d | _ g | _d | _d| _d| _d| _d| _t | _i | _	i | _
| | z|   W n6 ty   td| jrv| jd nd | jd  Y n0 d S )Nr   zInvalid timing data %s file)all_calleesfilesfcn_listr.   total_calls
prim_callsmax_name_lenset	top_levelstatssort_arg_dict
load_statsget_top_level_stats	Exceptionprintr0   )r6   r8   r!   r!   r"   r4   u   s(    
z
Stats.initc                 C   s   |d u ri | _ d S t|trt|d}t|| _ W d    n1 sH0    Y  z"t|}t	|j
d | }W n   Y n0 |g| _n t|dr|  |j | _ i |_ | j std| j|f d S )Nrbz    create_statsz.Cannot create or construct a %r object from %r)rF   
isinstancer   openmarshalloadosstatr   ctimest_mtimer?   hasattrrM   	TypeError	__class__)r6   r8   fZ
file_statsr!   r!   r"   rH      s*    
*


zStats.load_statsc                 C   s   | j  D ]p\}\}}}}}|  j|7  _|  j|7  _|  j|7  _d|v rZ| j| tt|| j	kr
tt|| _	q
d S )N)Zjprofiler   Zprofiler)
rF   itemsrA   rB   r.   rE   r5   r3   func_std_stringrC   )r6   funcccncttctcallersr!   r!   r"   rI      s    zStats.get_top_level_statsc                 G   s   |s| S t |D ]}t| t|kr,t|}|  j|j7  _|  j|j7  _|  j|j7  _|  j|j7  _|jD ]}| j| qr| j	|j	k r|j	| _	d | _
|j D ]<\}}|| jv r| j| }nddddi f}t||| j|< qq| S Nr   )reversedtyper   r?   rA   rB   r.   rE   r5   rC   r@   rF   rZ   add_func_stats)r6   arg_listitemr\   rS   Zold_func_statr!   r!   r"   r5      s(    

z	Stats.addc                 C   s<   t |d}t| j| W d    n1 s.0    Y  d S )Nwb)rO   rP   dumprF   )r6   r   rY   r!   r!   r"   
dump_stats   s    zStats.dump_stats)))r   r:   z
call count)))   r:   zcumulative time))   r   z	file name))   r   zline number))   r   zfunction name))rp   rl   rn   zname/file/line)))r   r:   zprimitive call count)))   r   zstandard name)))   r:   zinternal time)r
   r   r   r   r   r   r   r   r   r   r   r   r   c                 C   st   | j sni  | _ }i }| j D ]>\}}|}|r|s4q||v rFd||< q|||< |d d }q*q|D ]
}||= qb| j S )Nr   r:   )rG   sort_arg_dict_defaultrZ   )r6   dictZbad_listwordtupfragmentr!   r!   r"   get_sort_arg_defs   s     
zStats.get_sort_arg_defsc                 G   s\  |sd| _ | S t|dkrBt|d trBddddd|d  g}n:t|dkr||dd  D ] }t|t|d krZtd	qZ|  }d
}d| _d}|D ]B}t|tr|j	}||| d  }|  j||| d  7  _d}qg }| j
 D ]4\}\}	}
}}}||	|
||f| t||f  q|jtt|jd g  | _ }|D ]}||d  qB| S )Nr   r   r   r
   r   r   )r:   r   r   rs   rs   zCan't have mixed argument typer!   r;   z, )keyr:   )r@   r3   rN   r+   rd   rW   ry   	sort_typer   r   rF   rZ   appendr[   sortr   	TupleCompcompare)r6   Zfieldr8   Zsort_arg_defsZ
sort_tupleZ	connectorrv   Z
stats_listr\   r]   r^   r_   r`   ra   r@   tupler!   r!   r"   
sort_stats   sF    



zStats.sort_statsc                 C   s   | j r| j   | S N)r@   reverser6   r!   r!   r"   reverse_order  s    
zStats.reverse_orderc                 C   s   | j }i  | _ }d}| D ]\}\}}}}}	t|}
tt|
|krRtt|
}i }|	 D ]\}}||t|< q^|
|v rt||
 |||||f||
< q|||||f||
< q| j}t  | _}|D ]}|t| q|| _	d | _
d | _| S rb   )rF   rZ   func_strip_pathr3   r[   re   rE   rD   r5   rC   r@   r>   )r6   ZoldstatsZnewstatsrC   r\   r]   r^   r_   r`   ra   ZnewfuncZ
newcallersfunc2callerZold_topZnew_topr!   r!   r"   
strip_dirs  s0    

zStats.strip_dirsc           
      C   st   | j r
d S i  | _ }| j D ]P\}\}}}}}||vr@i ||< | D ]$\}}	||vr`i ||< |	|| |< qHqd S r   )r>   rF   rZ   )
r6   r>   r\   r]   r^   r_   r`   ra   r   r   r!   r!   r"   calc_callees5  s    
zStats.calc_calleesc                 C   s  |}t |trnzt|}W n( tjyD   |d| 7 }||f Y S 0 g }|D ]}|t|rN|| qNnzt|}t |t	rd|  krdk rn nt
|| d }|d | }n2t |t
rd|  kr|k rn n|}|d | }t|t|kr|dt|t||f 7 }||fS )Nz#   <Invalid regular expression %r>
g        g      ?g      ?r   z6   List reduced from %r to %r due to restriction <%r>
)rN   r   recompileerrorsearchr[   r|   r3   r-   r+   )r6   ZsellistmsgZnew_listZrexr\   countr!   r!   r"   eval_print_amountH  s.    
""zStats.eval_print_amountc              	   C   s  | j r| j d d  nt| j }|s0tdi S tt| j}i }t||}|D ]}| j| \}}}}	}
|\}}}||krt|nt|d t| }tt|}|dkrdntt|| }tt|	}|dkrdntt|	| }t	|||||||}|||< qP|S )Nr   /r:   )
r@   r   rF   keysr	   r-   f8r.   r   r   )r6   Z	func_listr.   r/   Zstats_profiler\   r]   r^   r_   r`   ra   r)   r*   	func_namer   r   r'   r   r(   Zfunc_profiler!   r!   r"   get_stats_profileb  s2    "


$	
zStats.get_stats_profilec                 C   s   | j }| jr*| jd d  }d| j d }nt| j }d}|D ]}| |||\}}q@t|}|sld|fS t|| j	d |t| jk rd}|D ] }tt
||krtt
|}q|d |fS )Nz   Ordered by: 
z!   Random listing order was used
r   r<   rs   )rC   r@   r{   r   rF   r   r   r3   rK   r0   r[   )r6   Zsel_listwidthZ	stat_listr   Z	selectionr   r\   r!   r!   r"   get_print_list  s$    zStats.get_print_listc                 G   s   | j D ]}t|| jd q| j r,t| jd d}| jD ]}t|t|| jd q6t|| jdd| jd | j| jkrtd| j d| jd td| j | jd t| jd | |\}}|r| 	  |D ]}| 
| qt| jd t| jd | S )Nr<           zfunction calls endr=   z(%d primitive calls)zin %.3f seconds)r?   rK   r0   rE   func_get_function_namerA   rB   r.   r   print_title
print_line)r6   amountr   indentr\   r   r   r!   r!   r"   print_stats  s(    

zStats.print_statsc                 G   sz   |  |\}}|rv|   | |d |D ]2}|| jv rN| ||| j|  q*| ||i  q*t| jd t| jd | S )Nz	called...r<   )r   r   print_call_headingr>   print_call_linerK   r0   )r6   r   r   r   r\   r!   r!   r"   print_callees  s    
zStats.print_calleesc           
      G   sh   |  |\}}|rd| |d |D ](}| j| \}}}}}	| |||	d q"t| jd t| jd | S )Nzwas called by...z<-r<   )r   r   rF   r   rK   r0   )
r6   r   r   r   r\   r]   r^   r_   r`   ra   r!   r!   r"   print_callers  s    zStats.print_callersc           
      C   sv   t d|| | jd d}| j D ]0\}}}}}|r&tt| }	t|	t} qXq&|rrt d| d | jd d S )Nz	Function r<   Fr   z    ncalls  tottime  cumtime)	rK   ljustr0   rF   r   nextiterrN   r   )
r6   	name_sizeZcolumn_titleZ	subheaderr]   r^   r_   r`   ra   r   r!   r!   r"   r     s    
zStats.print_call_heading->c                 C   s  t t||| d| jd |s2t | jd d S t| }d}|D ]}t|}|| }	t|	tr|	\}
}}}|
|krd|
|f }n
d|
f }d|dd	t	|  t
|t
||f }|d
 }n$d||	t
| j| d f }|d }t || | | jd d}qFd S )Nr   r   r<   r;   z%d/%dz%dz%s %s %s  %srr   rs   r   z	%s(%r) %srk   )rK   r[   r   r0   sortedr   rN   r   rjustr3   r   rF   )r6   r   sourceZ	call_dictZarrowZclistr   r\   r   r   r^   r]   r_   r`   ZsubstatsZ
left_widthr!   r!   r"   r     s,    


zStats.print_call_linec                 C   s"   t dd| jd t d| jd d S )Nz-   ncalls  tottime  percall  cumtime  percallr   r   zfilename:lineno(function)r<   rK   r0   r   r!   r!   r"   r     s    zStats.print_titlec                 C   s   | j | \}}}}}t|}||kr4|d t| }t|dd| jd tt|d| jd |dkrxtdd| jd ntt|| d| jd tt|d| jd |dkrtdd| jd ntt|| d| jd tt|| jd d S )Nr   	   r   r   r   r   r<   )rF   r   rK   r   r0   r   r[   )r6   r\   r]   r^   r_   r`   ra   cr!   r!   r"   r     s    zStats.print_line)r   )r#   r$   r%   r9   r4   rH   rI   r5   rj   rt   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r!   r!   r!   r"   r   I   sF   "

'#
r   c                   @   s   e Zd Zdd Zdd ZdS )r~   c                 C   s
   || _ d S r   comp_select_list)r6   r   r!   r!   r"   r9     s    zTupleComp.__init__c                 C   sF   | j D ]:\}}|| }|| }||k r0|   S ||kr|  S qdS rb   r   )r6   leftrightindex	directionlrr!   r!   r"   r     s    

zTupleComp.compareN)r#   r$   r%   r9   r   r!   r!   r!   r"   r~   	  s   r~   c                 C   s   | \}}}t j|||fS r   )rR   pathbasename)r   r   r   r   r!   r!   r"   r   "  s    
r   c                 C   s   | d S )Nrs   r!   )r\   r!   r!   r"   r   &  s    r   c                 C   sN   | d d dkrB| d }| dr<|dr<d|dd  S |S nd|  S d S )	Nrs   )~r   <>z{%s}r   r:   z	%s:%d(%s))
startswithendswith)r   r   r!   r!   r"   r[   )  s    r[   c                 C   s@   |\}}}}}| \}}}	}
}|| || ||	 ||
 t ||fS r   )add_callers)targetr   r]   r^   r_   r`   ra   Zt_ccZt_ncZt_ttZt_ctZ	t_callersr!   r!   r"   re   :  s
    re   c                 C   s   i }|   D ]\}}|||< q|  D ]V\}}||v rtt|trbtdd t||| D ||< q|||  |7  < q&|||< q&|S )Nc                 s   s   | ]\}}|| V  qd S r   r!   ).0ijr!   r!   r"   	<genexpr>J      zadd_callers.<locals>.<genexpr>)rZ   rN   r   zip)r   r   Znew_callersr\   r   r!   r!   r"   r   A  s    

"
r   c                 C   s   d}|   D ]}||7 }q|S rb   )r   )ra   r^   r
   r!   r!   r"   count_callsR  s    
r   c                 C   s   d|  S )Nz%8.3fr!   )xr!   r!   r"   r   ]  s    r   __main__c                   @   s   e Zd Zd6ddZdd Zdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 ZdS )7ProfileBrowserNc                 C   s6   t j|  d| _d | _tj| _|d ur2| | d S )N% )	cmdCmdr9   promptrF   r1   r2   r0   do_read)r6   profiler!   r!   r"   r9   l  s    zProfileBrowser.__init__c              	   C   s   |  }g }|D ]}z|t| W qW n ty<   Y n0 z<t|}|dksX|dk rjtd| jd W q|| W qW n ty   Y n0 || q| jrt| j||  ntd| jd dS )Nr   r   z#Fraction argument must be in [0, 1]r<   No statistics object is loaded.)	splitr|   r+   
ValueErrorr-   rK   r0   rF   getattr)r6   fnr   r7   Z	processedZtermZfracr!   r!   r"   generict  s,    
zProfileBrowser.genericc                 C   sX   t d| jd t d| jd t d| jd t d| jd t d| jd t d| jd d S )NzArguments may be:r<   z0* An integer maximum number of entries to print.z:* A decimal fractional number between 0 and 1, controllingz-  what fraction of selected entries to print.z8* A regular expression; only entries with function namesz  that match it are printed.r   r   r!   r!   r"   generic_help  s    zProfileBrowser.generic_helpc              
   C   sf   | j rTz| j | W qb tyP } z"td||f | jd W Y d }~qbd }~0 0 ntd| jd dS )Nz$Failed to load statistics for %s: %sr<   r   r   )rF   r5   OSErrorrK   r0   )r6   r   er!   r!   r"   do_add  s    .zProfileBrowser.do_addc                 C   s   t d| jd d S )Nz>Add profile info from given file to current statistics object.r<   r   r   r!   r!   r"   help_add  s    zProfileBrowser.help_addc                 C   s   |  d|S )Nr   r   r6   r   r!   r!   r"   
do_callees  s    zProfileBrowser.do_calleesc                 C   s   t d| jd |   d S )Nz6Print callees statistics from the current stat object.r<   rK   r0   r   r   r!   r!   r"   help_callees  s    zProfileBrowser.help_calleesc                 C   s   |  d|S )Nr   r   r   r!   r!   r"   
do_callers  s    zProfileBrowser.do_callersc                 C   s   t d| jd |   d S )Nz6Print callers statistics from the current stat object.r<   r   r   r!   r!   r"   help_callers  s    zProfileBrowser.help_callersc                 C   s   t d| jd dS )Nr;   r<   r   r   r   r!   r!   r"   do_EOF  s    zProfileBrowser.do_EOFc                 C   s   t d| jd d S NzLeave the profile browser.r<   r   r   r!   r!   r"   help_EOF  s    zProfileBrowser.help_EOFc                 C   s   dS )Nr   r!   r   r!   r!   r"   do_quit  s    zProfileBrowser.do_quitc                 C   s   t d| jd d S r   r   r   r!   r!   r"   	help_quit  s    zProfileBrowser.help_quitc              
   C   s   |rzt || _W nv tyL } z"t|jd | jd W Y d }~d S d }~0  ty } z&t|jjd || jd W Y d }~d S d }~0 0 |d | _	n6t
| j	dkr| j	d d }| | ntd| jd dS )	Nr   r<   :r   rs   z1No statistics object is current -- cannot reload.r   )r   rF   r   rK   r7   r0   rJ   rX   r#   r   r3   r   )r6   r   errr!   r!   r"   r     s    zProfileBrowser.do_readc                 C   s    t d| jd t d| jd d S )Nz+Read in profile data from a specified file.r<   z*Without argument, reload the current file.r   r   r!   r!   r"   	help_read  s    zProfileBrowser.help_readc                 C   s$   | j r| j   ntd| jd dS )Nr   r<   r   )rF   r   rK   r0   r   r!   r!   r"   
do_reverse  s    zProfileBrowser.do_reversec                 C   s   t d| jd d S )Nz/Reverse the sort order of the profiling report.r<   r   r   r!   r!   r"   help_reverse  s    zProfileBrowser.help_reversec                    s   | j std| jd d S | j   |rRt fdd| D rR| j j|   n<td| jd tj	 D ]"\}}td||d f | jd qjdS )	Nr   r<   c                 3   s   | ]}| v V  qd S r   r!   )r   r   Zabbrevsr!   r"   r     r   z)ProfileBrowser.do_sort.<locals>.<genexpr>z/Valid sort keys (unique prefixes are accepted):z%s -- %sr   r   )
rF   rK   r0   ry   allr   r   r   rt   rZ   )r6   r   rz   r   r!   r   r"   do_sort  s    
zProfileBrowser.do_sortc                 C   s    t d| jd t d| jd d S )Nz.Sort profile data according to specified keys.r<   z3(Typing `sort' without arguments lists valid keys.)r   r   r!   r!   r"   	help_sort  s    zProfileBrowser.help_sortc                    s    fddt jD S )Nc                    s   g | ]}|  r|qS r!   )r   )r   atextr!   r"   
<listcomp>  r   z0ProfileBrowser.complete_sort.<locals>.<listcomp>)r   rt   )r6   r   r7   r!   r   r"   complete_sort  s    zProfileBrowser.complete_sortc                 C   s   |  d|S )Nr   r   r   r!   r!   r"   do_stats  s    zProfileBrowser.do_statsc                 C   s   t d| jd |   d S )Nz.Print statistics from the current stat object.r<   r   r   r!   r!   r"   
help_stats  s    zProfileBrowser.help_statsc                 C   s$   | j r| j   ntd| jd d S )Nr   r<   )rF   r   rK   r0   r   r!   r!   r"   do_strip  s    zProfileBrowser.do_stripc                 C   s   t d| jd d S )Nz<Strip leading path information from filenames in the report.r<   r   r   r!   r!   r"   
help_strip  s    zProfileBrowser.help_stripc                 C   s   t d| jd d S )NzShow help for a given command.r<   r   r   r!   r!   r"   	help_help  s    zProfileBrowser.help_helpc                 C   s   |r|S d S r   r!   )r6   stopr   r!   r!   r"   postcmd  s    zProfileBrowser.postcmd)N)r#   r$   r%   r9   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!   r!   r!   r"   r   k  s4   
	r   r   rs   z*Welcome to the profile statistics browser.r<   zGoodbye.)+r1   rR   r   rP   r   enumr   	functoolsr   Zdataclassesr   typingr   __all__r   r   r   r	   r   r~   r   r   r[   re   r   r   r   r#   r   readlineImportErrorr   r   r3   argvZinitprofileZbrowserr   r   rK   r0   ZcmdloopKeyboardInterruptr!   r!   r!   r"   <module>   s\   	   C
 