o
    g=1                     @   s   d dl Z d dlZd dlZd dl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edkr`e  dS dS )    Nc                   @   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
   N/Users/admin/.pyenv/versions/3.10.0/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   sh   	 |d u r
t d}z| j|}W n ty&   t| j}| j| Y nw | j||t|f d S )N   )	r   	_getframer   index
ValueErrorlenappendr   ident)r	   r$   r#   Zframenor
   r
   r   r!   #   s   

zHookWatcher.add_eventc                    s0   	 t | jjt t g d | _ fdd| jD S )Nc                    s   g | ]
}|d   vr|qS )   r
   ).0itemZ
disallowedr
   r   
<listcomp>5   s    z*HookWatcher.get_events.<locals>.<listcomp>)r-   r!   __func__r   r   r   r
   r1   r   
get_events0   s   zHookWatcher.get_eventsr   )r   r   r   r   r&   r!   r4   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	   r6   r
   r
   r   r   9   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!   r7   r,   r	   r#   r
   r
   r   
trace_callB   s   zProfileSimulator.trace_callc                 C   s   |  d| | j  d S )Nr   )r!   r7   popr9   r
   r
   r   trace_returnF   s   zProfileSimulator.trace_returnc                 C   s   | j d d S )Nz2the profiler should never receive exception events)r6   failr9   r
   r
   r   trace_exceptionJ   s   z ProfileSimulator.trace_exceptionc                 C   r   r   r
   r9   r
   r
   r   
trace_passN   r   zProfileSimulator.trace_pass)r   r   r   Zc_callZc_returnZc_exceptionN)
r   r   r   r   r&   r:   r<   r>   r?   r8   r
   r
   r
   r   r5   8   s    
r5   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   rE   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 )ProfileHookTestCasec                 C   s   t  S r   )r   r   r
   r
   r   rB   d      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   fh   r   z*ProfileHookTestCase.test_simple.<locals>.fr'   r   r   r-   rE   r	   rK   f_identr
   r
   r   test_simpleg   
   
zProfileHookTestCase.test_simplec                 C   rH   )Nc                 S      dd  d S Nr'   r   r
   rI   r
   r
   r   rK   p      z-ProfileHookTestCase.test_exception.<locals>.fr'   r   r   rL   rM   r
   r
   r   test_exceptiono   rP   z"ProfileHookTestCase.test_exceptionc                 C   rH   )Nc                 S      zdd  W d S    Y d S rR   r
   rI   r
   r
   r   rK   x      z4ProfileHookTestCase.test_caught_exception.<locals>.fr'   r   r   rL   rM   r
   r
   r   test_caught_exceptionw   
   
z)ProfileHookTestCase.test_caught_exceptionc                 C   rH   )Nc                 S   rU   rR   r
   rI   r
   r
   r   rK      rV   z;ProfileHookTestCase.test_caught_nested_exception.<locals>.fr'   r   r   rL   rM   r
   r
   r   test_caught_nested_exception   rX   z0ProfileHookTestCase.test_caught_nested_exceptionc                 C   rH   )Nc                 S   rQ   rR   r
   rI   r
   r
   r   rK      rS   z4ProfileHookTestCase.test_nested_exception.<locals>.fr'   r   r   rL   rM   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   rQ   rR   r
   rI   r
   r
   r   rK      rS   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
   rI   rK   r
   r   g   s
   z>ProfileHookTestCase.test_exception_in_except_clause.<locals>.gr'   r   r.   r      rL   r	   r\   rN   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   rQ   rR   r
   rI   r
   r
   r   rK      rS   z9ProfileHookTestCase.test_exception_propagation.<locals>.fc              	      s&   z |  W |  d d S |  d w )Nfalling throughr    rI   r[   r
   r   r\      s   z9ProfileHookTestCase.test_exception_propagation.<locals>.gr'   r   r.   r   ra   rL   r^   r
   r[   r   test_exception_propagation   s   
z.ProfileHookTestCase.test_exception_propagationc                 C   rH   )Nc                 S   s$   zdd  W d S    dd  Y d S rR   r
   rI   r
   r
   r   rK      s   z/ProfileHookTestCase.test_raise_twice.<locals>.fr'   r   r   rL   rM   r
   r
   r   test_raise_twice   rX   z$ProfileHookTestCase.test_raise_twicec                 C   rH   )Nc                 S   s   zdd  W d S     rR   r
   rI   r
   r
   r   rK      s   z1ProfileHookTestCase.test_raise_reraise.<locals>.fr'   r   r   rL   rM   r
   r
   r   test_raise_reraise   rX   z&ProfileHookTestCase.test_raise_reraisec                 C   rH   )Nc                 S   s   t  r   )	ExceptionrI   r
   r
   r   rK      rG   z)ProfileHookTestCase.test_raise.<locals>.fr'   r   r   rL   rM   r
   r
   r   
test_raise   rP   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   rQ   rR   r
   r
   r
   r
   r   rK      rS   z5ProfileHookTestCase.test_distant_exception.<locals>.fc                      
      d S r   r
   r
   r[   r
   r   r\         
z5ProfileHookTestCase.test_distant_exception.<locals>.gc                      rh   r   r
   r
   r\   r
   r   h   ri   z5ProfileHookTestCase.test_distant_exception.<locals>.hc                      rh   r   r
   r
   rk   r
   r   i   ri   z5ProfileHookTestCase.test_distant_exception.<locals>.ic                    rh   r   r
   rI   rm   r
   r   j   ri   z5ProfileHookTestCase.test_distant_exception.<locals>.jr'   r   r.   r]         r   rL   r	   ro   rN   r_   Zh_identZi_identZj_identr
   rK   r\   rk   rm   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 Nr.   rangern   r
   r
   r   rK         z-ProfileHookTestCase.test_generator.<locals>.fc                         D ]}qd S r   r
   rJ   rm   r[   r
   r   r\         
z-ProfileHookTestCase.test_generator.<locals>.gr'   r   r.   r   rL   r^   r
   r[   r   test_generator   s   
z"ProfileHookTestCase.test_generatorc                    rv   )	Nc                  s   rw   rx   ry   rn   r
   r
   r   rK     r{   z2ProfileHookTestCase.test_stop_iteration.<locals>.fc                    r|   r   r
   r}   r[   r
   r   r\     r~   z2ProfileHookTestCase.test_stop_iteration.<locals>.gr'   r   r.   r   rL   r^   r
   r[   r   test_stop_iteration  s   
z'ProfileHookTestCase.test_stop_iterationN)r   r   r   rB   rO   rT   rW   rY   rZ   r`   rb   rc   rd   rf   rt   r   r   r
   r
   r
   r   rF   c   s    				rF   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   )r5   r   r
   r
   r   rB     s   z$ProfileSimulatorTestCase.new_watcherc                 C   rH   )Nc                 S   r   r   r
   rI   r
   r
   r   rK     r   z/ProfileSimulatorTestCase.test_simple.<locals>.fr'   r   r   rL   rM   r
   r
   r   rO     rP   z$ProfileSimulatorTestCase.test_simplec                 C   rH   )Nc                 S   rQ   rR   r
   rI   r
   r
   r   rK   %  rS   z8ProfileSimulatorTestCase.test_basic_exception.<locals>.fr'   r   r   rL   rM   r
   r
   r   test_basic_exception$  rP   z-ProfileSimulatorTestCase.test_basic_exceptionc                 C   rH   )Nc                 S   rU   rR   r
   rI   r
   r
   r   rK   -  rV   z9ProfileSimulatorTestCase.test_caught_exception.<locals>.fr'   r   r   rL   rM   r
   r
   r   rW   ,  rX   z.ProfileSimulatorTestCase.test_caught_exceptionc                    rg   )Nc                   S   rQ   rR   r
   r
   r
   r
   r   rK   6  rS   z:ProfileSimulatorTestCase.test_distant_exception.<locals>.fc                      rh   r   r
   r
   r[   r
   r   r\   8  ri   z:ProfileSimulatorTestCase.test_distant_exception.<locals>.gc                      rh   r   r
   r
   rj   r
   r   rk   :  ri   z:ProfileSimulatorTestCase.test_distant_exception.<locals>.hc                      rh   r   r
   r
   rl   r
   r   rm   <  ri   z:ProfileSimulatorTestCase.test_distant_exception.<locals>.ic                    rh   r   r
   rI   rn   r
   r   ro   >  ri   z:ProfileSimulatorTestCase.test_distant_exception.<locals>.jr'   r   r.   r]   rp   rq   r   rL   rr   r
   rs   r   rt   5  ru   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getrI   kwargsr
   r   rK   T     z7ProfileSimulatorTestCase.test_unbound_method.<locals>.fr'   r   r   rL   rM   r
   r   r   test_unbound_methodR     
z,ProfileSimulatorTestCase.test_unbound_methodc                 C   rH   )Nc                 S   s   t   d S r   r   rI   r
   r
   r   rK   \  rS   z?ProfileSimulatorTestCase.test_unbound_method_no_args.<locals>.fr'   r   r   rL   rM   r
   r
   r   test_unbound_method_no_args[  rP   z4ProfileSimulatorTestCase.test_unbound_method_no_argsc                 C   rH   )Nc                 S   s   t td d S r   r   r   printrI   r
   r
   r   rK   d  s   zDProfileSimulatorTestCase.test_unbound_method_invalid_args.<locals>.fr'   r   r   rL   rM   r
   r
   r    test_unbound_method_invalid_argsc  rP   z9ProfileSimulatorTestCase.test_unbound_method_invalid_argsc                    r   )Nc                    s   t jdi   d S )Nr
   r   rI   r   r
   r   rK   m  s   zGProfileSimulatorTestCase.test_unbound_method_no_keyword_args.<locals>.fr'   r   r   rL   rM   r
   r   r   #test_unbound_method_no_keyword_argsk  r   z<ProfileSimulatorTestCase.test_unbound_method_no_keyword_argsc                    r   )Nc                    s   t jtdfi   d S r   r   rI   r   r
   r   rK   v  r   zLProfileSimulatorTestCase.test_unbound_method_invalid_keyword_args.<locals>.fr'   r   r   rL   rM   r
   r   r   (test_unbound_method_invalid_keyword_argst  r   zAProfileSimulatorTestCase.test_unbound_method_invalid_keyword_argsN)r   r   r   rB   rO   r   rW   rt   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
   )rK   rJ   r
   r
   r   protect  rV   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   enabler4   )rD   rJ   Zold_gcr
   r
   r   rA     s   

rA   c                 C   s   dd l }| t|  d S )Nr   )rC   rA   )rD   rC   r
   r
   r   show_events  s   r   __main__r   )r   rC   r   ZunittestZTestCaser   r   r5   r@   rF   r   r-   r   Zprotect_identrA   r   r   mainr
   r
   r
   r   <module>   s&     # 6e
