B
    u9a5                 @   s   d dl Z d dlZdd ZG dd dejZG dd d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Z	dd Z
dddZG dd dejZG dd dejZG dd dejZedkre  dS )    Nc                  s   dd   fddS )Nc              S   s   G dd d} dd a | S )Nc               @   s   e Zd ZdS )z;global_function.<locals>.inner_function.<locals>.LocalClassN)__name__
__module____qualname__ r   r   $/usr/lib/python3.7/test_funcattrs.py
LocalClass   s   r   c              S   s   dd } | S )Nc               S   s   d S )Nr   r   r   r   r   inner_function2   s    z.inner_global_function.<locals>.inner_function2r   )r   r   r   r   inner_global_function
   s    r	   )r	   )r   r   r   r   inner_function   s    z'global_function.<locals>.inner_functionc                  s    S )Nr   r   )r
   r   r   <lambda>       z!global_function.<locals>.<lambda>r   r   r   )r
   r   global_function   s    	r   c               @   s   e Zd Zdd Zdd ZdS )FuncAttrsTestc             C   s.   G dd d}dd }| | _ || _|| _d S )Nc               @   s   e Zd Zdd ZdS )zFuncAttrsTest.setUp.<locals>.Fc             S   s   d S )Nr   )selfr   r   r   a   s    z FuncAttrsTest.setUp.<locals>.F.aN)r   r   r   r   r   r   r   r   F   s   r   c               S   s   dS )N   r   r   r   r   r   b   s    zFuncAttrsTest.setUp.<locals>.b)fir   r   )r   r   r   r   r   r   setUp   s
    zFuncAttrsTest.setUpc             C   sn   yt ||| W n |k
r$   Y nX | d||f  yt|| W n |k
rZ   Y nX | d|  d S )Nz!shouldn't be able to set %s to %rzshouldn't be able to del %s)setattrfaildelattr)r   objnamevalue
exceptionsr   r   r   cannot_set_attr   s    zFuncAttrsTest.cannot_set_attrN)r   r   r   r   r   r   r   r   r   r      s   
r   c               @   st   e Z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dd Zdd Zdd ZdS )FunctionPropertiesTestc             C   s   |  | jjt d S )N)assertEqualr   r   r   )r   r   r   r   test_module.   s    z"FunctionPropertiesTest.test_modulec             C   s@   d| j _| dt| j d d| jj_| dt| jjd d S )N   
known_attrz+set attributes not in dir listing of methodzEset attribute on function implementations, should show up in next dir)r   r"   ZassertIndirr   r   r   )r   r   r   r   test_dir_includes_correct_attrs1   s
    
z6FunctionPropertiesTest.test_dir_includes_correct_attrsc             C   s   dd }|  | j| d S )Nc               S   s   dS )zmy docstringr   r   r   r   r   r   	duplicate<   s    zJFunctionPropertiesTest.test_duplicate_function_equality.<locals>.duplicate)ZassertNotEqualr   )r   r%   r   r   r    test_duplicate_function_equality:   s    z7FunctionPropertiesTest.test_duplicate_function_equalityc             C   s2   dd }|  | d  | jj|_|  | d d S )Nc               S   s   d S )Nr   r   r   r   r   testB   r   z:FunctionPropertiesTest.test_copying___code__.<locals>.testr   )r   r   __code__)r   r'   r   r   r   test_copying___code__A   s    
z,FunctionPropertiesTest.test_copying___code__c             C   s,   |  | jjt  | | jddttf d S )N__globals__   )assertIsr   r*   globalsr   AttributeError	TypeError)r   r   r   r   test___globals__G   s    z'FunctionPropertiesTest.test___globals__c                sZ   d  fdd}|j }| |t | t|d | |d jjd | |d|t d S )N   c                  s   t   d S )N)printr   )r   r   r   fN   r   z2FunctionPropertiesTest.test___closure__.<locals>.f   r   cell__closure__)	r6   ZassertIsInstancetupler   len	__class__r   r   r.   )r   r3   cr   )r   r   test___closure__L   s    z'FunctionPropertiesTest.test___closure__c                sD    fdd}y|j d j W n tk
r0   Y nX | d d d S )Nc                  s   t   d S )N)r2   r   )r   r   r   r3   W   r   z1FunctionPropertiesTest.test_empty_cell.<locals>.fr   z'shouldn't be able to read an empty cellr1   )r6   cell_contents
ValueErrorr   )r   r3   r   )r   r   test_empty_cellV   s    
z&FunctionPropertiesTest.test_empty_cellc          	      s   d  fdd}|j }d|d _| |d jd | | d |  d |d `y|d j W n tk
rv   Y nX | d | t |  W d Q R X | t t  W d Q R X d S )Nr1   c                  s    S )Nr   r   )r   r   r   r3   b   r   z/FunctionPropertiesTest.test_set_cell.<locals>.f	   r   z'shouldn't be able to read an empty cell)	r6   r<   r   r=   r   ZassertRaises	NameErrorUnboundLocalErrorr2   )r   r3   r:   r   )r   r   test_set_cell`   s"    

