o
    HXhp;                     @   s   d dl Z 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roe  dS 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   =/opt/python-3.10.19/usr/lib/python3.10/test/test_funcattrs.py
LocalClass   s    r   c                  S   s   dd } | S )Nc                   S      d S Nr   r   r   r   r   inner_function2      .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 r
   r   r   r   r   r   <lambda>       !global_function.<locals>.<lambda>r   r   r   r   r   global_function   s   	r   c                   @      e Zd Zdd Zdd ZdS )FuncAttrsTestc                 C   s.   G dd d}dd }| | _ || _|| _d S )Nc                   @      e Zd Zdd ZdS )zFuncAttrsTest.setUp.<locals>.Fc                 S   r	   r
   r   selfr   r   r   a   r   z FuncAttrsTest.setUp.<locals>.F.aN)r   r   r   r   r   r   r   r   F       r   c                   S      dS )N   r   r   r   r   r   b   r   FuncAttrsTest.setUp.<locals>.b)fir   r!   )r   r   r!   r   r   r   setUp   s
   
FuncAttrsTest.setUpc                 C   sl   zt ||| W n	 |y   Y n
w | d||f  zt|| W n
 |y,   Y d S w | 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d ZdS ) FunctionPropertiesTestc                 C   s   |  | jjt d S r
   )assertEqualr!   r   r   r   r   r   r   test_module/      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!   r3   ZassertIndirr   r   r#   r   r   r   r   test_dir_includes_correct_attrs2   s   
z6FunctionPropertiesTest.test_dir_includes_correct_attrsc                 C   s   dd }|  | j| d S )Nc                   S   r   )zmy docstringr    r   r   r   r   r   	duplicate=   s   zJFunctionPropertiesTest.test_duplicate_function_equality.<locals>.duplicate)ZassertNotEqualr!   )r   r6   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   r	   r
   r   r   r   r   r   testC   r   z:FunctionPropertiesTest.test_copying___code__.<locals>.testr    )r/   r!   __code__)r   r8   r   r   r   test_copying___code__B   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	TypeErrorr   r   r   r   test___globals__H   s   z'FunctionPropertiesTest.test___globals__c                 C   s   |  | jjt | | jddttf dd }i }t||j|}|  |j| |  |jt | 	|dd | 	|i  t
d}dd i}t|d	}t|| |  |d
 j| |  |d j| |  |d
 jd | | d|d j d S )N__builtins__r<   c                 S   s   t | S r
   )len)sr   r   r   funcT       z6FunctionPropertiesTest.test___builtins__.<locals>.funcabcr    z\
            def func3(s): pass
            func4 = type(func3)(func3.__code__, {})
        None)typerB   Zfunc3Zfunc4)r=   r!   rB   r-   r?   r@   rI   r9   r;   r/   textwrapdedentexecZassertNotIn)r   rE   nsZfunc2codeZsafe_builtinsr   r   r   test___builtins__M   s&   


z(FunctionPropertiesTest.test___builtins__c                    sZ   d  fdd}|j }| |t | t|d | |d jjd | |d|t d S )N   c                         t   d S r
   printr   r   r   r   fn       z2FunctionPropertiesTest.test___closure__.<locals>.f   r   cell__closure__)	rY   ZassertIsInstancetupler/   rC   	__class__r   r-   r?   r   rU   cr   rT   r   test___closure__l   s   z'FunctionPropertiesTest.test___closure__c                 C   s^   t d}| |jd t  }d}| jt|d |j W d    d S 1 s(w   Y  d S )NrW   'shouldn't be able to read an empty cell)msg)typesCellTyper/   cell_contentsassertRaises
