a
    ze.6                     @   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 );global_function.<locals>.inner_function.<locals>.LocalClassN)__name__
__module____qualname__ r   r   )/usr/lib/python3.9/test/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    .inner_global_function.<locals>.inner_function2r   )r
   r   r   r   inner_global_function
   s    r   )r   )r   r   r   r   inner_function   s    'global_function.<locals>.inner_functionc                      s    S r	   r   r   r   r   r   <lambda>       !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 r	   r   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    FuncAttrsTest.setUp.<locals>.b)fir   r   )r   r   r   r   r   r   setUp   s
    FuncAttrsTest.setUpc                 C   sj   zt ||| W n |y"   Y n0 | d||f  zt|| W n |yV   Y n0 | 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                   @   s|   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d ZdS )FunctionPropertiesTestc                 C   s   |  | jjt d S r	   )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 r	   r   r   r   r   r   testB   r   z:FunctionPropertiesTest.test_copying___code__.<locals>.testr   )r(   r   __code__)r   r0   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   r3   globalsr&   AttributeError	TypeErrorr   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 r	   printr   r   r   r   fN   r   z2FunctionPropertiesTest.test___closure__.<locals>.f   r   cell__closure__)	rA   ZassertIsInstancetupler(   len	__class__r   r&   r7   r   r>   cr   r=   r   test___closure__L   s    z'FunctionPropertiesTest.test___closure__c                 C   s\   t d}| |jd t  }d}| jt|d |j W d    n1 sN0    Y  d S )Nr?   'shouldn't be able to read an empty cell)msg)typesCellTyper(   cell_contentsassertRaises
ValueError)r   Zcell_objrI   r   r   r   test_cell_newV   s    
z$FunctionPropertiesTest.test_cell_newc                    sB    fdd}z|j d j W n ty.   Y n0 | d d d S )Nc                      s   t   d S r	   r;   r   r=   r   r   r>   `   r   z1FunctionPropertiesTest.test_empty_cell.<locals>.fr   rH   r:   )rA   rL   rN   r    )r   r>   r   r=   r   test_empty_cell_   s    
z&FunctionPropertiesTest.test_empty_cellc                    s   d  fdd}|j }d|d _| |d jd | | d |  d |d `z|d j W n tyt   Y n0 | d | t |  W d    n1 s0    Y  | t t  W d    n1 s0    Y  d S )Nr:   c                      s    S r	   r   r   r=   r   r   r>   k   r   z/FunctionPropertiesTest.test_set_cell.<locals>.f	   r   rH   )	rA   rL   r(   rN   r    rM   	NameErrorUnboundLocalErrorr<   rE   r   r=   r   test_set_celli   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   rF   dr   r*   zdef f(): pass
f.__name____builtins__r   )	r(   r   r   r&   r8   execr   r   r7   )r   sr   r   r   test___name__}   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 )Nr   r   r   r   r   r   r   r   rF   rU   r   r*   )	r(   r   r   r   r   r   r   r&   r8   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}|||||fD ]}|  t|jtj qL|  | d |  | d |j|_|  |j|j |  | d z|j|_W n ty   Y n0 | d z|j|_W n ty   Y n0 | d d S )N)r*      c                   S   s   d S r	   r   r   r   r   r   r      r   z/FunctionPropertiesTest.test___code__.<locals>.ac                   S   s   dS )Nr:   r   r   r   r   r   r      r   z/FunctionPropertiesTest.test___code__.<locals>.bc                      s    S r	   r   r   )num_oner   r   rF      r   z/FunctionPropertiesTest.test___code__.<locals>.cc                      s    S r	   r   r   )num_twor   r   rU      r   z/FunctionPropertiesTest.test___code__.<locals>.dc                      s    fS r	   r   r   r\   r]   r   r   e   r   z/FunctionPropertiesTest.test___code__.<locals>.er*   r[   zC__code__ with different numbers of free vars should not be possible)r(   typer1   rJ   CodeTyperN   r    )r   r   r   rF   rU   r_   funcr   r^   r   test___code__   s.    
z$FunctionPropertiesTest.test___code__c                 C   s*   |  | jjd  | j`|  | jjd  d S r	   )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  z
|  W n ty   Y n0 | d d S )Nc                 S   s   | | S r	   r   r   r   r   r   r   
first_func   s    zAFunctionPropertiesTest.test_func_default_args.<locals>.first_funcr?   r4   c                 S   s   | | S r	   r   rf   r   r   r   second_func   s    zBFunctionPropertiesTest.test_func_default_args.<locals>.second_func)r?   r4   r      r[   zE__defaults__ does not update; deleting it does not remove requirement)r?   r4   )r(   rd   r8   r    )r   rg   rh   r   r   r   test_func_default_args   s     

z-FunctionPropertiesTest.test_func_default_argsN)r   r   r   r)   r-   r/   r2   r9   rG   rO   rP   rT   rY   rZ   rc   re   rj   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 )NrD   )r(   r   r   __self__rD   r   r&   r8   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   rn   r   r&   r7   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 )Nrl   )r(   r   r   rl   r&   r7   r   r   r   r   test___self__   s    z$InstancemethodAttrTest.test___self__c                 C   sn   t t| j| j_| | j t| j z| jjj W n tyJ   Y n0 | d | | jjddt d S )N4using unknown attributes should raise AttributeErrorunknown_attrr4   )	rJ   
MethodTypeidr   r(   rr   r7   r    r&   r   r   r   r   test___func___non_method   s    
z/InstancemethodAttrTest.test___func___non_methodN)r   r   r   rm   ro   rp   ru   r   r   r   r   rk      s   rk   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )ArbitraryFunctionAttrTestc                 C   sH   d| j _| | j jd zd| jj_W n ty8   Y n0 | d d S )Nr*   z0setting attributes on methods should raise error)r   r+   r(   r   r   r7   r    r   r   r   r   test_set_attr   s    z'ArbitraryFunctionAttrTest.test_set_attrc                 C   s,   z
| j `W n ty   Y n0 | d d S )Nz1deleting unknown attribute should raise TypeError)r   rr   r7   r    r   r   r   r   test_delete_unknown_attr  s
    