z$FunctionPropertiesTest.test_set_cellc             C   s   |  | jjd d| j_|  | jjd d| j_|  | jjd | | jddt d}t|di i |  | jjjd | | jjddt d S )	Nr   r:   dr   r!   zdef f(): pass
f.__name____builtins__r   )	r   r   r   r   r/   execr   r   r.   )r   sr   r   r   test___name__t   s    z$FunctionPropertiesTest.test___name__c             C   s   |  | jjd |  tjjd |  tjd |  t jd |  t  jd |  t   jd |  tjd |  t jd d	| j_|  | jjd	 d
| j_|  | jjd
 | | jddt d S )NzFuncAttrsTest.setUp.<locals>.bzFuncAttrsTest.setUpr   z!global_function.<locals>.<lambda>z'global_function.<locals>.inner_functionz;global_function.<locals>.inner_function.<locals>.LocalClassr	   z.inner_global_function.<locals>.inner_function2r:   rC   r   r!   )	r   r   r   r   r   r   r	   r   r/   )r   r   r   r   test___qualname__   s     
z(FunctionPropertiesTest.test___qualname__c                s  d\ dd }dd } fdd}fdd	} fd
d}x*|||||gD ]}|  t|jtj qNW |  | d |  | d |j|_|  |j|j |  | d y|j|_W n tk
r   Y nX | d y|j|_W n tk
r   Y nX | d d S )N)r!      c               S   s   d S )Nr   r   r   r   r   r      r   z/FunctionPropertiesTest.test___code__.<locals>.ac               S   s   dS )Nr1   r   r   r   r   r   r      r   z/FunctionPropertiesTest.test___code__.<locals>.bc                  s    S )Nr   r   )num_oner   r   r:      r   z/FunctionPropertiesTest.test___code__.<locals>.cc                  s    S )Nr   r   )num_twor   r   rC      r   z/FunctionPropertiesTest.test___code__.<locals>.dc                  s    fS )Nr   r   )rJ   rK   r   r   e   r   z/FunctionPropertiesTest.test___code__.<locals>.er!   rI   zC__code__ with different numbers of free vars should not be possible)r   typer(   typesCodeTyper=   r   )r   r   r   r:   rC   rL   funcr   )rJ   rK   r   test___code__   s.    
z$FunctionPropertiesTest.test___code__c             C   s*   |  | jjd  | j`|  | jjd  d S )N)r   r   __defaults__)r   r   r   r   test_blank_func_defaults   s    z/FunctionPropertiesTest.test_blank_func_defaultsc             C   s   dd }ddd}|  |jd  |  |jd d|_|  |jd |  | d |  |dd	 |  |dd	d
 |`|  |jd  y
|  W n tk
r   Y nX | d d S )Nc             S   s   | | S )Nr   )r   r   r   r   r   
first_func   s    zAFunctionPropertiesTest.test_func_default_args.<locals>.first_funcr4   r+   c             S   s   | | S )Nr   )r   r   r   r   r   second_func   s    zBFunctionPropertiesTest.test_func_default_args.<locals>.second_func)r4   r+   r      rI   zE__defaults__ does not update; deleting it does not remove requirement)r4   r+   )r   rR   r/   r   )r   rT   rU   r   r   r   test_func_default_args   s     

z-FunctionPropertiesTest.test_func_default_argsN)r   r   r   r    r$   r&   r)   r0   r;   r>   rB   rG   rH   rQ   rS   rW   r   r   r   r   r   ,   s   	

r   c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
InstancemethodAttrTestc             C   s0   |  | jjjj| j | | jjd| jt d S )Nr9   )r   r   r   __self__r9   r   r   r/   )r   r   r   r   test___class__   s    z%InstancemethodAttrTest.test___class__c             C   s2   |  | jjj| jj | | jjd| jjt d S )N__func__)r   r   r   r[   r   r   r.   )r   r   r   r   test___func__   s    z$InstancemethodAttrTest.test___func__c             C   s.   |  | jjj| j | | jjd| jt d S )NrY   )r   r   r   rY   r   r.   )r   r   r   r   test___self__   s    z$InstancemethodAttrTest.test___self__c             C   sp   t t| j| j_| | j t| j y| jjj W n tk
rL   Y nX | d | | jjddt d S )Nz4using unknown attributes should raise AttributeErrorunknown_attrr+   )	rN   
MethodTypeidr   r   r^   r.   r   r   )r   r   r   r   test___func___non_method   s    
z/InstancemethodAttrTest.test___func___non_methodN)r   r   r   rZ   r\   r]   ra   r   r   r   r   rX      s   rX   c               @   s$   e Zd Zdd Zdd Zdd ZdS )ArbitraryFunctionAttrTestc             C   sJ   d| j _| | j jd yd| jj_W n tk
r:   Y nX | d d S )Nr!   z0setting attributes on methods should raise error)r   r"   r   r   r   r.   r   )r   r   r   r   test_set_attr   s    z'ArbitraryFunctionAttrTest.test_set_attrc             C   s.   y
| j `W n tk
r   Y nX | d d S )Nz1deleting unknown attribute should raise TypeError)r   r^   r.   r   )r   r   r   r   test_delete_unknown_attr   s
    
z2ArbitraryFunctionAttrTest.test_delete_unknown_attrc          	   C   sF   x@| j | jjgD ].}y
|j W n tk
r2   Y qX | d qW d S )Nz4using unknown attributes should raise AttributeError)r   r   r   Znon_existent_attrr.   r   )r   rP   r   r   r   test_unset_attr  s    
z)ArbitraryFunctionAttrTest.test_unset_attrN)r   r   r   rc   rd   re   r   r   r   r   rb      s   
rb   c               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )FunctionDictsTestc             C   sD   |  | jdd t ddlm} |ddi}|  | jjjd|t d S )N__dict__r   )UserDictr"   r!   )r   r   r/   collectionsrh   r   r   r[   )r   rh   rC   r   r   r   test_setting_dict_to_invalid  s    z.FunctionDictsTest.test_setting_dict_to_invalidc             C   s   ddi}|| j _| || j j || jj_| || jjjj | || jjj | | j jd | | j jd d | | jjjjd | | jjjd d S )Nr"   r!   )	r   rg   r,   r   r   r   r[   r   r"   )r   rC   r   r   r   test_setting_dict_to_valid  s    
z,FunctionDictsTest.test_setting_dict_to_validc             C   s.   y
| j `W n tk
r   Y nX | d d S )Nz3deleting function dictionary should raise TypeError)r   rg   r/   r   )r   r   r   r   test_delete___dict__%  s
    
