grthtrhthjhtyjytjytkergtrhtrjytjerhrfhU iÛfÑjã@s„dZddlZddlZddlZddlZddlZddlmZddlm Z ddgZ Gdd„de eƒZ Gdd„dƒZ Gd d „d ƒZd d „Zd d„Zdd„Zdd„Zdd„Zdd„Zdd„Zedk�r€ddlZz ddlZWnek räYnXGdd„dejƒZeejƒdk�rejdZndZzPeeƒZejdd…D]Z e !e ¡�q0e"dej#d�e $¡e"d ej#d�Wne%k �r~YnXdS)!z3Class for printing reports on profiled python code.éN)ÚEnum)Ú cmp_to_keyÚStatsÚSortKeyc@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ú+/opt/alt/python38/lib64/python3.8/pstats.pyr-s  zSortKey.__new__N) Ú__name__Ú __module__Ú __qualname__ZCALLSZ CUMULATIVEZFILENAMEZLINEÚNAMEZNFLZPCALLSZSTDNAMEZTIMErrrrrr"sc@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/„Zd7d1d2„Zd3d4„Zd5d6„ZdS)8raÛ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Ústdoutr#ÚlenÚinitÚadd)Úselfr#ÚargsÚargrrrÚ__init__Ys   zStats.__init__cCs’d|_g|_d|_d|_d|_d|_d|_tƒ|_i|_ i|_ |  |¡z |  ¡Wn8t k rŒtd|jrx|jdnd|jd�‚YnXdS)NrzInvalid timing data %séÿÿÿÿÚ©Úfile)Ú all_calleesÚfilesÚfcn_listÚtotal_ttÚ total_callsÚ prim_callsÚ max_name_lenÚsetÚ top_levelÚstatsÚ sort_arg_dictÚ load_statsÚget_top_level_statsÚ ExceptionÚprintr#)r)r+rrrr'cs(  ÿÿz Stats.initc Cs´|dkri|_dSt|tƒrxt|dƒ�}t |¡|_W5QRXz"t |¡}t  |j ¡d|}Wn YnX|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)r:Ú isinstancerÚopenÚmarshalÚloadÚosÚstatrÚctimeÚst_mtimer2ÚhasattrrAÚ TypeErrorÚ __class__)r)r+ÚfZ file_statsrrrr<vs*     ÿzStats.load_statscCs€|j ¡D]p\}\}}}}}|j|7_|j|7_|j|7_d|krZ|j |¡tt|ƒƒ|j kr tt|ƒƒ|_ q dS)N)ZjprofilerZprofiler) r:Úitemsr5r6r4r9r(r&Úfunc_std_stringr7)r)ÚfuncÚccÚncÚttÚctÚcallersrrrr=Œ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 krÆ|j |}nddddif}t ||ƒ|j |<q¨q|S©Nr)ÚreversedÚtyperr2r5r6r4r9r(r7r3r:rNÚadd_func_stats)r)Úarg_listÚitemrPrGZ old_func_statrrrr(–s(     z Stats.addc Cs(t|dƒ�}t |j|¡W5QRXdS)z:Write the profile data to a file we know how to load back.ÚwbN)rCrDÚdumpr:)r)r rMrrrÚ dump_stats°s zStats.dump_stats)))rr-z call count)))ér-zcumulative time))©érz file name))©érz line number))©érz function name))rdr`rbzname/file/line)))rr-zprimitive call count)))érz standard name)))ér-z internal time) rrr rr r r r rrrrrcCst|jsni|_}i}|j ¡D]>\}}|}|r|s4q||krFd||<q|||<|dd…}q*q|D] }||=qb|jS)z)Expand all abbreviations that are unique.rNr-)r;Úsort_arg_dict_defaultrN)r)ÚdictZbad_listÚwordÚtupZfragmentrrrÚ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)Nrrrrrr)r-rrrgrgzCan't have mixed argument typerr.z, )Úkeyr-)r3r&rBÚintrXrKrlÚ sort_typerrr:rNÚappendrOÚsortrÚ TupleCompÚcompare)r)Zfieldr+Z sort_arg_defsZ sort_tupleZ connectorrjZ stats_listrPrQrRrSrTrUr3ÚtuplerrrÚ sort_statsÚsFýý    ÿ zStats.sort_statscCs|jr|j ¡|S©N)r3Úreverse©r)rrrÚ reverse_orders zStats.reverse_ordercCsî|j}i|_}d}| ¡D]�\}\}}}}} t|ƒ} tt| ƒƒ|krRtt| ƒƒ}i} |  ¡D]\} } | | t| ƒ<q^| |kršt|| ||||| fƒ|| <q||||| f|| <q|j}tƒ|_}|D]}| t|ƒ¡qÄ||_ d|_ d|_ |SrV) r:rNÚfunc_strip_pathr&rOrYr9r8r(r7r3r1)r)ZoldstatsZnewstatsr7rPrQrRrSrTrUZnewfuncZ newcallersÚfunc2ÚcallerZold_topZnew_toprrrÚ strip_dirss0   þ  zStats.strip_dirsc Cst|jr dSi|_}|j ¡D]P\}\}}}}}||kr@i||<| ¡D]$\}} ||kr`i||<| |||<qHqdSrv)r1r:rN) r)r1rPrQrRrSrTrUr{r|rrrÚ calc_callees#s zStats.calc_calleescCs|}t|tƒrpzt |¡}Wn*tjk rF|d|7}||fYSXg}|D]}| t|ƒ¡rP| |¡qPnzt|ƒ}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> ) rBrÚreÚcompileÚerrorÚsearchrOrpr&Úfloatrn)r)ZselÚlistÚmsgZnew_listZrexrPÚcountrrrÚeval_print_amount6s2  "" ÿzStats.eval_print_amountcCs¾|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/rg) r7r3ror„r:Úkeysr‡r&r?r#rO)r)Zsel_listÚwidthZ stat_listr…Z selectionr†rPrrrÚget_print_listPs$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ú ©Úendr0z(%d primitive calls)zin %.3f seconds) r2r?r#r9Úfunc_get_function_namer5r6r4r‹Ú print_titleÚ print_line)r)Úamountr ÚindentrPrŠr„rrrÚ print_statshs(        zStats.print_statscGsz| |¡\}}|rv| ¡| |d¡|D]2}||jkrN| |||j|¡q*| ||i¡q*t|jd�t|jd�|S)Nz called...r/)r‹r~Úprint_call_headingr1Úprint_call_liner?r#)r)r“rŠr„rPrrrÚ print_calleess    zStats.print_calleesc Gsh| |¡\}}|rd| |d¡|D](}|j|\}}}}} | ||| d¡q"t|jd�t|jd�|S)Nzwas called by...z<-r/)r‹r–r:r—r?r#) r)r“rŠr„rPrQrRrSrTrUrrrÚ 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) r?Úljustr#r:rÚnextÚiterrBrt) r)Ú name_sizeZ column_titleZ subheaderrQrRrSrTrUrrrrr–™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 %srfrgrz %s(%r) %sr_) r?rOršr#Úsortedr‰rBrtÚrjustr&Úf8r:)r)r�ÚsourceZ call_dictZarrowZclistr”rPr rrRrQrSrTZsubstatsZ left_widthrrrr—¥s0     ÿ zStats.print_call_linecCs"tdd|jd�td|jd�dS)Nz- ncalls tottime percall cumtime percallr�rŽzfilename:lineno(function)r/©r?r#rxrrrr‘¾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)Nú/é r�rŽrrŒr/)r:rr?r r#r¡rO)r)rPrQrRrSrTrUÚcrrrr’ÂszStats.print_line)rž)rr r!Ú__doc__r,r'r<r=r(r^rhrlruryr}r~r‡r‹r•r˜r™r–r—r‘r’rrrrr7sF!  ó'  c@s eZdZdZdd„Zdd„ZdS)rra•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 ||_dSrv©Úcomp_select_list)r)r©rrrr,ÜszTupleComp.__init__cCsF|jD]:\}}||}||}||kr0| S||kr|SqdSrVr¨)r)ÚleftÚrightÚindexÚ directionÚlÚrrrrrsßs  zTupleComp.compareN)rr r!r§r,rsrrrrrrÔsrrcCs|\}}}tj |¡||fSrv)rFÚpathÚbasename)Ú func_namer r r rrrrzís rzcCs|dS)Nrgr)rPrrrr�ñsr�cCsN|dd…dkrB|d}| d¡r<| d¡rz{%s}rr-z %s:%d(%s))Ú startswithÚendswith)r²r rrrrOôs rOc Cs@|\}}}}}|\}}} } } |||||| || t| |ƒfS)z3Add together all the stats for two profile entries.)Ú add_callers) Útargetr¢rQrRrSrTrUZt_ccZt_ncZt_ttZt_ctZ t_callersrrrrYs ÿrYcCs‚i}| ¡D]\}}|||<q | ¡D]V\}}||krtt|tƒrbtdd„t|||ƒDƒƒ||<q||||7<q&|||<q&|S)z*Combine two caller lists in a single list.css|]\}}||VqdSrvr)Ú.0ÚiÚjrrrÚ szadd_callers..)rNrBrtÚzip)r¹r¢Z new_callersrPr|rrrr¸ s  " r¸cCsd}| ¡D] }||7}q |S)z@Sum the caller statistics to get total number of calls received.r)r)rUrRrrrrÚ count_callss  r¿cCsd|S)Nz%8.3fr)Úxrrrr¡(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|_|dk r2| |¡dS)Nú% ) ÚcmdÚCmdr,Úpromptr:r$r%r#Údo_read)r)Úprofilerrrr,7s  zProfileBrowser.__init__c CsÈ| ¡}g}|D]Œ}z| t|ƒ¡WqWntk r>YnXzAdd profile info from given file to current statistics object.r/r£rxrrrÚhelp_addhszProfileBrowser.help_addcCs | d|¡S)Nr˜©rΩr)r rrrÚ do_calleeskszProfileBrowser.do_calleescCstd|jd�| ¡dS)Nz6Print callees statistics from the current stat object.r/©r?r#rÏrxrrrÚ help_calleesmszProfileBrowser.help_calleescCs | d|¡S)Nr™rÔrÕrrrÚ do_callersqszProfileBrowser.do_callerscCstd|jd�| ¡dS)Nz6Print callers statistics from the current stat object.r/r×rxrrrÚ help_callerssszProfileBrowser.help_callerscCstd|jd�dS)Nr.r/rr£rÕrrrÚdo_EOFwszProfileBrowser.do_EOFcCstd|jd�dS©NzLeave the profile browser.r/r£rxrrrÚhelp_EOFzszProfileBrowser.help_EOFcCsdS)NrrrÕrrrÚdo_quit}szProfileBrowser.do_quitcCstd|jd�dSrÜr£rxrrrÚ help_quitszProfileBrowser.help_quitc CsÔ|ršzt|ƒ|_Wnztk rN}zt|jd|jd�WY¢dSd}~XYn@tk rŒ}z"t|jjd||jd�WY¢dSd}~XYnX|d|_ n6t |j ƒdkrÂ|j dd…}|  |¡ntd|jd�dS) Nrr/ú:rÃrgéþÿÿÿz1No statistics object is current -- cannot reload.r) rr:rÐr?r*r#r>rLrrÆr&rÇ)r)r ÚerrrrrrÇ‚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£rxrrrÚ help_read“szProfileBrowser.help_readcCs$|jr|j ¡ntd|jd�dS)NrÉr/r)r:ryr?r#rÕrrrÚ do_reverse—s zProfileBrowser.do_reversecCstd|jd�dS)Nz/Reverse the sort order of the profiling report.r/r£rxrrrÚ 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) r:r?r#rlÚallrÊrurrhrN)r)r rmrrræ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£rxrrrÚ help_sort¬szProfileBrowser.help_sortcs‡fdd„tjDƒS)Ncsg|]}| ˆ¡r|‘qSr)r¶)rºÚa©ÚtextrrÚ °s z0ProfileBrowser.complete_sort..)rrh)r)rìr*rrërÚ complete_sort¯szProfileBrowser.complete_sortcCs | d|¡S)Nr•rÔrÕrrrÚdo_stats²szProfileBrowser.do_statscCstd|jd�| ¡dS)Nz.Print statistics from the current stat object.r/r×rxrrrÚ help_stats´szProfileBrowser.help_statscCs$|jr|j ¡ntd|jd�dS)NrÉr/)r:r}r?r#rÕrrrÚdo_strip¸s zProfileBrowser.do_stripcCstd|jd�dS)NzsR