o
    ‡CZ@  ã                   @   sè   d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZmZ ddlmZ eeƒZz
dd	lmZmZ W n ey]   e d
¡dkrYej d¡ e d¡ dZY nw G dd„ deƒZdd„ ZG dd„ deƒZdS )z#Raw data collector for coverage.py.é    N)Úenv)ÚlitemsÚrange)Úshort_stack©Úabs_file)ÚCoverageExceptionÚisolate_module)ÚPyTracer)ÚCTracerÚCFileDispositionÚCOVERAGE_TEST_TRACERÚcz:*** COVERAGE_TEST_TRACER is 'c' but can't import CTracer!
é   c                   @   s   e Zd ZdZdS )ÚFileDispositionz9A simple value type for recording what to do with a file.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úD/var/www/html/env/lib/python3.10/site-packages/coverage/collector.pyr   $   s    r   c                 C   s   | j j}| d¡r|S dS )zKWho-Tests-What hack: Determine whether this frame begins a new who-context.ÚtestN)Úf_codeÚco_nameÚ
startswith)ÚframeÚfn_namer   r   r   Úshould_start_context)   s   
r   c                   @   s�   e Zd ZdZg Zeg 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d„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS ) Ú	Collectora  Collects trace data.

    Creates a Tracer object for each thread, since they track stack
    information.  Each Tracer points to the same shared data, contributing
    traced data points.

    When the Collector is started, it creates a Tracer for the current thread,
    and installs a function to create Tracers for each new thread started.
    When the Collector is stopped, all active Tracers are stopped.

    Threads started while the Collector is stopped will never have Tracers
    associated with them.

    )ÚgreenletÚeventletÚgeventÚthreadc                 C   sb  || _ || _|| _|| _d| _tƒ | _d| _| j 	|¡}t
|ƒdkr(td| ƒ‚|r.| ¡ nd| _zC| jdkr@ddl}|j| _n3| jdkrOddl}	|	jj| _n$| jdkr]ddl}
|
j| _n| jd	kse| jsmddl}|| _ntd
| ƒ‚W n tyƒ   td| jf ƒ‚w tt dd¡ƒ| _|  ¡  |r—t| _ntpšt| _| jtu r©t| _d| _dS t| _d| _dS )aÑ  Create a collector.

        `should_trace` is a function, taking a file name and a frame, and
        returning a `coverage.FileDisposition object`.

        `check_include` is a function taking a file name and a frame. It returns
        a boolean: True if the file should be traced, False if not.

        If `timid` is true, then a slower simpler trace function will be
        used.  This is important for some environments where manipulation of
        tracing functions make the faster more sophisticated trace function not
        operate properly.

        If `branch` is true, then branches will be measured.  This involves
        collecting data on which statements followed each other (arcs).  Use
        `get_arc_data` to get the arc data.

        `warn` is a warning function, taking a single string message argument
        and an optional slug argument which will be a string or None, to be
        used if a warning needs to be issued.

        `concurrency` is a list of strings indicating the concurrency libraries
        in use.  Valid values are "greenlet", "eventlet", "gevent", or "thread"
        (the default).  Of these four values, only one can be supplied.  Other
        values are ignored.

        Nr   z$Conflicting concurrency settings: %sÚ r   r   r    r!   r"   zDon't understand concurrency=%sz?Couldn't trace with concurrency=%s, the module isn't installed.ÚCOVERAGE_WTWTF) Úshould_traceÚcheck_includeÚwarnÚbranchÚ	threadingr   ÚoriginÚconcur_id_funcÚSUPPORTED_CONCURRENCIESÚintersectionÚlenr   ÚpopÚconcurrencyr   Ú
getcurrentÚeventlet.greenthreadÚgreenthreadr!   ÚImportErrorÚintÚosÚgetenvÚwtwÚresetr
   Ú_trace_classr   r   Úfile_disposition_classÚsupports_pluginsr   )Úselfr%   r&   Útimidr(   r'   r0   Úthese_concurrenciesr   r    r!   r)   r   r   r   Ú__init__I   sV   




€ÿÿÿ	



zCollector.__init__c                 C   s   dt | ƒ|  ¡ f S )Nz<Collector at 0x%x: %s>)ÚidÚtracer_name©r=   r   r   r   Ú__repr__£   s   zCollector.__repr__c                 C   s   | j jS )z0Return the class name of the tracer we're using.)r:   r   rC   r   r   r   rB   ¦   s   zCollector.tracer_namec                 C   s"   | j  ¡  | jD ]}| ¡  qdS )z<Clear out existing data, but stay ready for more collection.N)ÚdataÚclearÚtracersÚreset_activity©r=   Útracerr   r   r   Ú_clear_dataª   s   


ÿzCollector._clear_datac                 C   sR   i | _ i | _| j | jd< i | _tjrddl}| d¡| _ni | _g | _|  	¡  dS )z2Clear collected data, and prepare to collect more.Nr   Úmodule)
rE   ÚcontextsÚfile_tracersr   ÚPYPYÚ__pypy__ÚnewdictÚshould_trace_cacherG   rK   )r=   rP   r   r   r   r9   ±   s   
zCollector.resetc                 C   sÖ   |   ¡ }| j|_| j|_| j|_| j|_| j|_t|dƒr"| j|_n| jr0t	d| j
|  ¡ f ƒ‚t|dƒr9| j|_t|dƒrB| j|_t|dƒrK| j|_| jr_t|dƒrVt|_t|dƒr_| j|_| ¡ }| j |¡ |S )z8Start a new Tracer object, and store it in self.tracers.r+   z@Can't support concurrency=%s with %s, only threads are supportedrN   r)   r&   r   Úswitch_context)r:   rE   r(   Ú
trace_arcsr%   rR   r'   Úhasattrr+   r   r0   rB   rN   r)   r&   r8   r   rS   ÚstartrG   Úappend)r=   rJ   Úfnr   r   r   Ú_start_tracerá   s8   


