a
    ze=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re  dS )    Nc                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestGetProfilec                 C   s   t d  d S Nsys
setprofileself r	   ./usr/lib/python3.9/test/test_sys_setprofile.pysetUp   s    zTestGetProfile.setUpc                 C   s   t d  d S r   r   r   r	   r	   r
   tearDown   s    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   s   d S r   r	   )argsr	   r	   r
   fn   s    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 )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yJ   t| j}| j| Y n0 | 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                    s.   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       z*HookWatcher.get_events.<locals>.<listcomp>)r)   r   __func__r   r   r   r	   r-   r
   
get_events0   s    zHookWatcher.get_events)N)r   r   r   r   r"   r   r1   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   r3   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   r4   r(   r   r   r	   r	   r
   
trace_callB   s    zProfileSimulator.trace_callc                 C   s   |  d| | j  d S )Nr   )r   r4   popr6   r	   r	   r
   trace_returnF   s    zProfileSimulator.trace_returnc                 C   s   | j d d S )Nz2the profiler should never receive exception events)r3   failr6   r	   r	   r
   trace_exceptionJ   s    z ProfileSimulator.trace_exceptionc                 C   s   d S r   r	   r6   r	   r	   r
   
trace_passN   s    zProfileSimulator.trace_pass)r   r   r   Zc_callZc_returnZc_exceptionN)
r   r   r   r   r"   r7   r9   r;   r<   r5   r	   r	   r	   r
   r2   8   s   r2   c                   @   s   e Zd Zdd ZdS )TestCaseBasec                 C   s8   t ||  }||kr4| dt|t|f  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   rB   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
   r?   d   s    zProfileHookTestCase.new_watcherc                 C   s0   dd }t |}| |dd|fdd|fg d S )Nc                 S   s   d S r   r	   pr	   r	   r
   fh   s    z*ProfileHookTestCase.test_simple.<locals>.fr#   r   r   r)   rB   r   rF   f_identr	   r	   r
   test_simpleg   s
    zProfileHookTestCase.test_simplec                 C   s0   dd }t |}| |dd|fdd|fg d S )Nc                 S   s   dd  d S Nr#   r   r	   rD   r	   r	   r
   rF   p   s    z-ProfileHookTestCase.test_exception.<locals>.fr#   r   r   rG   rH   r	   r	   r
   test_exceptiono   s
    z"ProfileHookTestCase.test_exceptionc                 C   s0   dd }t |}| |dd|fdd|fg d S )Nc                 S   s   zdd  W n   Y n0 d S rK   r	   rD   r	   r	   r
   rF   x   s    z4ProfileHookTestCase.test_caught_exception.<locals>.fr#   r   r   rG   rH   r	   r	   r
   test_caught_exceptionw   s
    z)ProfileHookTestCase.test_caught_exceptionc                 C   s0   dd }t |}| |dd|fdd|fg d S )Nc                 S   s   zdd  W n   Y n0 d S rK   r	   rD   r	   r	   r
   rF      s    z;ProfileHookTestCase.test_caught_nested_exception.<locals>.fr#   r   r   rG   rH   r	   r	   r
   test_caught_nested_exception   s
    z0ProfileHookTestCase.test_caught_nested_exceptionc                 C   s0   dd }t |}| |dd|fdd|fg d S )Nc                 S   s   dd  d S rK   r	   rD   r	   r	   r
   rF      s    z4ProfileHookTestCase.test_nested_exception.<locals>.fr#   r   r   rG   rH   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   s   dd  d S rK   r	   rD   r	   r	   r
   rF      s    z>ProfileHookTestCase.test_exception_in_except_clause.<locals>.fc              	      s8   z |  W n&   z |  W n   Y n0 Y n0 d S r   r	   rD   rF   r	   r
   g   s
    z>ProfileHookTestCase.test_exception_in_except_clause.<locals>.gr#   r   r*   r      rG   r   rQ   rI   g_identr	   rP   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   s   dd  d S rK   r	   rD   r	   r	   r
   rF      s    z9ProfileHookTestCase.test_exception_propagation.<locals>.fc              	      s(   z |  W |  d n|  d 0 d S )Nfalling throughr   rD   rP   r	   r
   rQ      s    z9ProfileHookTestCase.test_exception_propagation.<locals>.gr#   r   r*   r   rV   rG   rS   r	   rP   r
   test_exception_propagation   s    z.ProfileHookTestCase.test_exception_propagationc                 C   s0   dd }t |}| |dd|fdd|fg d S )Nc                 S   s&   zdd  W n   dd  Y n0 d S rK   r	   rD   r	   r	   r
   rF      s    z/ProfileHookTestCase.test_raise_twice.<locals>.fr#   r   r   rG   rH   r	   r	   r
   test_raise_twice   s
    z$ProfileHookTestCase.test_raise_twicec                 C   s0   dd }t |}| |dd|fdd|fg d S )Nc                 S   s    zdd  W n    Y n0 d S rK   r	   rD   r	   r	   r
   rF      s    z1ProfileHookTestCase.test_raise_reraise.<locals>.fr#   r   r   rG   rH   r	   r	   r
   test_raise_reraise   s
    z&ProfileHookTestCase.test_raise_reraisec                 C   s0   dd }t |}| |dd|fdd|fg d S )Nc                 S   s
   t  d S r   )	ExceptionrD   r	   r	   r
   rF      s    z)ProfileHookTestCase.test_raise.<locals>.fr#   r   r   rG   rH   r	   r	   r
   
