U e5d�t�@s�dZddgZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl mZddlZdZGdd�d�Zd d �Zd d �ZGd d�d�Zdd�Zdd�Zddd�Zdd�ZGdd�d�Zdd�Zedkr�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)� monotonicz#pragma NO COVERc@seZdZddd�Zdd�ZdS)�_IgnoreNcCs:|s t�nt|�|_|sgn dd�|D�|_ddi|_dS)NcSsg|]}tj�|��qS�)�os�path�normpath��.0�drr�/usr/lib64/python3.8/trace.py� Hs�z$_Ignore.__init__..z�)�set�_mods�_dirs�_ignore)�self�modules�dirsrrr �__init__Fs �z_Ignore.__init__cCs�||jkr|j|S||jkr,d|j|<dS|jD]"}|�|d�r2d|j|<dSq2|dkrld|j|<dS|jD]$}|�|tj�rrd|j|<dSqrd|j|<dS)Nr�.r)rr� startswithrr�sep)r�filename� modulename�modr rrr �namesLs$          z _Ignore.names)NN)�__name__� __module__� __qualname__rrrrrr rEs rcCs tj�|�}tj�|�\}}|S)z-Return a plausible module name for the patch.)rr�basename�splitext)r�baser�extrrr �_modnamews r&cCs�tj�|�}d}tjD]@}tj�|�}|�|�r|t|�tjkrt|�t|�kr|}q|rr|t|�dd�}n|}tj�|�\}}|�tjd�}tj r�|�tj d�}tj� |�\}}|� d�S)z,Return a plausible module name for the path.�rNr) rr�normcase�sysr�lenr� splitdrive�replace�altsepr#�lstrip)rZ comparepathZlongest�dirr$Zdriverr%rrr � _fullmodname~s    r0c@s:eZdZddd�Zdd�Zdd�Zdd d �Zdd d �ZdS)rNc Cs�||_|jdkri|_|j��|_||_|jdkr8i|_|j��|_||_|jdkrZi|_|j��|_||_||_|jr�z@t|jd��}t� |�\}}}W5QRX|� |� |||��Wn@t t tfk r�}ztd|j|ftjd�W5d}~XYnXdS)N�rbzSkipping counts file %r: %s��file)�counts�copyZcounter� calledfuncs�callers�infile�outfile�open�pickle�load�update� __class__�OSError�EOFError� ValueError�printr)�stderr)rr4r6r8r7r9�f�errrrr r�s2      ��zCoverageResults.__init__cCs|�d�o|�d�S)z_Return True if the filename does not refer to a file we want to have reported. �<�>)r�endswith)rrrrr �is_ignored_filename�sz#CoverageResults.is_ignored_filenamec Csn|j}|j}|j}|j}|j}|j}|D]}|�|d�||||<q(|D] }d||<qJ|D] }d||<q\dS)z.Merge in the data from another CoverageResultsrrN)r4r6r7�get) r�otherr4r6r7Z other_countsZother_calledfuncsZ other_callers�keyrrr r=�s zCoverageResults.updateTFc" Cs�|jr@t�td�|j}t|�D]\}}}td|||f�q"|jr�t�td�d}} t|j�D]h\\} } } \} }}| |kr�t�td| d�| }d} | | kr�| | kr�td| �| } td| | ||f�qfi}|jD].\}}|�|i�}||<|j||f||<q�i}|��D�]\}}|�|��r0�q|�d��rH|d d �}|d k�rpt j � t j � |��}t |�}n$|}t j �|��s�t �|�t|�}|�r�t|�}ni}t�|�}t j �||d �}t|d ��}t�|j�\}}W5QRX|�|||||�\}}|�r|�rtd ||�}||||f||<�q|�rt|�rttd�t|�D]&}||\}}}}td||��qL|j�r�z6t|jd�� } t�|j|j|jf| d�W5QRXWn6tk �r�}!ztd|!tj d�W5d }!~!XYnXd 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.pycN�����z.coverr1�dzlines cov% module (path)z%5d %3d%% %s (%s)�wbrz"Can't save counts files because %sr2)!r6rB�sortedr7r4rJ�itemsrIrHrr�dirname�abspathr&�exists�makedirsr0�_find_executable_linenos� linecache�getlines�joinr:�tokenize�detect_encoding�readline�write_results_file�intr9r;�dumpr?r)rC)"rZ show_missing�summary�coverdirZcallsrr�funcnameZlastfileZ lastcfileZpfileZpmodZpfunc�cfileZcmodZcfuncZper_file�lineno� lines_hitZsums�countr/�lnotab�sourceZ coverpath�fp�encoding�_�n_hits�n_linesZpercent�mrDrErrr � write_results�s� ��           �   �zCoverageResults.write_resultsc Cs�zt|d|d�}Wn>tk rP}z td||ftjd�WY�dSd}~XYnXd}d} |��t|d�D]r\} } | |kr�|�d || �| d7} |d7}n.| |kr�t| kr�|�d �|d7}n |�d �|�| �d ��qjW5QRX| |fS) z'Return a coverage results file in path.�w�rjz3trace: Could not open %r for writing: %s - skippingr2)rrNrrz%5d: z>>>>>> z �) r:r?rBr)rC� enumerate�write�PRAGMA_NOCOVER� expandtabs) rr�linesrgrerjr9rErmrlrd�linerrr r])s.��    z"CoverageResults.write_results_file)NNNNN)TFN)N)rr r!rrIr=ror]rrrr r�s�  \cCs,i}t�|�D]\}}||krd||<q|S)z:Return dict where keys are lines in the line number table.r)�disZfindlinestarts)�code�strs�linenosrkrdrrr �_find_lines_from_codeIs  r}cCs4t||�}|jD]}t�|�r|�t||��q|S)z.rcSsg|]}t|t�r|�qSr)� isinstancer�r rrr r�s �rcSsg|]}t|d�r|�qS)� __bases__)�hasattr)r r�rrr r�s �z%s.%s) �f_code� co_filenamer&�co_namer��gcZ get_referrersr*r) r�framerzrrrbZclsnameZfuncsZdicts�classesrrr �file_module_function_of�s,          zTrace.file_module_function_ofcCs0|dkr,|�|�}|�|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_funcZ parent_funcrrr r� s  zTrace.globaltrace_trackcallerscCs |dkr|�|�}d|j|<dS)zoHandler for call events. Adds (filename, modulename, funcname) to the self._calledfuncs dict. r�rN)r�r�)rr�r�r�r�rrr r�s zTrace.globaltrace_countfuncscCsj|dkrf|j}|j�dd�}|rbt|�}|dk rf|j�||�}|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_globalsrJr&r�rr�rBr�r�)rr�r�r�rzrrZ ignore_itrrr r�!s�zTrace.globaltrace_ltcCs�|dkr~|jj}|j}||f}|j�|d�d|j|<|jrTtdt�|jdd�tj � |�}td||t � ||�fdd�|j S) Nrxrr�%.2f� �r�� %s(%d): %sr')r�r��f_linenor4rJr�rBr�rrr"rW�getliner�)rr�r�r�rrdrL�bnamerrr r�8s  ��z Trace.localtrace_trace_and_countcCsd|dkr^|jj}|j}|jr4tdt�|jdd�tj�|�}td||t � ||�fdd�|j S)Nrxr�r�r�r�r') r�r�r�r�rBr�rrr"rWr�r�)rr�r�r�rrdr�rrr r�Gs  ��zTrace.localtrace_tracecCs<|dkr6|jj}|j}||f}|j�|d�d|j|<|jS)Nrxrr)r�r�r�r4rJr�)rr�r�r�rrdrLrrr r�Ts zTrace.localtrace_countcCst|j|j|j|j|jd�S)N)r8r9r6r7)rr4r8r9r�r�)rrrr �results\s  �z Trace.results) rrrrrrNNF)NN)rr r!rr�r�r��__text_signature__r�r�r�r�r�r�r�r�rrrr r�s&� 2 )   c s�ddl}|��}|jdddd�|�dd�}|jdd d d d �|jd dd dd �|jddd dd �|jddd dd �|�d�}|��}|jddd dd �|jddd dd �|jdddd �|jd!d"d#d �|jd$d%d d&d �|jd'd(d d)d �|jd*d+d d,d �|�d-d.�}|jd/d0gd1d2�|jd3d0gd4d2�|jd5d d6d7d2�|jd8d9d:d;�|jd<|jd=d;�|��}|j�r�t� d>��t� d?����fd@dA��dBdC�|j D�|_ �fdDdC�|jD�|_|j �r�|j �s�|� dE�t|j |j dF�}|�|j|j|j�St|j|j|j|jg��s |� dG�|j�rB|j�s8|j�rB|� dH�|j�r\|j�s\|� dI�|jdk�rr|� dJ�t|j|j|j|j|j |j|j |j |jdK� }z�|j�r�ddl}|j}|�|�\} } } | jf|j�t _!dL| j| j"| j#| ddM�} n^|jf|j�t _!t$j%�&|j�t j%d<t'�(|j��} t)| �*�|jdN�} W5QRX|jdLdddO�} |�+| | | �WnPt,k �r�}zt �-dPt j!d|f�W5d}~XYnt.k �r�YnX|�/�}|j0�s�|�|j|j|j�dS)QNrz --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 .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 . will be written to file //.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).z--moduleFzTrace a module. �progname�?zfile to run as main program)�nargsr�� argumentszarguments to the programZstdlibZ platstdlibcs4tj�tj�|��}|�d���d��}tj�|�S)Nz$prefixz $exec_prefix)rr� expanduser� expandvarsr,r )�s)� _exec_prefix�_prefixrr �parse_ignore_dir�szmain..parse_ignore_dircSs$g|]}|�d�D] }|���qqS)�,)�split�strip)r r�rrrr r�s �zmain..cs&g|]}|�tj�D] }�|��qqSr)r�r�pathsep)r r�r�)r�rr r�s�z-r/--report requires -f/--file)r8r9zLmust 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�r8r9r�r�)rr�� __package__� __loader__�__spec__� __cached__r�)r�rr�r�zCannot run file %r because: %s)1�argparse�ArgumentParser� add_argumentZadd_argument_groupZadd_mutually_exclusive_groupZ REMAINDER� parse_argsZ ignore_dir� sysconfigZget_pathZ ignore_moduleZreportr3�errorrroZmissingr`ra�anyr�rfZ listfuncsZ trackcallsr�rr��module�runpyZ_get_module_detailsr�r�r)�argv�parent�loaderrrrR�io� open_coder�r�r�r?�exit� SystemExitr�Z no_report)r��parserZgrpZ_grpZoptsr��tr�Z module_nameZmod_nameZmod_specrzZglobsrirEr)r�r�r�r �mainbs�� � � � �  � ��� � � �� � � �� �  � �      �� �(r�r�)N)�__doc__�__all__r�rWrr)r�r�rZrr�ryr;�timerr�r�rurr&r0rr}r�r�rVrr�rrrrr �s< 20  _