ó
…¾^Yc           @   s"  d  Z  d d l Z d d l Z d d l 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 WnL e k
 rè e j d	 ƒ d
 k rß e j j d ƒ e j d ƒ n  d Z n Xd e f d „  ƒ  YZ d „  Z d e f d „  ƒ  YZ d S(   s#   Raw data collector for coverage.py.iÿÿÿÿN(   t   env(   t   iitems(   t   abs_file(   t   CoverageExceptiont   isolate_module(   t   PyTracer(   t   CTracert   CFileDispositiont   COVERAGE_TEST_TRACERt   cs:   *** COVERAGE_TEST_TRACER is 'c' but can't import CTracer!
i   t   FileDispositionc           B   s   e  Z d  Z RS(   s9   A simple value type for recording what to do with a file.(   t   __name__t
   __module__t   __doc__(    (    (    s4   /tmp/pip-build-hU8Cw8/coverage/coverage/collector.pyR
   #   s   c         C   s#   |  j  j } | j d ƒ r | Sd S(   sK   Who-Tests-What hack: Determine whether this frame begins a new who-context.t   testN(   t   f_codet   co_namet
   startswith(   t   framet   fn_name(    (    s4   /tmp/pip-build-hU8Cw8/coverage/coverage/collector.pyt   should_start_context(   s    t	   Collectorc           B   sª   e  Z d  Z g  Z e d d d d g ƒ Z d „  Z d „  Z d „  Z d „  Z	 d	 „  Z
 d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s  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.

    t   greenlett   eventlett   geventt   threadc         C   sø  | |  _  | |  _ | |  _ | |  _ d |  _ d |  _ |  j j | ƒ } t	 | ƒ d k rm t
 d | ƒ ‚ n  | r | j ƒ  n d |  _ yÆ |  j d k rµ d d l } | j |  _ n˜ |  j d k râ d d l }	 |	 j j |  _ nk |  j d k rd d l }
 |
 j |  _ nA |  j d	 k s%|  j r=d d l } | |  _ n t
 d
 | ƒ ‚ Wn' t k
 rwt
 d |  j f ƒ ‚ n Xt t j d d ƒ ƒ |  _ |  j ƒ  | r¯t |  _ n t p¸t |  _ |  j t k rât |  _ t |  _ n t |  _ t  |  _ d S(   s€  Create a collector.

        `should_trace` is a function, taking a file name, 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,
        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.

        i   s$   Conflicting concurrency settings: %st    R   iÿÿÿÿNR   R   R   s   Don't understand concurrency=%ss?   Couldn't trace with concurrency=%s, the module isn't installed.t   COVERAGE_WTWi    (!   t   should_tracet   check_includet   warnt   brancht   Nonet	   threadingt   concur_id_funct   SUPPORTED_CONCURRENCIESt   intersectiont   lenR   t   popt   concurrencyR   t
   getcurrentt   eventlet.greenthreadt   greenthreadR   t   ImportErrort   intt   ost   getenvt   wtwt   resetR   t   _trace_classR   R   t   file_disposition_classt   Truet   supports_pluginsR
   t   False(   t   selfR   R   t   timidR   R   R'   t   these_concurrenciesR   R   R   R!   (    (    s4   /tmp/pip-build-hU8Cw8/coverage/coverage/collector.pyt   __init__G   sL    						
		c         C   s   d t  |  ƒ |  j ƒ  f S(   Ns   <Collector at 0x%x: %s>(   t   idt   tracer_name(   R6   (    (    s4   /tmp/pip-build-hU8Cw8/coverage/coverage/collector.pyt   __repr__ž   s    c         C   s
   |  j  j S(   s0   Return the class name of the tracer we're using.(   R1   R   (   R6   (    (    s4   /tmp/pip-build-hU8Cw8/coverage/coverage/collector.pyR;   ¡   s    c         C   s/   |  j  j ƒ  x |  j D] } | j ƒ  q Wd S(   s<   Clear out existing data, but stay ready for more collection.N(   t   datat   cleart   tracerst   reset_activity(   R6   t   tracer(    (    s4   /tmp/pip-build-hU8Cw8/coverage/coverage/collector.pyt   _clear_data¥   s    c         C   su   i  |  _  i  |  _ |  j  |  j d <i  |  _ t j rU d d l } | j d ƒ |  _ n	 i  |  _ g  |  _	 |  j
 ƒ  d S(   s2   Clear collected data, and prepare to collect more.iÿÿÿÿNt   module(   R=   t   contextsR    t   file_tracersR    t   PYPYt   __pypy__t   newdictt   should_trace_cacheR?   RB   (   R6   RG   (    (    s4   /tmp/pip-build-hU8Cw8/coverage/coverage/collector.pyR0   ¬   s    			
			c         C   sP  |  j  ƒ  } |  j | _ |  j | _ |  j | _ |  j | _ |  j | _ t | d ƒ rf |  j | _ n+ |  j r‘ t	 d |  j
 |  j ƒ  f ƒ ‚ n  t | d ƒ r¯ |  j | _ n  t | d ƒ rÍ |  j | _ n  t | d ƒ rë |  j | _ n  |  j r0t | d ƒ rt | _ n  t | d ƒ r0|  j | _ q0n  | j ƒ  } |  j j | ƒ | S(   s8   Start a new Tracer object, and store it in self.tracers.R"   s@   Can't support concurrency=%s with %s, only threads are supportedRE   R!   R   R   t   switch_context(   R1   R=   R   t
   trace_arcsR   RI   R   t   hasattrR"   R   R'   R;   RE   R!   R   R/   R   RJ   t   startR?   t   append(   R6   RA   t   fn(    (    s4   /tmp/pip-build-hU8Cw8/coverage/coverage/collector.pyt   _start_tracerÜ   s4    		c         C   s8   t  j d ƒ |  j ƒ  } | r4 | | | | ƒ } n  | S(   s0   Called on new threads, installs the real tracer.N(   t   syst   settraceR    RP   (   R6   R   t   eventt   argRO   (    (    s4   /tmp/pip-build-hU8Cw8/coverage/coverage/collector.pyt   _installation_trace  s
    c   
      C   s;  |  j  r |  j  d j ƒ  n  g  |  _ g  } t j ƒ  } | rn t | d d ƒ } | rn t | d g  ƒ } qn n  y |  j ƒ  } Wn' |  j  r¡ |  j  d j ƒ  n  ‚  n X|  j  j	 |  ƒ x] | D]U } | \ \ } } } }	 y | | | | d |	 ƒWq¿ t
 k
 rt d ƒ ‚ q¿ Xq¿ W|  j r7|  j j |  j ƒ n  d S(   s#   Start collecting trace information.iÿÿÿÿt   __self__t   tracest   linenos3   fullcoverage must be run with the C trace function.N(   t   _collectorst   pauseR?   RQ   t   gettracet   getattrR    RP   t   resumeRN   t	   TypeErrort	   ExceptionR!   RR   RU   (
   R6   t   traces0t   fn0t   tracer0RO   t   argsR   RS   RT   RX   (    (    s4   /tmp/pip-build-hU8Cw8/coverage/coverage/collector.pyRM     s0    				c         C   sw   |  j  s t ‚ |  j  d |  k s? t d |  |  j  d f ƒ ‚ |  j ƒ  |  j  j ƒ  |  j  rs |  j  d j ƒ  n  d S(   s"   Stop collecting trace information.iÿÿÿÿs0   Expected current collector to be %r, but it's %rN(   RY   t   AssertionErrorRZ   R&   R]   (   R6   (    (    s4   /tmp/pip-build-hU8Cw8/coverage/coverage/collector.pyt   stop;  s    
	c         C   sˆ   xe |  j  D]Z } | j ƒ  | j ƒ  } | r
 d GHx0 t | j ƒ  ƒ D] } d | | | f GHqD Wq
 q
 W|  j r„ |  j j d ƒ n  d S(   s+   Pause tracing, but be prepared to `resume`.s   
Coverage.py tracer stats:s   %20s: %sN(   R?   Re   t	   get_statst   sortedt   keysR!   RR   R    (   R6   RA   t   statst   k(    (    s4   /tmp/pip-build-hU8Cw8/coverage/coverage/collector.pyRZ   J  s    
	c         C   sK   x |  j  D] } | j ƒ  q
 W|  j r= |  j j |  j ƒ n
 |  j ƒ  d S(   s   Resume tracing after a `pause`.N(   R?   RM   R!   RR   RU   RP   (   R6   RA   (    (    s4   /tmp/pip-build-hU8Cw8/coverage/coverage/collector.pyR]   V  s
    	c         C   s   t  d „  |  j Dƒ ƒ S(   sk   Has any activity been traced?

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

        c         s   s   |  ] } | j  ƒ  Vq d  S(   N(   t   activity(   t   .0RA   (    (    s4   /tmp/pip-build-hU8Cw8/coverage/coverage/collector.pys	   <genexpr>e  s    (   t   anyR?   (   R6   (    (    s4   /tmp/pip-build-hU8Cw8/coverage/coverage/collector.pyt	   _activity_  s    c         C   s6   |  j  j | i  ƒ } x |  j D] } | | _ q Wd S(   s1   Who-Tests-What hack: switch to a new who-context.N(   RD   t
   setdefaultR?   R=   (   R6   t   new_contextR=   RA   (    (    s4   /tmp/pip-build-hU8Cw8/coverage/coverage/collector.pyRJ   g  s    c         C   sÍ   |  j  ƒ  s t Sd „  } |  j r; | j | |  j ƒ ƒ n | j | |  j ƒ ƒ | j | |  j ƒ ƒ |  j r¿ d d l	 } d j
 t j ƒ  ƒ } t | d ƒ  } | j	 |  j | ƒ Wd QXn  |  j ƒ  t S(   st   Save the collected data to a `CoverageData`.

        Returns True if there was data to save, False if not.
        c         S   s   t  d „  t |  ƒ Dƒ ƒ S(   s;   Return a dict like d, but with keys modified by `abs_file`.c         s   s'   |  ] \ } } t  | ƒ | f Vq d  S(   N(   R   (   Rl   Rj   t   v(    (    s4   /tmp/pip-build-hU8Cw8/coverage/coverage/collector.pys	   <genexpr>y  s    (   t   dictR   (   t   d(    (    s4   /tmp/pip-build-hU8Cw8/coverage/coverage/collector.pyt   abs_file_dictw  s    iÿÿÿÿNs   coverage_wtw_{:06}.pyt   w(   Rn   R5   R   t   add_arcsR=   t	   add_linest   add_file_tracersRE   R/   t   pprintt   formatR-   t   getpidt   openRD   RB   R3   (   R6   t   covdataRt   Ry   t   out_filet   wtw_out(    (    s4   /tmp/pip-build-hU8Cw8/coverage/coverage/collector.pyt	   save_datao  s    			
(   R   R   R   RY   t   setR#   R9   R<   R;   RB   R0   RP   RU   RM   Re   RZ   R]   Rn   RJ   R€   (    (    (    s4   /tmp/pip-build-hU8Cw8/coverage/coverage/collector.pyR   /   s"   	W				0	)		)						(   R   R-   RQ   t   coverageR    t   coverage.backwardR   t   coverage.filesR   t   coverage.miscR   R   t   coverage.pytracerR   t   coverage.tracerR   R   R+   R.   t   stderrt   writet   exitR    t   objectR
   R   R   (    (    (    s4   /tmp/pip-build-hU8Cw8/coverage/coverage/collector.pyt   <module>   s$   
	