test_raise   s
    zProfileHookTestCase.test_raisec                    s   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   s   dd  d S rK   r	   r	   r	   r	   r
   rF      s    z5ProfileHookTestCase.test_distant_exception.<locals>.fc                      s
      d S r   r	   r	   rP   r	   r
   rQ      s    z5ProfileHookTestCase.test_distant_exception.<locals>.gc                      s
      d S r   r	   r	   rQ   r	   r
   h   s    z5ProfileHookTestCase.test_distant_exception.<locals>.hc                      s
      d S r   r	   r	   r]   r	   r
   i   s    z5ProfileHookTestCase.test_distant_exception.<locals>.ic                    s
      d S r   r	   rD   r_   r	   r
   j   s    z5ProfileHookTestCase.test_distant_exception.<locals>.jr#   r   r*   rR         r   rG   r   ra   rI   rT   Zh_identZi_identZj_identr	   rF   rQ   r]   r_   r
   test_distant_exception   s*    z*ProfileHookTestCase.test_distant_exceptionc                    st   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   s   t dD ]
} | V  qd S Nr*   ranger`   r	   r	   r
   rF      s    z-ProfileHookTestCase.test_generator.<locals>.fc                    s     D ]}qd S r   r	   rE   r_   rP   r	   r
   rQ      s    
z-ProfileHookTestCase.test_generator.<locals>.gr#   r   r*   r   rG   rS   r	   rP   r
   test_generator   s    z"ProfileHookTestCase.test_generatorc                    st   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   s   t dD ]
} | V  qd S rg   rh   r`   r	   r	   r
   rF     s    z2ProfileHookTestCase.test_stop_iteration.<locals>.fc                    s     D ]}qd S r   r	   rj   rP   r	   r
   rQ     s    
