B
    u9a*  ใ               @   sถ   d Z ddlZddlZddlmZ G dd dZG dd deZG dd	 d	eZG d
d deeZG dd deZ	G dd de	Z
G dd deZG dd dejZedkrฒe ก  dS )z9Unit tests for zero-argument super() & related machinery.้    N)ฺcheck_warningsc               @   s    e Zd Zdd Zedd ZdS )ฺAc             C   s   dS )Nr   ฉ )ฺselfr   r   ๚ /usr/lib/python3.7/test_super.pyฺf	   s    zA.fc             C   s   | dfS )Nr   r   )ฺclsr   r   r   ฺcm   s    zA.cmN)ฺ__name__ฺ
__module__ฺ__qualname__r   ฺclassmethodr	   r   r   r   r   r      s   r   c                   s,   e Zd Z fddZe fddZ  ZS )ฺBc                s   t   ก d S )Nr   )ฺsuperr   )r   )ฺ	__class__r   r   r      s    zB.fc                s   | t   ก dfS )Nr   )r   r	   )r   )r   r   r   r	      s    zB.cm)r
   r   r   r   r   r	   ฺ__classcell__r   r   )r   r   r      s   r   c                   s,   e Zd Z fddZe fddZ  ZS )ฺCc                s   t   ก d S )Nr   )r   r   )r   )r   r   r   r      s    zC.fc                s   | t   ก dfS )Nr   )r   r	   )r   )r   r   r   r	      s    zC.cm)r
   r   r   r   r   r	   r   r   r   )r   r   r      s   r   c                   s(   e Zd Z fddZ fddZ  ZS )ฺDc                s   t   ก d S )Nr   )r   r   )r   )r   r   r   r      s    zD.fc                s   | t   ก dfS )Nr   )r   r	   )r   )r   r   r   r	       s    zD.cm)r
   r   r   r   r	   r   r   r   )r   r   r      s   r   c               @   s   e Zd ZdS )ฺEN)r
   r   r   r   r   r   r   r   #   s   r   c               @   s   e Zd ZejZdS )ฺFN)r
   r   r   r   r   r   r   r   r   r   &   s   r   c               @   s   e Zd ZdS )ฺGN)r
   r   r   r   r   r   r   r   )   s   r   c                   sะ   e Zd Z fddZdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
 fddZdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Z fd'd(Zd)d* Z fd+d,Z  ZS )-ฺ	TestSuperc                s   t  d S )N)r   )r   )r   r   r   ฺtearDown/   s    zTestSuper.tearDownc             C   s   |   t  ก dก d S )NฺABCD)ฺassertEqualr   r   )r   r   r   r   ฺtest_basics_working4   s    zTestSuper.test_basics_workingc             C   s   |   t t กdก d S )Nr   )r   r   r   )r   r   r   r   ฺtest_class_getattr_working7   s    z$TestSuper.test_class_getattr_workingc             C   s*   |   t  ก dก |   t t กdก d S )Nr   )r   r   r   )r   r   r   r   ฺ!test_subclass_no_override_working:   s    z+TestSuper.test_subclass_no_override_workingc             C   s*   |   t  ก dก |   t t กdก d S )Nr   )r   r   r   )r   r   r   r   ฺ$test_unbound_method_transfer_working>   s    z.TestSuper.test_unbound_method_transfer_workingc             C   sX   |   t ก tdfก |   t  ก tdfก |   t ก tdfก |   t  ก tdfก d S )Nr   )r   r   r	   r   )r   r   r   r   ฺ test_class_methods_still_workingB   s    z*TestSuper.test_class_methods_still_workingc             C   s\   t  }|  | ก |t t t dfdfdfdfก t }|  | ก |tttdfdfdfdfก d S )Nr   r   r   r   )r   r   r	   r   )r   ฺdฺer   r   r   ฺ#test_super_in_class_methods_workingH   s    &z-TestSuper.test_super_in_class_methods_workingc             C   s&   G dd dt }|  |  ก dก d S )Nc                   s   e Zd Z fddZ  ZS )z,TestSuper.test_super_with_closure.<locals>.Ec                s    fdd}t   ก d S )Nc                  s     d S )Nr   r   )r   r   r   ฺnestedS   s    z>TestSuper.test_super_with_closure.<locals>.E.f.<locals>.nestedr   )r   r   )r   r#   )r   )r   r   r   R   s    z.TestSuper.test_super_with_closure.<locals>.E.f)r
   r   r   r   r   r   r   )r   r   r   Q   s   r   ZAE)r   r   r   )r   r   r   r   r   ฺtest_super_with_closureN   s    z!TestSuper.test_super_with_closurec          	      sๆ   G dd dt }| }|  | ก dก |  |jdก G  fddd}|  |jt| ก |  tก}t	dt
 i  W d Q R X |  t|jtก G dd d}|  t
 d d	ก t
 d= |  d|jก G  fd
