a
    yŒ@hr  ã                   @   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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rîeƒ  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   úlib/python3.9/trace.pyÚ
<listcomp>H   s   ÿz$_Ignore.__init__.<locals>.<listcomp>z<string>é   )ÚsetÚ_modsÚ_dirsÚ_ignore)ÚselfÚmodulesÚdirsr   r   r   Ú__init__F   s
    ÿz_Ignore.__init__c                 C   s¦   || j v r| j | S || jv r,d| j |< dS | jD ]"}| |d ¡r2d| j |<  dS q2|d u 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   ÚnamesL   s$    









z_Ignore.names)NN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   E   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   Ú_modnamew   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 u ri | _ | j  ¡ | _|| _| jd u r8i | _| j ¡ | _|| _| jd u rZi | _| j ¡ | _|| _|| _| j�rzTt| jdƒ� }t 	|¡\}}}W d   ƒ n1 s®0    Y  |  
|  |||¡¡ W nD tttf�y } z$td| j|f tjd� W Y d }~n
d }~0 0 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rÐtƒ  tdƒ d }}	t| jƒD ]h\\}
}}\}}}|
|krœtƒ  td|
dƒ |
}d}	||
krº|	|krºtd|ƒ |}	td||||f ƒ qfi }| jD ].\}}| |i ¡ }||< | j||f ||< qÚi }| ¡ D �],\}}|  |¡�r0�q| d¡�rH|d	d
… }|d	u �rpt	j
 t	j
 |¡¡}t|ƒ}n$|}t	j
 |¡�sŒt	 |¡ t|ƒ}|�r¤t|ƒ}ni }t |¡}t	j
 ||d ¡}t|dƒ� }t |j¡\}}W d	  ƒ n1 �sö0    Y  |  |||||¡\}}|�r|�rtd| | ƒ}||||f||< �q|�rŠ|�rŠtdƒ t|ƒD ]&}|| \}}}}td||  ƒ �qb| j�rzLt| jdƒ�*} t | j| j | jf| d¡ W d	  ƒ n1 �sÒ0    Y  W n8 t�y }! ztd|! tj d� W Y d	}!~!n
d	}!~!0 0 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.pycNéÿÿÿÿz.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„    
ÿÿ





0ÿÿ(zCoverageResults.write_resultsc              
   C   s  zt |d|d�}W n< tyN } z$td||f tjd� W Y d}~dS d}~0 0 d}d}	|�Ž t|dƒD ]r\}
}|
|v rœ| d	||
  ¡ |	d7 }	|d7 }n.|
|v rÀt|vrÀ| d
¡ |d7 }n
| d¡ | | d¡¡ qhW d  ƒ n1 sð0    Y  |	|fS )z'Return a coverage results file in path.Úw©rj   z3trace: Could not open %r for writing: %s - skippingr2   N)r   r   r   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.    ÿÿ



0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 ]\}}||v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_codeI  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€   S  s
    


r€   c                 C   sž   i }t j}t| |d��t}t |j¡}|D ]R\}}}}	}
|t jkrv|t jkrv|\}}|	\}}t||d ƒD ]}d||< qh|}q(W d  ƒ n1 s�0    Y  |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.
    rq   r   N)Ú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 d  ƒ n1 s00    Y  W n> tyz } z&td| |f tjd� i W  Y d}~S d}~0 0 t|| dƒ}t	| |ƒ}t
||ƒS )zAReturn dict where keys are line numbers in the line number table.Nz%Not printing coverage data for %r: %sr2   Úexec)rZ   r:   Úreadrj   r?   rB   r)   rC   Úcompiler‹   r€   )r   rD   Úprogrj   rE   rz   r{   r   r   r   rV   v  s    (ÿÿ
rV   c                	   @   sp   e Zd Zddd„Zdd	„ Zd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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   s‚   |d u ri }|d u ri }| j s6t | j¡ t | j¡ z*t|||ƒ W | j s~t d ¡ t d ¡ n| j s|t d ¡ t d ¡ 0 d S ©N)r‘   Ú	threadingÚsettracer™   r)   rŒ   )r   r¨   ÚglobalsÚlocalsr   r   r   r§   ¼  s    
þ
zTrace.runctxc             	   O   sP   d }| j st | j¡ z"||i |¤Ž}W | j sLt d ¡ n| j sJt d ¡ 0 |S r«   )r‘   r)   r­   r™   )r   ÚfuncÚargsÚkwÚresultr   r   r   ÚrunfuncÉ  s    ÿzTrace.runfuncc           
      C   sð   |j }|j}|rt|ƒ}nd }|j}d }|| jv rL| j| d urÒ| j| }n†d | j|< dd„ t |¡D ƒ}t|ƒdkrÒdd„ t |d ¡D ƒ}t|ƒdkrÒdd„ t |d ¡D ƒ}	t|	ƒdkrÒ|	d j}|| j|< |d uræd||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urf| j ||¡}|sf| jrZtd||jf ƒ | j	S ndS dS )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_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œ   (  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ž   7  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Ÿ   D  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   ÚresultsL  s
    
ýzTrace.results)	r   r   r   r   r   r   NNF)NN)r   r    r!   r   rª   r§   r´   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�rˆt 	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 u �rr| dJ¡ t|j|j|j|j|j
|j|j|j|jdK�	}zÖ|j�rðdd l}|j}| |¡\}	}
}|jg|j¢t _!dL|j|
j"|
j#|
d dMœ}nt|jg|j¢t _!t$j% &|j¡t j%d< t' (|j¡�"}t)| *¡ |jdNƒ}W d   ƒ n1 �sJ0    Y  |jdLd d dOœ}| +|||¡ W nP t,�y² } z$t  -dPt j!d |f ¡ W Y d }~nd }~0  t.�yÄ   Y n0 | /¡ }|j0�sê| |j|j|j¡ d S )QNr   z	--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 <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 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__rŒ   )rÄ   r   râ   rå   zCannot run file %r because: %s)1Ú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ÚrunpyÚ_get_module_detailsr¹   r×   r)   ÚargvÚparentÚloaderr   r   rR   ÚioÚ	open_coderŽ   r�   r§   r?   ÚexitÚ
SystemExitrÍ   Z	no_report)ræ   ÚparserZgrpZ_grpZoptsrÍ   Útrî   Zmodule_nameÚmod_nameÚmod_specrz   Zglobsri   rE   r   )rÛ   rÜ   rÝ   r   ÚmainR  sì    ÿ
ÿ
ÿ
ÿ
ÿ

ÿ
ÿÿÿ
ÿ
ÿ
ÿÿ
ÿ
ÿ
ÿÿ
ÿ

ÿ
ÿ




ýú	2ü,rû   r¥   )N)Ú__doc__Ú__all__ró   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

 O 