o
    ·›îY�&  ã                   @   s  d Z ddlZddlZddlZddlZddlZzddlZW n ey)   ddlZY nw ddl	m
Z
 ddlmZ eeƒZg ZdZG dd„ deƒZG dd	„ d	eƒZd
d„ Zdd„ Zdd„ Zd%dd„Zd&dd„Zdd„ Zdd„ Zdd„ ZG dd„ deƒZG dd„ deƒZd'dd „Zd!d"„ Zd#d$„ ZdS )(z'Control of and utilities for debugging.é    N)ÚStringIO)Úisolate_moduleÚ c                   @   s>   e Zd ZdZdd„ Zdd„ Zdd„ Zejdd	„ ƒZ	d
d„ Z
dS )ÚDebugControlz!Control and output for debugging.c                 C   sN   t |ƒt | _|| _d| _g }|  d¡r| t¡ t| j|  d¡|d�| _	dS )z4Configure the options and output file for debugging.FÚpidÚprocess©Úshow_processÚfiltersN)
ÚlistÚFORCED_DEBUGÚoptionsÚ
raw_outputÚsuppress_callersÚshouldÚappendÚadd_pid_and_tidÚDebugOutputFileÚoutput)Úselfr   r   r
   © r   ú@/var/www/html/env/lib/python3.10/site-packages/coverage/debug.pyÚ__init__"   s   

ýzDebugControl.__init__c                 C   s   d| j | jf S )Nz'<DebugControl options=%r raw_output=%r>)r   r   ©r   r   r   r   Ú__repr__1   s   zDebugControl.__repr__c                 C   s   |dkr	| j r	dS || jv S )z@Decide whether to output debug information in category `option`.ÚcallersF)r   r   )r   Úoptionr   r   r   r   4   s   
zDebugControl.shouldc                 c   s*   � | j }d| _ z	dV  W || _ dS || _ w )z;A context manager to prevent call stacks from being logged.TN)r   )r   Úoldr   r   r   Úwithout_callers:   s   €zDebugControl.without_callersc                 C   s6   | j  |d ¡ |  d¡rt| j dd� | j  ¡  dS )zhWrite a line of debug output.

        `msg` is the line to write. A newline will be appended.

        Ú
r   é   ©ÚoutÚskipN)r   Úwriter   Údump_stack_framesÚflush)r   Úmsgr   r   r   r$   D   s   
zDebugControl.writeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Ú
contextlibÚcontextmanagerr   r$   r   r   r   r   r      s    
	r   c                       s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )ÚDebugControlStringz8A `DebugControl` that writes to a StringIO, for testing.c                    s   t t| ƒ |tƒ ¡ d S ©N)Úsuperr.   r   r   )r   r   ©Ú	__class__r   r   r   R   s   zDebugControlString.__init__c                 C   s
   | j  ¡ S )z,Get the output text from the `DebugControl`.)r   Úgetvaluer   r   r   r   Ú
get_outputU   s   
zDebugControlString.get_output)r(   r)   r*   r+   r   r4   Ú__classcell__r   r   r1   r   r.   P   s    r.   c                 C   s   d  d|  d ¡S )zMake a nice header string.z--{0:-<60s}ú )Úformat)Úlabelr   r   r   Úinfo_headerZ   s   r9   c                 c   s’   � t | ƒ} | s	dS tdd„ | D ƒƒ}| D ]2\}}|g krd}t|t ttfƒr>d||f }|D ]}d|d ||f V  d}q.qd	|||f V  qdS )
z¦Produce a sequence of formatted lines from info.

    `info` is a sequence of pairs (label, data).  The produced lines are
    nicely formatted, ready to print.

    Nc                 s   s   � | ]	\}}t |ƒV  qd S r/   )Úlen)Ú.0ÚlÚ_dr   r   r   Ú	<genexpr>i   s   € z!info_formatter.<locals>.<genexpr>z-none-z%*s:z%*s %sr    r   z%*s: %s)r   ÚmaxÚ