ValueError)r   Zcell_objr`   r   r   r   test_cell_newv   s   
"z$FunctionPropertiesTest.test_cell_newc                    sH    fdd}z|j d j W n ty   Y d d S w | d d d S )Nc                      rQ   r
   rR   r   rT   r   r   rU      rV   z1FunctionPropertiesTest.test_empty_cell.<locals>.fr   r_   rP   )rY   rc   re   r'   )r   rU   r   rT   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y:   Y nw | d | t |  W d    n1 sSw   Y  | t t  W d    d S 1 smw   Y  d S )NrP   c                      r   r
   r   r   rT   r   r   rU      r   z/FunctionPropertiesTest.test_set_cell.<locals>.f	   r   r_   )	rY   rc   r/   re   r'   rd   	NameErrorUnboundLocalErrorrS   r\   r   rT   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   r2   zdef f(): pass
f.__name__rB   r   )	r/   r!   r   r-   r@   rL   r#   r   r?   )r   rD   r   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   r]   rl   r   r2   )	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}|||||fD ]}|  t|jtj q&|  | d |  | d |j|_|  |j|j |  | d z|j|_W n	 tyc   Y nw | d z|j|_W n
 tyy   Y d S w | d d S )N)r2      c                   S   r	   r
   r   r   r   r   r   r      r   z/FunctionPropertiesTest.test___code__.<locals>.ac                   S   r   )NrP   r   r   r   r   r   r!      r   z/FunctionPropertiesTest.test___code__.<locals>.bc                      r   r
   r   r   )num_oner   r   r]      r   z/FunctionPropertiesTest.test___code__.<locals>.cc                      r   r
   r   r   )num_twor   r   rl      r   z/FunctionPropertiesTest.test___code__.<locals>.dc                      s    fS r
   r   r   rp   rq   r   r   e   rF   z/FunctionPropertiesTest.test___code__.<locals>.er2   ro   zC__code__ with different numbers of free vars should not be possible)r/   rI   r9   ra   CodeTypere   r'   )r   r   r!   r]   rl   rs   rE   r   rr   r   test___code__   s2   
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yQ   Y d S w | d d S )Nc                 S      | | S r
   r   r   r!   r   r   r   
first_func      zAFunctionPropertiesTest.test_func_default_args.<locals>.first_funcrW   r<   c                 S   rx   r
   r   ry   r   r   r   second_func   r{   zBFunctionPropertiesTest.test_func_default_args.<locals>.second_func)rW   r<   r       ro   zE__defaults__ does not update; deleting it does not remove requirement)r/   rv   r@   r'   )r   rz   r|   r   r   r   test_func_default_args   s"   

z-FunctionPropertiesTest.test_func_default_argsN)r   r   r   r0   r5   r7   r:   rA   rO   r^   rf   rg   rk   rm   rn   ru   rw   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	S )
InstancemethodAttrTestc                 C   s0   |  | jjjj| j | | jjd| jt d S )Nr[   )r/   r#   r   __self__r[   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 )Nr   )r/   r#   r   r   r-   r?   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y%   Y nw | d | | jjddt d S )N4using unknown attributes should raise AttributeErrorunknown_attrr<   )	ra   
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   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S )ArbitraryFunctionAttrTestc                 C   sJ   d| j _| | j jd zd| jj_W n
 ty   Y d S w | d d S )Nr2   z0setting attributes on methods should raise error)r!   r3   r/   r#   r   r?   r'   r   r   r   r   test_set_attr  s   z'ArbitraryFunctionAttrTest.test_set_attrc                 C   .   z| j `W n
 ty   Y d S w | d d S )Nz1deleting unknown attribute should raise TypeError)r!   r   r?   r'   r   r   r   r   test_delete_unknown_attr$     
z2ArbitraryFunctionAttrTest.test_delete_unknown_attrc              	   C   s@   | j | jjfD ]}z|j W n	 ty   Y qw | d qd S )Nr   )r!   r#   r   Znon_existent_attrr?   r'   )r   rE   r   r   r   test_unset_attr,  s   
z)ArbitraryFunctionAttrTest.test_unset_attrN)r   r   r   r   r   r   r   r   r   r   r     s    
r   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   )UserDictr3   r2   )r-   r!   r@   collectionsr   r#   r   r   )r   r   rl   r   r   r   test_setting_dict_to_invalid8  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 )Nr3   r2   )	r!   r   r=   r   r   r#   r   r/   r3   )r   rl   r   r   r   test_setting_dict_to_valid>  s   
z,FunctionDictsTest.test_setting_dict_to_validc                 C   r   )Nz3deleting function dictionary should raise TypeError)r!   r   r@   r'   r   r   r   r   test_delete___dict__N  r   z&FunctionDictsTest.test_delete___dict__c                 C   s   |  | jji  d S r
   )r/   r!   r   r   r   r   r   test_unassigned_dictV  r1   z&FunctionDictsTest.test_unassigned_dictc                 C   s(   d}i }||| j < | || j  | d S )NzSome string)r!   r/   )r   r+   rl   r   r   r   test_func_as_dict_keyY  s   
z'FunctionDictsTest.test_func_as_dict_keyN)r   r   r   r   r   r   r   r   r   r   r   r   r   7  s    r   c                   @   r   )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-   r?   )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                      rQ   r
   rR   r   rT   r   r   rU   r     zcell.<locals>.fr   rY   )r+   rU   r   rT   r   rX   p  s   
rX   Tc                    s    fdd}| s
d |j d S )zCreate an empty cell.c                      rQ   r
   rR   r   rT   r   r   rU   y  r   zempty_cell.<locals>.fi  r   r   )emptyrU   r   rT   r   
empty_cellw  s   
r   c                   @   r   )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)
assertTruerX   r   r   r   r   r   test_comparison  s
   zCellTest.test_comparisonN)r   r   r   r   r   r   r   r   r     r   r   c                   @   r   )StaticMethodAttrsTestc                 C   s<   dd }t |}| |j|u  t|}| |j|u  d S )Nc                   S   r	   r
   r   r   r   r   r   rU     r   z4StaticMethodAttrsTest.test_func_attribute.<locals>.f)classmethodr   r   staticmethod)r   rU   r]   rD   r   r   r   test_func_attribute  s
   z)StaticMethodAttrsTest.test_func_attributeN)r   r   r   r   r   r   r   r   r     r   r   c                   @   r   )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)rW   r<   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   ra   Zunittestr   ZTestCaser   r.   r   r   r   r   rX   r   r   r   r   r   mainr   r   r   r   <module>   s&     O)