dd}|   d	ก d S )Nc                   s    e Zd Z fddZdZ  ZS )z7TestSuper.test_various___class___pathologies.<locals>.Xc                s
   t   ก S )N)r   r   )r   )r   r   r   r   \   s    z9TestSuper.test_various___class___pathologies.<locals>.X.fi  )r
   r   r   r   r   r   r   r   )r   r   ฺX[   s   r%   r   i  c                   s    e Zd ZZ fddZ  ZS )z7TestSuper.test_various___class___pathologies.<locals>.Xc                  s     d S )Nr   r   )r   r   r   r   d   s    z9TestSuper.test_various___class___pathologies.<locals>.X.f)r
   r   r   ฺxr   r   r   )r   )r   r   r%   b   s   zYclass X:
                __class__
                def f():
                    __class__c                   s    e Zd Zda fddZ  ZS )z7TestSuper.test_various___class___pathologies.<locals>.X้*   c                  s     d S )Nr   r   )r   r   r   r   p   s    z9TestSuper.test_various___class___pathologies.<locals>.X.f)r
   r   r   r   r   r   r   r   )r   r   r%   m   s   r   r'   c                   s    e Zd Zd fddZ  ZS )z7TestSuper.test_various___class___pathologies.<locals>.Xr'   c                  s     d S )Nr   r   )r   r   r   r   x   s    z9TestSuper.test_various___class___pathologies.<locals>.X.f)r
   r   r   r   r   r   )r   )r   r   r%   u   s   )r   r   r   r   ฺassertIsr&   ฺtypeฺassertRaisesฺ	NameErrorฺexecฺglobalsZ	exceptionฺassertNotInฺ__dict__)r   r%   r&   r!   )r   r   r   ฺ"test_various___class___pathologiesY   s     z,TestSuper.test_various___class___pathologiesc             C   s$   G dd d}|   |  ก |ก d S )Nc                   s   e Zd Z fddZ  ZS )z2TestSuper.test___class___instancemethod.<locals>.Xc                s    S )Nr   )r   )r   r   r   r      s    z4TestSuper.test___class___instancemethod.<locals>.X.f)r
   r   r   r   r   r   r   )r   r   r%   ~   s   r%   )r(   r   )r   r%   r   r   r   ฺtest___class___instancemethod|   s    z'TestSuper.test___class___instancemethodc             C   s"   G dd d}|   | ก |ก d S )Nc                   s    e Zd Ze fddZ  ZS )z/TestSuper.test___class___classmethod.<locals>.Xc                s    S )Nr   )r   )r   r   r   r      s    z1TestSuper.test___class___classmethod.<locals>.X.f)r
   r   r   r   r   r   r   r   )r   r   r%      s   r%   )r(   r   )r   r%   r   r   r   ฺtest___class___classmethod   s    z$TestSuper.test___class___classmethodc             C   s"   G dd d}|   | ก |ก d S )Nc                   s    e Zd Ze fddZ  ZS )z0TestSuper.test___class___staticmethod.<locals>.Xc                  s    S )Nr   r   )r   r   r   r      s    z2TestSuper.test___class___staticmethod.<locals>.X.f)r
   r   r   ฺstaticmethodr   r   r   r   )r   r   r%      s   r%   )r(   r   )r   r%   r   r   r   ฺtest___class___staticmethod   s    z%TestSuper.test___class___staticmethodc                s:   d  G  fdddt }G dd d|d}|   |ก d S )Nc                   s   e Zd Z fddZ  ZS )z*TestSuper.test___class___new.<locals>.Metac                s   t   | |||ก}| ก |S )N)r   ฺ__new__r   )r   ฺnameฺbasesฺ	namespacer   )r   ฺ
