o w[er@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_ _all_values)clsvaluesvalueobj other_valuer#/usr/lib/python3.10/pstats.pyr/s  zSortKey.__new__N) __name__ __module__ __qualname__CALLS CUMULATIVEFILENAMELINENAMENFLPCALLSSTDNAMETIMErr#r#r#r$r$s rT) 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__r7r6rrrr#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)sysstdoutr<leninitadd)selfr<argsargr#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_listr9 total_calls prim_calls max_name_lenset top_levelstats sort_arg_dict load_statsget_top_level_stats Exceptionprintr<)rBrDr#r#r$r@us*  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)rR isinstanceropenmarshalloadosstatrctimest_mtimerKhasattrrY TypeError __class__)rBrDf file_statsr#r#r$rTs,     zStats.load_statscCs|jD]8\}\}}}}}|j|7_|j|7_|j|7_d|vr-|j|tt||j kr=tt||_ qdS)N)jprofilerprofiler) rRitemsrMrNr9rQrAr?func_std_stringrO)rBfuncccncttctcallersr#r#r$rUs 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)reversedtyperrKrMrNr9rQrArOrLrRriadd_func_stats)rBarg_listitemrkr_ old_func_statr#r#r$rAs*     z Stats.addcCs>t|d}t|j|WddS1swYdS)z:Write the profile data to a file we know how to load back.wbN)r[r\dumprR)rBrrer#r#r$ dump_statss "zStats.dump_stats)))rrFz call count)))rFzcumulative time))rz file name))rz line number))rz function name))rr|r~zname/file/line)))rrFzprimitive call count)))rz standard name)))rFz 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.rNrF)rSsort_arg_dict_defaultri)rBdictbad_listwordtupfragmentr#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 )rFrrrrzCan't have mixed argument typer#rGz, )keyrF)rLr?rZr8rsrcr sort_typerr rRriappendrjsortr TupleCompcompare)rBfieldrD sort_arg_defs sort_tuple connectorr stats_listrkrlrmrnrorprLtupler#r#r$ sort_statssH    zStats.sort_statscCs|jr|j|SN)rLreverserBr#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|_ |Srq) rRrifunc_strip_pathr?rjrtrQrPrArOrLrJ)rBoldstatsnewstatsrOrkrlrmrnrorpnewfunc newcallersfunc2callerold_topnew_topr#r#r$ strip_dirss0     zStats.strip_dirsc Cst|jrdSi|_}|jD](\}\}}}}}||vr i||<|D]\}} ||vr0i||<| |||<q$qdSr)rJrRri) rBrJrkrlrmrnrorprrr#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> ) rZrrecompileerrorsearchrjrr?r7r8)rBsellistmsgnew_listrexrkcountr#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/rF) rLrrRkeysr r7f8r9rr)rB func_listr9r: stats_profilerkrlrmrnrorpr4r5 func_namer rr2r r3 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 rrHr) rOrLrrrRrrr?rWr<rj)rBsel_listwidth stat_listr selectionrrkr#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)NrH zfunction calls endrIz(%d primitive calls)zin %.3f seconds) rKrWr<rQfunc_get_function_namerMrNr9r print_title print_line)rBamountrindentrkrrr#r#r$ print_statss(        zStats.print_statscGsz||\}}|r;|||d|D]}||jvr'||||j|q|||iqt|jdt|jd|S)Nz called...rH)rrprint_call_headingrJprint_call_linerWr<)rBrrrrkr#r#r$ print_calleess    zStats.print_calleesc Gsh||\}}|r2||d|D]}|j|\}}}}} |||| dqt|jdt|jd|S)Nzwas called by...z<-rH)rrrRrrWr<) rBrrrrkrlrmrnrorpr#r#r$ print_callerss   zStats.print_callersc Csztd|||jdd}|jD]\}}}}}|r+tt|} t| t}nq|r;td|d|jddSdS)Nz Function rHFrz ncalls tottime cumtime) rWljustr<rRrnextiterrZr) rB name_size column_title subheaderrlrmrnrorpr r#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) NrrrHrGz%d/%dz%dz %s %s %s %srrrz %s(%r) %sr{) rWrjrr<sortedrrZrrjustr?rrR)rBrsource call_dictarrowclistrrkrr rmrlrnrosubstats left_widthr#r#r$rs.      zStats.print_call_linecCs"tdd|jdtd|jddS)Nz- ncalls tottime percall cumtime percallrrzfilename:lineno(function)rHrWr<rr#r#r$rszStats.print_titlecCs|j|\}}}}}t|}||kr|dt|}t|dd|jdtt|d|jd|dkrz{%s}rrFz %s:%d(%s)) startswithendswith)rrr#r#r$rj)s rjc Cs@|\}}}}}|\}}} } } |||||| || t| |fS)z3Add together all the stats for two profile entries.) add_callers) targetrrlrmrnrorpt_cct_nct_ttt_ct t_callersr#r#r$rt:s rtcCsi}|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..)rirZrzip)rr new_callersrkrr#r#r$rAs  " rcCsd}|D]}||7}q|S)z@Sum the caller statistics to get total number of calls received.r)r)rprmr 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% ) cmdCmdrEpromptrRr=r>r<do_read)rBprofiler#r#r$rEls 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]rHNo statistics object is loaded.) splitrr8 ValueErrorr7rWr<rRgetattr)rBfnrrC processedtermfracr#r#r$genericts2    zProfileBrowser.genericcCsXtd|jdtd|jdtd|jdtd|jdtd|jdtd|jddS)NzArguments may be:rHz0* 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: %srHrr)rRrAOSErrorrWr<)rBrer#r#r$do_adds zProfileBrowser.do_addcCtd|jddS)Nz>Add profile info from given file to current statistics object.rHrrr#r#r$help_addzProfileBrowser.help_addcC |d|S)NrrrBrr#r#r$ do_callees zProfileBrowser.do_calleescCtd|jd|dS)Nz6Print callees statistics from the current stat object.rHrWr<rrr#r#r$ help_callees zProfileBrowser.help_calleescCr)Nrrrr#r#r$ do_callersrzProfileBrowser.do_callerscCr)Nz6Print callers statistics from the current stat object.rHrrr#r#r$ help_callersrzProfileBrowser.help_callerscCstd|jddS)NrGrHrrrr#r#r$do_EOFszProfileBrowser.do_EOFcCrNzLeave the profile browser.rHrrr#r#r$help_EOFrzProfileBrowser.help_EOFcCsdS)Nrr#rr#r#r$do_quitszProfileBrowser.do_quitcCrr#rrr#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) NrrH:rrz1No statistics object is current -- cannot reload.r) rrRrrWrCr<rVrdr%rr?r)rBrerrr#r#r$rs(  zProfileBrowser.do_readcC td|jdtd|jddS)Nz+Read in profile data from a specified file.rHz*Without argument, reload the current file.rrr#r#r$ help_readzProfileBrowser.help_readcCs&|jr |jdStd|jddS)NrrHr)rRrrWr<rr#r#r$ do_reverses  zProfileBrowser.do_reversecCr)Nz/Reverse the sort order of the profiling report.rHrrr#r#r$ help_reverserzProfileBrowser.help_reversecs|js td|jddS|j|r*tfdd|Dr*|jj|dStd|jdtj D]\}}td||df|jdq6dS) NrrHc3s|]}|vVqdSrr#)rrabbrevsr#r$rsz)ProfileBrowser.do_sort..z/Valid sort keys (unique prefixes are accepted):z%s -- %srr) rRrWr<rallrrrrri)rBrrr r#r/r$do_sorts zProfileBrowser.do_sortcCr*)Nz.Sort profile data according to specified keys.rHz3(Typing `sort' without arguments lists valid keys.)rrr#r#r$ help_sortr,zProfileBrowser.help_sortcsfddtjDS)Ncsg|] }|r|qSr#)r)ratextr#r$ sz0ProfileBrowser.complete_sort..)rr)rBr6rCr#r5r$ complete_sortszProfileBrowser.complete_sortcCr)Nrrrr#r#r$do_statsrzProfileBrowser.do_statscCr)Nz.Print statistics from the current stat object.rHrrr#r#r$ help_statsrzProfileBrowser.help_statscCs&|jr |jdStd|jddS)NrrH)rRrrWr<rr#r#r$do_stripszProfileBrowser.do_stripcCr)Nzsh     C