U
    pdt                     @   s   d Z ddgZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlmZ ddlZdZG dd dZd	d
 Zdd ZG dd dZdd Zdd ZdddZdd ZG dd dZdd Zedkre  dS )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                   @   s   e Zd ZdddZdd ZdS )_IgnoreNc                 C   s:   |s
t  nt || _|sg ndd |D | _ddi| _d S )Nc                 S   s   g | ]}t j|qS  )ospathnormpath.0dr   r   /usr/lib/python3.8/trace.py
<listcomp>G   s   z$_Ignore.__init__.<locals>.<listcomp>z<string>   )set_mods_dirs_ignore)selfmodulesdirsr   r   r   __init__E   s
    z_Ignore.__init__c                 C   s   || j kr| j | S || jkr,d| j |< dS | jD ]"}||d r2d| j |<  dS q2|d krld| j |< dS | jD ]$}||tj rrd| j |<  dS qrd| j |< dS )Nr   .r   )r   r   
startswithr   r   sep)r   filename
modulenamemodr   r   r   r   namesK   s$    









z_Ignore.names)NN)__name__
__module____qualname__r   r   r   r   r   r   r   D   s   
r   c                 C   s    t j| }t j|\}}|S )z-Return a plausible module name for the patch.)r   r   basenamesplitext)r   baser   extr   r   r   _modnamev   s    r&   c                 C   s   t j| }d}tjD ]@}t j|}||r|t| t jkrt|t|kr|}q|rr| t|d d }n| }t j|\}}|t jd}t j	r|t j	d}t j
|\}}|dS )z,Return a plausible module name for the path. r   Nr   )r   r   normcasesysr   lenr   
splitdrivereplacealtsepr#   lstrip)r   ZcomparepathZlongestdirr$   Zdriver   r%   r   r   r   _fullmodname}   s     
r0   c                   @   s:   e Zd ZdddZdd Zdd Zdd
dZdddZdS )r   Nc              
   C   s   || _ | j d kri | _ | j  | _|| _| jd kr8i | _| j | _|| _| jd krZi | _| j | _|| _|| _| jrz@t| jd}t	|\}}}W 5 Q R X | 
| ||| W n@ tttfk
r } ztd| j|f tjd W 5 d }~X Y nX d S )NrbzSkipping counts file %r: %sfile)countscopyZcountercalledfuncscallersinfileoutfileopenpickleloadupdate	__class__OSErrorEOFError
ValueErrorprintr)   stderr)r   r4   r6   r8   r7   r9   ferrr   r   r   r      s2    


zCoverageResults.__init__c                 C   s   | do|dS )z_Return True if the filename does not refer to a file
        we want to have reported.
        <>)r   endswith)r   r   r   r   r   is_ignored_filename   s    z#CoverageResults.is_ignored_filenamec           	      C   sn   | j }| j}| j}|j }|j}|j}|D ]}||d||  ||< q(|D ]}d||< qJ|D ]}d||< q\dS )z.Merge in the data from another CoverageResultsr   r   N)r4   r6   r7   get)	r   otherr4   r6   r7   Zother_countsZother_calledfuncsZother_callerskeyr   r   r   r=      s    
zCoverageResults.updateTFc           "   
   C   s  | j r@t  td | j }t|D ]\}}}td|||f  q"| jrt  td d }}	t| jD ]h\\}
}}\}}}|
|krt  td|
d |
}d}	||
kr|	|krtd| |}	td||||f  qfi }| jD ].\}}||i  }||< | j||f ||< qi }| D ]\}}| |r0q|drH|d	d
 }|d	krpt	j
