o
    HXh4                     @   s   d dl Z d dlZd dlZd dlZd dlmZ G dd dejZG dd dZG dd deZ	G d	d
 d
ejZ
G dd de
ZG dd de
Zdd Zdd ZeeZdddZdd ZG dd dejZedkroe  dS dS )    N)supportc                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestGetProfilec                 C      t d  d S Nsys
setprofileself r   B/opt/python-3.10.19/usr/lib/python3.10/test/test_sys_setprofile.pysetUp	      zTestGetProfile.setUpc                 C   r   r   r   r	   r   r   r   tearDown   r   zTestGetProfile.tearDownc                 C   s   |  t  d S r   )ZassertIsNoner   
getprofiler	   r   r   r   
test_empty   s   zTestGetProfile.test_emptyc                 C   s&   dd }t | | t  | d S )Nc                  W      d S r   r   argsr   r   r   fn      z&TestGetProfile.test_setget.<locals>.fn)r   r   ZassertIsr   )r
   r   r   r   r   test_setget   s   
zTestGetProfile.test_setgetN)__name__
__module____qualname__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dZdd	 ZdS )HookWatcherc                 C   s   g | _ g | _d S r   )frameseventsr	   r   r   r   __init__   s   
zHookWatcher.__init__c                 C   s,   |dks|dks|dkr|  || d S d S )Ncallreturn	exception	add_eventr
   frameeventargr   r   r   callback   s
   zHookWatcher.callbackNc                 C   sf   |du r	t d}z| j|}W n ty%   t| j}| j| Y nw | j||t|f dS )zAdd an event to the log.N   )	r   	_getframer   index
ValueErrorlenappendr   ident)r
   r&   r%   Zframenor   r   r   r#   $   s   

zHookWatcher.add_eventc                    s.   t | jjt t g d| _ fdd| jD S )zRemove calls to add_event().Nc                    s   g | ]
}|d   vr|qS )   r   ).0itemZ
disallowedr   r   
<listcomp>6   s    z*HookWatcher.get_events.<locals>.<listcomp>)r/   r#   __func__r   r   r	   r   r3   r   
get_events1   s   zHookWatcher.get_eventsr   )r   r   r   r   r(   r#   r6   r   r   r   r   r      s
    
r   c                   @   sN   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZeeeeeedZ	dS )ProfileSimulatorc                 C   s   || _ g | _t|  d S r   )testcasestackr   r   )r
   r8   r   r   r   r   :   s   zProfileSimulator.__init__c                 C   s   | j | | | d S r   )dispatchr$   r   r   r   r(   ?   s   zProfileSimulator.callbackc                 C   s   |  d| | j| d S )Nr   )r#   r9   r.   r
   r%   r   r   r   
trace_callC   s   zProfileSimulator.trace_callc                 C   s   |  d| | j  d S )Nr    )r#   r9   popr;   r   r   r   trace_returnG   s   zProfileSimulator.trace_returnc                 C   s   | j d d S )Nz2the profiler should never receive exception events)r8   failr;   r   r   r   trace_exceptionK   s   z ProfileSimulator.trace_exceptionc                 C   r   r   r   r;   r   r   r   
trace_passO   r   zProfileSimulator.trace_pass)r   r!   r    Zc_callZc_returnZc_exceptionN)
r   r   r   r   r(   r<   r>   r@   rA   r:   r   r   r   r   r7   9   s    
r7   c                   @   s   e Zd Zdd ZdS )TestCaseBasec                 C   s<   t ||  }||kr| dt|t|f  d S d S )Nz'Expected events:
%s
Received events:
%s)capture_eventsnew_watcherr?   pprintZpformat)r
   callableZexpectedr   r   r   r   check_events]   s   
zTestCaseBase.check_eventsN)r   r   r   rG   r   r   r   r   rB   \   s    rB   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 )ProfileHookTestCasec                 C   s   t  S r   )r   r	   r   r   r   rD   e      zProfileHookTestCase.new_watcherc                 C   0   dd }t |}| |dd|fdd|fg d S )Nc                 S   r   r   r   pr   r   r   fi   r   z*ProfileHookTestCase.test_simple.<locals>.fr)   r   r    r/   rG   r
   rM   f_identr   r   r   test_simpleh   
   
