a
    ´ir  ã                   @   sî   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 )Ú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/lib64/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 ©N)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 )NÚ r   r   )r   r   ÚnormcaseÚsysr   Úlenr   Ú
splitdriveÚreplaceÚaltsepr$   Úlstrip)r   ZcomparepathZlongestÚdirr%   Údriver   r&   r   r   r   Ú_fullmodname~   s     
r2   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   r6   r8   r:   r9   r;   ÚfÚerrr   r   r   r   ›   s2    


.ÿÿzCoverageResults.__init__c                 C   s   |  d¡o| d¡S )Nú<ú>)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 ©Nr   r   )r6   r8   r9   Úget)	r   Úotherr6   r8   r9   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 )Nzfunctions called:z*filename: %s, modulename: %s, funcname: %szcalling relationships:r(   z***z  -->z    %s.%s -> %s.%sz.pycéÿÿÿÿz.coverr3   éd   zlines   cov%   module   (path)z%5d   %3d%%   %s   (%s)Úwbr   z"Can't save counts files because %sr4   )!r8   rD   Úsortedr9   r6   rM   ÚitemsrK   rJ   r   r   ÚdirnameÚabspathr'   ÚexistsÚmakedirsr2   Ú_find_executable_linenosÚ	linecacheÚgetlinesÚjoinr<   ÚtokenizeÚdetect_encodingÚreadlineÚwrite_results_fileÚintr;   r=   ÚdumprA   r*   rE   )"r   Zshow_missingÚsummaryÚcoverdirZcallsr   r   ÚfuncnameZlastfileZ	lastcfileZpfileZpmodZpfuncÚcfileZcmodZcfuncZper_fileÚlinenoÚ	lines_hitZsumsÚcountr0   ÚlnotabÚsourceZ	coverpathÚfpÚencodingÚ_Ún_hitsÚn_linesÚpercentÚmrF   rG   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 )NÚw©rm   z3trace: Could not open %r for writing: %s - skippingr4   )r   r   r   r   z%5d: z>>>>>> z       é   )	r<   rA   rD   r*   rE   Ú	enumerateÚwriteÚPRAGMA_NOCOVERÚ
expandtabs)r   r   Úlinesrj   rh   rm   r;   rG   rp   ro   rg   Úliner   r   r   r`   )  s.    ÿÿ



0z"CoverageResults.write_results_file)NNNNN)TFN)N)r   r    r!   r   rK   r?   rs   r`   r   r   r   r   r   š   s     ÿ

\c                 C   s,   i }t  | ¡D ]\}}||vrd||< q|S )Nr   )ÚdisZfindlinestarts)ÚcodeÚstrsÚlinenosrn   rg   r   r   r   Ú_find_lines_from_codeI  s
    
r�   c                 C   s4   t | |ƒ}| jD ]}t |¡r| t||ƒ¡ q|S r"   )r�   Ú	co_constsÚinspectZiscoder?   Ú_find_lines)r~   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 )Nru   r   )ÚtokenÚINDENTr<   r]   Úgenerate_tokensr_   ÚSTRINGÚrange)r   rm   r   Z
prev_ttyperF   ÚtokZttypeZtstrÚstartÚendr|   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 )Nz%Not printing coverage data for %r: %sr4   Úexec)r]   r<   Úreadrm   rA   rD   r*   rE   Úcompiler�   r„   )r   rF   Úprogrm   rG   r~   r   r   r   r   rY   v  s    (ÿÿ
rY   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 rL   )r:   r;   r   Úignorer6   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   ri   r–   Ú
countfuncsÚcountcallersÚ
ignoremodsÚ
ignoredirsr:   r;   Ú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 r"   )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   rF   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   Úframer~   r   r   re   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 ©NÚcallr   )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 rÂ   )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 )NrÃ   Ú__file__z! --- modulename: %s, funcname: %s)
r»   Ú	f_globalsrM   r'   r”   r   r–   rD   r½   r¡   )r   r¿   rÅ   rÆ   r~   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 )	Nr|   r   r   ú%.2fú ©r�   ú
%s(%d): %sr(   )r»   r¼   Úf_linenor6   rM   rš   rD   r›   r   r   r#   rZ   Úgetliner¡   )r   r¿   rÅ   rÆ   r   rg   rO   Ú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 )Nr|   rÊ   rË   rÌ   rÍ   r(   )r»   r¼   rÎ   rš   rD   r›   r   r   r#   rZ   rÏ   r¡   )r   r¿   rÅ   rÆ   r   rg   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 )Nr|   r   r   )r»   r¼   rÎ   r6   rM   r¡   )r   r¿   rÅ   rÆ   r   rg   rO   r   r   r   r£   D  s    zTrace.localtrace_countc                 C   s   t | j| j| j| j| jd�S )N)r:   r;   r8   r9   )r   r6   r:   r;   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)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 --reportz3progname is missing: required with the main options)r¤   r¥   r¦   r§   r:   r;   r¨   r©   )r   rÈ   Ú__package__Ú
__loader__Ú__spec__Ú
__cached__r�   )rÈ   r   ræ   ré   zCannot run file %r because: %s)1ÚargparseÚArgumentParserÚadd_argumentÚadd_argument_groupÚadd_mutually_exclusive_groupÚ	REMAINDERÚ
parse_argsZ
ignore_dirÚ	sysconfigZget_pathZignore_moduleÚreportr5   Úerrorr   rs   Úmissingrc   rd   Úanyr–   ri   Z	listfuncsZ
trackcallsrØ   r   r¨   ÚmoduleÚrunpyÚ_get_module_detailsr¼   rÛ   r*   ÚargvÚparentÚloaderr   r   rU   ÚioÚ	open_coder’   r‘   r«   rA   ÚexitÚ
SystemExitrÑ   Z	no_report)rê   ÚparserÚgrpZ_grpZoptsrÑ   Útr÷   Úmodule_nameÚmod_nameÚmod_specr~   Zglobsrl   rG   r   )rß   rà   rá   r   ÚmainR  sì    ÿ
ÿ
ÿ
ÿ
ÿ

ÿ
ÿÿÿ
ÿ
ÿ
ÿÿ
ÿ
ÿ
ÿÿ
ÿ

ÿ
ÿ




ýú	2ü,r  r©   )N)Ú__all__rü   rZ   r   r*   rñ   r†   r]   rƒ   r¾   r}   r=   Útimer   r›   r¯   ry   r   r'   r2   r   r�   r„   r�   rY   r   r  r   r   r   r   r   Ú<module>2   s:   2 0

 O 