a
    ze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re  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/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 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 s0    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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 u rpt	j
t	j
|}t|}n$|}t	j
|st	| t|}|rt|}ni }t|}t	j
||d
 }t|d }t|j\}}W d    n1 s0    Y  | |||||\}}|r|rtd| | }||||f||< q|r|rtd t|D ]&}|| \}}}}td||   qb| jrzLt| jd*} t| j| j | jf| d W d    n1 s0    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.pycz.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 rt|vr|d	 |d7 }n
|d
 ||d qhW d    n1 s0    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 s0    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| }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 urd||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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 u rr|dJ t|j|j|j|j|j
|j|j|j|jdK	}z|jrd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_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)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runpyZ_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_nameZmod_nameZ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 