isinstanceÚsetÚtuple)ÚinfoÚ	label_lenr8   ÚdataÚprefixÚer   r   r   Úinfo_formatter_   s    €þ÷rH   c                 C   s.   |   t|ƒ¡ t|ƒD ]	}|   d| ¡ qdS )z.Write a sequence of (label,data) pairs nicely.z %sN)r$   r9   rH   )ÚwriterÚheaderrC   Úliner   r   r   Úwrite_formatted_infov   s   ÿrL   c                 C   s&   t  ¡ | |d… }d dd„ |D ƒ¡S )a�  Return a string summarizing the call stack.

    The string is multi-line, with one line per stack frame. Each line shows
    the function name, the file name, and the line number:

        ...
        start_import_stop : /Users/ned/coverage/trunk/tests/coveragetest.py @95
        import_local_file : /Users/ned/coverage/trunk/tests/coveragetest.py @81
        import_local_file : /Users/ned/coverage/trunk/coverage/backward.py @159
        ...

    `limit` is the number of frames to include, defaulting to all of them.

    `skip` is the number of frames to skip, so that debugging functions can
    call this and not be included in the result.

    éÿÿÿÿr   c                 s   s*   � | ]}d |d |d |d f V  qdS )z%30s : %s @%dé   r    é   Nr   )r;   Útr   r   r   r>   �   s   €( zshort_stack.<locals>.<genexpr>)ÚinspectÚstackÚjoin)Úlimitr#   rR   r   r   r   Úshort_stack}   s   rU   c                 C   s.   |pt j}| t| |d d�¡ | d¡ dS )z:Print a summary of the stack to stdout, or someplace else.r    )rT   r#   r   N)ÚsysÚstdoutr$   rU   )rT   r"   r#   r   r   r   r%   “   s   
r%   c                 C   s*   d}t dddƒD ]}|| |? N }q|d@ S )z-Given a 64-bit id, make a shorter 16-bit one.r   é@   é   iÿÿ  )Úrange)Úid64Úid16Úoffsetr   r   r   Úshort_idš   s   r^   c                 C   s(   d  tt ¡ ƒ¡}d  t ¡ || ¡} | S )z.A filter to add pid and tid to debug messages.z{0:04x}z{0:5d}.{1}: {2})r7   r^   Ú_threadÚ	get_identÚosÚgetpid)ÚtextÚtidr   r   r   r   ¢   s   r   c                 C   s\   |   ¡ }| t|ƒd… }|} |D ]}g }|  ¡ D ]}| ||ƒ ¡ ¡ qd |¡} q| | S )zëRun `text` through a series of filters.

    `filters` is a list of functions. Each takes a string and returns a
    string.  Each is run in turn.

    Returns: the final string that results after all of the filters have
    run.

    Nr   )Úrstripr:   Ú
splitlinesÚextendrS   )rc   r
   Ú
clean_textÚendingÚfnÚlinesrK   r   r   r   Úfilter_textª   s   
rl   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú
CwdTrackerz*A class to add cwd info to debug messages.c                 C   s
   d | _ d S r/   )Úcwdr   r   r   r   r   Á   s   
zCwdTracker.__init__c                 C   s*   t  ¡ }|| jkrd |¡| }|| _|S )z#Add a cwd message for each new cwd.zcwd is now {0!r}
)ra   Úgetcwdrn   r7   )r   rc   rn   r   r   r   ÚfilterÄ   s
   
zCwdTracker.filterN)r(   r)   r*   r+   r   rp   r   r   r   r   rm   ¿   s    rm   c                   @   s:   e Zd ZdZdd„ ZdZeddd	„ƒZd
d„ Zdd„ Z	dS )r   z9A file-like object that includes pid and cwd information.c                 C   sŒ   || _ || _t|ƒ| _| jrB| j tƒ j¡ d tt	ddgƒ¡}|  
dt	jf ¡ |  
d|f ¡ ttdƒrD|  
dt ¡ f ¡ d S d S d S )Nr6   Úargvz???zNew process: executable: %s
zNew process: cmd: %s
ÚgetppidzNew process: parent pid: %s
)Úoutfiler	   r   r
   r   rm   rp   rS   ÚgetattrrV   r$   Ú
