mfc@sdZddlZddlZddlZddlZddlZddlmZdgZdfdYZ dfdYZ dZ d Z d Z d Zd Zd ZdZedkrddlZyddlZWnek rnXdejfdYZddlZeejdkrFejdZneZy4eeZejdIJejejdIJWnek rnXndS(s3Class for printing reports on profiled python code.iN(t cmp_to_keytStatscBs<eZdZdZdZdZdZdZdZid8d 6d;d 6d>d6dAd6dDd6dGd6dJd6dMd6dPd6dQdRdSfdfd6dVd6dYd"6d\d%6d_d&6Z d'Z d(Z d)Z d*Z d+Zd,Zd-Zd.Zd/Zd0Zd1Zd2d3Zd4Zd5ZRS(`s<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). It takes an arbitrary number of quoted strings to select the sort order. For example sort_stats('time', '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) cOstj|_d|kr/|d|_|d=n|r|j}|jdjg|D]}d|||f^qX}td|nt|sd}n|d}|d}|j ||j |dS(Ntstreams, s%s=%ssunrecognized keyword args: %sii( tsyststdoutRtkeystsorttjoint ValueErrortlentNonetinittadd(tselftargstkwdsRtktextrastarg((s/usr/lib64/python2.7/pstats.pyt__init__>s      0     cCsd|_g|_d|_d|_d|_d|_d|_i|_i|_ i|_ |j |d}z|j d}Wd|r|j dI|jr|j |jdIn|j JnXdS(NiisInvalid timing datai(R t all_calleestfilestfcn_listttotal_ttt total_callst prim_callst max_name_lent top_leveltstatst sort_arg_dictt load_statstget_top_level_statsR(R Rttrouble((s/usr/lib64/python2.7/pstats.pyR Ts(               cCs|si|_nt|trt|d}tj||_|jy-tj|}t j |j d|}WnnX|g|_ n1t |dr|j|j|_i|_n|jstd|j|fndS(Ntrbs t create_statss.Cannot create or construct a %r object from %r(Rt isinstancet basestringtopentmarshaltloadtclosetoststatttimetctimetst_mtimeRthasattrR"t TypeErrort __class__(R Rtft file_stats((s/usr/lib64/python2.7/pstats.pyRjs(      cCsx|jjD]\}\}}}}}|j|7_|j|7_|j|7_d|krtd|j|(R tdicttbad_listtwordttuptfragment((s/usr/lib64/python2.7/pstats.pytget_sort_arg_defss        cGs|sd|_|St|dkrnt|dttfrnidd6dd6dd6dd6|dg}n|j}d }d |_d }xA|D]9}|||d}|j|||d7_d }qWg}xY|jjD]H\}\}} } } } |j || | | f|t ||fqW|j d t t |jg|_} x|D]}| j |dqhW|S( NiiRQiRHR+RKits, tkey((RR R#tinttlongRYt sort_typeRR>tappendR6RRt TupleComptcompare(R tfieldt sort_arg_defst sort_tuplet connectorRVt stats_listR7R8R9R:R;R<Rttuple((s/usr/lib64/python2.7/pstats.pyt sort_statss2 +     +  cCs|jr|jjn|S(N(Rtreverse(R ((s/usr/lib64/python2.7/pstats.pyt reverse_orders cCsX|j}i|_}d}x|jD]\}\}}}}} t|} tt| |kr}tt| }ni} x*| jD]\} } | | t| tfunc_strip_pathR R6R?RR RRR(R toldstatstnewstatsRR7R8R9R:R;R<tnewfunct newcallerstfunc2tcallertold_toptnew_top((s/usr/lib64/python2.7/pstats.pyt strip_dirss.  (        c Cs|jr dSi|_}x|jjD]x\}\}}}}}||kr^i||( R RR7R8R9R:R;R<RpRq((s/usr/lib64/python2.7/pstats.pyt calc_calleess  +    c Csw|}t|trytj|}Wn)tjk rS|d|7}||fSXg}x|D]+}|jt|ra|j|qaqaWnt|}t|t rd|kodknrt ||d}|| }nDt|t t fr/d|ko|knr/|}|| }nt|t|krm|dt|t||f7}n||fS(Ns# gg?g?is6 List reduced from %r to %r due to restriction <%r> ( R#R$tretcompileterrortsearchR6R_R tfloatR\R](R tseltlisttmsgtnew_listtrexR7tcount((s/usr/lib64/python2.7/pstats.pyteval_print_amount%s,   + 1  cCs|j}|jr0|j}d|jd}n|jj}d}x)|D]!}|j|||\}}qLWt|}|sd|fS|j|IJ|t|jkrd}x>|D]3}tt||krtt|}qqWn|d|fS(Ns Ordered by: s s! Random listing order was used ii( RRR^RRRR RR6(R tsel_listtwidtht stat_listR}t selectionRR7((s/usr/lib64/python2.7/pstats.pytget_print_list?s$        cGsx|jD]}|j|IJq W|jr4|jJnd}x*|jD]}|j|It|IJqDW|j|I|jIdI|j|jkr|jd|jIn|jd|jIJ|jJ|j|\}}|r|jx|D]}|j |qW|jJ|jJn|S(Nt isfunction callss(%d primitive calls)sin %.3f secondss ( RRRtfunc_get_function_nameRRRRt print_titlet print_line(R tamountRFtindentR7RR|((s/usr/lib64/python2.7/pstats.pyt print_statsWs(    cGs|j|\}}|r|j|j|dxM|D]E}||jkrn|j|||j|q<|j||iq<W|jJ|jJn|S(Ns called...(RRutprint_call_headingRtprint_call_lineR(R RRR|R7((s/usr/lib64/python2.7/pstats.pyt print_calleesms   c Gs|j|\}}|r|j|dx@|D]8}|j|\}}}}} |j||| dq2W|jJ|jJn|S(Nswas called by...s<-(RRRRR( R RRR|R7R8R9R:R;R<((s/usr/lib64/python2.7/pstats.pyt print_callers|s  c Cs|jdj||IJt}xQ|jjD]@\}}}}}|r0|jj} t| t}Pq0q0W|r|jd|dIJndS(Ns Function Rs ncalls tottime cumtime(RtljusttFalseRt itervaluestnextR#Rg( R t name_sizet column_titlet subheaderR8R9R:R;R<tvalue((s/usr/lib64/python2.7/pstats.pyRs%s->cCsT|jt|j||I|s1|jJdS|j}|jd}x|D]}t|}||} t| tr| \} } } } | | krd| | f}n d| f}d|jddt|t | t | |f}|d}n.d|| t |j |d f}|d }|j|||IJd }qTWdS( NRZs%d/%ds%ds %s %s %s %siiis %s(%r) %siR( RR6RRRR#RgtrjustR tf8R(R Rtsourcet call_dicttarrowtclistRR7RNRR9R8R:R;tsubstatst left_width((s/usr/lib64/python2.7/pstats.pyRs,         $ cCs|jdI|jdIJdS(Ns- ncalls tottime percall cumtime percallsfilename:lineno(function)(R(R ((s/usr/lib64/python2.7/pstats.pyRs cCs|j|\}}}}}t|}||krK|dt|}n|j|jdI|jt|I|dkr|jdIn|jtt||I|jt|I|dkr|jdIn|jtt||I|jt|IJdS(Nt/i iRis s (RtstrRRRRzR6(R R7R8R9R:R;R<tc((s/usr/lib64/python2.7/pstats.pyRs    (ii((ii(((iis call count(ii((ii(((iis call count(ii((ii(((iiscumulative time(ii((ii(((iiscumulative time(ii((ii(((iis file name(ii((ii(((iis file name(ii((ii(((iis line number(ii((ii(((iis file name(ii((ii(((iis function name(ii(ii(ii(ii((ii(((iisprimitive call count(ii((ii(((iis standard name(ii((ii(((iis internal time(ii((ii(((iis internal time(t__name__t __module__t__doc__RR RRR RGRSRYRhRjRtRuRRRRRRRRR(((s/usr/lib64/python2.7/pstats.pyR sH                R`cBs eZdZdZdZRS(sThis 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 ||_dS(N(tcomp_select_list(R R((s/usr/lib64/python2.7/pstats.pyRscCsSxL|jD]A\}}||}||}||kr;| S||kr |Sq WdS(Ni(R(R tlefttrighttindext directiontltr((s/usr/lib64/python2.7/pstats.pyRas    (RRRRRa(((s/usr/lib64/python2.7/pstats.pyR`s cCs(|\}}}tjj|||fS(N(R)tpathtbasename(t func_nameRFRLRN((s/usr/lib64/python2.7/pstats.pyRkscCs|dS(Ni((R7((s/usr/lib64/python2.7/pstats.pyRscCsZ|d d krN|d}|jdrG|jdrGd|dd!S|Snd |SdS( Nit~its{%s}iis %s:%d(%s)(Ri(t startswithtendswith(RRN((s/usr/lib64/python2.7/pstats.pyR6s  c CsV|\}}}}}|\}}} } } |||||| || t| |fS(s3Add together all the stats for two profile entries.(t add_callers( ttargetRR8R9R:R;R<tt_cctt_nctt_tttt_ctt t_callers((s/usr/lib64/python2.7/pstats.pyR?scCsi}x$|jD]\}}|||R#Rgtzip(RRt new_callersR7Rqti((s/usr/lib64/python2.7/pstats.pyRs 9cCs+d}x|jD]}||7}qW|S(s@Sum the caller statistics to get total number of calls received.i(R(R<R9RH((s/usr/lib64/python2.7/pstats.pyt count_calls scCsd|S(Ns%8.3f((tx((s/usr/lib64/python2.7/pstats.pyRst__main__tProfileBrowsercBseZddZdZdZdZdZdZdZ dZ dZ d Z d Z d Zd Zd ZdZdZdZdZdZdZdZdZdZdZdZdZRS(cCsNtjj|d|_d|_tj|_|dk rJ|j |ndS(Ns% ( tcmdtCmdRtpromptR RRRRtdo_read(R tprofile((s/usr/lib64/python2.7/pstats.pyR&s     cCs|j}g}x|D]}y|jt|wWntk rLnXyKt|}|dkst|dkr|jdIJwn|j|wWntk rnX|j|qW|jrt|j||n |jdIJdS(Niis#Fraction argument must be in [0, 1]sNo statistics object is loaded.(tsplitR_R\RRzRRtgetattr(R tfnRLRt processedttermtfrac((s/usr/lib64/python2.7/pstats.pytgeneric.s,         cCsR|jdIJ|jdIJ|jdIJ|jdIJ|jdIJ|jdIJdS(NsArguments may be:s0* An integer maximum number of entries to print.s:* A decimal fractional number between 0 and 1, controllings- what fraction of selected entries to print.s8* A regular expression; only entries with function namess that match it are printed.(R(R ((s/usr/lib64/python2.7/pstats.pyt generic_helpFs      cCs-|jr|jj|n |jdIJdS(NsNo statistics object is loaded.i(RR R(R RL((s/usr/lib64/python2.7/pstats.pytdo_addNs  cCs|jdIJdS(Ns>Add profile info from given file to current statistics object.(R(R ((s/usr/lib64/python2.7/pstats.pythelp_addTscCs|jd|S(NR(R(R RL((s/usr/lib64/python2.7/pstats.pyt do_calleesWscCs|jdIJ|jdS(Ns6Print callees statistics from the current stat object.(RR(R ((s/usr/lib64/python2.7/pstats.pyt help_calleesYs cCs|jd|S(NR(R(R RL((s/usr/lib64/python2.7/pstats.pyt do_callers]scCs|jdIJ|jdS(Ns6Print callers statistics from the current stat object.(RR(R ((s/usr/lib64/python2.7/pstats.pyt help_callers_s cCs|jdIJdS(NRZi(R(R RL((s/usr/lib64/python2.7/pstats.pytdo_EOFcs cCs|jdIJdS(NsLeave the profile brower.(R(R ((s/usr/lib64/python2.7/pstats.pythelp_EOFfscCsdS(Ni((R RL((s/usr/lib64/python2.7/pstats.pytdo_quitiscCs|jdIJdS(NsLeave the profile brower.(R(R ((s/usr/lib64/python2.7/pstats.pyt help_quitkscCs|ryt||_WnUtk r@}|j|dIJdStk rp}|j|jjdI|IJdSX|d|_n?t|jdkr|jd }|j |n |jdIJdS(Nit:s% iis1No statistics object is current -- cannot reload.i( RRtIOErrorRt ExceptionR0RRR R(R RLRterr((s/usr/lib64/python2.7/pstats.pyRns  cCs|jdIJ|jdIJdS(Ns+Read in profile data from a specified file.s*Without argument, reload the current file.(R(R ((s/usr/lib64/python2.7/pstats.pyt help_reads cCs*|jr|jjn |jdIJdS(NsNo statistics object is loaded.i(RRjR(R RL((s/usr/lib64/python2.7/pstats.pyt do_reverses  cCs|jdIJdS(Ns/Reverse the sort order of the profiling report.(R(R ((s/usr/lib64/python2.7/pstats.pyt help_reversescs|js|jdIJdS|jj|rjtfd|jDrj|jj|jnH|jdIJx8tjjD]'\}}|jd||dfIJqWdS(NsNo statistics object is loaded.c3s|]}|kVqdS(N((t.0R(tabbrevs(s/usr/lib64/python2.7/pstats.pys ss/Valid sort keys (unique prefixes are accepted):s%s -- %sii( RRRYtallRRhRRSR>(R RLR[R((Rs/usr/lib64/python2.7/pstats.pytdo_sorts  ( cCs|jdIJ|jdIJdS(Ns.Sort profile data according to specified keys.s3(Typing `sort' without arguments lists valid keys.)(R(R ((s/usr/lib64/python2.7/pstats.pyt help_sorts cGs)gtjD]}|j|r |^q S(N(RRSR(R ttextRta((s/usr/lib64/python2.7/pstats.pyt complete_sortscCs|jd|S(NR(R(R RL((s/usr/lib64/python2.7/pstats.pytdo_statsscCs|jdIJ|jdS(Ns.Print statistics from the current stat object.(RR(R ((s/usr/lib64/python2.7/pstats.pyt help_statss cCs*|jr|jjn |jdIJdS(NsNo statistics object is loaded.(RRtR(R RL((s/usr/lib64/python2.7/pstats.pytdo_strips cCs|jdIJdS(Ns<Strip leading path information from filenames in the report.(R(R ((s/usr/lib64/python2.7/pstats.pyt help_stripscCs|jdIJdS(NsShow help for a given command.(R(R ((s/usr/lib64/python2.7/pstats.pyt help_helpscCs|r |SdS(N(R (R tstopRL((s/usr/lib64/python2.7/pstats.pytpostcmdsN(RRR RRRRRRRRRRRRRRRRRRRRRRRRRR(((s/usr/lib64/python2.7/pstats.pyR%s4                        is*Welcome to the profile statistics browser.sGoodbye.( RRR)R+R&Rvt functoolsRt__all__RR`RkRR6R?RRRRRtreadlinet ImportErrorRRR targvt initprofileR tbrowserRtcmdlooptKeyboardInterrupt(((s/usr/lib64/python2.7/pstats.pytsF