ó
X¾÷Xc           @   sç   d  Z  d d l Z d d l Z y d d l Z Wn e k
 rM d d l Z n Xy d d l Z Wn e k
 r} d d l Z n Xd e j f d „  ƒ  YZ	 d e j
 f d „  ƒ  YZ e e d ƒ rÑ e Z e j Z n e j
 Z d „  Z d S(	   s[  A subclass of unittest.TestCase which checks for reference leaks.

To use:
- Use testing_refleak.BaseTestCase instead of unittest.TestCase
- Configure and compile Python with --with-pydebug

If sys.gettotalrefcount() is not available (because Python was built without
the Py_DEBUG option), then this module is a no-op and tests will run normally.
iÿÿÿÿNt   LocalTestResultc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   sH   A TestResult which forwards events to a parent object, except for Skips.c         C   s   t  j j |  ƒ | |  _ d  S(   N(   t   unittestt
   TestResultt   __init__t   parent_result(   t   selfR   (    (    sK   /tmp/pip-build-h1VYrz/protobuf/google/protobuf/internal/testing_refleaks.pyR   :   s    c         C   s   |  j  j | | ƒ d  S(   N(   R   t   addError(   R   t   testt   error(    (    sK   /tmp/pip-build-h1VYrz/protobuf/google/protobuf/internal/testing_refleaks.pyR   >   s    c         C   s   |  j  j | | ƒ d  S(   N(   R   t
   addFailure(   R   R   R   (    (    sK   /tmp/pip-build-h1VYrz/protobuf/google/protobuf/internal/testing_refleaks.pyR	   A   s    c         C   s   d  S(   N(    (   R   R   t   reason(    (    sK   /tmp/pip-build-h1VYrz/protobuf/google/protobuf/internal/testing_refleaks.pyt   addSkipD   s    (   t   __name__t
   __module__t   __doc__R   R   R	   R   (    (    (    sK   /tmp/pip-build-h1VYrz/protobuf/google/protobuf/internal/testing_refleaks.pyR    7   s
   			t   ReferenceLeakCheckerTestCasec           B   s)   e  Z d  Z d Z d d „ Z d „  Z RS(   sH   A TestCase which runs tests multiple times, collecting reference counts.i   c         C   s  t  j j ƒ  |  _ t t |  ƒ j d | ƒ t t |  ƒ j d | ƒ d } t | ƒ } g  } xY t |  j	 ƒ D]H } |  j
 ƒ  } t t |  ƒ j d | ƒ |  j
 ƒ  } | j | | ƒ ql W| |  f GHy |  j | d g |  j	 ƒ Wn' t k
 r
| j |  t j ƒ  ƒ n Xd  S(   Nt   resulti    (   t   copyregt   dispatch_tablet   copyt   _saved_pickle_registryt   superR   t   runR    t   ranget   NB_RUNSt   _getRefcountst   appendt   assertEqualt	   ExceptionR   t   syst   exc_info(   R   R   t   oldrefcountt   local_resultt   refcount_deltast   _t   newrefcount(    (    sK   /tmp/pip-build-h1VYrz/protobuf/google/protobuf/internal/testing_refleaks.pyR   M   s     c         C   sH   t  j j ƒ  t  j j |  j ƒ t j ƒ  t j ƒ  t j ƒ  t j ƒ  S(   N(	   R   R   t   cleart   updateR   t   gct   collectR   t   gettotalrefcount(   R   (    (    sK   /tmp/pip-build-h1VYrz/protobuf/google/protobuf/internal/testing_refleaks.pyR   g   s    


N(   R   R   R   R   t   NoneR   R   (    (    (    sK   /tmp/pip-build-h1VYrz/protobuf/google/protobuf/internal/testing_refleaks.pyR   H   s   R(   c         C   s   ~  d „  } | S(   Nc         S   s   |  S(   N(    (   t   func(    (    sK   /tmp/pip-build-h1VYrz/protobuf/google/protobuf/internal/testing_refleaks.pyt   Same|   s    (    (   R
   R+   (    (    sK   /tmp/pip-build-h1VYrz/protobuf/google/protobuf/internal/testing_refleaks.pyt   SkipReferenceLeakCheckerz   s    	(   R   R&   R   t   copy_regR   t   ImportErrort	   unittest2R   R   R    t   TestCaseR   t   hasattrt   BaseTestCaset   skipR,   (    (    (    sK   /tmp/pip-build-h1VYrz/protobuf/google/protobuf/internal/testing_refleaks.pyt   <module>'   s"   *	