ó
ßüÚ\c           @  s  d  Z  d d l m Z m Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 Z
 d „  Z d „  Z i  Z d „  Z d e f d „  ƒ  YZ e d	 ƒ d
 e f d „  ƒ  Yƒ Z e d ƒ d e f d „  ƒ  Yƒ Z e d ƒ d e f d „  ƒ  Yƒ Z d e j d d ƒ f d „  ƒ  YZ e d ƒ d e f d „  ƒ  Yƒ Z e d ƒ d e f d „  ƒ  Yƒ Z d e f d „  ƒ  YZ d S(    s*   
A pytest plugin to trace resource leaks.
iÿÿÿÿ(   t   print_functiont   divisionNc         C  sÕ   |  j  d ƒ } | j d d d d d d d d	 j d
 d j t d „  t Dƒ ƒ ƒ ƒ ƒ| j d d d d d d d d d d d ƒ| j d d d d d d t d d ƒ| j d d d d t d d d d d d ƒd  S(   Ns   resource leakss   -Ls   --leakst   actiont   storet   destt   leakst   helps¡   List of resources to monitor for leaks before and after each test.
Can be 'all' or a comma-separated list of resource names
(possible values: {known_checkers}).
t   known_checkerss   , c         s  s   |  ] } d  | Vq d S(   s   '%s'N(    (   t   .0t   s(    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pys	   <genexpr>   s    s   --leaks-timeoutt   typet   floatt   leaks_timeoutt   defaultg      à?sS   Wait at most this number of seconds to mark a test leaking
(default: %(default)s).
s   --leaks-failt
   store_truet   leaks_mark_faileds   Mark leaked tests failed.s   --leak-retriest   leak_retriesi   sa   Max number of times to retry a test when it leaks, to ignore
warmup-related issues (default: 1).
(   t   getgroupt	   addoptiont   formatt   joint   sortedt   all_checkerst   Falset   int(   t   parsert   group(    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyt   pytest_addoption   s8    	#c      
   C  sí   |  j  d ƒ } | ré | d k r0 t t ƒ } n | j d ƒ } t t | ƒ t t ƒ ƒ } | rw t d | f ƒ ‚ n  g  | D] } t | ƒ  ^ q~ } t d | d |  j  d ƒ d |  j  d	 ƒ d
 |  j  d ƒ ƒ } |  j j | d ƒ n  d  S(   NR   t   allt   ,s   unknown resources: %rt   checkerst   grace_delayR   t   mark_failedR   t   max_retriesR   t   leaks_checker(	   t   getvalueR   R   t   splitt   sett
   ValueErrort   LeakCheckert   pluginmanagert   register(   t   configR   t   unknownt   leakR   t   checker(    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyt   pytest_configure?   s     c           s   ‡  f d †  } | S(   Nc           s;   t  |  t ƒ s t |  ƒ ‚ ˆ  t k s- t ‚ |  t ˆ  <|  S(   N(   t
   issubclasst   ResourceCheckert   AssertionErrorR   (   t   cls(   t   name(    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyt   decorateX   s    
(    (   R3   R4   (    (   R3   s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyt   register_checkerW   s    R0   c           B  s>   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C  s   d  S(   N(    (   t   self(    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyt   on_start_testb   s    c         C  s   d  S(   N(    (   R6   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyt   on_stop_teste   s    c         C  s   d  S(   N(    (   R6   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyt   on_retryh   s    c         C  s
   t  ‚ d  S(   N(   t   NotImplementedError(   R6   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyt   measurek   s    c         C  s
   t  ‚ d  S(   N(   R:   (   R6   t   beforet   after(    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyt   has_leakn   s    c         C  s
   t  ‚ d  S(   N(   R:   (   R6   R<   R=   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyR   q   s    (   t   __name__t
   __module__R7   R8   R9   R;   R>   R   (    (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyR0   a   s   					t   fdst	   FDCheckerc           B  s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C  s3   t  j d k r+ d d  l } | j ƒ  j ƒ  Sd Sd  S(   Nt   posixiÿÿÿÿi    (   t   osR3   t   psutilt   Processt   num_fds(   R6   RE   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyR;   w   s    c         C  s
   | | k S(   N(    (   R6   R<   R=   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyR>      s    c         C  s   d | | S(   Ns   leaked %d file descriptor(s)(    (   R6   R<   R=   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyR   ‚   s    (   R?   R@   R;   R>   R   (    (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyRB   u   s   		t   memoryt   RSSMemoryCheckerc           B  s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C  s   d d  l  } | j ƒ  j ƒ  j S(   Niÿÿÿÿ(   RE   RF   t   memory_infot   rss(   R6   RE   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyR;   ˆ   s    c         C  s   | | d k S(   Ng    ÐcA(    (   R6   R<   R=   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyR>      s    c         C  s   d | | d S(   Ns   leaked %d MB of RSS memoryg    €„.A(    (   R6   R<   R=   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyR      s    (   R?   R@   R;   R>   R   (    (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyRI   †   s   		t   threadst   ActiveThreadsCheckerc           B  s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C  s   t  t j ƒ  ƒ S(   N(   R%   t	   threadingt	   enumerate(   R6   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyR;   –   s    c         C  s   | | k S(   N(    (   R6   R<   R=   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyR>   ™   s    c         C  s6   | | } | s t  ‚ d t | ƒ t | d t ƒf S(   Ns   leaked %d Python threads: %st   key(   R1   t   lenR   t   str(   R6   R<   R=   t   leaked(    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyR   œ   s    
(   R?   R@   R;   R>   R   (    (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyRM   ”   s   		t   _ChildProcesst   pidR3   t   cmdlinec           B  s   e  Z e d  „  ƒ Z RS(   c         C  s   |  | j  | j ƒ  | j ƒ  ƒ S(   N(   RU   R3   RV   (   R2   t   p(    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyt   from_process¥   s    (   R?   R@   t   classmethodRX   (    (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyRT   ¢   s   t	   processest   ChildProcessesCheckerc           B  s#   e  Z d  „  Z d „  Z d „  Z RS(   c      	   C  s	  d d  l  } i  } | j ƒ  } xä | j d t ƒ D]Ð } y³ | j ƒ  ¡ | j ƒ  | j k rÁ t j j	 | j
 ƒ  t j ƒ rÁ | j ƒ  } t d „  | Dƒ ƒ r¢ w1 n  t d „  | Dƒ ƒ rÁ w1 qÁ n  t j | ƒ | | j | j ƒ  f <Wd  QXWq1 | j k
 r q1 Xq1 W| S(   Niÿÿÿÿt	   recursivec         s  s   |  ] } | j  d  ƒ Vq d S(   s2   from multiprocessing.semaphore_tracker import mainN(   t
   startswith(   R   t   a(    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pys	   <genexpr>º   s   c         s  s   |  ] } | j  d  ƒ Vq d S(   s+   from multiprocessing.forkserver import mainN(   R]   (   R   R^   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pys	   <genexpr>Â   s   (   RE   RF   t   childrent   Truet   oneshott   ppidRU   RD   t   patht   samefilet   exet   syst
   executableRV   t   anyRT   RX   t   create_timet   NoSuchProcess(   R6   RE   R_   RW   t   cRV   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyR;   ¬   s(    3	,c         C  s   t  | ƒ t  | ƒ k S(   N(   R%   (   R6   R<   R=   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyR>   Í   s    c         C  s   t  | ƒ t  | ƒ } | s" t ‚ g  } x7 t | ƒ D]) } | | } | j d j d | ƒ ƒ q5 Wd t | ƒ d j | ƒ f S(   Ns7     - pid={p.pid}, name={p.name!r}, cmdline={p.cmdline!r}RW   s   leaked %d processes:
%ss   
(   R%   R1   R   t   appendR   RQ   R   (   R6   R<   R=   RS   t	   formattedRP   RW   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyR   Ð   s    
(   R?   R@   R;   R>   R   (    (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyR[   ª   s   	!	t   tracemalloct   TracemallocMemoryCheckerc           B  s>   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C  s   d d  l  a  d  S(   Niÿÿÿÿ(   Rn   (   R6   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyt   __init__Þ   s    c         C  s   t  j d ƒ d  S(   Ni   (   Rn   t   start(   R6   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyR7   â   s    c         C  s   t  j ƒ  d  S(   N(   Rn   t   stop(   R6   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyR8   å   s    c         C  s4   d d  l  } | j ƒ  \ } } | j ƒ  } | | f S(   Niÿÿÿÿ(   Rn   t   get_traced_memoryt   take_snapshot(   R6   Rn   t   currentt   peakt   snap(    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyR;   è   s    c         C  s   | d | d d k S(   Ni    g    €„.A(    (   R6   R<   R=   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyR>   ï   s    c         C  sð   | \ } } | \ } } | j  | d ƒ } d } d }	 g  }
 |
 d | | d g 7}
 x‹ | |  D] } | j pu | j } | |	 k  rˆ Pn  | j p— | j } |
 d | d | f g 7}
 |
 g  | j j ƒ  D] } d | ^ qÈ 7}
 q` Wd j |
 ƒ S(	   Nt	   tracebacki   g     jAs&   leaked %.1f MB of traced Python memoryg    €„.As"     - leaked %.1f MB in %d calls at:s       s   
(   t
   compare_tot	   size_difft   sizet
   count_difft   countRx   R   R   (   R6   R<   R=   t   bytes_beforet   snap_beforet   bytes_aftert
   snap_aftert   difft   ndifft   min_size_difft   linest   statRz   R}   t   line(    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyR   ò   s"    .(   R?   R@   Rp   R7   R8   R;   R>   R   (    (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyRo   Ü   s   					R'   c           B  sÔ   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z	 e
 j d e ƒ d „  ƒ Z e
 j d e ƒ d	 „  ƒ Z e
 j d e d
 e ƒ d „  ƒ Z e
 j d e d
 e ƒ d „  ƒ Z e
 j d „  ƒ Z RS(   c         C  s^   | |  _  | |  _ | |  _ | |  _ i  |  _ i  |  _ i  |  _ t j t	 ƒ |  _
 t |  _ d  S(   N(   R   R   R    R!   t   skip_checkerst   countersR   t   collectionst   defaultdictR%   t   outcomesR   t	   _retrying(   R6   R   R   R    R!   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyRp   
  s    							c         C  s   t  j ƒ  d  S(   N(   t   gct   collect(   R6   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyt   cleanup  s    c         C  s5   g  |  j  D]' } | |  j j | d ƒ k r
 | ^ q
 S(   N(    (   R   Rˆ   t   get(   R6   t   nodeidRk   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyt   checks_for_item  s    c         C  s,   g  |  j  | ƒ D] } | | j ƒ  f ^ q S(   N(   R“   R;   (   R6   R’   Rk   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyR;   "  s    c         C  sx   x! |  j  | ƒ D] } | j ƒ  q WxM |  j | ƒ D]< \ } } | d  k	 sR t ‚ |  j | | j | d  f ƒ q4 Wd  S(   N(   R“   R7   R;   t   NoneR1   R‰   Rl   (   R6   R’   R-   R<   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyt   measure_before_test&  s
    c   
        sT  ˆ j  ˆ  } | s t ‚ | d h k r, d  S‡  ‡ f d †  } t j ƒ  } | ˆ j } | ƒ  } | r  ˆ j ƒ  x! | D] \ } } } | j ƒ  qw W| ƒ  } n  x` | rt j ƒ  | k  rt j d ƒ ˆ j ƒ  x! | D] \ } } } | j ƒ  qÙ W| ƒ  } q£ W| r| ˆ j ˆ  <n ˆ j j ˆ  d  ƒ x! ˆ j
 ˆ  ƒ D] }	 |	 j ƒ  q<Wd  S(   Nt   passedc            s˜   g  }  x‹ ˆ j  ˆ  ƒ D]z \ } } | d  k	 s4 t ‚ ˆ j ˆ  | } | d \ } } | | f | d <| j | | ƒ r |  j | | | f ƒ q q W|  S(   Niÿÿÿÿ(   R;   R”   R1   R‰   R>   Rl   (   R   R-   R=   Rk   R<   t   _(   R’   R6   (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyt   run_measurements4  s    gš™™™™™¹?(   RŒ   R1   t   timeR   R   R9   t   sleepR   t   popR”   R“   R8   (
   R6   R’   RŒ   R˜   t   t1t   deadlineR   Rk   R—   R-   (    (   R’   R6   s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyt   measure_after_test-  s0    	

c   	        sÁ   ‡  ‡ f d †  } ˆ  j  } |  j j | ƒ } | r½ t |  _ zq y% x t |  j ƒ D] } | ƒ  qR WWn3 t k
 r™ } t d ƒ d d  l	 } | j
 ƒ  n X|  j j | ƒ } Wd  t |  _ Xn  | S(   Nc            s6   d d l  m }  ˆ  j ƒ  |  ˆ  d ˆ d t ƒ} d  S(   Niÿÿÿÿ(   t   runtestprotocolt   nextitemt   log(   t   _pytest.runnerRŸ   t   _initrequestR   (   RŸ   t   reports(   t   itemR    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyt   run_test_againX  s    
s&   --- Exception when re-running test ---iÿÿÿÿ(   R’   R   R‘   R`   R   t   rangeR!   t	   Exceptiont   printRx   t	   print_excR   (	   R6   R¥   R    R¦   R’   R   t   it   eRx   (    (   R¥   R    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyt   maybe_retryW  s     		 
t   hookwrapperc         #  sß   |  j  sÖ | j } | |  j k s' t ‚ d „  |  j Dƒ |  j | <| j d ƒ } | d  k	 rÖ t t | j	 ƒ t t
 ƒ ƒ } | r— t d | f ƒ ‚ n  t d „  | j	 Dƒ ƒ ‰  ‡  f d †  |  j Dƒ |  j | <qÖ n  d  Vd  S(   Nc         S  s   i  |  ] } g  | “ q S(    (    (   R   Rk   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pys
   <dictcomp>ƒ  s   	 t   leakings)   pytest.mark.leaking: unknown resources %rc         s  s   |  ] } t  | Vq d  S(   N(   R   (   R   R^   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pys	   <genexpr>Œ  s    c           s%   h  |  ] } t  | ˆ  ƒ r | ’ q S(    (   t
   isinstance(   R   Rk   (   t   classes(    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pys	   <setcomp>Ž  s   	 (   R   R’   R‰   R1   R   t
   get_markerR”   R   R%   t   argsR   R&   t   tupleRˆ   (   R6   R¥   R    R’   R¯   R+   (    (   R±   s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyt   pytest_runtest_protocol~  s    		&c         c  s   |  j  | j ƒ d  Vd  S(   N(   R•   R’   (   R6   R¥   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyt   pytest_runtest_setup“  s    t   trylastc         c  sP   d  V|  j  | j ƒ |  j sL |  j | ƒ } | rL |  j rL t j ƒ  qL n  d  S(   N(   Rž   R’   R   R­   R    t   pytestt   fail(   R6   R¥   R   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyt   pytest_runtest_teardown˜  s    	c   	      c  sà   | j  } |  j | } | j | j ƒ d  V} |  j sÜ | j d k rÜ |  j j | j  ƒ } | rÙ |  j rÆ | j	 d ƒ d | _ d j
 g  | D]+ \ } } } d | | j | | ƒ f ^ qŒ ƒ | _ qÖ | j	 d	 ƒ qÙ qÜ n  d  S(
   Nt   teardownt   failedt   Lt   LEAKEDs   
s   %s %sRS   (   R¼   R½   R¾   (   RS   R½   R¾   (   R’   RŒ   t   addt   outcomeR   t   whenR   R‘   R    t   force_resultR   R   t   longrepr(	   R6   t   reportR’   RŒ   RÀ   R   R-   R<   R=   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyt   pytest_report_teststatus¢  s    				>c   
      C  s“   | } | j  d ƒ } | r | j d d ƒ xa | D]V } | j } xD |  j | D]5 \ } } }	 | j d | j | j | |	 ƒ f ƒ qO Wq2 Wn  d  S(   NRS   t   =s   RESOURCE LEAKSs   %s %s(   t
   getreportst	   write_sepR’   R   R‡   R   (
   R6   t   terminalreportert
   exitstatust   trRS   t   repR’   R-   R<   R=   (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyt   pytest_terminal_summary¹  s    	N(   R?   R@   Rp   R   R“   R;   R•   Rž   R”   R­   R¸   t   hookimplR`   Rµ   R¶   Rº   RÅ   RÍ   (    (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyR'   	  s   						*'!
!(   RU   R3   RV   (   t   __doc__t
   __future__R    R   RŠ   RŽ   R™   RD   Rf   RN   R¸   R   R.   R   R5   t   objectR0   RB   RI   RM   t
   namedtupleRT   R[   Ro   R'   (    (    (    s?   lib/python2.7/site-packages/distributed/pytest_resourceleaks.pyt   <module>   s4   	.		
1,