zProfileHookTestCase.test_simplec                 C   rJ   )Nc                 S      dd  d S Nr)   r   r   rK   r   r   r   rM   q      z-ProfileHookTestCase.test_exception.<locals>.fr)   r   r    rN   rO   r   r   r   test_exceptionp   rR   z"ProfileHookTestCase.test_exceptionc                 C   rJ   )Nc                 S      zdd  W d S    Y d S rT   r   rK   r   r   r   rM   y      z4ProfileHookTestCase.test_caught_exception.<locals>.fr)   r   r    rN   rO   r   r   r   test_caught_exceptionx   
   
z)ProfileHookTestCase.test_caught_exceptionc                 C   rJ   )Nc                 S   rW   rT   r   rK   r   r   r   rM      rX   z;ProfileHookTestCase.test_caught_nested_exception.<locals>.fr)   r   r    rN   rO   r   r   r   test_caught_nested_exception   rZ   z0ProfileHookTestCase.test_caught_nested_exceptionc                 C   rJ   )Nc                 S   rS   rT   r   rK   r   r   r   rM      rU   z4ProfileHookTestCase.test_nested_exception.<locals>.fr)   r   r    rN   rO   r   r   r   test_nested_exception   s
   
z)ProfileHookTestCase.test_nested_exceptionc                    sd   dd   fdd}t  }t |}| |dd|fdd|fdd|fd	d|fd	d|fdd|fg d S )
Nc                 S   rS   rT   r   rK   r   r   r   rM      rU   z>ProfileHookTestCase.test_exception_in_except_clause.<locals>.fc              	      s6   z |  W d S    z |  W Y d S    Y Y d S r   r   rK   rM   r   r   g   s
   z>ProfileHookTestCase.test_exception_in_except_clause.<locals>.gr)   r   r0   r       rN   r
   r^   rP   g_identr   r]   r   test_exception_in_except_clause   s   
z3ProfileHookTestCase.test_exception_in_except_clausec              
      s\   dd   fdd}t  }t |}| |dd|fdd|fdd|fdd	|fdd|fg d S )
Nc                 S   rS   rT   r   rK   r   r   r   rM      rU   z9ProfileHookTestCase.test_exception_propagation.<locals>.fc              	      s&   z |  W |  d d S |  d w )Nfalling throughr"   rK   r]   r   r   r^      s   z9ProfileHookTestCase.test_exception_propagation.<locals>.gr)   r   r0   r    rc   rN   r`   r   r]   r   test_exception_propagation   s   
z.ProfileHookTestCase.test_exception_propagationc                 C   rJ   )Nc                 S   s$   zdd  W d S    dd  Y d S rT   r   rK   r   r   r   rM      s   z/ProfileHookTestCase.test_raise_twice.<locals>.fr)   r   r    rN   rO   r   r   r   test_raise_twice   rZ   z$ProfileHookTestCase.test_raise_twicec                 C   rJ   )Nc                 S   s   zdd  W d S     rT   r   rK   r   r   r   rM      s   z1ProfileHookTestCase.test_raise_reraise.<locals>.fr)   r   r    rN   rO   r   r   r   test_raise_reraise   rZ   z&ProfileHookTestCase.test_raise_reraisec                 C   rJ   )Nc                 S   s   t  r   )	ExceptionrK   r   r   r   rM      rI   z)ProfileHookTestCase.test_raise.<locals>.fr)   r   r    rN   rO   r   r   r   
test_raise   rR   zProfileHookTestCase.test_raisec                       dd   fddfddfddfd	d
}t  }t }t }t }t |}| |dd|fdd|fdd|fdd|fdd|fdd|fdd|fdd|fdd|fdd|fg
 d S )Nc                   S   rS   rT   r   r   r   r   r   rM      rU   z5ProfileHookTestCase.test_distant_exception.<locals>.fc                      
      d S r   r   r   r]   r   r   r^         
