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"   r4   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   s   	 dS r    r   r   r   r   r   	duplicate=   s   zJFunctionPropertiesTest.test_duplicate_function_equality.<locals>.duplicate)ZassertNotEqualr"   )r   r7   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!   )r0   r"   __code__)r   r9   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)typerC   Zfunc3Zfunc4)r>   r"   rC   r.   r@   rA   rJ   r:   r<   r0   textwrapdedentexecZassertNotIn)r   rF   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__)	rZ   ZassertIsInstancetupler0   rD   	__class__r   r.   r@   r   rV   cr   rU   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 )NrX   'shouldn't be able to read an empty cell)msg)typesCellTyper0   cell_contentsassertRaises
ValueError)r   Zcell_objra   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                      rR   r
   rS   r   rU   r   r   rV      rW   z1FunctionPropertiesTest.test_empty_cell.<locals>.fr   r`   rQ   )rZ   rd   rf   r(   )r   rV   r   rU   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 )NrQ   c                      r   r
   r   r   rU   r   r   rV      r   z/FunctionPropertiesTest.test_set_cell.<locals>.f	   r   r`   )	rZ   rd   r0   rf   r(   re   	NameErrorUnboundLocalErrorrT   r]   r   rU   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   r3   zdef f(): pass
f.__name__rC   r   )	r0   r"   r   r.   rA   rM   r$   r   r@   )r   rE   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^   rm   r   r3   )	r0   r"   r   r   r%   r   r   r.   rA   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)r3      c                   S   r	   r
   r   r   r   r   r   r      r   z/FunctionPropertiesTest.test___code__.<locals>.ac                   S   r   )NrQ   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   rm      r   z/FunctionPropertiesTest.test___code__.<locals>.dc                      s    fS r
   r   r   rq   rr   r   r   e   rG   z/FunctionPropertiesTest.test___code__.<locals>.er3   rp   zC__code__ with different numbers of free vars should not be possible)r0   rJ   r:   rb   CodeTyperf   r(   )r   r   r"   r^   rm   rt   rF   r   rs   r   test___code__   s2   
z$FunctionPropertiesTest.test___code__c                 C   s*   |  | jjd  | j`|  | jjd  d S r
   )r0   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_funcrX   r=   c                 S   ry   r
   r   rz   r   r   r   second_func   r|   zBFunctionPropertiesTest.test_func_default_args.<locals>.second_func)rX   r=   r!      rp   zE__defaults__ does not update; deleting it does not remove requirement)r0   rw   rA   r(   )r   r{   r}   r   r   r   test_func_default_args   s"   

z-FunctionPropertiesTest.test_func_default_argsN)r   r   r   r1   r6   r8   r;   rB   rP   r_   rg   rh   rl   rn   ro   rv   rx   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\   )r0   r$   r   __self__r\   r   r.   rA   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__)r0   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   )r0   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=   )	rb   
MethodTypeidr$   r0   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 )Nr3   z0setting attributes on methods should raise error)r"   r4   r0   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   rF   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   )UserDictr4   r3   )r.   r"   rA   collectionsr   r$   r   r   )r   r   rm   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 )Nr4   r3   )	r"   r   r>   r   r   r$   r   r0   r4   )r   rm   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   rA   r(   r   r   r   r   test_delete___dict__N  r   z&FunctionDictsTest.test_delete___dict__c                 C   s   |  | jji  d S r
   )r0   r"   r   r   r   r   r   test_unassigned_dictV  r2   z&FunctionDictsTest.test_unassigned_dictc                 C   s(   d}i }||| j < | || j  | d S )NzSome string)r"   r0   )r   r,   rm   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__)r0   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   r0   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 )Nc                      rR   r
   rS   r   rU   r   r   rV   r     zcell.<locals>.fr   rZ   )r,   rV   r   rU   r   rY   p  s   
rY   Tc                    s    	  fdd}| sd |j d S )Nc                      rR   r
   rS   r   rU   r   r   rV   y  r   zempty_cell.<locals>.fi  r   r   )emptyrV   r   rU   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)
assertTruerY   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   rV     r   z4StaticMethodAttrsTest.test_func_attribute.<locals>.f)classmethodr   r   staticmethod)r   rV   r^   rE   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   rD   timezdict.fromkeyszfloat.__getformat__zstr.maketranszbytes.maketrans)rX   r=   r!   zlist.appendZfooZbarzdict.pop)r   r0   rD   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)rK   rb   Zunittestr   ZTestCaser   r/   r   r   r   r   rY   r   r   r   r   r   mainr   r   r   r   <module>   s&     O)