z2ProfileHookTestCase.test_stop_iteration.<locals>.gr#   r   r*   r   rG   rS   r	   rP   r
   test_stop_iteration  s    z'ProfileHookTestCase.test_stop_iterationN)r   r   r   r?   rJ   rL   rM   rN   rO   rU   rW   rX   rY   r[   rf   rk   rl   r	   r	   r	   r
   rC   c   s   				rC   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   )r2   r   r	   r	   r
   r?     s    z$ProfileSimulatorTestCase.new_watcherc                 C   s0   dd }t |}| |dd|fdd|fg d S )Nc                 S   s   d S r   r	   rD   r	   r	   r
   rF     s    z/ProfileSimulatorTestCase.test_simple.<locals>.fr#   r   r   rG   rH   r	   r	   r
   rJ     s
    z$ProfileSimulatorTestCase.test_simplec                 C   s0   dd }t |}| |dd|fdd|fg d S )Nc                 S   s   dd  d S rK   r	   rD   r	   r	   r
   rF   %  s    z8ProfileSimulatorTestCase.test_basic_exception.<locals>.fr#   r   r   rG   rH   r	   r	   r
   test_basic_exception$  s
    z-ProfileSimulatorTestCase.test_basic_exceptionc                 C   s0   dd }t |}| |dd|fdd|fg d S )Nc                 S   s   zdd  W n   Y n0 d S rK   r	   rD   r	   r	   r
   rF   -  s    z9ProfileSimulatorTestCase.test_caught_exception.<locals>.fr#   r   r   rG   rH   r	   r	   r
   rM   ,  s
    z.ProfileSimulatorTestCase.test_caught_exceptionc                    s   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   s   dd  d S rK   r	   r	   r	   r	   r
   rF   6  s    z:ProfileSimulatorTestCase.test_distant_exception.<locals>.fc                      s
      d S r   r	   r	   rP   r	   r
   rQ   8  s    z:ProfileSimulatorTestCase.test_distant_exception.<locals>.gc                      s
      d S r   r	   r	   r\   r	   r
   r]   :  s    z:ProfileSimulatorTestCase.test_distant_exception.<locals>.hc                      s
      d S r   r	   r	   r^   r	   r
   r_   <  s    z:ProfileSimulatorTestCase.test_distant_exception.<locals>.ic                    s
      d S r   r	   rD   r`   r	   r
   ra   >  s    z:ProfileSimulatorTestCase.test_distant_exception.<locals>.jr#   r   r*   rR   rb   rc   r   rG   rd   r	   re   r
   rf   5  s*    z/ProfileSimulatorTestCase.test_distant_exceptionc                    s8   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getrD   kwargsr	   r
   rF   T  s    z7ProfileSimulatorTestCase.test_unbound_method.<locals>.fr#   r   r   rG   rH   r	   rt   r
   test_unbound_methodR  s    z,ProfileSimulatorTestCase.test_unbound_methodc                 C   s0   dd }t |}| |dd|fdd|fg d S )Nc                 S   s   t   d S r   rq   rD   r	   r	   r
   rF   \  s    z?ProfileSimulatorTestCase.test_unbound_method_no_args.<locals>.fr#   r   r   rG   rH   r	   r	   r
   test_unbound_method_no_args[  s
    z4ProfileSimulatorTestCase.test_unbound_method_no_argsc                 C   s0   dd }t |}| |dd|fdd|fg d S )Nc                 S   s   t td d S ro   rr   rs   printrD   r	   r	   r
   rF   d  s    zDProfileSimulatorTestCase.test_unbound_method_invalid_args.<locals>.fr#   r   r   rG   rH   r	   r	   r
    test_unbound_method_invalid_argsc  s
    z9ProfileSimulatorTestCase.test_unbound_method_invalid_argsc                    s8   i   fdd}t |}| |dd|fdd|fg d S )Nc                    s   t jf i   d S r   rq   rD   rt   r	   r
   rF   m  s    zGProfileSimulatorTestCase.test_unbound_method_no_keyword_args.<locals>.fr#   r   r   rG   rH   r	   rt   r
   #test_unbound_method_no_keyword_argsk  s    z<ProfileSimulatorTestCase.test_unbound_method_no_keyword_argsc                    s8   i   fdd}t |}| |dd|fdd|fg d S )Nc                    s   t jtdfi   d S ro   rx   rD   rt   r	   r
   rF   v  s    zLProfileSimulatorTestCase.test_unbound_method_invalid_keyword_args.<locals>.fr#   r   r   rG   rH   r	   rt   r
   (test_unbound_method_invalid_keyword_argst  s    zAProfileSimulatorTestCase.test_unbound_method_invalid_keyword_argsN)r   r   r   r?   rJ   rn   rM   rf   rv   rw   rz   r{   r|   r	   r	   r	   r
   rm     s   			rm   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 n   Y n0 d S r   r	   )rF   rE   r	   r	   r
   protect  s    r   c                 C   sn   |d u rt  }t }t  z0t|j t| | td  W |r^t  n|r\t  0 |	 dd S )Nr#   )
r   gc	isenableddisabler   r   r"   r   enabler1   )rA   rE   Zold_gcr	   r	   r
   r>     s    


r>   c                 C   s   dd l }| t|  d S )Nr   )r@   r>   )rA   r@   r	   r	   r
   show_events  s    r   __main__)N)r   r@   r   ZunittestZTestCaser   r   r2   r=   rC   rm   r)   r   Zprotect_identr>   r   r   mainr	   r	   r	   r
   <module>   s"    # 6e
