????
Your IP : 3.144.13.230
3
\=p�@s,dZddgZddlZddlZddlZddlZddlZddlZddlZddl Z ddl
Z
ddlZddlZddl
mZyddlZWn"ek
r�ejZdd�ZYnXdd �Zd
d�ZdZGdd
�d
�Zdd�Zdd�ZGdd�d�Zdd�Zdd�Zddd�Zdd�ZGdd�d�Zdd�Ze dk�r(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)� monotoniccCstjd�dS)N)�sys�settrace�rr�/usr/lib64/python3.6/trace.py�_unsettraceEsr cCstj|�tj|�dS)N)� threadingrr)�funcrrr� _settraceHs
rcCstjd�tjd�dS)N)rrr
rrrrr Ls
z#pragma NO COVERc@seZdZddd�Zdd�ZdS)�_IgnoreNcCs:|s
t�nt|�|_|sgndd�|D�|_ddi|_dS)NcSsg|]}tjj|��qSr)�os�path�normpath)�.0�drrr�
<listcomp>Usz$_Ignore.__init__.<locals>.<listcomp>z<string>�)�set�_mods�_dirs�_ignore)�self�modules�dirsrrr�__init__Ss
z_Ignore.__init__cCs�||jkr|j|S||jkr,d|j|<dSx*|jD] }|j|d�r4d|j|<dSq4W|dkrnd|j|<dSx,|jD]"}|j|tj�rvd|j|<dSqvWd|j|<dS)Nr�.r)rr�
startswithrr�sep)r�filename�
modulename�modrrrr�namesYs$
z
_Ignore.names)NN)�__name__�
__module__�__qualname__rr#rrrrr
Rs
r
cCs tjj|�}tjj|�\}}|S)z-Return a plausible module name for the patch.)rr�basename�splitext)r�baser �extrrr�_modname�sr+cCs�tjj|�}d}xJtjD]@}tjj|�}|j|�r|t|�tjkrt|�t|�kr|}qW|rv|t|�dd�}n|}tjj|�\}}|jtjd�}tj r�|jtj d�}tjj
|�\}}|jd�S)z,Return a plausible module name for the path.�rNr)rr�normcaserr�lenr�
splitdrive�replace�altsepr(�lstrip)rZcomparepathZlongest�dirr)Zdriver r*rrr�_fullmodname�s r4c@s:eZdZddd�Zdd�Zdd�Zdd
d�Zddd
�ZdS)rNcCs�||_|jdkri|_|jj�|_||_|jdkr8i|_|jj�|_||_|jdkrZi|_|jj�|_||_||_|jr�y@t|jd��}tj |�\}}}WdQRX|j
|j|||��Wn@tt
tfk
r�}ztd|j|ftjd�WYdd}~XnXdS)N�rbzSkipping counts file %r: %s)�file)�counts�copyZcounter�calledfuncs�callers�infile�outfile�open�pickle�load�update� __class__�OSError�EOFError�
ValueError�printr�stderr)rr7r9r;r:r<�f�errrrrr�s,
zCoverageResults.__init__cCs|jd�o|jd�S)z_Return True if the filename does not refer to a file
we want to have reported.
�<�>)r�endswith)rr rrr�is_ignored_filename�sz#CoverageResults.is_ignored_filenamec Csz|j}|j}|j}|j}|j}|j}x$|D]}|j|d�||||<q*Wx|D]}d||<qPWx|D]}d||<qfWdS)z.Merge in the data from another CoverageResultsrrN)r7r9r:�get) r�otherr7r9r:Zother_countsZother_calledfuncsZ
other_callers�keyrrrr@�s
zCoverageResults.updateTFc!Cs�|jrDt�td�|j}x(t|�D]\}}}td|||f�q$W|jr�t�td�d}} xvt|j�D]h\\}
}}\}
}}|
|kr�t�td|
d�|
}d} |
|
kr�| |
kr�td|
�|
} td||||f�qlWi}x8|jD].\}}|j|i�}||<|j||f||<q�Wi}�x$|j�D�]\}}|j|��r@�q&|jd��rX|d d�}|d k�r�t j
jt j
j|��}t
|�}n$|}t j
j|��s�t j|�t|�}|�r�t|�}ni}tj|�}t j
j||d�}t|d��}tj|j�\}}Wd QRX|j|||||�\}}|�r&|�r&td
||�}||||f||<�q&W|�r�|�r�td�x2t|�D]&}||\}}}}td||��q`W|j�r�y&tj|j|j|jft|jd�d
�Wn6tk
�r�} ztd| tj d�WYd d } ~ XnXd 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.pycNrz.coverr5�dzlines cov% module (path)z%5d %3d%% %s (%s)�wbz"Can't save counts files because %s)r6���)!r9rE�sortedr:r7rM�itemsrLrKrr�dirname�abspathr+�exists�makedirsr4�_find_executable_linenos� linecache�getlines�joinr=�tokenize�detect_encoding�readline�write_results_file�intr<r>�dumprBrrF)!rZshow_missing�summary�coverdirZcallsr r!�funcnameZlastfileZ lastcfileZpfileZpmodZpfunc�cfileZcmodZcfuncZper_file�lineno� lines_hitZsums�countr3�lnotab�sourceZ coverpath�fp�encoding�_�n_hits�n_linesZpercent�mrHrrr�
write_results�s|
zCoverageResults.write_resultscCs�yt|d|d�}Wn6tk
rH}ztd||ftjd�dSd}~XnXd}d} |��x�t|d�D]r\}
}|
|kr�|jd||
�| d7} |d7}n.|
|kr�t|kr�|jd �|d7}n
|jd
�|j|jd��qdWWdQRX| |fS)
z'Return a coverage results file in path.�w)rmz3trace: Could not open %r for writing: %s - skipping)r6rNrz%5d: z>>>>>> z �)rr) r=rBrErrF� enumerate�write�PRAGMA_NOCOVER�
expandtabs)rr�linesrjrhrmr<rHrprorg�linerrrr`5s(
z"CoverageResults.write_results_file)NNNNN)TFN)N)r$r%r&rrLr@rrr`rrrrr�s
[cCs0i}x&tj|�D]\}}||krd||<qW|S)z:Return dict where keys are lines in the line number table.r)�disZfindlinestarts)�code�strs�linenosrnrgrrr�_find_lines_from_codeUs
rcCs8t||�}x(|jD]}tj|�r|jt||��qW|S)z<Return lineno dict for all code objects reachable from code.)r� co_consts�inspectZiscoder@�_find_lines)r|r}r~�crrrr�_s
r�c
Cs�i}tj}t||d��r}tj|j�}x^|D]V\}}}} }
|tjkr||tjkr||\}}| \}
}xt||
d�D]}d||<qlW|}q*WWdQRX|S)z�Return a dict of possible docstring positions.
The dict maps line numbers to strings. There is an entry for
line that contains only a string or a part of a triple-quoted
string.
)rmrN)�token�INDENTr=r]�generate_tokensr_�STRING�range)r rmrZ
prev_ttyperG�tokZttypeZtstr�start�endrzZslineZscolZelineZecol�irrr�
_find_stringsks
r�cCs�y(tj|��}|j�}|j}WdQRXWn6tk
r^}ztd||ftjd�iSd}~XnXt||d�}t ||�}t
||�S)zAReturn dict where keys are line numbers in the line number table.Nz%Not printing coverage data for %r: %s)r6�exec)r]r=�readrmrBrErrF�compiler�r�)r rG�progrmrHr|r}rrrrY�s
rYc @s�eZdZddddffdddf dd�Zdd�Zdd d
�Zdd�Zd
d�Zdd�Zdd�Z dd�Z
dd�Zdd�Zdd�Z
dd�ZdS)rrrNFc
Cs�||_||_t||�|_i|_i|_d|_||_i|_i|_ i|_
d|_| rTt�|_|rb|j
|_nZ|rp|j|_nL|r�|r�|j|_|j|_n2|r�|j|_|j|_n|r�|j|_|j|_nd|_dS)ax
@param count true iff it should count number of times each
line is executed
@param trace true iff it should print out each line that is
being counted
@param countfuncs true iff it should just output a list of
(filename, modulename, funcname,) for functions
that were called at least once; This overrides
`count' and `trace'
@param ignoremods a list of the names of modules to ignore
@param ignoredirs a list of the names of directories to ignore
all of the (recursive) contents of
@param infile file from which to read stored counts to be
added into the results
@param outfile file in which to write the results
@param timing true iff timing information be displayed
rNr)r;r<r
�ignorer7Zpathtobasename� donothing�trace�_calledfuncs�_callers�
_caller_cache�
start_time�_time�globaltrace_trackcallers�globaltrace�globaltrace_countfuncs�globaltrace_lt�localtrace_trace_and_count�
localtrace�localtrace_trace�localtrace_count)
rrir��
countfuncs�countcallers�
ignoremods�
ignoredirsr;r<�timingrrrr�s6
zTrace.__init__cCs ddl}|j}|j|||�dS)Nr)�__main__�__dict__�runctx)r�cmdr��dictrrr�run�sz Trace.runc
CsL|dkri}|dkri}|js(t|j�zt|||�Wd|jsFt�XdS)N)r�rr�r�r )rr��globals�localsrrrr��s
zTrace.runctxcOs<d}|jstj|j�z|||�}Wd|js6tjd�X|S)N)r�rrr�)rr�args�kw�resultrrr�runfunc�sz
Trace.runfuncc
Cs�|j}|j}|rt|�}nd}|j}d}||jkrL|j|dk r�|j|}n�d|j|<dd�tj|�D�}t|�dkr�dd�tj|d�D�}t|�dkr�dd�tj|d�D�} t| �dkr�| dj}||j|<|dk r�d||f}|||fS)NcSsg|]}tj|�r|�qSr)r�Z
isfunction)rrGrrrr�sz1Trace.file_module_function_of.<locals>.<listcomp>rcSsg|]}t|t�r|�qSr)�
isinstancer�)rrrrrr�srcSsg|]}t|d�r|�qS)� __bases__)�hasattr)rr�rrrr�sz%s.%s) �f_code�co_filenamer+�co_namer��gcZ
get_referrersr.r$)
r�framer|r r!reZclsnameZfuncsZdicts�classesrrr�file_module_function_of�s,
zTrace.file_module_function_ofcCs0|dkr,|j|�}|j|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_funcZparent_funcrrrr�s
zTrace.globaltrace_trackcallerscCs |dkr|j|�}d|j|<dS)zoHandler for call events.
Adds (filename, modulename, funcname) to the self._calledfuncs dict.
r�rN)r�r�)rr�r�r�r�rrrr�s
zTrace.globaltrace_countfuncscCsj|dkrf|j}|jjdd�}|rbt|�}|dk rf|jj||�}|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_globalsrMr+r�r#r�rEr�r�)rr�r�r�r|r r!Z ignore_itrrrr�szTrace.globaltrace_ltcCs�|dkr~|jj}|j}||f}|jj|d�d|j|<|jrTtdt�|jdd�tj j
|�}td||tj||�fdd�|j
S) Nrzrrz%.2f� )r�z
%s(%d): %sr,)r�r��f_linenor7rMr�rEr�rrr'rZ�getliner�)rr�r�r�r rgrO�bnamerrrr�2sz Trace.localtrace_trace_and_countcCsd|dkr^|jj}|j}|jr4tdt�|jdd�tjj|�}td||t j
||�fdd�|jS)Nrzz%.2fr�)r�z
%s(%d): %sr,)r�r�r�r�rEr�rrr'rZr�r�)rr�r�r�r rgr�rrrr�AszTrace.localtrace_tracecCs<|dkr6|jj}|j}||f}|jj|d�d|j|<|jS)Nrzrr)r�r�r�r7rMr�)rr�r�r�r rgrOrrrr�NszTrace.localtrace_countcCst|j|j|j|j|jd�S)N)r;r<r9r:)rr7r;r<r�r�)rrrr�resultsVs
z
Trace.results)NN)r$r%r&rr�r�r�r�r�r�r�r�r�r�r�rrrrr�s0
)
cs�tj�}|jdddd�|jdd�}|jddd d
d�|jdd
d dd�|jddd dd�|jddd dd�|jd�}|j�}|jddd dd�|jddd dd�|jdddd�|jd d!d"d�|jd#d$d d%d�|jd&d'd d(d�|jd)d*d d+d�|jd,d-�}|jd.d/gd0d1�|jd2d/gd3d1�|jd4d5d6d7�|jd8tjd9d7�|j�}|j�r�d:d;jt j
�f}tjj
t jf|���tjj
t jf|�����fd<d=��d>d?�|jD�|_�fd@d?�|jD�|_|j�r|j�s�|jdA�t|j|jdB�}|j|j|j|j�St|j|j|j|jg��s*|jdC�|j�rL|j�sB|j�rL|jdD�|j�rh|j�rh|jdE�|jdk�r~|jdF�|jf|j�t _ tjj!|j�t jdG<t"|j|j|j|j|j|j|j|j|j#dH� }yJt$|j��}t%|j&�|jdI�}WdQRX|jdJdddK�} |j'|| | �WnPt(k
�rT}
zt j)dLt j dG|
f�WYdd}
~
Xnt*k
�rhYnX|j+�}|j,�s�|j|j|j|j�dS)MNz --version�versionz trace 2.0)�actionr�zMain 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 <module>.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 <package>.<module> will be written to file <dir>/<package>/<module>.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).r �?zfile to run as main program)�nargsr�� argumentszarguments to the program�libzpython{0.major}.{0.minor}cs4tjjtjj|��}|jd��jd��}tjj|�S)Nz$prefixz$exec_prefix)rr�
expanduser�
expandvarsr0r)�s)�_exec_prefix�_prefixrr�parse_ignore_dir�szmain.<locals>.parse_ignore_dircSs$g|]}|jd�D]}|j��qqS)�,)�split�strip)rr�r"rrrr�szmain.<locals>.<listcomp>cs&g|]}|jtj�D]}�|��qqSr)r�r�pathsep)rr�r�)r�rrr�sz-r/--report requires -f/--file)r;r<zLmust 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 --reportz3filename is missing: required with the main optionsr)r�r�r�r�r;r<r�r�r�)r�r$�__package__�
__cached__zCannot run file %r because: %s)-�argparse�ArgumentParser�add_argumentZadd_argument_groupZadd_mutually_exclusive_groupZ REMAINDER�
parse_argsZ
ignore_dir�formatr�version_inforrr\�base_prefix�base_exec_prefixZ
ignore_moduleZreportr6�errorrrrZmissingrcrd�anyr�riZ listfuncsZ
trackcallsr r��argvrUrr�r=r�r�r�rB�exit�
SystemExitr�Z no_report)�parserZgrpZ_grpZoptsZrel_pathr��trlr|ZglobsrHr)r�r�r�r�main\s�
(r�r�)N)!�__doc__�__all__r�rZr�rerr�r]r�r�r{r>Ztimerr�r
�ImportErrorrrr rwr
r+r4rrr�r�rYrr�r$rrrr�<module>1sH2/
M