a �DOg�r�@s�dZddlZddlZddlZddlZddlZddlmZddlm Z ddl m Z ddl m Z gd�ZGdd �d ee�Ze d d �Gd d �d ��Ze d d �Gdd�d��ZGdd�d�ZGdd�d�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zed"k�r�ddlZz ddlZWne�y,Yn0Gd#d$�d$ej �Z!e"ej#�d%k�r\ej#d%Z$ndZ$zPe!e$�Z%ej#d&d�D]Z&e%�'e&��qxe(d'e%j)d(�e%�*�e(d)e%j)d(�Wne+�y�Yn0dS)*z3Class for printing reports on profiled python code.�N)�Enum)� cmp_to_key)� dataclass)�Dict)�Stats�SortKey�FunctionProfile� StatsProfilec@s8eZdZdZdZdZdZdZdZdZ dZ d Z d d �Z d S) r)�calls�ncalls)� cumulative�cumtime)�filename�module�line�name�nfl�pcalls�stdname)�time�tottimecGs@|d}t�||�}||_|dd�D]}||j|<q&||_|S�Nr�)�str�__new__�_value_�_value2member_map_Z _all_values)�cls�values�value�objZ other_value�r!�/usr/lib64/python3.9/pstats.pyr/s  zSortKey.__new__N) �__name__� __module__� __qualname__ZCALLSZ CUMULATIVEZFILENAMEZLINE�NAMEZNFLZPCALLSZSTDNAMEZTIMErr!r!r!r"r$srT)Z unsafe_hashc@sFeZdZUeed<eed<eed<eed<eed<eed<eed<dS) rr r�percall_tottimer �percall_cumtime� file_name� line_numberN)r#r$r%�int�__annotations__�floatrr!r!r!r"r9s rc@s*eZdZUdZeed<eeefed<dS)r z0Class for keeping track of an item in inventory.�total_tt� func_profilesN) r#r$r%�__doc__r-r,rrrr!r!r!r"r Cs r c@s�eZdZdZdd�dd�Zdd�Zdd �Zd d �Zd d �Zdd�Z dddddddddddddd� Z dd�Z dd�Z dd�Z d d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd9d3d4�Zd5d6�Zd7d8�ZdS):ra�This class is used for creating reports from data generated by the Profile class. It is a "friend" of that class, and imports data either by direct access to members of Profile class, or by reading in a dictionary that was emitted (via marshal) from the Profile class. The big change from the previous Profiler (in terms of raw functionality) is that an "add()" method has been provided to combine Stats from several distinct profile runs. Both the constructor and the add() method now take arbitrarily many file names as arguments. All the print methods now take an argument that indicates how many lines to print. If the arg is a floating point number between 0 and 1.0, then it is taken as a decimal percentage of the available lines to be printed (e.g., .1 means print 10% of all available lines). If it is an integer, it is taken to mean the number of lines of data that you wish to have printed. The sort_stats() method now processes some additional options (i.e., in addition to the old -1, 0, 1, or 2 that are respectively interpreted as 'stdname', 'calls', 'time', and 'cumulative'). It takes either an arbitrary number of quoted strings or SortKey enum to select the sort order. For example sort_stats('time', 'name') or sort_stats(SortKey.TIME, SortKey.NAME) sorts on the major key of 'internal function time', and on the minor key of 'the name of the function'. Look at the two tables in sort_stats() and get_sort_arg_defs(self) for more examples. All methods return self, so you can string together commands like: Stats('foo', 'goo').strip_dirs().sort_stats('calls'). print_stats(5).print_callers(5) N)�streamcGsF|ptj|_t|�sd}n|d}|dd�}|�|�|j|�dSr)�sys�stdoutr1�len�init�add)�selfr1�args�argr!r!r"�__init__ks   zStats.__init__cCs�d|_g|_d|_d|_d|_d|_d|_t�|_i|_ i|_ |� |�z |� �Wn6t y�td|jrv|jdnd|jd��Yn0dS)NrzInvalid 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�printr1)r7r9r!r!r"r5us(   ��z Stats.initcCs�|duri|_dSt|t�r�t|d��}t�|�|_Wd�n1sH0Yz"t�|�}t� |j �d|}Wn Yn0|g|_ n t |d�r�|� �|j|_i|_|js�td|j|f��dS)N�rbz � create_statsz.Cannot create or construct a %r object from %r)rG� isinstancer�open�marshal�load�os�statr�ctime�st_mtimer@�hasattrrN� TypeError� __class__)r7r9�fZ file_statsr!r!r"rI�s*  *   �zStats.load_statscCs�|j��D]p\}\}}}}}|j|7_|j|7_|j|7_d|vrZ|j�|�tt|��|j kr tt|��|_ q dS)N)ZjprofilerZprofiler) rG�itemsrBrCr.rFr6r4�func_std_stringrD)r7�func�cc�nc�tt�ct�callersr!r!r"rJ�s zStats.get_top_level_statscGs�|s|St|�D]�}t|�t|�kr,t|�}|j|j7_|j|j7_|j|j7_|j|j7_|jD]}|j�|�qr|j |j kr�|j |_ d|_ |j � �D]<\}}||j vr�|j |}nddddif}t ||�|j |<q�q|S�Nr)�reversed�typerr@rBrCr.rFr6rDrArGr[�add_func_stats)r7�arg_list�itemr]rTZ old_func_statr!r!r"r6�s(     z Stats.addcCs<t|d��}t�|j|�Wd�n1s.0YdS)z:Write the profile data to a file we know how to load back.�wbN)rPrQ�dumprG)r7rrZr!r!r"� dump_stats�s zStats.dump_stats)))rr;z call count)))�r;zcumulative time))��rz file name))��rz line number))��rz function name))rqrmrozname/file/line)))rr;zprimitive call count)))�rz standard name)))�r;z internal time) r r r r rrrrrrrrrcCst|jsni|_}i}|j��D]>\}}|}|r|s4q||vrFd||<q|||<|dd�}q*q|D] }||=qb|jS)z)Expand all abbreviations that are unique.rNr;)rH�sort_arg_dict_defaultr[)r7�dictZbad_list�word�tup�fragmentr!r!r"�get_sort_arg_defs�s  zStats.get_sort_arg_defscGs\|sd|_|St|�dkrBt|dt�rBddddd�|dg}n:t|�dkr||dd�D] }t|�t|d�krZtd ��qZ|��}d }d |_d }|D]B}t|t�r�|j }|||d}|j|||d7_d }q�g}|j � �D]4\}\} } } } } |� | | | | f|t |�|f�q�|jtt|�j�d �g|_}|D]}|� |d��qB|S)Nrrrr rr )r;rrrtrtzCan't have mixed argument typer!r<z, )�keyr;)rAr4rOr+rerXrz� sort_typerrrGr[�appendr\�sortr� TupleComp�compare)r7Zfieldr9Z sort_arg_defsZ sort_tupleZ connectorrwZ stats_listr]r^r_r`rarbrA�tupler!r!r"� sort_stats�sF��    � zStats.sort_statscCs|jr|j��|S�N)rA�reverse�r7r!r!r"� reverse_orders zStats.reverse_ordercCs�|j}i|_}d}|��D]�\}\}}}}} t|�} tt| ��|krRtt| ��}i} | ��D]\} } | | t| �<q^| |vr�t|| ||||| f�|| <q||||| f|| <q|j}t�|_}|D]}|�t|��q�||_ d|_ d|_ |Src) rGr[�func_strip_pathr4r\rfrFrEr6rDrAr?)r7ZoldstatsZnewstatsrDr]r^r_r`rarbZnewfuncZ newcallers�func2�callerZold_topZnew_topr!r!r"� strip_dirss0   �  zStats.strip_dirsc Cst|jr dSi|_}|j��D]P\}\}}}}}||vr@i||<|��D]$\}} ||vr`i||<| |||<qHqdSr�)r?rGr[) r7r?r]r^r_r`rarbr�r�r!r!r"� calc_callees5s zStats.calc_calleescCs|}t|t�rnzt�|�}Wn(tjyD|d|7}||fYS0g}|D]}|�t|��rN|�|�qNnzt|�}t|t �r�d|kr�dkr�nnt ||d�}|d|�}n2t|t �r�d|kr�|kr�nn|}|d|�}t|�t|�k�r|dt|�t|�|f7}||fS)Nz# gg�?g�?rz6 List reduced from %r to %r due to restriction <%r> ) rOr�re�compile�error�searchr\r}r4r-r+)r7Zsel�list�msgZnew_listZrexr]�countr!r!r"�eval_print_amountHs.  "" �zStats.eval_print_amountc Cs|jr|jdd�n t|j���}|s0tdi�Stt|j��}i}t||�}|D]�}|j|\}}}} } |\} } } ||kr�t|�nt|�dt|�}tt|��}|dkr�dntt||��}tt| ��}|dkr�dntt| |��}t |||||| | �}||| <qP|S)a?This method returns an instance of StatsProfile, which contains a mapping of function names to instances of FunctionProfile. Each FunctionProfile instance holds information related to the function's profile such as how long the function took to run, how many times it was called, etc... Nr�/r;) rAr�rG�keysr r-�f8r.rr)r7Z func_listr.r/Z stats_profiler]r^r_r`rarbr)r*� func_namer rr'r r(Z func_profiler!r!r"�get_stats_profilebs2"   $  � zStats.get_stats_profilecCs�|j}|jr*|jdd�}d|jd}nt|j���}d}|D]}|�|||�\}}q@t|�}|sld|fSt||j d�|t|j�kr�d}|D] }tt |��|kr�tt |��}q�|d|fS)Nz Ordered by: � z! Random listing order was used rr=rt) rDrAr|r�rGr�r�r4rLr1r\)r7Zsel_list�widthZ stat_listr�Z selectionr�r]r!r!r"�get_print_list�s$zStats.get_print_listcGs�|jD]}t||jd�q|jr,t|jd�d}|jD]}t|t|�|jd�q6t||jdd|jd�|j|jkr�td|jd|jd�td|j|jd�t|jd�|�|�\}}|r�|� �|D]}|� |�q�t|jd�t|jd�|S)Nr=� zfunction calls� ��endr>z(%d primitive calls)zin %.3f seconds) r@rLr1rF�func_get_function_namerBrCr.r�� print_title� print_line)r7�amountr�indentr]r�r�r!r!r"� print_stats�s(        zStats.print_statscGsz|�|�\}}|rv|��|�|d�|D]2}||jvrN|�|||j|�q*|�||i�q*t|jd�t|jd�|S)Nz called...r=)r�r��print_call_headingr?�print_call_linerLr1)r7r�r�r�r]r!r!r"� print_callees�s    zStats.print_calleesc Gsh|�|�\}}|rd|�|d�|D](}|j|\}}}}} |�||| d�q"t|jd�t|jd�|S)Nzwas called by...z<-r=)r�r�rGr�rLr1) r7r�r�r�r]r^r_r`rarbr!r!r"� print_callers�s   zStats.print_callersc Csvtd�|�||jd�d}|j��D]0\}}}}}|r&tt|����} t| t�}qXq&|rrtd|d|jd�dS)Nz Function r=Fr�z ncalls tottime cumtime) rL�ljustr1rGr�next�iterrOr�) r7� name_sizeZ column_titleZ subheaderr^r_r`rarbrr!r!r"r��s zStats.print_call_heading�->cCstt|��|�|d|jd�|s2t|jd�dSt|���}d}|D]�}t|�}||} t| t�r�| \} } } } | | kr�d| | f}n d| f}d|�dd t |��t | �t | �|f}|d }n$d || t |j |d �f}|d }t||||jd�d}qFdS) Nr�r�r=r<z%d/%dz%dz %s %s %s %srsrtrz %s(%r) %srl) rLr\r�r1�sortedr�rOr��rjustr4r�rG)r7r��sourceZ call_dictZarrowZclistr�r]rrr_r^r`raZsubstatsZ left_widthr!r!r"r��s,     � zStats.print_call_linecCs"tdd|jd�td|jd�dS)Nz- ncalls tottime percall cumtime percallr�r�zfilename:lineno(function)r=�rLr1r�r!r!r"r��szStats.print_titlecCs�|j|\}}}}}t|�}||kr4|dt|�}t|�d�d|jd�tt|�d|jd�|dkrxtdd|jd�ntt||�d|jd�tt|�d|jd�|dkr�tdd|jd�ntt||�d|jd�tt|�|jd�dS)Nr�� r�r�rr�r=)rGrrLr�r1r�r\)r7r]r^r_r`rarb�cr!r!r"r��szStats.print_line)r�)r#r$r%r0r:r5rIrJr6rkrurzr�r�r�r�r�r�r�r�r�r�r�r�r�r�r!r!r!r"rIsH!  �'#  rc@s eZdZdZdd�Zdd�ZdS)ra�This class provides a generic function for comparing any two tuples. Each instance records a list of tuple-indices (from most significant to least significant), and sort direction (ascending or decending) for each tuple-index. The compare functions can then be used as the function argument to the system sort() function when a list of tuples need to be sorted in the instances order.cCs ||_dSr���comp_select_list)r7r�r!r!r"r:szTupleComp.__init__cCsF|jD]:\}}||}||}||kr0| S||kr|SqdSrcr�)r7�left�right�index� direction�l�rr!r!r"r�s  zTupleComp.compareN)r#r$r%r0r:r�r!r!r!r"r srcCs|\}}}tj�|�||fSr�)rS�path�basename)r�rrrr!r!r"r�"s r�cCs|dS)Nrtr!)r]r!r!r"r�&sr�cCsN|dd�dkrB|d}|�d�r<|�d�rz{%s}rr;z %s:%d(%s))� startswith�endswith)r�rr!r!r"r\)s r\c Cs@|\}}}}}|\}}} } } |||||| || t| |�fS)z3Add together all the stats for two profile entries.)� add_callers) �targetr�r^r_r`rarbZt_ccZt_ncZt_ttZt_ctZ t_callersr!r!r"rf:s �rfcCs�i}|��D]\}}|||<q |��D]V\}}||vrtt|t�rbtdd�t|||�D��||<q||||7<q&|||<q&|S)z*Combine two caller lists in a single list.css|]\}}||VqdSr�r!)�.0�i�jr!r!r"� J�zadd_callers..)r[rOr��zip)r�r�Z new_callersr]r�r!r!r"r�As  " r�cCsd}|��D] }||7}q |S)z@Sum the caller statistics to get total number of calls received.r)r)rbr_r r!r!r"� count_callsRs  r�cCsd|S)Nz%8.3fr!)�xr!r!r"r�]sr��__main__c@s�eZdZd6dd�Zdd�Zdd�Zdd �Zd d �Zd d �Zdd�Z dd�Z dd�Z dd�Z dd�Z dd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�ZdS)7�ProfileBrowserNcCs6tj�|�d|_d|_tj|_|dur2|�|�dS)N�% ) �cmd�Cmdr:�promptrGr2r3r1�do_read)r7�profiler!r!r"r:ls  zProfileBrowser.__init__c Cs�|��}g}|D]�}z|�t|��WqWnty<Yn0zAdd profile info from given file to current statistics object.r=r�r�r!r!r"�help_add�szProfileBrowser.help_addcCs |�d|�S)Nr��r��r7rr!r!r"� do_callees�szProfileBrowser.do_calleescCstd|jd�|��dS)Nz6Print callees statistics from the current stat object.r=�rLr1r�r�r!r!r"� help_callees�szProfileBrowser.help_calleescCs |�d|�S)Nr�r�r�r!r!r"� do_callers�szProfileBrowser.do_callerscCstd|jd�|��dS)Nz6Print callers statistics from the current stat object.r=r�r�r!r!r"� help_callers�szProfileBrowser.help_callerscCstd|jd�dS)Nr<r=rr�r�r!r!r"�do_EOF�szProfileBrowser.do_EOFcCstd|jd�dS�NzLeave the profile browser.r=r�r�r!r!r"�help_EOF�szProfileBrowser.help_EOFcCsdS)Nrr!r�r!r!r"�do_quit�szProfileBrowser.do_quitcCstd|jd�dSr�r�r�r!r!r"� help_quit�szProfileBrowser.help_quitc Cs�|r�zt|�|_WnvtyL}z"t|jd|jd�WYd}~dSd}~0ty�}z&t|jjd||jd�WYd}~dSd}~00|d|_ n6t |j �dkr�|j dd�}|� |�ntd|jd�dS) Nrr=�:r�rt�����z1No statistics object is current -- cannot reload.r) rrGr�rLr8r1rKrYr#r�r4r�)r7r�errr!r!r"r��s  zProfileBrowser.do_readcCs td|jd�td|jd�dS)Nz+Read in profile data from a specified file.r=z*Without argument, reload the current file.r�r�r!r!r"� help_read�szProfileBrowser.help_readcCs$|jr|j��ntd|jd�dS)Nr�r=r)rGr�rLr1r�r!r!r"� do_reverse�s zProfileBrowser.do_reversecCstd|jd�dS)Nz/Reverse the sort order of the profiling report.r=r�r�r!r!r"� help_reverse�szProfileBrowser.help_reversecs�|jstd|jd�dS|j���|rRt�fdd�|��D��rR|jj|���n.z/Valid sort keys (unique prefixes are accepted):z%s -- %srr) rGrLr1rz�allr�r�rrur[)r7rr{rr!r�r"�do_sort�s zProfileBrowser.do_sortcCs td|jd�td|jd�dS)Nz.Sort profile data according to specified keys.r=z3(Typing `sort' without arguments lists valid keys.)r�r�r!r!r"� help_sort�szProfileBrowser.help_sortcs�fdd�tjD�S)Ncsg|]}|���r|�qSr!)r�)r��a��textr!r"� �r�z0ProfileBrowser.complete_sort..)rru)r7r�r8r!r�r"� complete_sort�szProfileBrowser.complete_sortcCs |�d|�S)Nr�r�r�r!r!r"�do_stats�szProfileBrowser.do_statscCstd|jd�|��dS)Nz.Print statistics from the current stat object.r=r�r�r!r!r"� help_stats�szProfileBrowser.help_statscCs$|jr|j��ntd|jd�dS)Nr�r=)rGr�rLr1r�r!r!r"�do_strip�s zProfileBrowser.do_stripcCstd|jd�dS)Nzs^     C