관리-도구
편집 파일: trace.cpython-39.opt-1.pyc
a �����DOgr����������������������@���s����d�Z�ddgZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddlZddlZddl mZ�ddlZdZG�dd��d�Zd d ��Zdd��ZG�d d��d�Zdd��Zdd��Zddd�Zdd��ZG�dd��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�������������������@���s���e�Zd�Zddd�Zdd��ZdS�)�_IgnoreNc�����������������C���s:���|s t���nt�|�|�_|sg�ndd��|D��|�_ddi|�_d�S�)Nc�����������������S���s���g�|�]}t�j�|��qS���)�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�||�jv�r,d|�j�|<�dS�|�jD�]"}|�|d��r2d|�j�|<��dS�q2|d�u�rld|�j�|<�dS�|�jD�]$}|�|tj��rrd|�j�|<��dS�qrd|�j�|<�dS�)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}tjD�]@}t�j�|�}|�|�r|t|��t�jkrt|�t|�kr|}q|rr|�t|�d�d��}n|�}t�j�|�\}}|�t�jd�}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���ZcomparepathZlongest�dirr$����driver���r%���r���r���r ����_fullmodname~���s ���� r1���c�������������������@���s:���e�Zd�Zddd�Zdd��Zdd��Zdd d�Zddd �ZdS�)r���Nc�������������� ���C���s��||�_�|�j�d�u�ri�|�_�|�j����|�_||�_|�jd�u�r8i�|�_|�j���|�_||�_|�jd�u�rZi�|�_|�j���|�_||�_||�_|�j�rzTt|�jd�� }t� |�\}}}W�d�����n1�s�0����Y��|�� |��|||���W�nD�tt tf�y�}�z$td|�j|f�tjd��W�Y�d�}~n d�}~0�0�d�S�)N�rbzSkipping counts file %r: %s��file)�counts�copyZcounter�calledfuncs�callers�infile�outfile�open�pickle�load�update� __class__�OSError�EOFError� ValueError�printr)����stderr)r���r5���r7���r9���r8���r:����f�errr���r���r ���r�������s2���� .��zCoverageResults.__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\dS�)z.Merge in the data from another CoverageResultsr���r���N)r5���r7���r8����get) r����otherr5���r7���r8���Zother_countsZother_calledfuncsZ other_callers�keyr���r���r ���r>�������s���� zCoverageResults.updateTFc�����������"��� ���C���s��|�j�r@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 u��rpt j �t j �|��}t |�}n$|}t j �|��s�t �|��t|�}|�r�t|�}ni�}t�|�}t j �||d��}t|d�� }t�|j�\}}W�d ����n1��s�0����Y��|��|||||�\}}|�r|�rtd |�|��}||||f||<��q|�r�|�r�td��t|�D�]&}||�\}}}}td||�����qb|�j�rzLt|�jd��*} t�|�j|�j�|�jf| d��W�d ����n1��s�0����Y��W�n8�t�y�}!�ztd|!�tj 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. zfunctions called:z*filename: %s, modulename: %s, funcname: %szcalling relationships:r'���z***z -->z %s.%s -> %s.%sz.pycN���z.coverr2����d���zlines cov% module (path)z%5d %3d%% %s (%s)�wbr���z"Can't save counts files because %sr3���)!r7���rC����sortedr8���r5���rK����itemsrJ���rI���r���r����dirname�abspathr&����exists�makedirsr1����_find_executable_linenos� linecache�getlines�joinr;����tokenize�detect_encoding�readline�write_results_file�intr:���r<����dumpr@���r)���rD���)"r���Zshow_missing�summary�coverdirZcallsr���r����funcnameZlastfileZ lastcfileZpfileZpmodZpfunc�cfileZcmodZcfuncZper_file�lineno� lines_hitZsums�countr/����lnotab�sourceZ coverpath�fp�encoding�_�n_hits�n_lines�percent�mrE���rF���r���r���r ���� write_results����s����� �� 0��(zCoverageResults.write_resultsc�������������� ���C���s��zt�|d|d�}W�n<�tyN�}�z$td||f�tjd��W�Y�d}~dS�d}~0�0�d}d} |���t|d�D�]r\} }| |v�r�|�d || ����| d7�} |d7�}n.| |v�r�t|vr�|�d ��|d7�}n |�d��|�|�d���qhW�d����n1�s�0����Y��| |fS�) z'Return a coverage results file in path.�w�rk���z3trace: Could not open %r for writing: %s - skippingr3���N)r���r���r���r���z%5d: z>>>>>> z ����) r;���r@���rC���r)���rD���� enumerate�write�PRAGMA_NOCOVER� expandtabs)r���r����linesrh���rf���rk���r:���rF���rn���rm���re����liner���r���r ���r^���)��s.������ 0z"CoverageResults.write_results_file)NNNNN)TFN)N)r���r ���r!���r���rJ���r>���rq���r^���r���r���r���r ���r�������s������ \c�����������������C���s,���i�}t��|��D�]\}}||vrd||<�q|S�)z:Return dict where keys are lines in the line number table.r���)�disZfindlinestarts)�code�strs�linenosrl���re���r���r���r ����_find_lines_from_codeI��s ���� r���c�����������������C���s4���t�|�|�}|�jD�]}t�|�r|�t||���q|S�)z<Return lineno dict for all code objects reachable from code.)r���� co_consts�inspectZiscoder>����_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�jkrv|t�jkrv|\}}| \} }t|| d��D�]}d||<�qh|}q(W�d����n1�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. rs���r���N)�token�INDENTr;���r[����generate_tokensr]����STRING�range)r���rk���r���Z prev_ttyperE����tokZttypeZtstr�start�endrz���ZslineZscolZelineZecol�ir���r���r ���� _find_strings_��s���� $r����c�������������� ���C���s����z<t��|���}|���}|j}W�d����n1�s00����Y��W�n>�tyz�}�z&td|�|f�tjd��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: %sr3����exec)r[���r;����readrk���r@���rC���r)���rD����compiler����r����)r���rE����progrk���rF���r|���r}���r���r���r ���rW���v��s����(�� rW���c���������������� ���@���sp���e�Zd�Zddd�Zdd ��Zdd d�Zdd ��Zdd��Zdd��Zdd��Z dd��Z dd��Zdd��Zdd��Z dd��ZdS�) 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|�_nd|�_dS�)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���)r9���r:���r����ignorer5���Zpathtobasename� 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���rg���r����� countfuncs�countcallers� ignoremods� ignoredirsr9���r:����timingr���r���r ���r������s6���� zTrace.__init__c�����������������C���s ���dd�l�}|j}|��|||��d�S�)Nr���)�__main__�__dict__�runctx)r����cmdr�����dictr���r���r ����run���s����z Trace.runc�������������� ���C���s����|d�u�ri�}|d�u�ri�}|�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���� � zTrace.runctxc������������� ���O���sP���d�}|�j�st�|�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}|rt|�}nd�}|j}d�}||�jv�rL|�j|�d�ur�|�j|�}n�d�|�j|<�dd��t�|�D��}t|�dkr�dd��t�|d��D��}t|�dkr�dd��t�|d��D��} t| �dkr�| d�j}||�j|<�|d�ur�d||f�}|||fS�)Nc�����������������S���s���g�|�]}t��|�r|�qS�r���)r����Z isfunction)r���rE���r���r���r ���r������s��� �z1Trace.file_module_function_of.<locals>.<listcomp>r���c�����������������S���s���g�|�]}t�|t�r|�qS�r���)� isinstancer����r ���r���r���r ���r������s��� �r���c�����������������S���s���g�|�]}t�|d��r|�qS�)� __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���rc���ZclsnameZfuncsZdicts�classesr���r���r ����file_module_function_of���s,���� zTrace.file_module_function_ofc�����������������C���s0���|dkr,|���|�}|���|j�}d|�j||f<�dS�)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_funcZparent_funcr���r���r ���r�������s���� zTrace.globaltrace_trackcallersc�����������������C���s ���|dkr|���|�}d|�j|<�dS�)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���� zTrace.globaltrace_countfuncsc�����������������C���sj���|dkrf|j�}|j�dd�}|rbt|�}|durf|�j�||�}|sf|�jrZtd||jf���|�j S�ndS�dS�)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_globalsrK���r&���r����r���r����rC���r����r����)r���r����r����r����r|���r���r���Z ignore_itr���r���r ���r������s�����zTrace.globaltrace_ltc�����������������C���s����|dkr~|j�j}|j}||f}|�j�|d�d�|�j|<�|�jrTtdt��|�j��dd��tj � |�}td||t�||�f�dd��|�j S�) Nrz���r���r����%.2f� �r����� %s(%d): %sr'���)r����r�����f_linenor5���rK���r����rC���r����r���r���r"���rX����getliner����)r���r����r����r����r���re���rM����bnamer���r���r ���r����(��s���� ��z Trace.localtrace_trace_and_countc�����������������C���sd���|dkr^|j�j}|j}|�jr4tdt��|�j��dd��tj�|�}td||t � ||�f�dd��|�jS�)Nrz���r����r����r����r����r'���)r����r����r����r����rC���r����r���r���r"���rX���r����r����)r���r����r����r����r���re���r����r���r���r ���r����7��s���� ��zTrace.localtrace_tracec�����������������C���s<���|dkr6|j�j}|j}||f}|�j�|d�d�|�j|<�|�jS�)Nrz���r���r���)r����r����r����r5���rK���r����)r���r����r����r����r���re���rM���r���r���r ���r����D��s����zTrace.localtrace_countc�����������������C���s���t�|�j|�j|�j|�j|�jd�S�)N)r9���r:���r7���r8���)r���r5���r9���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���dd�l�}�|����}|jdddd��|�dd�}|jdd d dd��|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/d0g�d1d2��|jd3d0g�d4d2��|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|jdF�}|�|j|j|j�S�t|j|j|j|jg��s |� dG��|j�rB|j�s8|j�rB|� dH��|j�r\|j�s\|� dI��|jd�u��rr|� dJ��t|j|j|j|j|j |j|j|j|jdK� }z�|j�r�dd�l}|j}|�|�\} } }|jg|j�t _!dL|j| j"| j#| d�dM�}nt|jg|j�t _!t$j%�&|j�t j%d<�t'�(|j��"} t)| �*��|jdN�}W�d�����n1��sJ0����Y��|jdLd�d�dO�}|�+|||��W�nP�t,�y��}�z$t �-dPt j!d�|f���W�Y�d�}~nd�}~0��t.�y����Y�n0�|�/��}|j0�s�|�|j|j|j��d�S�)QNr���z --version�versionz trace 2.0)�actionr����zMain 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 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 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����zmain.<locals>.parse_ignore_dirc�����������������S���s$���g�|�]}|��d��D�]}|����qqS�)�,)�split�strip)r���r����r���r���r���r ���r������s����zmain.<locals>.<listcomp>c��������������������s&���g�|�]}|��tj�D�]}��|��qqS�r���)r����r����pathsep)r���r����r����)r����r���r ���r������s����z-r/--report requires -f/--file)r9���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����r9���r:���r����r����)r���r�����__package__� __loader__�__spec__� __cached__r����)r����r���r����r����zCannot run file %r because: %s)1�argparse�ArgumentParser�add_argument�add_argument_group�add_mutually_exclusive_group� REMAINDER� parse_argsZ ignore_dir� sysconfigZget_pathZ ignore_module�reportr4����errorr���rq����missingra���rb����anyr����rg���Z listfuncsZ trackcallsr����r���r�����module�runpy�_get_module_detailsr����r����r)����argv�parent�loaderr���r���rS����io� open_coder����r����r����r@����exit� SystemExitr����Z no_report)r�����parser�grpZ_grpZoptsr�����tr�����module_name�mod_name�mod_specr|���Zglobsrj���rF���r���)r����r����r����r ����mainR��s������ � � � � � ��� � � �� � � �� � � � �� 2�,r��r����)N)�__doc__�__all__r����rX���r���r)���r����r����r[���r����r����r{���r<����timer���r����r����rw���r���r&���r1���r���r���r����r����rW���r���r��r���r���r���r���r ����<module>���s<���2�0 �O