executableÚhasattrra   rr   )r   rs   r	   r
   Úcmdr   r   r   r   Ï   s   

úzDebugOutputFile.__init__z'$coverage.debug.DebugOutputFile.the_oneNTr   c                 C   s>   t j | j¡}|du r|dusJ ‚| |||ƒ t j| j< }|S )zøGet the process-wide singleton DebugOutputFile.

        If it doesn't exist yet, then create it as a wrapper around the file
        object `fileobj`. `show_process` controls whether the debug file adds
        process-level information.

        N)rV   ÚmodulesÚgetÚSYS_MOD_NAME)ÚclsÚfileobjr	   r
   Úthe_oner   r   r   r}   Þ   s
   zDebugOutputFile.the_onec                 C   s"   | j  t|| jƒ¡ | j  ¡  dS )z9Just like file.write, but filter through all our filters.N)rs   r$   rl   r
   r&   )r   rc   r   r   r   r$   ñ   s   zDebugOutputFile.writec                 C   s   | j  ¡  dS )zFlush our file.N)rs   r&   r   r   r   r   r&   ö   s   zDebugOutputFile.flush)NTr   )
r(   r)   r*   r+   r   rz   Úclassmethodr}   r$   r&   r   r   r   r   r   Í   s    r   Fc                 C   s.   t  ¡ }| | d ¡ |rt|dd� dS dS )z.Write a log message as forcefully as possible.r   r    r!   N)r   r}   r$   r%   )r'   rR   r"   r   r   r   Úlogû   s
   ÿr   c                 C   s   t  dd| ¡} | S )zMAspectlib prints stack traces, but includes its own frames.  Scrub those out.z$(?<= )aspectlib/[^.]+\.py:\d+:\w+ < r   )ÚreÚsub)rc   r   r   r   Úfilter_aspectlib_frames  s   r‚   c                  C   s–   t j dd¡} | sdS ddl}ddl}t j dd¡}ttg}tjt	|dƒd|d	�}|j
j|d
gddd�}t d¡}|  d¡D ]
}|j|||d� q>dS )a°  For debugging, we can use aspectlib to trace execution.

    Define COVERAGE_ASPECTLIB to enable and configure aspectlib to trace
    execution::

        $ export COVERAGE_LOG=covaspect.txt
        $ export COVERAGE_ASPECTLIB=coverage.Coverage:coverage.data.CoverageData
        $ coverage run blah.py ...

    This will trace all the public methods on Coverage and CoverageData,
    writing the information to covaspect.txt.

    ÚCOVERAGE_ASPECTLIBr   Nr   ÚCOVERAGE_LOGz/tmp/covlog.txtÚaTr   Úidé   F)Úprint_toÚ
attributesÚ
stacktraceÚuse_loggingz^(__init__|[a-zA-Z].*)$ú:)Úmethods)ra   Úenvironry   Ú	aspectlibÚaspectlib.debugr   r‚   r   r}   ÚopenÚdebugr   r€   ÚcompileÚsplitÚweave)Úaspectsr�   Úfilenamer
   Úaspects_fileÚ
aspect_logÚpublic_methodsÚaspectr   r   r   Úenable_aspectlib_maybe
  s   
ÿ
ÿrœ   )Nr   )NNr   )F)r+   r,   rQ   ra   r€   rV   r_   ÚImportErrorÚthreadÚcoverage.backwardr   Úcoverage.miscr   r   Ú_TEST_NAME_FILEÚobjectr   r.   r9   rH   rL   rU   r%   r^   r   rl   rm   r   r   r‚   rœ   r   r   r   r   Ú<module>   s>   ÿ1



.