z&FunctionDictsTest.test_delete___dict__c             C   s   |  | jji  d S )N)r   r   rg   )r   r   r   r   test_unassigned_dict-  s    z&FunctionDictsTest.test_unassigned_dictc             C   s(   d}i }||| j < | || j  | d S )NzSome string)r   r   )r   r   rC   r   r   r   test_func_as_dict_key0  s    
z'FunctionDictsTest.test_func_as_dict_keyN)r   r   r   rj   rk   rl   rm   rn   r   r   r   r   rf     s
   rf   c               @   s   e Zd Zdd Zdd ZdS )FunctionDocstringTestc             C   s`   |  | jjd  d}|| j_|| jj_|  | jj| |  | jjj| | | jjd|t d S )NzA test method that does nothing__doc__)r   r   rp   r   r   r   r   r.   )r   Zdocstrr   r   r   test_set_docstring_attr8  s    
z-FunctionDocstringTest.test_set_docstring_attrc             C   s"   d| j _| j `| | j jd  d S )NzThe docstring)r   rp   r   )r   r   r   r   test_delete_docstringA  s    z+FunctionDocstringTest.test_delete_docstringN)r   r   r   rq   rr   r   r   r   r   ro   7  s   	ro   c                s    fdd}|  |j d S )z)Create a cell containing the given value.c                  s   t   d S )N)r2   r   )r   r   r   r3   I  s    zcell.<locals>.fr   )r6   )r   r3   r   )r   r   r5   G  s    r5   Tc                s    fdd}| sd |j d S )zCreate an empty cell.c                  s   t   d S )N)r2   r   )r   r   r   r3   P  s    zempty_cell.<locals>.fi  r   )r6   )emptyr3   r   )r   r   
empty_cellN  s    rt   c               @   s   e Zd Zdd ZdS )CellTestc             C   sj   |  tdtdk  |  t tdk  |  t t k |  tdtdk |  tdt k d S )Nr+   r   Zsaturdayig      BT)
assertTruer5   rt   )r   r   r   r   test_comparison[  s
    zCellTest.test_comparisonN)r   r   r   rw   r   r   r   r   ru   Z  s   ru   c               @   s   e Zd Zdd ZdS )StaticMethodAttrsTestc             C   s<   dd }t |}| |j|k t|}| |j|k d S )Nc               S   s   d S )Nr   r   r   r   r   r3   i  s    z4StaticMethodAttrsTest.test_func_attribute.<locals>.f)classmethodrv   r[   staticmethod)r   r3   r:   rF   r   r   r   test_func_attributeh  s
    z)StaticMethodAttrsTest.test_func_attributeN)r   r   r   r{   r   r   r   r   rx   g  s   rx   c               @   s   e Zd Zdd ZdS )BuiltinFunctionPropertiesTestc             C   s   dd l }| tjd | |j jd | tjjd | tjjd | tj	jd | t
j	jd | dd	d
gjjd | ddijjd d S )Nr   r8   timezdict.fromkeyszfloat.__getformat__zstr.maketranszbytes.maketransr4   r+   r   zlist.appendZfooZbarzdict.pop)r}   r   r8   r   dictfromkeysfloat__getformat__str	maketransbytesappendpop)r   r}   r   r   r   test_builtin__qualname__w  s    
z6BuiltinFunctionPropertiesTest.test_builtin__qualname__N)r   r   r   r   r   r   r   r   r|   s  s   r|   __main__)T)rN   Zunittestr   ZTestCaser   r   rX   rb   rf   ro   r5   rt   ru   rx   r|   r   mainr   r   r   r   <module>   s     ')
