
    z%i5                     P   d dl Z d dlZd dlZd dlZ G d dej                        Z G d d      Z G d de      Z G d d	ej                        Z G d
 de      Z	 G d de      Z
d Zd Z ee      ZddZd Z G d dej                        Zedk(  r ej$                          yy)    Nc                   $    e Zd Zd Zd Zd Zd Zy)TestGetProfilec                 .    t        j                  d        y Nsys
setprofileselfs    L/home/uftp/.pyenv/versions/3.12.0/lib/python3.12/test/test_sys_setprofile.pysetUpzTestGetProfile.setUp       t    c                 .    t        j                  d        y r   r   r
   s    r   tearDownzTestGetProfile.tearDown   r   r   c                 J    | j                  t        j                                y r   )assertIsNoner   
getprofiler
   s    r   
test_emptyzTestGetProfile.test_empty   s    #..*+r   c                 |    d }t        j                  |       | j                  t        j                         |       y )Nc                       y r    argss    r   fnz&TestGetProfile.test_setget.<locals>.fn       r   )r   r	   assertIsr   )r   r   s     r   test_setgetzTestGetProfile.test_setget   s*    	 	rcnn&+r   N)__name__
__module____qualname__r   r   r   r   r   r   r   r   r      s    ,,r   r   c                   &    e Zd Zd Zd ZddZd Zy)HookWatcherc                      g | _         g | _        y r   )frameseventsr
   s    r   __init__zHookWatcher.__init__   s    r   c                 H    |dk(  s
|dk(  s|dk(  r| j                  ||       y y )Ncallreturn	exception	add_eventr   frameeventargs       r   callbackzHookWatcher.callback   s,    VO #NN5%( $r   Nc                 8   	 |t        j                  d      }	 | j                  j                  |      }| j                  j                  ||t        |      f       y # t        $ r3 t        | j                        }| j                  j                  |       Y cw xY w)N   )	r   	_getframer%   index
ValueErrorlenappendr&   ident)r   r0   r/   framenos       r   r-   zHookWatcher.add_event#   s    &=MM!$E	&kk''.G
 	GUE%L9:	  	&$++&GKKu%	&s   A 9BBc                     	 t        | j                  j                        t        t               g}d | _        | j                  D cg c]  }|d   |vs| c}S c c}w N   )r:   r-   __func__r%   r&   )r   
disalloweditems      r   
get_eventszHookWatcher.get_events0   sL    *DNN334eElC
!%JQz0IJJJs   AAr   )r   r    r!   r'   r2   r-   rB   r   r   r   r#   r#      s    );Kr   r#   c                   B    e Zd Zd Zd Zd Zd Zd Zd ZeeeeeedZ	y)	ProfileSimulatorc                 J    || _         g | _        t        j                  |        y r   )testcasestackr#   r'   )r   rF   s     r   r'   zProfileSimulator.__init__9   s     
T"r   c                 0     | j                   |   | |       y r   )dispatchr.   s       r   r2   zProfileSimulator.callback>   s    eT5)r   c                 ^    | j                  d|       | j                  j                  |       y )Nr)   )r-   rG   r9   r   r/   s     r   
trace_callzProfileSimulator.trace_callB   s"    vu%

% r   c                 \    | j                  d|       | j                  j                          y )Nr*   )r-   rG   poprK   s     r   trace_returnzProfileSimulator.trace_returnF   s    x'