test_classr   r   r5      s    z2TestSuper.test___class___new.<locals>.Meta.__new__)r
   r   r   r5   r   r   )r9   )r   r   ฺMeta   s   r:   c                   s    e Zd Ze fddZ  ZS )z'TestSuper.test___class___new.<locals>.Ac                  s    S )Nr   r   )r   r   r   r       s    z)TestSuper.test___class___new.<locals>.A.f)r
   r   r   r3   r   r   r   r   )r   r   r      s   r   )ฺ	metaclass)r)   r(   )r   r:   r   r   )r9   r   ฺtest___class___new   s    zTestSuper.test___class___newc          	      s   d  G  fdddt }t $}t dtก G dd d|d}W d Q R X |  |jg ก |  |d ก t dd }|  | ก |ก d S )	Nc                   s   e Zd Z fddZdS )z.TestSuper.test___class___delayed.<locals>.Metac                s   | d S )Nr   )r   r6   r7   r8   )ฺtest_namespacer   r   r5   ซ   s    z6TestSuper.test___class___delayed.<locals>.Meta.__new__N)r
   r   r   r5   r   )r=   r   r   r:   ช   s   r:   ฺalwaysc                   s    e Zd Ze fddZ  ZS )z+TestSuper.test___class___delayed.<locals>.Ac                  s    S )Nr   r   )r   r   r   r   ด   s    z-TestSuper.test___class___delayed.<locals>.A.f)r
   r   r   r3   r   r   r   r   )r   r   r   ณ   s   r   )r;   r   r   )r)   r   ฺwarningsฺsimplefilterฺDeprecationWarningr   r(   r   )r   r:   ฺwr   r   r   )r=   r   ฺtest___class___delayedฆ   s    z TestSuper.test___class___delayedc                s:   d  G dd dt }G  fddd|d}|   |ก d S )Nc                   s   e Zd Z fddZ  ZS )z*TestSuper.test___class___mro.<locals>.Metac                s   | j d   t  ก S )Nr   )r/   r   ฺmro)r   )r   r   r   rD   ร   s    z.TestSuper.test___class___mro.<locals>.Meta.mro)r
   r   r   rD   r   r   r   )r   r   r:   ย   s   r:   c                   s   e Zd Z fddZ  ZS )z'TestSuper.test___class___mro.<locals>.Ac                  s    d S )Nr   r   )r   r9   r   r   r   ษ   s    z)TestSuper.test___class___mro.<locals>.A.f)r
   r   r   r   r   r   )r9   )r   r   r   ศ   s   r   )r;   )r)   r(   )r   r:   r   r   )r9   r   ฺtest___class___mroพ   s    zTestSuper.test___class___mroc          	      s   G  fdddt }d  G dd d|d}|  d ก d  G dd d|d} d }|jj}|  t|d	ก |  ||d
 ก |  tก |j	 W d Q R X d S )Nc                   s   e Zd Z fddZ  ZS )z=TestSuper.test___classcell___expected_behaviour.<locals>.Metac                s   |  ก t  | |||กS )N)ฺcopyr   r5   )r   r6   r7   r8   )r   ฺnamespace_snapshotr   r   r5   า   s    zETestSuper.test___classcell___expected_behaviour.<locals>.Meta.__new__)r
   r   r   r5   r   r   )rG   )r   r   r:   ั   s   r:   c               @   s   e Zd ZdS )zHTestSuper.test___classcell___expected_behaviour.<locals>.WithoutClassRefN)r
   r   r   r   r   r   r   ฺWithoutClassRefฺ   s   rH   )r;   r   c                   s   e Zd Z fddZ  ZS )zETestSuper.test___classcell___expected_behaviour.<locals>.WithClassRefc                s    S )Nr   )r   )r   r   r   r   ใ   s    zGTestSuper.test___classcell___expected_behaviour.<locals>.WithClassRef.f)r
   r   r   r   r   r   r   )r   r   ฺWithClassRefโ   s   rI   ้   r   )
r)   r.   r   ฺ__closure__r   ฺlenr(   r*   ฺAttributeErrorr   )r   r:   rH   rI   Z
class_cellZmethod_closurer   )rG   r   ฺ%test___classcell___expected_behaviourฯ   s    z/TestSuper.test___classcell___expected_behaviourc          
   C   sๆ   G dd dt }t $}t dtก G dd d|d}W d Q R X |  |jg ก dtf}t|$ t dtก G dd	 d	|d}W d Q R X |  |  ก |ก t ก : t d
tก |  	tก G dd	 d	|d}W d Q R X W d Q R X d S )Nc                   s   e Zd Z fddZ  ZS )z2TestSuper.test___classcell___missing.<locals>.Metac                s   |  dd ก t  | |||กS )Nr   )ฺpopr   r5   )r   r6   r7   r8   )r   r   r   r5   ๓   s    z:TestSuper.test___classcell___missing.<locals>.Meta.__new__)r
   r   r   r5   r   r   r   )r   r   r:   ๒   s   r:   r>   c               @   s   e Zd ZdS )z=TestSuper.test___classcell___missing.<locals>.WithoutClassRefN)r
   r   r   r   r   r   r   rH   ๚   s   rH   )r;   z+__class__ not set.*__classcell__ propagatedc                   s   e Zd Z fddZ  ZS )z:TestSuper.test___classcell___missing.<locals>.WithClassRefc                s    S )Nr   )r   )r   r   r   r   
  s    z<TestSuper.test___classcell___missing.<locals>.WithClassRef.f)r
   r   r   r   r   r   r   )r   r   rI   	  s   rI   ฺerrorc                   s   e Zd Z fddZ  ZS )z:TestSuper.test___classcell___missing.<locals>.WithClassRefc                s    S )Nr   )r   )r   r   r   r     s    z<TestSuper.test___classcell___missing.<locals>.WithClassRef.f)r
   r   r   r   r   r   r   )r   r   rI     s   )
