B
    ê¹`¼E  ã               @   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ydd	lmZmZ W n< ek
r¼   e d
¡dkr´ej d¡ e d¡ dZY nX G dd„ deƒZdS )z#Raw data collector for coverage.py.é    N)Úenv)ÚlitemsÚrange)Úshort_stack)ÚFileDisposition)ÚCoverageExceptionÚisolate_module)ÚPyTracer)ÚCTracerÚCFileDispositionZCOVERAGE_TEST_TRACERÚcz:*** COVERAGE_TEST_TRACER is 'c' but can't import CTracer!
é   c               @   s¸   e Zd ZdZg Zddddh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"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   sz  || _ || _|| _|| _|| _|| _d| _d| _d| _t	ƒ | _
d| _i | _| j |¡}	t|	ƒdkrntd| ƒ‚|	rz|	 ¡ nd| _y†| jdkržddl}
|
j| _nf| jdkr¼ddl}|jj| _nH| jdkrØddl}|j| _n,| jd	ksè| jsøddl}|| _ntd
| ƒ‚W n& tk
r,   td| jf ƒ‚Y nX |  ¡  |rDt| _ntpLt| _| jtkrjt| _d| _n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.

        `should_start_context` is a function taking a frame, and returning a
        string. If the frame should be the start of a new context, the string
        is the new context. If the frame should not be the start of a new
        context, return None.

        `file_mapper` is a function taking a filename, and returning a Unicode
        filename.  The result is the name that will be recorded in the data
        file.

        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.TF)!Úshould_traceÚcheck_includeÚshould_start_contextÚfile_mapperÚwarnÚbranchÚ	threadingÚcovdataÚstatic_contextr   ÚoriginÚconcur_id_funcÚmapped_file_cacheÚSUPPORTED_CONCURRENCIESÚintersectionÚlenr   ÚpopÚconcurrencyr   Z
getcurrentZeventlet.greenthreadZgreenthreadr   ÚImportErrorÚresetr	   Ú_trace_classr
   r   Zfile_disposition_classZsupports_pluginsr   )Úselfr   r   r   r   Ztimidr   r   r$   Zthese_concurrenciesr   r   r   r   © r)   úZ/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/coverage/collector.pyÚ__init__<   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¡ dS )z!Use `covdata` for recording data.N)r   r   Úset_context)r(   r   Úcontextr)   r)   r*   Úuse_data§   s    zCollector.use_datac             C   s   | j jS )z0Return the class name of the tracer we're using.)r'   Ú__name__)r(   r)   r)   r*   r-   ­   s    zCollector.tracer_namec             C   s8   x| j  ¡ D ]}| ¡  qW x| jD ]}| ¡  q$W dS )z<Clear out existing data, but stay ready for more collection.N)ÚdataÚvaluesÚclearÚtracersZreset_activity)r(   ÚdÚtracerr)   r)   r*   Ú_clear_data±   s    zCollector._clear_datac             C   sH   i | _ i | _tƒ | _tjr0ddl}| d¡| _ni | _g | _	|  
¡  dS )z2Clear collected data, and prepare to collect more.r   NÚmodule)r3   Úfile_tracersÚsetÚdisabled_pluginsr   ZPYPYÚ__pypy__ZnewdictÚshould_trace_cacher6   r9   )r(   r>   r)   r)   r*   r&   ¼   s    
zCollector.resetc             C   sÚ   |   ¡ }| j|_| j|_| j|_| j|_| j|_t|dƒrD| j|_n| jr`t	d| j
|  ¡ f ƒ‚t|dƒrr| j|_t|dƒr„| j|_t|dƒr–| j|_t|dƒr°| j|_| j|_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 supportedr;   r   r   r   Údisable_plugin)r'   r3   r   Z
trace_arcsr   r?   r   Úhasattrr   r   r$   r-   r;   r   r   r   Úswitch_contextr@   Ústartr6   Úappend)r(   r8   Úfnr)   r)   r*   Ú_start_tracerê   s4    






zCollector._start_tracerc             C   s&   t  d¡ |  ¡ }|r"||||ƒ}|S )z0Called on new threads, installs the real tracer.N)ÚsysÚsettracerF   )r(   ÚframeÚeventÚargrE   r)   r)   r*   Ú_installation_trace  s
    
zCollector._installation_tracec       
   	   C   sè   | j r| j d  ¡  g | _g }t ¡ }|rFt|ddƒ}|rFt|dg ƒ}y|  ¡ }W n"   | j rn| j d  ¡  ‚ Y nX | j  | ¡ xL|D ]D}|\\}}}}	y|||||	d W qˆ t	k