t	j
|}t|}n$|}t	j
|st	| t|}|rt|}ni }t|}t	j
||d }t|d}t|j\}}W 5 Q R X | |||||\}}|r|rtd| | }||||f||< q|rt|rttd t|D ]&}|| \}}}}td||   qL| jrz6t| jd } t| j| j | jf| d W 5 Q R X W n6 tk
r }! ztd|! tj d W 5 d	}!~!X Y nX d	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.
        zfunctions called:z*filename: %s, modulename: %s, funcname: %szcalling relationships:r'   z***z  -->z    %s.%s -> %s.%sz.pycNz.coverr1   d   zlines   cov%   module   (path)z%5d   %3d%%   %s   (%s)wbr   z"Can't save counts files because %sr2   )!r6   rB   sortedr7   r4   rJ   itemsrI   rH   r   r   dirnameabspathr&   existsmakedirsr0   _find_executable_linenos	linecachegetlinesjoinr:   tokenizedetect_encodingreadlinewrite_results_fileintr9   r;   dumpr?   r)   rC   )"r   Zshow_missingsummarycoverdirZcallsr   r   funcnameZlastfileZ	lastcfileZpfileZpmodZpfunccfileZcmodZcfuncZper_filelineno	lines_hitZsumscountr/   lnotabsourceZ	coverpathfpencoding_n_hitsn_linesZpercentmrD   rE   r   r   r   write_results   s    






   zCoverageResults.write_resultsc              
   C   s   zt |d|d}W n> tk
rP } z td||f tjd W Y dS d}~X Y nX d}d}	| t|dD ]r\}
}|
|kr|d	||
   |	d7 }	|d7 }n.|
|krt|kr|d
 |d7 }n
|d ||d qjW 5 Q R X |	|fS )z'Return a coverage results file in path.wrj   z3trace: Could not open %r for writing: %s - skippingr2   )r   r   Nr   r   z%5d: z>>>>>> z          )	r:   r?   rB   r)   rC   	enumeratewritePRAGMA_NOCOVER
expandtabs)r   r   linesrg   re   rj   r9   rE   rm   rl   rd   liner   r   r   r]   (  s.    



z"CoverageResults.write_results_file)NNNNN)TFN)N)r   r    r!   r   rI   r=   ro   r]   r   r   r   r   r      s       

\c                 C   s,   i }t | D ]\}}||krd||< q|S )z:Return dict where keys are lines in the line number table.r   )disZfindlinestarts)codestrslinenosrk   rd   r   r   r   _find_lines_from_codeH  s
    
r}   c                 C   s4   t | |}| jD ]}t|r|t|| q|S )z<Return lineno dict for all code objects reachable from code.)r}   	co_constsinspectZiscoder=   _find_lines)rz   r{   r|   cr   r   r   r   R  s
    


r   c              	   C   s   i }t j}t| |dj}t|j}|D ]R\}}}}	}
|t jkrv|t jkrv|\}}|	\}}t||d D ]}d||< qh|}q(W 5 Q R X |S )zReturn 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.
    rq   r   )tokenINDENTr:   rZ   generate_tokensr\   STRINGrange)r   rj   r   Z
prev_ttyperD   tokZttypeZtstrstartendrx   ZslineZscolZelineZecolir   r   r   _find_strings^  s    


r   c              
   C   s   z(t | }| }|j}W 5 Q R X W n@ tk
rh } z"td| |f tjd i  W Y S d}~X Y nX t|| d}t	| |}t
||S )zAReturn dict where keys are line numbers in the line number table.z%Not printing coverage data for %r: %sr2   Nexec)rZ   r:   readrj   r?   rB   r)   rC   compiler   r   )r   rD   progrj   rE   rz   r{   r   r   r   rV   u  s    
rV   c                	   @   sv   e Zd ZdddZdd	 Zd d
dZdd Zde_dd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )!r   r   r   r   NFc
           
      C   s   || _ || _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| _nd| _dS )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
        r   Nr   )r8   r9   r   ignorer4   Zpathtobasename	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)
r   rf   r   
countfuncscountcallers
ignoremods
ignoredirsr8   r9   timingr   r   r   r     s6    