z2ArbitraryFunctionAttrTest.test_delete_unknown_attrc              	   C   s@   | j | jjfD ],}z
|j W n ty.   Y q0 | d qd S )Nrq   )r   r   r   Znon_existent_attrr7   r    )r   rb   r   r   r   test_unset_attr  s    
z)ArbitraryFunctionAttrTest.test_unset_attrN)r   r   r   rw   rx   ry   r   r   r   r   rv      s   
rv   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   r8   collectionsr|   r   r   rn   )r   r|   rU   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   r{   r5   r   r   r   rn   r(   r+   )r   rU   r   r   r   test_setting_dict_to_valid  s    
z,FunctionDictsTest.test_setting_dict_to_validc                 C   s,   z
| j `W n ty   Y n0 | d d S )Nz3deleting function dictionary should raise TypeError)r   r{   r8   r    r   r   r   r   test_delete___dict__.  s
    
z&FunctionDictsTest.test_delete___dict__c                 C   s   |  | jji  d S r	   )r(   r   r{   r   r   r   r   test_unassigned_dict6  s    z&FunctionDictsTest.test_unassigned_dictc                 C   s(   d}i }||| j < | || j  | d S )NzSome string)r   r(   )r   r$   rU   r   r   r   test_func_as_dict_key9  s    
z'FunctionDictsTest.test_func_as_dict_keyN)r   r   r   r~   r   r   r   r   r   r   r   r   rz     s
   rz   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   r   r   r   r   r&   r7   )r   Zdocstrr   r   r   test_set_docstring_attrA  s    
z-FunctionDocstringTest.test_set_docstring_attrc                 C   s"   d| j _| j `| | j jd  d S )NzThe docstring)r   r   r(   r   r   r   r   test_delete_docstringJ  s    z+FunctionDocstringTest.test_delete_docstringN)r   r   r   r   r   r   r   r   r   r   @  s   	r   c                    s    fdd}|  |j d S )z)Create a cell containing the given value.c                      s   t   d S r	   r;   r   r=   r   r   r>   R  s    zcell.<locals>.fr   rA   )r$   r>   r   r=   r   r@   P  s    r@   Tc                    s    fdd}| sd |j d S )zCreate an empty cell.c                      s   t   d S r	   r;   r   r=   r   r   r>   Y  s    zempty_cell.<locals>.fi  r   r   )emptyr>   r   r=   r   
empty_cellW  s    r   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 )Nr4   r   Zsaturdayig      BT)
assertTruer@   r   r   r   r   r   test_comparisond  s
    zCellTest.test_comparisonN)r   r   r   r   r   r   r   r   r   c  s   r   c                   @   s   e Zd Zdd ZdS )StaticMethodAttrsTestc                 C   s<   dd }t |}| |j|u  t|}| |j|u  d S )Nc                   S   s   d S r	   r   r   r   r   r   r>   r  s    z4StaticMethodAttrsTest.test_func_attribute.<locals>.f)classmethodr   rn   staticmethod)r   r>   rF   rX   r   r   r   test_func_attributeq  s
    z)StaticMethodAttrsTest.test_func_attributeN)r   r   r   r   r   r   r   r   r   p  s   r   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 | g djjd	 | d
dijjd d S )Nr   rC   timezdict.fromkeyszfloat.__getformat__zstr.maketranszbytes.maketrans)r?   r4   r   zlist.appendZfooZbarzdict.pop)r   r(   rC   r   dictfromkeysfloat__getformat__str	maketransbytesappendpop)r   r   r   r   r   test_builtin__qualname__  s    
z6BuiltinFunctionPropertiesTest.test_builtin__qualname__N)r   r   r   r   r   r   r   r   r   |  s   r   __main__)T)rJ   Zunittestr   ZTestCaser   r'   rk   rv   rz   r   r@   r   r   r   r   r   mainr   r   r   r   <module>   s     0)
