U e5d®tã@sòdZddgZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl mZddlZdZGdd„dƒZd d „Zd d „ZGd d„dƒZdd„Zdd„Zddd„Zdd„ZGdd„dƒZdd„ZedkrîeƒdS)a·program/module to trace Python program or function execution Sample use, command line: trace.py -c -f counts --ignore-dir '$prefix' spam.py eggs trace.py -t --ignore-dir '$prefix' spam.py eggs trace.py --trackcalls spam.py eggs Sample use, programmatically import sys # create a Trace object, telling it what to ignore, and whether to # do tracing or line-counting or both. tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,], trace=0, count=1) # run the new command using the given tracer tracer.run('main()') # make a report, placing output in /tmp r = tracer.results() r.write_results(show_missing=True, coverdir="/tmp") ÚTraceÚCoverageResultséN)Ú monotonicz#pragma NO COVERc@seZdZddd„Zdd„ZdS)Ú_IgnoreNcCs:|s tƒnt|ƒ|_|sgn dd„|Dƒ|_ddi|_dS)NcSsg|]}tj |¡‘qS©)ÚosÚpathÚnormpath©Ú.0Údrrú/usr/lib64/python3.8/trace.pyÚ Hsÿz$_Ignore.__init__..zé)ÚsetÚ_modsÚ_dirsÚ_ignore)ÚselfÚmodulesÚdirsrrr Ú__init__Fs ÿz_Ignore.__init__cCs¦||jkr|j|S||jkr,d|j|<dS|jD]"}| |d¡r2d|j|<dSq2|dkrld|j|<dS|jD]$}| |tj¡rrd|j|<dSqrd|j|<dS)NrÚ.r)rrÚ startswithrrÚsep)rÚfilenameÚ modulenameÚmodr rrr ÚnamesLs$          z _Ignore.names)NN)Ú__name__Ú __module__Ú __qualname__rrrrrr rEs rcCs tj |¡}tj |¡\}}|S)z-Return a plausible module name for the patch.)rrÚbasenameÚsplitext)rÚbaserÚextrrr Ú_modnamews r&cCsÂtj |¡}d}tjD]@}tj |¡}| |¡r|t|ƒtjkrt|ƒt|ƒkr|}q|rr|t|ƒdd…}n|}tj |¡\}}| tjd¡}tj r¨| tj d¡}tj  |¡\}}|  d¡S)z,Return a plausible module name for the path.ÚrNr) rrÚnormcaseÚsysrÚlenrÚ splitdriveÚreplaceÚaltsepr#Úlstrip)rZ comparepathZlongestÚdirr$Zdriverr%rrr Ú _fullmodname~s    r0c@s:eZdZddd„Zdd„Zdd„Zdd d „Zdd d „ZdS)rNc Csþ||_|jdkri|_|j ¡|_||_|jdkr8i|_|j ¡|_||_|jdkrZi|_|j ¡|_||_||_|jrúz@t|jdƒ�}t  |¡\}}}W5QRX|  |  |||¡¡Wn@t t tfk rø}ztd|j|ftjd�W5d}~XYnXdS)NÚrbzSkipping counts file %r: %s©Úfile)ÚcountsÚcopyZcounterÚ calledfuncsÚcallersÚinfileÚoutfileÚopenÚpickleÚloadÚupdateÚ __class__ÚOSErrorÚEOFErrorÚ ValueErrorÚprintr)Ústderr)rr4r6r8r7r9ÚfÚerrrrr r›s2      ÿÿzCoverageResults.__init__cCs| d¡o| d¡S)z_Return True if the filename does not refer to a file we want to have reported. ú<ú>)rÚendswith)rrrrr Úis_ignored_filenameµsz#CoverageResults.is_ignored_filenamec Csn|j}|j}|j}|j}|j}|j}|D]}| |d¡||||<q(|D] }d||<qJ|D] }d||<q\dS)z.Merge in the data from another CoverageResultsrrN)r4r6r7Úget) rÚotherr4r6r7Z other_countsZother_calledfuncsZ other_callersÚkeyrrr r=»s zCoverageResults.updateTFc" Csî|jr@tƒtdƒ|j}t|ƒD]\}}}td|||fƒq"|jrÐtƒtdƒd}} t|jƒD]h\\} } } \} }}| |krœtƒtd| dƒ| }d} | | krº| | krºtd| ƒ| } td| | ||fƒqfi}|jD].\}}| |i¡}||<|j||f||<qÚi}| ¡D�]\}}| |¡�r0�q| d¡�rH|d d …}|d k�rpt j   t j   |¡¡}t |ƒ}n$|}t j  |¡�sŒt  |¡t|ƒ}|�r¤t|ƒ}ni}t |¡}t j  ||d ¡}t|d ƒ�}t |j¡\}}W5QRX| |||||¡\}}|�r|�rtd ||ƒ}||||f||<�q|�rt|�rttdƒt|ƒD]&}||\}}}}td||ƒ�qL|j�rêz6t|jdƒ� } t |j|j|jf| d¡W5QRXWn6tk �rè}!ztd|!tj d�W5d }!~!XYnXd S)af Write the coverage results. :param show_missing: Show lines that had no hits. :param summary: Include coverage summary per module. :param coverdir: If None, the results of each module are placed in its directory, otherwise it is included in the directory specified. zfunctions called:z*filename: %s, modulename: %s, funcname: %szcalling relationships:r'z***z -->z %s.%s -> %s.%sz.pycNéÿÿÿÿz.coverr1édzlines cov% module (path)z%5d %3d%% %s (%s)Úwbrz"Can't save counts files because %sr2)!r6rBÚsortedr7r4rJÚitemsrIrHrrÚdirnameÚabspathr&ÚexistsÚmakedirsr0Ú_find_executable_linenosÚ linecacheÚgetlinesÚjoinr:ÚtokenizeÚdetect_encodingÚreadlineÚwrite_results_fileÚintr9r;Údumpr?r)rC)"rZ show_missingÚsummaryÚcoverdirZcallsrrÚfuncnameZlastfileZ lastcfileZpfileZpmodZpfuncÚcfileZcmodZcfuncZper_fileÚlinenoÚ lines_hitZsumsÚcountr/ÚlnotabÚsourceZ coverpathÚfpÚencodingÚ_Ún_hitsÚn_linesZpercentÚmrDrErrr Ú write_resultsÍsŠ ÿÿ           ÿ   ÿzCoverageResults.write_resultsc Csðzt|d|d�}Wn>tk rP}z td||ftjd�WY¢dSd}~XYnXd}d} |�„t|dƒD]r\} } | |krž| d || ¡| d7} |d7}n.| |krÂt| krÂ| d ¡|d7}n | d ¡| |  d ¡¡qjW5QRX| |fS) z'Return a coverage results file in path.Úw©rjz3trace: Could not open %r for writing: %s - skippingr2)rrNrrz%5d: z>>>>>> z é) r:r?rBr)rCÚ enumerateÚwriteÚPRAGMA_NOCOVERÚ expandtabs) rrÚlinesrgrerjr9rErmrlrdÚlinerrr r])s.ÿÿ    z"CoverageResults.write_results_file)NNNNN)TFN)N)rr r!rrIr=ror]rrrr ršsÿ  \cCs,i}t |¡D]\}}||krd||<q|S)z:Return dict where keys are lines in the line number table.r)ÚdisZfindlinestarts)ÚcodeÚstrsÚlinenosrkrdrrr Ú_find_lines_from_codeIs  r}cCs4t||ƒ}|jD]}t |¡r| t||ƒ¡q|S)z.rcSsg|]}t|tƒr|‘qSr)Ú isinstancer©r rrr rûs ÿrcSsg|]}t|dƒr|‘qS)Ú __bases__)Úhasattr)r r�rrr rþs ÿz%s.%s) Úf_codeÚ co_filenamer&Úco_namer•ÚgcZ get_referrersr*r) rÚframerzrrrbZclsnameZfuncsZdictsÚclassesrrr Úfile_module_function_ofäs,          zTrace.file_module_function_ofcCs0|dkr,| |¡}| |j¡}d|j||f<dS)zkHandler for call events. Adds information about who called who to the self._callers dict. ÚcallrN)rÄÚf_backr”)rrÂÚwhyÚargÚ this_funcZ parent_funcrrr r˜ s  zTrace.globaltrace_trackcallerscCs |dkr| |¡}d|j|<dS)zoHandler for call events. Adds (filename, modulename, funcname) to the self._calledfuncs dict. rÅrN)rÄr“)rrÂrÇrÈrÉrrr ršs zTrace.globaltrace_countfuncscCsj|dkrf|j}|j dd¡}|rbt|ƒ}|dk rf|j ||¡}|sf|jrZtd||jfƒ|j SndSdS)z’Handler for call events. If the code block being entered is to be ignored, returns `None', else returns self.localtrace. rÅÚ__file__Nz! --- modulename: %s, funcname: %s) r¾Ú f_globalsrJr&r�rr’rBrÀr�)rrÂrÇrÈrzrrZ ignore_itrrr r›!sÿzTrace.globaltrace_ltcCs„|dkr~|jj}|j}||f}|j |d¡d|j|<|jrTtdtƒ|jdd�tj   |¡}td||t   ||¡fdd�|j S) Nrxrrú%.2fú ©r‰ú %s(%d): %sr')r¾r¿Úf_linenor4rJr–rBr—rrr"rWÚgetliner�)rrÂrÇrÈrrdrLÚbnamerrr rœ8s  ÿÿz Trace.localtrace_trace_and_countcCsd|dkr^|jj}|j}|jr4tdtƒ|jdd�tj |¡}td||t   ||¡fdd�|j S)NrxrÌrÍrÎrÏr') r¾r¿rÐr–rBr—rrr"rWrÑr�)rrÂrÇrÈrrdrÒrrr ržGs  ÿÿzTrace.localtrace_tracecCs<|dkr6|jj}|j}||f}|j |d¡d|j|<|jS)Nrxrr)r¾r¿rÐr4rJr�)rrÂrÇrÈrrdrLrrr rŸTs zTrace.localtrace_countcCst|j|j|j|j|jd�S)N)r8r9r6r7)rr4r8r9r“r”)rrrr Úresults\s  ýz Trace.results) rrrrrrNNF)NN)rr r!rrªr§rºÚ__text_signature__rÄr˜ršr›rœržrŸrÓrrrr r„s&þ 2 )   c sØddl}| ¡}|jdddd�| dd¡}|jdd d d d �|jd dd dd �|jddd dd �|jddd dd �| d¡}| ¡}|jddd dd �|jddd dd �|jdddd �|jd!d"d#d �|jd$d%d d&d �|jd'd(d d)d �|jd*d+d d,d �| d-d.¡}|jd/d0gd1d2�|jd3d0gd4d2�|jd5d d6d7d2�|jd8d9d:d;�|jd<|jd=d;�| ¡}|j�rˆt  d>¡‰t  d?¡‰‡‡fd@dA„‰dBdC„|j Dƒ|_ ‡fdDdC„|jDƒ|_|j �rü|j �sØ|  dE¡t|j |j dF�}| |j|j|j¡St|j|j|j|jgƒ�s |  dG¡|j�rB|j�s8|j�rB|  dH¡|j�r\|j�s\|  dI¡|jdk�rr|  dJ¡t|j|j|j|j|j |j|j |j |jdK� }zÀ|j�rðddl}|j}| |¡\} } } | jf|j•t _!dL| j| j"| j#| ddMœ} n^|jf|j•t _!t$j% &|j¡t j%d<t' (|j¡�} t)|  *¡|jdNƒ} W5QRX|jdLdddOœ} | +| | | ¡WnPt,k �rš}zt  -dPt j!d|f¡W5d}~XYnt.k �r®YnX| /¡}|j0�sÔ| |j|j|j¡dS)QNrz --versionÚversionz trace 2.0)ÚactionrÕz Main optionsz(One of these (or --report) must be givenz-cz--countÚ store_truez»Count the number of times each line is executed and write the counts to .cover for each module executed, in the module's directory. See also --coverdir, --file, --no-report below.)rÖÚhelpz-tz--tracez3Print each line to sys.stdout before it is executedz-lz --listfuncsz§Keep track of which functions are executed at least once and write the results to sys.stdout after the program exits. Cannot be specified alongside --trace or --count.z-Tz --trackcallsz^Keep track of caller/called pairs and write the results to sys.stdout after the program exits.Z Modifiersz-rz--reportz¶Generate a report from a counts file; does not execute any code. --file must specify the results file to read, which must have been created in a previous run with --count --file=FILEz-Rz --no-reportz^Do not generate the coverage report files. Useful if you want to accumulate over several runs.z-fz--filez+File to accumulate counts over several runs)rØz-Cz --coverdirz†Directory where the report files go. The coverage report for . will be written to file //.coverz-mz --missingz?Annotate executable lines that were not executed with ">>>>>> "z-sz --summaryz\Write a brief summary for each file to sys.stdout. Can only be used with --count or --reportz-gz--timingzQPrefix each line with the time since the program started. Only used while tracingZFilterszCan be specified multiple timesz--ignore-moduleÚappendzqIgnore the given module(s) and its submodules (if it is a package). Accepts comma separated list of module names.)rÖÚdefaultrØz --ignore-dirzWIgnore files in the given directory (multiple directories can be joined by os.pathsep).z--moduleFzTrace a module. Úprognameú?zfile to run as main program)ÚnargsrØÚ argumentszarguments to the programZstdlibZ platstdlibcs4tj tj |¡¡}| dˆ¡ dˆ¡}tj |¡S)Nz$prefixz $exec_prefix)rrÚ expanduserÚ expandvarsr,r )Ús)Ú _exec_prefixÚ_prefixrr Úparse_ignore_dir­szmain..parse_ignore_dircSs$g|]}| d¡D] }| ¡‘qqS)ú,)ÚsplitÚstrip)r rŠrrrr r²s ÿzmain..cs&g|]}| tj¡D] }ˆ|ƒ‘qqSr)rærÚpathsep)r rŠrá)rärr r´sÿz-r/--report requires -f/--file)r8r9zLmust specify one of --trace, --count, --report, --listfuncs, or --trackcallsz8cannot specify both --listfuncs and (--trace or --count)z3--summary can only be used with --count or --reportz3progname is missing: required with the main options)r r¡r¢r£r8r9r¤r¥)rrÊÚ __package__Ú __loader__Ú__spec__Ú __cached__rŒ)rÊrrérìzCannot run file %r because: %s)1ÚargparseÚArgumentParserÚ add_argumentZadd_argument_groupZadd_mutually_exclusive_groupZ REMAINDERÚ parse_argsZ ignore_dirÚ sysconfigZget_pathZ ignore_moduleZreportr3ÚerrorrroZmissingr`raÚanyr’rfZ listfuncsZ trackcallsrÛrr¤ÚmoduleÚrunpyZ_get_module_detailsr¿rÞr)ÚargvÚparentÚloaderrrrRÚioÚ open_coderŽr�r§r?ÚexitÚ SystemExitrÓZ no_report)ríÚparserZgrpZ_grpZoptsrÓÚtrõZ module_nameZmod_nameZmod_specrzZglobsrirEr)rârãrär Úmainbsòÿ ÿ ÿ ÿ ÿ  ÿ ÿÿÿ ÿ ÿ ÿÿ ÿ ÿ ÿÿ ÿ  ÿ ÿ      ýú ü(rÿr¥)N)Ú__doc__Ú__all__rùrWrr)rñr‚rZrrÁryr;Útimerr—r«rurr&r0rr}r€r‹rVrrÿrrrrr Ús< 20  _