r)   r   r?   r@   rA   r   r(   r   ฺcatch_warningsr*   )r   r:   rB   rH   Zexpected_warningrI   r   r   r   ฺtest___classcell___missing๎   s    

z$TestSuper.test___classcell___missingc             C   sn   G dd dt }xXd ddt fD ]F}| j|d0 |  tก G dd d||d}W d Q R X W d Q R X q W d S )	Nc                   s   e Zd Z fddZ  ZS )z4TestSuper.test___classcell___overwrite.<locals>.Metac                s   ||d< t   | |||กS )Nr   )r   r5   )r   r6   r7   r8   ฺcell)r   r   r   r5     s    z<TestSuper.test___classcell___overwrite.<locals>.Meta.__new__)r
   r   r   r5   r   r   r   )r   r   r:     s   r:   r   ฺ )ฺbad_cellc               @   s   e Zd ZdS )z1TestSuper.test___classcell___overwrite.<locals>.AN)r
   r   r   r   r   r   r   r   "  s   r   )r;   rS   )r)   ฺobjectZsubTestr*   ฺ	TypeError)r   r:   rU   r   r   r   r   ฺtest___classcell___overwrite  s
    z&TestSuper.test___classcell___overwritec          	   C   s<   G dd dt }|  tก G dd d|d}W d Q R X d S )Nc                   s   e Zd Z fddZ  ZS )z5TestSuper.test___classcell___wrong_cell.<locals>.Metac                s"   t   | |||ก} tdd|}| S )Nr   r   )r   r5   r)   )r   r6   r7   r8   r   )r   r   r   r5   )  s    z=TestSuper.test___classcell___wrong_cell.<locals>.Meta.__new__)r
   r   r   r5   r   r   r   )r   r   r:   (  s   r:   c                   s   e Zd Z fddZ  ZS )z2TestSuper.test___classcell___wrong_cell.<locals>.Ac                s    S )Nr   )r   )r   r   r   r   0  s    z4TestSuper.test___classcell___wrong_cell.<locals>.A.f)r
   r   r   r   r   r   r   )r   r   r   /  s   r   )r;   )r)   r*   rW   )r   r:   r   r   r   r   ฺtest___classcell___wrong_cell%  s    z'TestSuper.test___classcell___wrong_cellc                sT    fdd}|   t|ก  fdd}|   t|d ก G dd d}|   t| jก d S )Nc                  s
   t   d S )N)r   r   )r   r   r   r   4  s    z.TestSuper.test_obscure_super_errors.<locals>.fc                s   ~ t   d S )N)r   )r&   )r   r   r   r   7  s    c                   s   e Zd Z fddZ  ZS )z.TestSuper.test_obscure_super_errors.<locals>.Xc                s    t   d S )N)r   )r&   )r   r   r   r   <  s    z0TestSuper.test_obscure_super_errors.<locals>.X.f)r
   r   r   r   r   r   r   )r   r   r%   ;  s   r%   )r*   ฺRuntimeErrorr   )r   r   r%   )r   r   r   ฺtest_obscure_super_errors3  s    z#TestSuper.test_obscure_super_errorsc                s:   G dd d  fdd}| j d }|  t j|ก d S )Nc                   s   e Zd Z fddZ  ZS )z&TestSuper.test_cell_as_self.<locals>.Xc                s
   t   d S )N)r   )r   )r   r   r   ฺmethD  s    z+TestSuper.test_cell_as_self.<locals>.X.meth)r
   r   r   r\   r   r   r   )r   r   r%   C  s   r%   c                 s      fdd} | S )Nc                  s    S )Nr   r   )ฺkr   r   ฺgI  s    z1TestSuper.test_cell_as_self.<locals>.f.<locals>.gr   )r^   )r%   )r]   r   r   G  s    z&TestSuper.test_cell_as_self.<locals>.fr   )rK   r*   rW   r\   )r   r   ฺcr   )r%   r   ฺtest_cell_as_selfB  s    zTestSuper.test_cell_as_selfc                s.   t td}xtdD ]}t  |t|ก qW d S )Ng      ๐?i่  )r   ฺfloatฺrangeฺ__init__ฺint)r   ฺspฺi)r   r   r   ฺtest_super_init_leaksO  s    
zTestSuper.test_super_init_leaks)r
   r   r   r   r   r   r   r   r   r"   r$   r0   r1   r2   r4   r<   rC   rE   rN   rR   rX   rY   r[   r`   rg   r   r   r   )r   r   r   -   s,   #)r   ฺ__main__)ฺ__doc__Zunittestr?   Ztest.supportr   r   r   r   r   r   r   r   ZTestCaser   r
   ฺmainr   r   r   r   ฺ<module>   s     .