a
    còfð  ã                   @   st   d Z ddlZddlZddlZddlZG dd„ dejƒZG dd„ deƒZe	edƒr`dd	„ Z
ejZnd
d	„ Z
dd„ ZdS )a[  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.
é    Nc                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚLocalTestResultzHA TestResult which forwards events to a parent object, except for Skips.c                 C   s   t j | ¡ || _d S ©N)ÚunittestÚ
TestResultÚ__init__Úparent_result)Úselfr   © r	   úf/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/google/protobuf/internal/testing_refleaks.pyr      s    zLocalTestResult.__init__c                 C   s   | j  ||¡ d S r   )r   ÚaddError©r   ÚtestÚerrorr	   r	   r
   r      s    zLocalTestResult.addErrorc                 C   s   | j  ||¡ d S r   )r   Ú
addFailurer   r	   r	   r
   r   "   s    zLocalTestResult.addFailurec                 C   s   d S r   r	   )r   r   Úreasonr	   r	   r
   ÚaddSkip%   s    zLocalTestResult.addSkipN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r	   r	   r	   r
   r      s
   r   c                       s.   e Zd ZdZdZd‡ fdd„	Zdd„ Z‡  ZS )	ÚReferenceLeakCheckerMixinz:A mixin class for TestCase, which checks reference counts.é   Nc           
         s  t | | jƒ}t |ddƒ}t | ddƒ}|s,|r0d S tj ¡ | _tt| ƒj|d� tt| ƒj|d� d}t	|ƒ}d}g }t
|ƒ| jk rÒ|  ¡ }tt| ƒj|d� |  ¡ }	|	|k r¾|dk r¾|d7 }qtd}| |	| ¡ qtt|| ƒ z|  |dg| j ¡ W n$ t�y   | | t ¡ ¡ Y n0 d S )NZ__unittest_expecting_failure__F)Úresultr   é   é   )ÚgetattrZ_testMethodNameÚcopyregÚdispatch_tableÚcopyÚ_saved_pickle_registryÚsuperr   Úrunr   ÚlenÚNB_RUNSÚ_getRefcountsÚappendÚprintÚassertEqualÚ	Exceptionr   ÚsysÚexc_info)
r   r   Z
testMethodZexpecting_failure_methodZexpecting_failure_classZoldrefcountZlocal_resultZ
num_flakesZrefcount_deltasZnewrefcount©Ú	__class__r	   r
   r!   .   s4    
zReferenceLeakCheckerMixin.runc                 C   s8   t j ¡  t j | j¡ t ¡  t ¡  t ¡  t ¡ S r   )	r   r   ÚclearÚupdater   ÚgcZcollectr)   Úgettotalrefcount)r   r	   r	   r
   r$   Y   s    
z'ReferenceLeakCheckerMixin._getRefcounts)N)r   r   r   r   r#   r!   r$   Ú__classcell__r	   r	   r+   r
   r   )   s   +r   r0   c                 C   s(   t f| j }t| ƒ| j|t| jƒƒ}|S r   )r   Ú	__bases__Útyper   ÚdictÚ__dict__)Ú
test_classÚ	new_basesÚ	new_classr	   r	   r
   ÚTestCasef   s
    ÿr9   c                 C   s   | S r   r	   )r6   r	   r	   r
   r9   p   s    c                 C   s   ~ dd„ }|S )Nc                 S   s   | S r   r	   )Úfuncr	   r	   r
   ÚSameu   s    z&SkipReferenceLeakChecker.<locals>.Samer	   )r   r;   r	   r	   r
   ÚSkipReferenceLeakCheckers   s    r<   )r   r   r/   r)   r   r   r   Úobjectr   Úhasattrr9   Úskipr<   r	   r	   r	   r
   Ú<module>   s   
;
