o ?Ogr@sdZddlZddlZddlZddlZddlZddlmZddlm Z ddl m Z ddl m Z gdZGdd d eeZe d d Gd d d Ze d d GdddZGdddZGdddZddZddZddZddZddZddZd d!Zed"krddlZzddlZWn eyYnwGd#d$d$ej Z!e"ej#d%krej#d%Z$ndZ$z(e!e$Z%ej#d&dD]Z&e%'e&qe(d'e%j)d(e%*e(d)e%j)d(WdSe+yYdSwdS)*z3Class for printing reports on profiled python code.N)Enum) cmp_to_key) dataclass)Dict)StatsSortKeyFunctionProfile StatsProfilec@s8eZdZdZdZdZdZdZdZdZ dZ d Z d d Z d S) r)callsncalls) cumulativecumtime)filenamemodulelinenamenflpcallsstdname)timetottimecGs@|d}t||}||_|ddD]}||j|<q||_|SNr)str__new___value__value2member_map_Z _all_values)clsvaluesvalueobjZ other_valuer!-/opt/alt/python310/lib64/python3.10/pstats.pyr/s  zSortKey.__new__N) __name__ __module__ __qualname__ZCALLSZ CUMULATIVEZFILENAMEZLINENAMEZNFLZPCALLSZSTDNAMEZTIMErr!r!r!r"r$s rT)Z unsafe_hashc@sFeZdZUeed<eed<eed<eed<eed<eed<eed<dS) rr rpercall_tottimer percall_cumtime file_name line_numberN)r#r$r%r__annotations__floatintr!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@seZdZdZddddZddZdd Zd d Zd d ZddZ dddddddddddddd Z ddZ ddZ ddZ d d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd9d3d4Zd5d6Zd7d8ZdS):raThis 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|s d}n |d}|dd}|||j|dSr)sysstdoutr1leninitadd)selfr1argsargr!r!r"__init__ks   zStats.__init__cCsd|_g|_d|_d|_d|_d|_d|_t|_i|_ i|_ | |z| WdSt yDtd|jr<|jdnd|jdw)NrzInvalid timing data %sfile) all_calleesfilesfcn_listr. total_calls prim_calls max_name_lenset top_levelstats sort_arg_dict load_statsget_top_level_stats Exceptionprintr1)r7r9r!r!r"r5us*  z Stats.initcCs|dur i|_dSt|trDt|d}t||_Wdn1s$wYzt|}t |j d|}WnY|g|_ nt |drT| |j|_i|_|js`td|j|fdS)Nrbz create_statsz.Cannot create or construct a %r object from %r)rG isinstanceropenmarshalloadosstatrctimest_mtimer@hasattrrN TypeError __class__)r7r9fZ file_statsr!r!r"rIs,     zStats.load_statscCs|jD]8\}\}}}}}|j|7_|j|7_|j|7_d|vr-|j|tt||j kr=tt||_ qdS)N)ZjprofilerZprofiler) rGitemsrBrCr.rFr6r4func_std_stringrD)r7funcccncttctcallersr!r!r"rJs zStats.get_top_level_statscGs|s|St|D]k}t|t|krt|}|j|j7_|j|j7_|j|j7_|j|j7_|jD]}|j|q9|j |j krL|j |_ d|_ |j D]\}}||j vrc|j |}nddddif}t |||j |<qTq|SNr)reversedtyperr@rBrCr.rFr6rDrArGr[add_func_stats)r7arg_listitemr]rTZ old_func_statr!r!r"r6s*     z Stats.addcCs>t|d}t|j|WddS1swYdS)z:Write the profile data to a file we know how to load back.wbN)rPrQdumprG)r7rrZr!r!r" dump_statss "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 rrrrrrrrrcCsv|js8i|_}i}|jD] \}}|}|r/|sn||vr#d||<n |||<|dd}|sq|D]}||=q2|jS)z)Expand all abbreviations that are unique.rNr;)rHsort_arg_dict_defaultr[)r7dictZbad_listwordtupZfragmentr!r!r"get_sort_arg_defss$  zStats.get_sort_arg_defscGsZ|sd|_|St|dkr!t|dtr!ddddd|dg}nt|dkr>|ddD]}t|t|dkr=td q-|}d }d |_d }|D]!}t|trU|j }|||d}|j|||d7_d }qKg}|j D]\}\} } } } } | | | | | f|t ||fqt|jtt|jd g|_}|D] }| |dq|S)Nrrrr rr )r;rrrtrtzCan't have mixed argument typer!r<z, )keyr;)rAr4rOr-rerXry sort_typerrrGr[appendr\sortr TupleCompcompare)r7Zfieldr9Z sort_arg_defsZ sort_tupleZ connectorrwZ stats_listr]r^r_r`rarbrAtupler!r!r" sort_statssH    zStats.sort_statscCs|jr|j|SN)rAreverser7r!r!r" reverse_orders zStats.reverse_ordercCs|j}i|_}d}|D]H\}\}}}}} t|} tt| |kr)tt| }i} | D] \} } | | t| <q/| |vrMt|| ||||| f|| <q||||| f|| <q|j}t|_}|D] }|t|qb||_ d|_ d|_ |Src) rGr[func_strip_pathr4r\rfrFrEr6rDrAr?)r7ZoldstatsZnewstatsrDr]r^r_r`rarbZnewfuncZ newcallersfunc2callerZold_topZnew_topr!r!r" strip_dirss0     zStats.strip_dirsc Cst|jrdSi|_}|jD](\}\}}}}}||vr i||<|D]\}} ||vr0i||<| |||<q$qdSr)r?rGr[) r7r?r]r^r_r`rarbrrr!r!r" calc_callees5s zStats.calc_calleescCs|}t|tr7zt|}Wntjy"|d|7}||fYSwg}|D]}|t|r5||q'n=t|}t|t r[d|krJdkr[nnt ||d}|d|}nt|t rtd|krj|krtnn|}|d|}t|t|kr|dt|t||f7}||fS)Nz# gg?g?rz6 List reduced from %r to %r due to restriction <%r> ) rOrrecompileerrorsearchr\r|r4r,r-)r7ZsellistmsgZnew_listZrexr]countr!r!r"eval_print_amountHs4    "" zStats.eval_print_amountc Cs|jr |jddnt|j}|stdiStt|j}i}t||}|D]Y}|j|\}}}} } |\} } } ||krAt|n t|dt|}tt|}|dkrWdntt||}tt| }|dkrkdntt| |}t |||||| | }||| <q(|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;) rArrGkeysr 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}n t|j}d}|D] }||||\}}q t|}|s6d|fSt||j d|t|jkrYd}|D]}tt ||krXtt |}qH|d|fS)Nz Ordered by:  z! Random listing order was used rr=rt) rDrAr{rrGrrr4rLr1r\)r7Zsel_listwidthZ stat_listrZ selectionrr]r!r!r"get_print_lists&  zStats.get_print_listcGs|jD] }t||jdq|jrt|jdd}|jD] }t|t||jdqt||jdd|jd|j|jkrDtd|jd|jdtd|j|jdt|jd||\}}|rw| |D]}| |qct|jdt|jd|S)Nr= zfunction calls endr>z(%d primitive calls)zin %.3f seconds) r@rLr1rFfunc_get_function_namerBrCr.r print_title print_line)r7amountrindentr]rrr!r!r" print_statss(        zStats.print_statscGsz||\}}|r;|||d|D]}||jvr'||||j|q|||iqt|jdt|jd|S)Nz called...r=)rrprint_call_headingr?print_call_linerLr1)r7rrrr]r!r!r" print_calleess    zStats.print_calleesc Gsh||\}}|r2||d|D]}|j|\}}}}} |||| dqt|jdt|jd|S)Nzwas called by...z<-r=)rrrGrrLr1) r7rrrr]r^r_r`rarbr!r!r" print_callerss   zStats.print_callersc Csztd|||jdd}|jD]\}}}}}|r+tt|} t| t}nq|r;td|d|jddSdS)Nz Function r=Frz ncalls tottime cumtime) rLljustr1rGrnextiterrOr) r7 name_sizeZ column_titleZ subheaderr^r_r`rarbrr!r!r"rs zStats.print_call_heading->cCstt|||d|jd|st|jddSt|}d}|D]^}t|}||} t| trb| \} } } } | | krCd| | f}nd| f}d|dd t |t | t | |f}|d }nd || t |j |d f}|d }t||||jdd}q#dS) Nrrr=r<z%d/%dz%dz %s %s %s %srsrtrz %s(%r) %srl) rLr\rr1sortedrrOrrjustr4rrG)r7rsourceZ call_dictZarrowZclistrr]rrr_r^r`raZsubstatsZ left_widthr!r!r"rs.      zStats.print_call_linecCs"tdd|jdtd|jddS)Nz- ncalls tottime percall cumtime percallrrzfilename:lineno(function)r=rLr1rr!r!r"rszStats.print_titlecCs|j|\}}}}}t|}||kr|dt|}t|dd|jdtt|d|jd|dkrz{%s}rr;z %s:%d(%s)) startswithendswith)rrr!r!r"r\)s r\c Cs@|\}}}}}|\}}} } } |||||| || t| |fS)z3Add together all the stats for two profile entries.) add_callers) targetrr^r_r`rarbZt_ccZt_ncZt_ttZt_ctZ t_callersr!r!r"rf:s rfcCsi}|D]\}}|||<q|D]+\}}||vr:t|tr1tddt|||D||<q|||7<q|||<q|S)z*Combine two caller lists in a single list.css|] \}}||VqdSrr!).0ijr!r!r" Jszadd_callers..)r[rOrzip)rrZ new_callersr]rr!r!r"rAs  " rcCsd}|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  rcCsd|S)Nz%8.3fr!)xr!r!r"r]rr__main__c@seZdZd6ddZddZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ ddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5ZdS)7ProfileBrowserNcCs:tj|d|_d|_tj|_|dur||dSdS)N% ) cmdCmdr:promptrGr2r3r1do_read)r7profiler!r!r"r:ls zProfileBrowser.__init__c Cs|}g}|D]@}z |t|WqtyYnwzt|}|dks*|dkr3td|jdWq||WqtyBYnw||q|jrVt|j||dStd|jddS)Nrrz#Fraction argument must be in [0, 1]r=No statistics object is loaded.) splitr|r- ValueErrorr,rLr1rGgetattr)r7fnrr8Z processedZtermZfracr!r!r"genericts2    zProfileBrowser.genericcCsXtd|jdtd|jdtd|jdtd|jdtd|jdtd|jddS)NzArguments 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.rrr!r!r" generic_helps zProfileBrowser.generic_helpc Csh|jr+z |j|WdSty*}ztd||f|jdWYd}~dSd}~wwtd|jddS)Nz$Failed to load statistics for %s: %sr=rr)rGr6OSErrorrLr1)r7rer!r!r"do_adds zProfileBrowser.do_addcCtd|jddS)Nz>Add profile info from given file to current statistics object.r=rrr!r!r"help_addzProfileBrowser.help_addcC |d|S)Nrrr7rr!r!r" do_callees zProfileBrowser.do_calleescCtd|jd|dS)Nz6Print callees statistics from the current stat object.r=rLr1rrr!r!r" help_callees zProfileBrowser.help_calleescCr)Nrrrr!r!r" do_callersrzProfileBrowser.do_callerscCr)Nz6Print callers statistics from the current stat object.r=rrr!r!r" help_callersrzProfileBrowser.help_callerscCstd|jddS)Nr<r=rrrr!r!r"do_EOFszProfileBrowser.do_EOFcCrNzLeave the profile browser.r=rrr!r!r"help_EOFrzProfileBrowser.help_EOFcCsdS)Nrr!rr!r!r"do_quitszProfileBrowser.do_quitcCrrrrr!r!r" help_quitrzProfileBrowser.help_quitc Cs|rLzt||_Wn;ty&}zt|jd|jdWYd}~dSd}~wtyD}zt|jjd||jdWYd}~dSd}~ww|d|_ dSt |j dkra|j dd}| |dStd|jddS) Nrr=:rrtz1No statistics object is current -- cannot reload.r) rrGrrLr8r1rKrYr#rr4r)r7rerrr!r!r"rs(  zProfileBrowser.do_readcC td|jdtd|jddS)Nz+Read in profile data from a specified file.r=z*Without argument, reload the current file.rrr!r!r" help_readzProfileBrowser.help_readcCs&|jr |jdStd|jddS)Nrr=r)rGrrLr1rr!r!r" do_reverses  zProfileBrowser.do_reversecCr)Nz/Reverse the sort order of the profiling report.r=rrr!r!r" help_reverserzProfileBrowser.help_reversecs|js td|jddS|j|r*tfdd|Dr*|jj|dStd|jdtj D]\}}td||df|jdq6dS) Nrr=c3s|]}|vVqdSrr!)rrZabbrevsr!r"rsz)ProfileBrowser.do_sort..z/Valid sort keys (unique prefixes are accepted):z%s -- %srr) rGrLr1ryallrrrrur[)r7rrzrr!rr"do_sorts zProfileBrowser.do_sortcCr)Nz.Sort profile data according to specified keys.r=z3(Typing `sort' without arguments lists valid keys.)rrr!r!r" help_sortrzProfileBrowser.help_sortcsfddtjDS)Ncsg|] }|r|qSr!)r)ratextr!r" sz0ProfileBrowser.complete_sort..)rru)r7rr8r!rr" complete_sortszProfileBrowser.complete_sortcCr)Nrrrr!r!r"do_statsrzProfileBrowser.do_statscCr)Nz.Print statistics from the current stat object.r=rrr!r!r" help_statsrzProfileBrowser.help_statscCs&|jr |jdStd|jddS)Nrr=)rGrrLr1rr!r!r"do_stripszProfileBrowser.do_stripcCr)Nzsh     C