rÊ   t
dƒ‚Y qˆX qˆW | jrä| j | j¡ dS )z#Start collecting trace information.éÿÿÿÿÚ__self__NZtraces)Úlinenoz3fullcoverage must be run with the C trace function.)Ú_collectorsÚpauser6   rG   ÚgettraceÚgetattrrF   ÚresumerD   Ú	TypeErrorÚ	Exceptionr   rH   rL   )
r(   Ztraces0Zfn0Ztracer0rE   ÚargsrI   rJ   rK   rO   r)   r)   r*   rC      s0    
zCollector.startc             C   s   | j s
t‚| j d | k	rBtdƒ x | j D ]}td ||j¡ƒ q(W | j d | ksftd| | j d f ƒ‚|  ¡  | j  ¡  | j rŒ| j d  ¡  dS )z"Stop collecting trace information.rM   zself._collectors:z	  {!r}
{}z0Expected current collector to be %r, but it's %rN)rP   ÚAssertionErrorÚprintÚformatr   rQ   r#   rT   )r(   r   r)   r)   r*   ÚstopI  s    

zCollector.stopc             C   sl   xT| j D ]J}| ¡  | ¡ }|rtdƒ x(t| ¡ ƒD ]}td||| f ƒ q6W qW | jrh| j d¡ dS )z+Pause tracing, but be prepared to `resume`.z
Coverage.py tracer stats:z%20s: %sN)r6   r[   Z	get_statsrY   ÚsortedÚkeysr   rH   )r(   r8   ÚstatsÚkr)   r)   r*   rQ   \  s    zCollector.pausec             C   s:   x| j D ]}| ¡  qW | jr.| j | j¡ n|  ¡  dS )zResume tracing after a `pause`.N)r6   rC   r   rH   rL   rF   )r(   r8   r)   r)   r*   rT   h  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)Zactivity)Ú.0r8   r)   r)   r*   ú	<genexpr>w  s    z&Collector._activity.<locals>.<genexpr>)Úanyr6   )r(   r)   r)   r*   Ú	_activityq  s    zCollector._activityc             C   s:   |   ¡  | jr&| j}|r*|d| 7 }n|}| j |¡ dS )z Switch to a new dynamic context.ú|N)Ú
flush_datar   r   r/   )r(   Znew_contextr0   r)   r)   r*   rB   y  s    zCollector.switch_contextc             C   s2   |j }|j}|j}|  d |¡¡ d|_d|_dS )z.Disable the plugin mentioned in `disposition`.z0Disabling plug-in {!r} due to previous exceptionFN)Úfile_tracerZ_coverage_pluginÚ_coverage_plugin_namer   rZ   Z_coverage_enabledÚtrace)r(   Údispositionrf   ÚpluginZplugin_namer)   r)   r*   r@   „  s    zCollector.disable_pluginc          	   C   s@   t |ƒ|f}y
| j| S  tk
r:   | j ||  |¡¡S X dS )zBA locally cached version of file names mapped through file_mapper.N)Útyper   ÚKeyErrorÚ
setdefaultr   )r(   ÚfilenameÚkeyr)   r)   r*   Úcached_mapped_file  s
    
zCollector.cached_mapped_filec                sf   d}xJt dƒD ]:}yt|ƒ}W n& tk
rD } z|}W dd}~X Y qX P qW |‚t‡ fdd„|D ƒƒS )z<Return a dict like d, but with keys modified by file_mapper.Né   c             3   s$   | ]\}}|rˆ   |¡|fV  qd S )N)rp   )r`   r_   Úv)r(   r)   r*   ra   ¦  s    z-Collector.mapped_file_dict.<locals>.<genexpr>)r   r   ÚRuntimeErrorÚdict)r(   r7   Zruntime_errÚ_ÚitemsÚexr)   )r(   r*   Úmapped_file_dict•  s    zCollector.mapped_file_dictc             C   s   | j  |j¡ dS )z1Record that `plugin` was disabled during the run.N)r=   Úaddrg   )r(   rj   r)   r)   r*   Úplugin_was_disabled¨  s    zCollector.plugin_was_disabledc                sr   ˆ   ¡ sdS ˆ jr(ˆ j ˆ  ˆ j¡¡ nˆ j ˆ  ˆ j¡¡ ‡ fdd„ˆ j ¡ D ƒ}ˆ j 	ˆ  |¡¡ ˆ  
¡  dS )zíSave the collected data to our associated `CoverageData`.

        Data may have also been saved along the way. This forces the
        last of the data to be saved.

        Returns True if there was data to save, False if not.
        Fc                s    i | ]\}}|ˆ j kr||“qS r)   )r=   )r`   r_   rr   )r(   r)   r*   ú
<dictcomp>¼  s   z(Collector.flush_data.<locals>.<dictcomp>T)rc   r   r   Zadd_arcsrx   r3   Z	add_linesr;   rv   Zadd_file_tracersr9   )r(   r;   r)   )r(   r*   re   ¬  s    
zCollector.flush_dataN)r2   Ú
__module__Ú__qualname__Ú__doc__rP   r    r+   r.   r1   r-   r9   r&   rF   rL   rC   r[   rQ   rT   rc   rB   r@   rp   rx   rz   re   r)   r)   r)   r*   r   $   s,   h.))		r   )r~   ÚosrG   Zcoverager   Zcoverage.backwardr   r   Zcoverage.debugr   Zcoverage.dispositionr   Zcoverage.miscr   r   Zcoverage.pytracerr	   Zcoverage.tracerr
   r   r%   ÚgetenvÚstderrÚwriteÚexitÚobjectr   r)   r)   r)   r*   Ú<module>   s"   