zTrace.__init__c                 C   s    dd l }|j}| ||| d S )Nr   )__main____dict__runctx)r   cmdr   dictr   r   r   run  s    z	Trace.runc              	   C   sh   |d kri }|d kri }| j s6t| j t| j zt||| W 5 | j sbtd  td  X d S )N)r   	threadingsettracer   r)   r   )r   r   globalslocalsr   r   r   r     s      
zTrace.runctxc               	   O   s   t | dkr| ^}}} nV| s&tdnHd|krZ|d}| ^}} dd l}|jdtdd ntdt | d  d }|jst|j	 z|| |}W 5 |jstd  X |S )	N   z8descriptor 'runfunc' of 'Trace' object needs an argumentfuncr   z0Passing 'func' as keyword argument is deprecated)
stacklevelz7runfunc expected at least 1 positional argument, got %dr   )
r*   	TypeErrorpopwarningswarnDeprecationWarningr   r)   r   r   )argskwr   r   r   resultr   r   r   runfunc  s.    

 
zTrace.runfuncz($self, func, /, *args, **kw)c           
      C   s   |j }|j}|rt|}nd }|j}d }|| jkrL| j| d k	r| j| }nd | j|< dd t|D }t|dkrdd t|d D }t|dkrdd t|d D }	t|	dkr|	d j}|| j|< |d k	rd||f }|||fS )Nc                 S   s   g | ]}t |r|qS r   )r   Z
isfunction)r   rD   r   r   r   r     s    
z1Trace.file_module_function_of.<locals>.<listcomp>r   c                 S   s   g | ]}t |tr|qS r   )
isinstancer   r
   r   r   r   r     s    
r   c                 S   s   g | ]}t |d r|qS )	__bases__)hasattr)r   r   r   r   r   r     s    
z%s.%s)	f_codeco_filenamer&   co_namer   gcZget_referrersr*   r   )
r   framerz   r   r   rb   ZclsnameZfuncsZdictsclassesr   r   r   file_module_function_of  s,    




zTrace.file_module_function_ofc                 C   s0   |dkr,|  |}|  |j}d| j||f< dS )zkHandler for call events.

        Adds information about who called who to the self._callers dict.
        callr   N)r   f_backr   )r   r   whyarg	this_funcZparent_funcr   r   r   r     s    
zTrace.globaltrace_trackcallersc                 C   s    |dkr|  |}d| j|< dS )zoHandler for call events.

        Adds (filename, modulename, funcname) to the self._calledfuncs dict.
        r   r   N)r   r   )r   r   r   r   r   r   r   r   r     s    