z5ProfileHookTestCase.test_distant_exception.<locals>.gc                      rj   r   r   r   r^   r   r   h   rk   z5ProfileHookTestCase.test_distant_exception.<locals>.hc                      rj   r   r   r   rm   r   r   i   rk   z5ProfileHookTestCase.test_distant_exception.<locals>.ic                    rj   r   r   rK   ro   r   r   j   rk   z5ProfileHookTestCase.test_distant_exception.<locals>.jr)   r   r0   r_         r    rN   r
   rq   rP   ra   Zh_identZi_identZj_identr   rM   r^   rm   ro   r   test_distant_exception   *   
z*ProfileHookTestCase.test_distant_exceptionc                    t   dd   fdd}t  }t |}| |dd|fdd|fdd|fdd|fdd|fdd|fdd|fdd|fg d S )	Nc                  s       t dD ]} | V  qd S Nr0   rangerp   r   r   r   rM         z-ProfileHookTestCase.test_generator.<locals>.fc                         D ]}qd S r   r   rL   ro   r]   r   r   r^         
z-ProfileHookTestCase.test_generator.<locals>.gr)   r   r0   r    rN   r`   r   r]   r   test_generator   s   
z"ProfileHookTestCase.test_generatorc                    rx   )	Nc                  s   ry   rz   r{   rp   r   r   r   rM     r}   z2ProfileHookTestCase.test_stop_iteration.<locals>.fc                    r~   r   r   r   r]   r   r   r^     r   z2ProfileHookTestCase.test_stop_iteration.<locals>.gr)   r   r0   r    rN   r`   r   r]   r   test_stop_iteration  s   
z'ProfileHookTestCase.test_stop_iterationN)r   r   r   rD   rQ   rV   rY   r[   r\   rb   rd   re   rf   rh   rv   r   r   r   r   r   r   rH   d   s    				rH   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S )ProfileSimulatorTestCasec                 C   s   t | S r   )r7   r	   r   r   r   rD     s   z$ProfileSimulatorTestCase.new_watcherc                 C   rJ   )Nc                 S   r   r   r   rK   r   r   r   rM     r   z/ProfileSimulatorTestCase.test_simple.<locals>.fr)   r   r    rN   rO   r   r   r   rQ     rR   z$ProfileSimulatorTestCase.test_simplec                 C   rJ   )Nc                 S   rS   rT   r   rK   r   r   r   rM   &  rU   z8ProfileSimulatorTestCase.test_basic_exception.<locals>.fr)   r   r    rN   rO   r   r   r   test_basic_exception%  rR   z-ProfileSimulatorTestCase.test_basic_exceptionc                 C   rJ   )Nc                 S   rW   rT   r   rK   r   r   r   rM   .  rX   z9ProfileSimulatorTestCase.test_caught_exception.<locals>.fr)   r   r    rN   rO   r   r   r   rY   -  rZ   z.ProfileSimulatorTestCase.test_caught_exceptionc                    ri   )Nc                   S   rS   rT   r   r   r   r   r   rM   7  rU   z:ProfileSimulatorTestCase.test_distant_exception.<locals>.fc                      rj   r   r   r   r]   r   r   r^   9  rk   z:ProfileSimulatorTestCase.test_distant_exception.<locals>.gc                      rj   r   r   r   rl   r   r   rm   ;  rk   z:ProfileSimulatorTestCase.test_distant_exception.<locals>.hc                      rj   r   r   r   rn   r   r   ro   =  rk   z:ProfileSimulatorTestCase.test_distant_exception.<locals>.ic                    rj   r   r   rK   rp   r   r   rq   ?  rk   z:ProfileSimulatorTestCase.test_distant_exception.<locals>.jr)   r   r0   r_   rr   rs   r    rN   rt   r   ru   r   rv   6  rw   z/ProfileSimulatorTestCase.test_distant_exceptionc                    8   i   fdd}t |}| |dd|fdd|fg d S )Nc                    s   t ji dfi   d S N*   dictgetrK   kwargsr   r   rM   U     z7ProfileSimulatorTestCase.test_unbound_method.<locals>.fr)   r   r    rN   rO   r   r   r   test_unbound_methodS     
z,ProfileSimulatorTestCase.test_unbound_methodc                 C   rJ   )Nc                 S   s   t   d S r   r   rK   r   r   r   rM   ]  rU   z?ProfileSimulatorTestCase.test_unbound_method_no_args.<locals>.fr)   r   r    rN   rO   r   r   r   test_unbound_method_no_args\  rR   z4ProfileSimulatorTestCase.test_unbound_method_no_argsc                 C   rJ   )Nc                 S   s   t td d S r   r   r   printrK   r   r   r   rM   e  s   zDProfileSimulatorTestCase.test_unbound_method_invalid_args.<locals>.fr)   r   r    rN   rO   r   r   r    test_unbound_method_invalid_argsd  rR   z9ProfileSimulatorTestCase.test_unbound_method_invalid_argsc                    r   )Nc                    s   t jdi   d S )Nr   r   rK   r   r   r   rM   n  s   zGProfileSimulatorTestCase.test_unbound_method_no_keyword_args.<locals>.fr)   r   r    rN   rO   r   r   r   #test_unbound_method_no_keyword_argsl  r   z<ProfileSimulatorTestCase.test_unbound_method_no_keyword_argsc                    r   )Nc                    s   t jtdfi   d S r   r   rK   r   r   r   rM   w  r   zLProfileSimulatorTestCase.test_unbound_method_invalid_keyword_args.<locals>.fr)   r   r    rN   rO   r   r   r   (test_unbound_method_invalid_keyword_argsu  r   zAProfileSimulatorTestCase.test_unbound_method_invalid_keyword_argsN)r   r   r   rD   rQ   r   rY   rv   r   r   r   r   r   r   r   r   r   r     s    			r   c                 C   s$   t | dr	| j}n| j}|j|jfS )Nf_code)hasattrr   __code__co_firstlinenoco_name)functioncoder   r   r   r/   ~  s   
r/   c                 C   s   z| | W d S    Y d S r   r   )rM   rL   r   r   r   protect  rX   r   c                 C   sp   |d u rt  }t }t  zt|j t| | td  W |r't  n|r/t  w w |	 dd S )Nr)   )
r   gc	isenableddisabler   r   r(   r   enabler6   )rF   rL   Zold_gcr   r   r   rC     s   

