ó
…¾^Yc           @   s{  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z y d d l Z Wn e k
 rq d d l Z n Xd d l	 m
 Z
 d d l m Z e e ƒ Z g  Z d Z d e f d „  ƒ  YZ d e f d	 „  ƒ  YZ d
 „  Z d „  Z d „  Z d d d „ Z d d d d „ Z d „  Z d „  Z d „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ e d „ Z d „  Z d „  Z  d S(   s'   Control of and utilities for debugging.iÿÿÿÿN(   t   StringIO(   t   isolate_modulet    t   DebugControlc           B   sD   e  Z d  Z d „  Z d „  Z d „  Z e j d „  ƒ Z d „  Z	 RS(   s!   Control and output for debugging.c         C   su   t  | ƒ t |  _ | |  _ t |  _ g  } |  j d ƒ rJ | j t ƒ n  t	 |  j d |  j d ƒ d | ƒ|  _
 d S(   s4   Configure the options and output file for debugging.t   pidt   show_processt   processt   filtersN(   t   listt   FORCED_DEBUGt   optionst
   raw_outputt   Falset   suppress_callerst   shouldt   appendt   add_pid_and_tidt   DebugOutputFilet   output(   t   selfR
   R   R   (    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyt   __init__"   s    			c         C   s   d |  j  |  j f S(   Ns'   <DebugControl options=%r raw_output=%r>(   R
   R   (   R   (    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyt   __repr__1   s    c         C   s&   | d k r |  j  r t S| |  j k S(   s@   Decide whether to output debug information in category `option`.t   callers(   R   R   R
   (   R   t   option(    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyR   4   s    c         c   s,   |  j  } t |  _  z	 d VWd | |  _  Xd S(   s;   A context manager to prevent call stacks from being logged.N(   R   t   True(   R   t   old(    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyt   without_callers:   s
    			c         C   sM   |  j  j | d ƒ |  j d ƒ r< t d |  j  d d ƒ n  |  j  j ƒ  d S(   sh   Write a line of debug output.

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

        s   
R   t   outt   skipi   N(   R   t   writeR   t   dump_stack_framest   flush(   R   t   msg(    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyR   D   s    (
   t   __name__t
   __module__t   __doc__R   R   R   t
   contextlibt   contextmanagerR   R   (    (    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyR      s   			
t   DebugControlStringc           B   s    e  Z d  Z d „  Z d „  Z RS(   s8   A `DebugControl` that writes to a StringIO, for testing.c         C   s    t  t |  ƒ j | t ƒ  ƒ d  S(   N(   t   superR&   R   R    (   R   R
   (    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyR   R   s    c         C   s   |  j  j ƒ  S(   s,   Get the output text from the `DebugControl`.(   R   t   getvalue(   R   (    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyt
   get_outputU   s    (   R!   R"   R#   R   R)   (    (    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyR&   P   s   	c         C   s   d j  d |  d ƒ S(   s   Make a nice header string.s   --{0:-<60s}t    (   t   format(   t   label(    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyt   info_headerZ   s    c         c   sÆ   t  |  ƒ }  |  s d St d „  |  Dƒ ƒ } x“ |  D]‹ \ } } | g  k rT d } n  t | t  t t f ƒ r¬ d | | f } x? | D]" } d | d | | f Vd } qƒ Wq3 d | | | f Vq3 Wd S(	   s¦   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(   N(   t   len(   t   .0t   lt   _d(    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pys	   <genexpr>i   s    s   -none-s   %*s:s   %*s %si   R   s   %*s: %s(   R   t   maxt
   isinstancet   sett   tuple(   t   infot	   label_lenR,   t   datat   prefixt   e(    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyt   info_formatter_   s    	c         C   s?   |  j  t | ƒ ƒ x% t | ƒ D] } |  j  d | ƒ q  Wd S(   s.   Write a sequence of (label,data) pairs nicely.s    %sN(   R   R-   R;   (   t   writert   headerR6   t   line(    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyt   write_formatted_infov   s    i    c         C   s0   t  j ƒ  |  | d … } d j d „  | Dƒ ƒ S(   s  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.

    iÿÿÿÿs   
c         s   s.   |  ]$ } d  | d | d | d f Vq d S(   s   %30s : %s @%di   i   i   N(    (   R/   t   t(    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pys	   <genexpr>   s    (   t   inspectt   stackt   join(   t   limitR   RB   (    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyt   short_stack}   s    c         C   s@   | p t  j } | j t d |  d | d ƒ ƒ | j d ƒ d S(   s:   Print a summary of the stack to stdout, or someplace else.RD   R   i   s   
N(   t   syst   stdoutR   RE   (   RD   R   R   (    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyR   “   s     c         C   s9   d } x( t  d d d ƒ D] } | |  | ?N} q W| d @S(   s-   Given a 64-bit id, make a shorter 16-bit one.i    i@   i   iÿÿ  (   t   range(   t   id64t   id16t   offset(    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyt   short_idš   s    c         C   s:   d j  t t j ƒ  ƒ ƒ } d j  t j ƒ  | |  ƒ }  |  S(   s.   A filter to add pid and tid to debug messages.s   {0:04x}s   {0:5d}.{1}: {2}(   R+   RL   t   _threadt	   get_identt   ost   getpid(   t   textt   tid(    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyR   ¢   s    c         C   s€   |  j  ƒ  } |  t | ƒ } | }  xS | D]K } g  } x- |  j ƒ  D] } | j | | ƒ j ƒ  ƒ qB Wd j | ƒ }  q) W|  | S(   së   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.

    s   
(   t   rstripR.   t
   splitlinest   extendRC   (   RQ   R   t
   clean_textt   endingt   fnt   linesR>   (    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyt   filter_textª   s    
t
   CwdTrackerc           B   s    e  Z d  Z d „  Z d „  Z RS(   s*   A class to add cwd info to debug messages.c         C   s   d  |  _ d  S(   N(   t   Nonet   cwd(   R   (    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyR   Á   s    c         C   s>   t  j ƒ  } | |  j k r: d j | ƒ | } | |  _ n  | S(   s#   Add a cwd message for each new cwd.s   cwd is now {0!r}
(   RO   t   getcwdR]   R+   (   R   RQ   R]   (    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyt   filterÄ   s
    (   R!   R"   R#   R   R_   (    (    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyR[   ¿   s   	R   c           B   sG   e  Z d  Z d „  Z d Z e d e d d „ ƒ Z d „  Z	 d „  Z
 RS(   s9   A file-like object that includes pid and cwd information.c         C   s¼   | |  _  | |  _ t | ƒ |  _ |  j r¸ |  j j t ƒ  j ƒ d j t t	 d d g ƒ ƒ } |  j
 d t	 j f ƒ |  j
 d | f ƒ t t d ƒ r¸ |  j
 d t j ƒ  f ƒ q¸ n  d  S(   NR*   t   argvs   ???s   New process: executable: %s
s   New process: cmd: %s
t   getppids   New process: parent pid: %s
(   t   outfileR   R   R   R   R[   R_   RC   t   getattrRF   R   t
   executablet   hasattrRO   Ra   (   R   Rb   R   R   t   cmd(    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyR   Ï   s    			s'   $coverage.debug.DebugOutputFile.the_onec         C   sZ   t  j j |  j ƒ } | d k rV | d k	 s3 t ‚ |  | | | ƒ t  j |  j <} n  | S(   sø   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(   RF   t   modulest   gett   SYS_MOD_NAMER\   t   AssertionError(   t   clst   fileobjR   R   t   the_one(    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyRm   Þ   s
    #c         C   s-   |  j  j t | |  j ƒ ƒ |  j  j ƒ  d S(   s9   Just like file.write, but filter through all our filters.N(   Rb   R   RZ   R   R   (   R   RQ   (    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyR   ñ   s    c         C   s   |  j  j ƒ  d S(   s   Flush our file.N(   Rb   R   (   R   (    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyR   ö   s    N(    (   R!   R"   R#   R   Ri   t   classmethodR\   R   Rm   R   R   (    (    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyR   Í   s   		c         C   s=   t  j ƒ  } | j |  d ƒ | r9 t d | d d ƒ n  d S(   s.   Write a log message as forcefully as possible.s   
R   R   i   N(   R   Rm   R   R   (   R    RB   R   (    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyt   logû   s    c         C   s   t  j d d |  ƒ }  |  S(   sM   Aspectlib prints stack traces, but includes its own frames.  Scrub those out.s$   (?<= )aspectlib/[^.]+\.py:\d+:\w+ < R   (   t   ret   sub(   RQ   (    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyt   filter_aspectlib_frames  s    c       	   C   sé   t  j j d d ƒ }  |  s d Sd d l } d d l } t  j j d d ƒ } t t g } t j t	 | d ƒ d t
 d	 | ƒ} | j j d
 | d d g d d d t ƒ } t j d ƒ } x- |  j d ƒ D] } | j | | d | ƒqÅ Wd S(   s°  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.

    t   COVERAGE_ASPECTLIBR   Niÿÿÿÿt   COVERAGE_LOGs   /tmp/covlog.txtt   aR   R   t   print_tot
   attributest   idt
   stacktracei   t   use_loggings   ^(__init__|[a-zA-Z].*)$t   :t   methods(   RO   t   environRh   t	   aspectlibt   aspectlib.debugR   Rr   R   Rm   t   openR   t   debugRo   R   Rp   t   compilet   splitt   weave(   t   aspectsR~   t   filenameR   t   aspects_filet
   aspect_logt   public_methodst   aspect(    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyt   enable_aspectlib_maybe
  s    $(!   R#   R$   RA   RO   Rp   RF   RM   t   ImportErrort   threadt   coverage.backwardR    t   coverage.miscR   R	   t   _TEST_NAME_FILEt   objectR   R&   R-   R;   R?   R\   RE   R   RL   R   RZ   R[   R   R   Ro   Rr   R‹   (    (    (    s0   /tmp/pip-build-hU8Cw8/coverage/coverage/debug.pyt   <module>   s:   1
						.	