zTrace.globaltrace_countfuncsc                 C   sj   |dkrf|j }|jdd}|rbt|}|dk	rf| j||}|sf| jrZtd||jf  | j	S ndS dS )zHandler 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_globalsrJ   r&   r   r   r   rB   r   r   )r   r   r   r   rz   r   r   Z	ignore_itr   r   r   r      s    zTrace.globaltrace_ltc                 C   s   |dkr~|j j}|j}||f}| j|dd | j|< | jrTtdt | j  dd tj	
|}td||t||f dd | jS )	Nrx   r   r   %.2f r   
%s(%d): %sr'   )r   r   f_linenor4   rJ   r   rB   r   r   r   r"   rW   getliner   )r   r   r   r   r   rd   rL   bnamer   r   r   r   7  s    
z Trace.localtrace_trace_and_countc                 C   sd   |dkr^|j j}|j}| jr4tdt | j  dd tj|}td||t	
||f dd | jS )Nrx   r   r   r   r   r'   )r   r   r   r   rB   r   r   r   r"   rW   r   r   )r   r   r   r   r   rd   r   r   r   r   r   F  s    
zTrace.localtrace_tracec                 C   s<   |dkr6|j j}|j}||f}| j|dd | j|< | jS )Nrx   r   r   )r   r   r   r4   rJ   r   )r   r   r   r   r   rd   rL   r   r   r   r   S  s    zTrace.localtrace_countc                 C   s   t | j| j| j| j| jdS )N)r8   r9   r6   r7   )r   r4   r8   r9   r   r   )r   r   r   r   results[  s
    
zTrace.results)	r   r   r   r   r   r   NNF)NN)r   r    r!   r   r   r   r   __text_signature__r   r   r   r   r   r   r   r   r   r   r   r   r     s&            
2
)	c                     s  dd l } |  }|jdddd |dd}|jdd	d
dd |jddd
dd |jddd
dd |jddd
dd |d}| }|jddd
dd |jddd
dd |jdddd  |jd!d"d#d  |jd$d%d
d&d |jd'd(d
d)d |jd*d+d
d,d |d-d.}|jd/d0g d1d2 |jd3d0g d4d2 |jd5d
d6d7d2 |jd8d9d:d; |jd<| jd=d; | }|jrt	d>t	d?  fd@dAdBdC |j
D |_
fdDdC|jD |_|jr|js|dE t|j|jdF}||j|j|jS t|j|j|j|jgs |dG |jrB|js8|jrB|dH |jr\|js\|dI |jd krr|dJ t|j|j|j|j|j
|j|j|j|jdK	}z|jrdd l}|j}||\}	}
}|jf|jt _!dL|j|
j"|
j#|
d dM}n^|jf|jt _!t$j%&|jt j%d< t'|jdN}t(|) |jdO}W 5 Q R X |jdLd d dP}|*||| W nP t+k
r } zt ,dQt j!d |f  W 5 d }~X Y n t-k
r   Y nX |. }|j/s||j|j|j d S )RNr   z	--versionversionz	trace 2.0)actionr   zMain optionsz(One of these (or --report) must be givenz-cz--count
store_truezCount 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--listfuncszKeep 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--reportzGenerate 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
--coverdirzDirectory 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 tracingZFilterszCan 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--moduleFzTrace a module. progname?zfile to run as main program)nargsr   	argumentszarguments to the programZstdlibZ
platstdlibc                    s4   t jt j| } | dd } t j| S )Nz$prefixz$exec_prefix)r   r   
expanduser
expandvarsr,   r	   )s)_exec_prefix_prefixr   r   parse_ignore_dir  s    zmain.<locals>.parse_ignore_dirc                 S   s$   g | ]}| d D ]}| qqS ),)splitstrip)r   r   r   r   r   r   r     s     zmain.<locals>.<listcomp>c                    s&   g | ]}| tjD ]} |qqS r   )r   r   pathsep)r   r   r   )r   r   r   r     s     z-r/--report requires -f/--file)r8   r9   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 --reportz3progname is missing: required with the main options)r   r   r   r   r8   r9   r   r   )r   r   __package__
__loader____spec__
__cached__r1   r   )r   r   r   r   zCannot run file %r because: %s)0argparseArgumentParseradd_argumentZadd_argument_groupZadd_mutually_exclusive_groupZ	REMAINDER
parse_argsZ
ignore_dir	sysconfigZget_pathZignore_moduleZreportr3   errorr   ro   Zmissingr`   ra   anyr   rf   Z	listfuncsZ
trackcallsr   r   r   modulerunpyZ_get_module_detailsr   r   r)   argvparentloaderr   r   rR   r:   r   r   r   r?   exit
SystemExitr   Z	no_report)r   parserZgrpZ_grpZoptsr   tr   Zmodule_nameZmod_nameZmod_specrz   Zglobsri   rE   r   )r   r   r   r   maina  s    





















   	(r   r   )N)__doc____all__rW   r   r)   r   r   rZ   r   r   ry   r;   timer   r   r   ru   r   r&   r0   r   r}   r   r   rV   r   r   r   r   r   r   r   <module>   s:   2 0

 _ 