ÿÿ




zCollector._start_tracerc                 C   s&   t  d¡ |  ¡ }|r||||ƒ}|S )z0Called on new threads, installs the real tracer.N)ÚsysÚsettracerY   )r=   r   ÚeventÚargrX   r   r   r   Ú_installation_trace
  s
   
zCollector._installation_tracec           
   	   C   sÚ   | j r
| j d  ¡  g | _g }t ¡ }|r#t|ddƒ}|r#t|dg ƒ}z|  ¡ }W n   | j r7| j d  ¡  ‚ | j  | ¡ |D ]}|\\}}}}	z
|||||	d� W q@ t	y^   t
dƒ‚w | jrk| j | j¡ dS dS )z#Start collecting trace information.éÿÿÿÿÚ__self__NÚtraces)Úlinenoz3fullcoverage must be run with the C trace function.)Ú_collectorsÚpauserG   rZ   ÚgettraceÚgetattrrY   ÚresumerW   Ú	TypeErrorÚ	Exceptionr)   r[   r^   )
r=   Útraces0Úfn0Útracer0rX   Úargsr   r\   r]   rb   r   r   r   rV     s4   ÿÿzCollector.startc                 C   s�   | j sJ ‚| j d | urtdƒ | j D ]}td ||j¡ƒ q| j d | u s1J d| | j d f ƒ‚|  ¡  | j  ¡  | j rF| j d  ¡  dS dS )z"Stop collecting trace information.r_   zself._collectors:z	  {!r}
{}z0Expected current collector to be %r, but it's %rN)rc   ÚprintÚformatr*   rd   r/   rg   )r=   r   r   r   r   Ústop@  s   

ÿ
ÿzCollector.stopc                 C   sh   | j D ]#}| ¡  | ¡ }|r&tdƒ t| ¡ ƒD ]}td||| f ƒ qq| jr2| j d¡ dS dS )z+Pause tracing, but be prepared to `resume`.z
Coverage.py tracer stats:z%20s: %sN)rG   rp   Ú	get_statsrn   ÚsortedÚkeysr)   r[   )r=   rJ   ÚstatsÚkr   r   r   rd   S  s   
€ÿzCollector.pausec                 C   s8   | j D ]}| ¡  q| jr| j | j¡ dS |  ¡  dS )zResume tracing after a `pause`.N)rG   rV   r)   r[   r^   rY   rI   r   r   r   rg   _  s
   

zCollector.resumec                 C   s   t dd„ | jD ƒƒS )zkHas any activity been traced?

        Returns a boolean, True if any trace function was invoked.

        c                 s   s   � | ]}|  ¡ V  qd S ©N)Úactivity)Ú.0rJ   r   r   r   Ú	<genexpr>n  s   € z&Collector._activity.<locals>.<genexpr>)ÚanyrG   rC   r   r   r   Ú	_activityh  s   zCollector._activityc                 C   s$   | j  |i ¡}| jD ]}||_q
dS )z1Who-Tests-What hack: switch to a new who-context.N)rM   Ú
setdefaultrG   rE   )r=   Únew_contextrE   rJ   r   r   r   rS   p  s   
ÿzCollector.switch_contextc                 C   s¬   |   ¡ sdS dd„ }| jr| || jƒ¡ n| || jƒ¡ | || jƒ¡ | jrPddl}d 	t
 ¡ ¡}t|dƒ�}| | j|¡ W d  ƒ n1 sKw   Y  |  ¡  dS )	ztSave the collected data to a `CoverageData`.

        Returns True if there was data to save, False if not.
        Fc                 S   s`   d}t dƒD ]}zt| ƒ}W n ty" } z|}W Y d}~qd}~ww  n|‚tdd„ |D ƒƒS )z;Return a dict like d, but with keys modified by `abs_file`.Né   c                 s   s    � | ]\}}t |ƒ|fV  qd S rv   r   )rx   ru   Úvr   r   r   ry   ‘  s   € z=Collector.save_data.<locals>.abs_file_dict.<locals>.<genexpr>)r   r   ÚRuntimeErrorÚdict)ÚdÚruntime_errÚ_ÚitemsÚexr   r   r   Úabs_file_dict€  s   €ÿz*Collector.save_data.<locals>.abs_file_dictr   Nzcoverage_wtw_{:06}.pyÚwT)r{   r(   Úadd_arcsrE   Ú	add_linesÚadd_file_tracersrN   r8   Úpprintro   r6   ÚgetpidÚopenrM   rK   )r=   Úcovdatar‡   rŒ   Úout_fileÚwtw_outr   r   r   Ú	save_datax  s   ÿzCollector.save_dataN)r   r   r   r   rc   Úsetr,   r@   rD   rB   rK   r9   rY   r^   rV   rp   rd   rg   r{   rS   r’   r   r   r   r   r   1   s$    Z0))	r   )r   r6   rZ   Úcoverager   Úcoverage.backwardr   r   Úcoverage.debugr   Úcoverage.filesr   Úcoverage.miscr   r	   Úcoverage.pytracerr
   Úcoverage.tracerr   r   r4   r7   ÚstderrÚwriteÚexitÚobjectr   r   r   r   r   r   r   Ú<module>   s*   
õ