rC   c                 C   s   dd l }| t|  d S )Nr   )rE   rC   )rF   rE   r   r   r   show_events  s   r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestEdgeCasesc                 C   s    |  tjt  td  d S r   )Z
addCleanupr   r   r   r	   r   r   r   r     s   zTestEdgeCases.setUpc                    s   dd }dd  G  fddd}t |  t }t | | |jj|j | |jj	t
 W d    n1 s<w   Y  | t  | d S )Nc                  W   r   r   r   r   r   r   r   foo  r   z*TestEdgeCases.test_reentrancy.<locals>.fooc                  W   r   r   r   r   r   r   r   bar  r   z*TestEdgeCases.test_reentrancy.<locals>.barc                       s    e Zd Zdd Z fddZdS )z(TestEdgeCases.test_reentrancy.<locals>.Ac                 W   r   r   r   )r
   r   r   r   r   __call__  r   z1TestEdgeCases.test_reentrancy.<locals>.A.__call__c                    s   t   d S r   r   r	   r   r   r   __del__  r   z0TestEdgeCases.test_reentrancy.<locals>.A.__del__N)r   r   r   r   r   r   r   r   r   A  s    r   )r   r   r   Zcatch_unraisable_exceptionZassertEqualZ
unraisableobjectr   ZassertIsInstance	exc_valueRuntimeErrorr   )r
   r   r   cmr   r   r   test_reentrancy  s   

zTestEdgeCases.test_reentrancyc                 C   s&   dd }t | ~t t   d S )Nc                  W   r   r   r   r   r   r   r   r     r   z+TestEdgeCases.test_same_object.<locals>.foo)r   r   r   )r
   r   r   r   r   test_same_object  s   
zTestEdgeCases.test_same_objectN)r   r   r   r   r   r   r   r   r   r   r     s    r   __main__r   )r   rE   r   Zunittesttestr   ZTestCaser   r   r7   rB   rH   r   r/   r   Zprotect_identrC   r   r   r   mainr   r   r   r   <module>   s*     # 6e
&