r   c                 :    | j                   j                  d       y )Nz2the profiler should never receive exception events)rF   failrK   s     r   trace_exceptionz ProfileSimulator.trace_exceptionJ   s    @	Br   c                      y r   r   rK   s     r   
trace_passzProfileSimulator.trace_passN   s    r   )r)   r+   r*   c_callc_returnc_exceptionN)
r   r    r!   r'   r2   rL   rO   rR   rT   rI   r   r   r   rD   rD   8   s<    #
*!B $!
Hr   rD   c                       e Zd Zd Zy)TestCaseBasec                     t        || j                               }||k7  r>| j                  dt        j                  |      dt        j                  |             y y )NzExpected events:
z
Received events:
)capture_eventsnew_watcherrQ   pprintpformat)r   callableexpectedr&   s       r   check_eventszTestCaseBase.check_events\   sN    $*:*:*<=XII16>>&3IK L r   N)r   r    r!   ra   r   r   r   rY   rY   [   s    Lr   rY   c                   `    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zy)ProfileHookTestCasec                     t               S r   )r#   r
   s    r   r\   zProfileHookTestCase.new_watcherd   s
    }r   c                 T    d }t        |      }| j                  |dd|fdd|fg       y )Nc                      y r   r   ps    r   fz*ProfileHookTestCase.test_simple.<locals>.fh   r   r   r4   r)   r*   r:   ra   r   ri   f_idents      r   test_simplezProfileHookTestCase.test_simpleg   8    	(!q&'2 (G4  	!r   c                 T    d }t        |      }| j                  |dd|fdd|fg       y )Nc                     ddz   y Nr4   r   r   rg   s    r   ri   z-ProfileHookTestCase.test_exception.<locals>.fp   	    aCr   r4   r)   r*   rj   rk   s      r   test_exceptionz"ProfileHookTestCase.test_exceptiono   8    	(!q&'2 (G4  	!r   c                 T    d }t        |      }| j                  |dd|fdd|fg       y )Nc                     ddz   y #  Y y xY wrq   r   rg   s    r   ri   z4ProfileHookTestCase.test_caught_exception.<locals>.fx       1D    r4   r)   r*   rj   rk   s      r   test_caught_exceptionz)ProfileHookTestCase.test_caught_exceptionw   :    	 (!q&'2 (G4  	!r   c                 T    d }t        |      }| j                  |dd|fdd|fg       y )Nc                     ddz   y #  Y y xY wrq   r   rg   s    r   ri   z;ProfileHookTestCase.test_caught_nested_exception.<locals>.f   rw   rx   r4   r)   r*   rj   rk   s      r   test_caught_nested_exceptionz0ProfileHookTestCase.test_caught_nested_exception   rz   r   c                 T    d }t        |      }| j                  |dd|fdd|fg       y )Nc                     ddz   y rq   r   rg   s    r   ri   z4ProfileHookTestCase.test_nested_exception.<locals>.f   rr   r   r4   r)   r*   rj   rk   s      r   test_nested_exceptionz)ProfileHookTestCase.test_nested_exception   s:    	(!q&'2  !(G4	  	!r   c                     d fd}t              }t        |      }| j                  |dd|fdd|fdd|fdd|fdd|fdd|fg       y )Nc                     ddz   y rq   r   rg   s    r   ri   z>ProfileHookTestCase.test_exception_in_except_clause.<locals>.f   rr   r   c                 F    	  |        y #   |        Y y #  Y Y y xY wxY wr   r   rh   ri   s    r   gz>ProfileHookTestCase.test_exception_in_except_clause.<locals>.g   s#    !qTs       r4   r)   r>   r*      rj   r   r   rl   g_identri   s       @r   test_exception_in_except_clausez3ProfileHookTestCase.test_exception_in_except_clause   sq    		 ((!q&'2 &'2 (G4 &'2 (G4 (G4  	!r   c           
          d fd}t              }t        |      }| j                  |dd|fdd|fdd|fdd|fdd|fg       y )Nc                     ddz   y rq   r   rg   s    r   ri   z9ProfileHookTestCase.test_exception_propagation.<locals>.f   rr   r   c                 d     |        | j                  d       y # | j                  d       w xY w)Nfalling throughr,   r   s    r   r   z9ProfileHookTestCase.test_exception_propagation.<locals>.g   s%    1[[!23Q[[!23s    /r4   r)   r>   r*   r   rj   r   s       @r   test_exception_propagationz.ProfileHookTestCase.test_exception_propagation   sh    		4 ((!q&'2 &'2 (G4 "3W= (G4	  	!r   c                 T    d }t        |      }| j                  |dd|fdd|fg       y )Nc                 &    ddz   y #  ddz   Y y xY wrq   r   rg   s    r   ri   z/ProfileHookTestCase.test_raise_twice.<locals>.f   s    1AaCs    r4   r)   r*   rj   rk   s      r   test_raise_twicez$ProfileHookTestCase.test_raise_twice   s:    	 (!q&'2 (G4  	!r   c                 T    d }t        |      }| j                  |dd|fdd|fg       y )Nc                     ddz   y #   xY wrq   r   rg   s    r   ri   z1ProfileHookTestCase.test_raise_reraise.<locals>.f   s    1Es    
r4   r)   r*   rj   rk   s      r   test_raise_reraisez&ProfileHookTestCase.test_raise_reraise   s:    	 (!q&'2 (G4  	!r   c                 T    d }t        |      }| j                  |dd|fdd|fg       y )Nc                     t               r   )	Exceptionrg   s    r   ri   z)ProfileHookTestCase.test_raise.<locals>.f   s
    +r   r4   r)   r*   rj   rk   s      r   
test_raisezProfileHookTestCase.test_raise   s8    	(!q&'2 (G4  	!r   c                   	
 d fdfd		fd

fd}t              }t              }t        	      }t        
      }t        |      }| j                  |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
       y )Nc                      ddz   y rq   r   r   r   r   ri   z5ProfileHookTestCase.test_distant_exception.<locals>.f   rr   r   c                                y r   r   ri   s   r   r   z5ProfileHookTestCase.test_distant_exception.<locals>.g       Cr   c                                y r   r   r   s   r   hz5ProfileHookTestCase.test_distant_exception.<locals>.h   r   r   c                                y r   r   r   s   r   iz5ProfileHookTestCase.test_distant_exception.<locals>.i   r   r   c                              y r   r   rh   r   s    r   jz5ProfileHookTestCase.test_distant_exception.<locals>.j   r   r   r4   r)   r>   r         r*   rj   r   r   rl   r   h_identi_identj_identri   r   r   r   s          @@@@r   test_distant_exceptionz*ProfileHookTestCase.test_distant_exception       					(((((!q&'2 &'2 &'2 &'2 &'2 (G4 (G4 (G4 (G4 (G4
  
	!r   c                     d fd}t              }t        |      }| j                  |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       y )Nc               3   4   K   t        d      D ]  } |   y wr=   ranger   s    r   ri   z-ProfileHookTestCase.test_generator.<locals>.f        1X    c                             D ]  } y r   r   rh   r   ri   s     r   r   z-ProfileHookTestCase.test_generator.<locals>.g       S r   r4   r)   r>   r*   rj   r   s       @r   test_generatorz"ProfileHookTestCase.test_generator   s    		 ((!q&'2 &'2 (G4 &'2 (G4  !&'2 (G4 (G4  	!r   c                     d fd}t              }t        |      }| j                  |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       y )Nc               3   4   K   t        d      D ]  } |   y wr=   r   r   s    r   ri   z2ProfileHookTestCase.test_stop_iteration.<locals>.f  r   r   c                             D ]  } y r   r   r   s     r   r   z2ProfileHookTestCase.test_stop_iteration.<locals>.g  r   r   r4   r)   r>   r*   rj   r   s       @r   test_stop_iterationz'ProfileHookTestCase.test_stop_iteration  s    		 ((!q&'2 &'2 (G4 &'2 (G4 &'2 (G4 (G4
  
	!r   N)r   r    r!   r\   rm   rs   ry   r}   r   r   r   r   r   r   r   r   r   r   r   r   rc   rc   c   sH    !!!!	!!&!!!!!8!,!r   rc   c                   H    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)ProfileSimulatorTestCasec                     t        |       S r   )rD   r
   s    r   r\   z$ProfileSimulatorTestCase.new_watcher  s    %%r   c                 T    d }t        |      }| j                  |dd|fdd|fg       y )Nc                      y r   r   rg   s    r   ri   z/ProfileSimulatorTestCase.test_simple.<locals>.f  r   r   r4   r)   r*   rj   rk   s      r   rm   z$ProfileSimulatorTestCase.test_simple  rn   r   c                 T    d }t        |      }| j                  |dd|fdd|fg       y )Nc                     ddz   y rq   r   rg   s    r   ri   z8ProfileSimulatorTestCase.test_basic_exception.<locals>.f%  rr   r   r4   r)   r*   rj   rk   s      r   test_basic_exceptionz-ProfileSimulatorTestCase.test_basic_exception$  rt   r   c                 T    d }t        |      }| j                  |dd|fdd|fg       y )Nc                     ddz   y #  Y y xY wrq   r   rg   s    r   ri   z9ProfileSimulatorTestCase.test_caught_exception.<locals>.f-  rw   rx   r4   r)   r*   rj   rk   s      r   ry   z.ProfileSimulatorTestCase.test_caught_exception,  rz   r   c                   	
 d fdfd		fd

fd}t              }t              }t        	      }t        
      }t        |      }| j                  |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
       y )Nc                      ddz   y rq   r   r   r   r   ri   z:ProfileSimulatorTestCase.test_distant_exception.<locals>.f6  rr   r   c                                y r   r   r   s   r   r   z:ProfileSimulatorTestCase.test_distant_exception.<locals>.g8  r   r   c                                y r   r   r   s   r   r   z:ProfileSimulatorTestCase.test_distant_exception.<locals>.h:  r   r   c                                y r   r   r   s   r   r   z:ProfileSimulatorTestCase.test_distant_exception.<locals>.i<  r   r   c                              y r   r   r   s    r   r   z:ProfileSimulatorTestCase.test_distant_exception.<locals>.j>  r   r   r4   r)   r>   r   r   r   r*   rj   r   s          @@@@r   r   z/ProfileSimulatorTestCase.test_distant_exception5  r   r   c                 ^    i fd}t        |      }| j                  |dd|fdd|fg       y )Nc                 4    t        j                  i dfi  y N*   dictgetrh   kwargss    r   ri   z7ProfileSimulatorTestCase.test_unbound_method.<locals>.fT  s    HHR&v&r   r4   r)   r*   rj   r   ri   rl   r   s      @r   test_unbound_methodz,ProfileSimulatorTestCase.test_unbound_methodR  s>    	'(!q&'2 (G46 	7r   c                 T    d }t        |      }| j                  |dd|fdd|fg       y )Nc                 ,    t         j                          y r   r   rg   s    r   ri   z?ProfileSimulatorTestCase.test_unbound_method_no_args.<locals>.f\  s    HHJr   r4   r)   r*   rj   rk   s      r   test_unbound_method_no_argsz4ProfileSimulatorTestCase.test_unbound_method_no_args[  s8    	(!q&'2 (G46 	7r   c                 T    d }t        |      }| j                  |dd|fdd|fg       y )Nc                 8    t         j                  t        d       y r   r   r   printrg   s    r   ri   zDProfileSimulatorTestCase.test_unbound_method_invalid_args.<locals>.fd  s    HHUBr   r4   r)   r*   rj   rk   s      r    test_unbound_method_invalid_argsz9ProfileSimulatorTestCase.test_unbound_method_invalid_argsc  s8    	 (!q&'2 (G46 	7r   c                 ^    i fd}t        |      }| j                  |dd|fdd|fg       y )Nc                 0    t        j                  di  y )Nr   r   r   s    r   ri   zGProfileSimulatorTestCase.test_unbound_method_no_keyword_args.<locals>.fm  s    HHvr   r4   r)   r*   rj   r   s      @r   #test_unbound_method_no_keyword_argsz<ProfileSimulatorTestCase.test_unbound_method_no_keyword_argsk  s>    	(!q&'2 (G46 	7r   c                 ^    i fd}t        |      }| j                  |dd|fdd|fg       y )Nc                 <    t        j                  t        dfi  y r   r   r   s    r   ri   zLProfileSimulatorTestCase.test_unbound_method_invalid_keyword_args.<locals>.fv  s    HHUB)&)r   r4   r)   r*   rj   r   s      @r   (test_unbound_method_invalid_keyword_argszAProfileSimulatorTestCase.test_unbound_method_invalid_keyword_argst  s>    	*(!q&'2 (G46 	7r   N)r   r    r!   r\   rm   r   ry   r   r   r   r   r   r   r   r   r   r   r     s4    &!!!!:77777r   r   c                 |    t        | d      r| j                  }n| j                  }|j                  |j                  fS )Nf_code)hasattrr   __code__co_firstlinenoco_name)functioncodes     r   r:   r:   }  s5    x"  ,,r   c                 "     | |       y #  Y y xY wr   r   )ri   rh   s     r   protectr     s    	
1Ds   
 c                 x   |
t               }t        j                         }t        j                          	 t	        j
                  |j                         t        | |       t	        j
                  d        |rt        j                          	 |j                         dd S # |rt        j                          w w xY w)Nr4   )
r#   gc	isenableddisabler   r	   r2   r   enablerB   )r_   rh   old_gcs      r   r[   r[     s    yM \\^FJJLqzz"!tIIK<<>!B IIK s   A B   B9c                 B    dd l } |j                   t        |              y )Nr   )r]   r[   )r_   r]   s     r   show_eventsr     s    FMM.*+r   c                   $    e Zd Zd Zd Zd Zd Zy)TestEdgeCasesc                     | j                  t        j                  t        j                                t        j                  d        y r   )
addCleanupr   r	   r   r
   s    r   r   zTestEdgeCases.setUp  s'    (89tr   c                     d }d  G fdd      }t        j                   |              t        j                  |       | j                  t        j                                y )Nc                       y r   r   r   s    r   fooz*TestEdgeCases.test_reentrancy.<locals>.foo      r   c                       y r   r   r   s    r   barz*TestEdgeCases.test_reentrancy.<locals>.bar  r  r   c                       e Zd Zd Z fdZy)(TestEdgeCases.test_reentrancy.<locals>.Ac                      y r   r   )r   r   s     r   __call__z1TestEdgeCases.test_reentrancy.<locals>.A.__call__  s    r   c                 0    t        j                         y r   r   )r   r  s    r   __del__z0TestEdgeCases.test_reentrancy.<locals>.A.__del__  s    s#r   N)r   r    r!   r  r	  )r  s   r   Ar    s    $r   r
  )r   r	   assertEqualr   )r   r   r
  r  s      @r   test_reentrancyzTestEdgeCases.test_reentrancy  sJ    			$ 	$ 	qss)3/r   c                     d }t        j                  |       ~t        j                  t        j                                y )Nc                       y r   r   r   s    r   r   z+TestEdgeCases.test_same_object.<locals>.foo  r  r   )r   r	   r   )r   r   s     r   test_same_objectzTestEdgeCases.test_same_object  s+    	 	ss~~'(r   c                    d }dt        j                         _        t        j                         }t        j                  d         |        t        j                  |       t        j
                  d         |        y )Nc                       y r   r   r   r   r   ri   z9TestEdgeCases.test_profile_after_trace_opcodes.<locals>.f  r  r   Tc                       y r   r   r   s    r   <lambda>z@TestEdgeCases.test_profile_after_trace_opcodes.<locals>.<lambda>      r   c                       y r   r   r   s    r   r  z@TestEdgeCases.test_profile_after_trace_opcodes.<locals>.<lambda>  r  r   )r   r5   f_trace_opcodesgettracesettracer	   )r   ri   
prev_traces      r    test_profile_after_trace_opcodesz.TestEdgeCases.test_profile_after_trace_opcodes  sR    	 +/'\\^
'(	Z )*	r   N)r   r    r!   r   r  r  r  r   r   r   r   r     s    0$)
r   r   __main__r   )r   r]   r   unittestTestCaser   r#   rD   rY   rc   r   r:   r   protect_identr[   r   r   r   mainr   r   r   <module>r      s    	  
 ,X&& ,"K K@ 
{  
FL8$$ Lr!, r!jb7| b7J- g ",
*H%% *Z zHMMO r   