
    <e4                     p   d dl Z d dlZd dlZd dlZd dlm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                     dS dS )    N)supportc                   &    e Zd Zd Zd Zd Zd ZdS )TestGetProfilec                 .    t          j        d            d S Nsys
setprofileselfs    //usr/lib/python3.11/test/test_sys_setprofile.pysetUpzTestGetProfile.setUp	       t    c                 .    t          j        d            d S r   r   r   s    r   tearDownzTestGetProfile.tearDown   r   r   c                 R    |                      t          j                               d S r   )assertIsNoner	   
getprofiler   s    r   
test_emptyzTestGetProfile.test_empty   s$    #.**+++++r   c                     d }t          j        |           |                     t          j                    |           d S )Nc                      d S r    argss    r   fnz&TestGetProfile.test_setget.<locals>.fn       Dr   )r	   r
   assertIsr   )r   r   s     r   test_setgetzTestGetProfile.test_setget   sE    	 	 	 	rcn&&+++++r   N)__name__
__module____qualname__r   r   r   r   r   r   r   r   r      sP            , , ,, , , , ,r   r   c                   (    e Zd Zd Zd ZddZd ZdS )HookWatcherc                 "    g | _         g | _        d S r   )frameseventsr   s    r   __init__zHookWatcher.__init__   s    r   c                 Z    |dk    s|dk    s|dk    r|                      ||           d S d S )Ncallreturn	exception	add_eventr   frameeventargs       r   callbackzHookWatcher.callback   sC    VOO  ##NN5%((((( $#r   Nc                 <   	 |t          j        d          }	 | j                            |          }n># t          $ r1 t          | j                  }| j                            |           Y nw xY w| j                            ||t          |          f           d S )N   )	r	   	_getframer&   index
ValueErrorlenappendr'   ident)r   r1   r0   framenos       r   r.   zHookWatcher.add_event$   s    &=M!$$E	&k''..GG 	& 	& 	&$+&&GKu%%%%%	& 	GUE%LL9:::::s   4 8A/.A/c                     	 t          | j        j                  t          t                     gd | _        fd| j        D             S )Nc                 (    g | ]}|d          v|S )   r   ).0item
disalloweds     r   
<listcomp>z*HookWatcher.get_events.<locals>.<listcomp>6   s'    JJJQz0I0I0I0I0Ir   )r;   r.   __func__r&   r'   )r   rB   s    @r   
get_eventszHookWatcher.get_events1   sF    *DN344eEllC
JJJJJJJJr   r   )r    r!   r"   r(   r3   r.   rE   r   r   r   r$   r$      sZ          ) ) ); ; ; ;K K K K Kr   r$   c                   D    e Zd Zd Zd Zd Zd Zd Zd ZeeeeeedZ	dS )	ProfileSimulatorc                 V    || _         g | _        t                              |            d S r   )testcasestackr$   r(   )r   rI   s     r   r(   zProfileSimulator.__init__:   s*     
T"""""r   c                 4     | j         |         | |           d S r   )dispatchr/   s       r   r3   zProfileSimulator.callback?   s!    eT5)))))r   c                 f    |                      d|           | j                            |           d S )Nr*   )r.   rJ   r:   r   r0   s     r   
trace_callzProfileSimulator.trace_callC   s2    vu%%%
%     r   c                 d    |                      d|           | j                                         d S )Nr+   )r.   rJ   poprN   s     r   trace_returnzProfileSimulator.trace_returnG   s.    x'''
r   c                 :    | j                             d           d S )Nz2the profiler should never receive exception events)rI   failrN   s     r   trace_exceptionz ProfileSimulator.trace_exceptionK   s.    @	B 	B 	B 	B 	Br   c                     d S r   r   rN   s     r   
trace_passzProfileSimulator.trace_passO   s    r   )r*   r,   r+   c_callc_returnc_exceptionN)
r    r!   r"   r(   r3   rO   rR   rU   rW   rL   r   r   r   rG   rG   9   s        # # #
* * *! ! !  B B B   $!
 
HHHr   rG   c                       e Zd Zd ZdS )TestCaseBasec                     t          ||                                           }||k    rA|                     dt          j        |          dt          j        |                     d S d S )NzExpected events:
z
Received events:
)capture_eventsnew_watcherrT   pprintpformat)r   callableexpectedr'   s       r   check_eventszTestCaseBase.check_events]   s    $*:*:*<*<==XIII11116>&3I3I3IK L L L L L r   N)r    r!   r"   rd   r   r   r   r\   r\   \   s(        L L L L Lr   r\   c                   b    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dS )ProfileHookTestCasec                     t                      S r   )r$   r   s    r   r_   zProfileHookTestCase.new_watchere   s    }}r   c                 f    d }t          |          }|                     |dd|fdd|fg           d S )Nc                     d S r   r   ps    r   fz*ProfileHookTestCase.test_simple.<locals>.fi   r   r   r5   r*   r+   r;   rd   r   rl   f_idents      r   test_simplezProfileHookTestCase.test_simpleh   X    	 	 	((!q&'2 (G4  	! 	! 	! 	! 	!r   c                 f    d }t          |          }|                     |dd|fdd|fg           d S )Nc                     ddz   d S Nr5   r   r   rj   s    r   rl   z-ProfileHookTestCase.test_exception.<locals>.fq       aCCCCr   r5   r*   r+   rm   rn   s      r   test_exceptionz"ProfileHookTestCase.test_exceptionp   X    	 	 	((!q&'2 (G4  	! 	! 	! 	! 	!r   c                 f    d }t          |          }|                     |dd|fdd|fg           d S )Nc                      ddz   d S #  Y d S xY wrt   r   rj   s    r   rl   z4ProfileHookTestCase.test_caught_exception.<locals>.fy       1DDD    r5   r*   r+   rm   rn   s      r   test_caught_exceptionz)ProfileHookTestCase.test_caught_exceptionx   Z    	 	 	 ((!q&'2 (G4  	! 	! 	! 	! 	!r   c                 f    d }t          |          }|                     |dd|fdd|fg           d S )Nc                      ddz   d S #  Y d S xY wrt   r   rj   s    r   rl   z;ProfileHookTestCase.test_caught_nested_exception.<locals>.f   rz   r{   r5   r*   r+   rm   rn   s      r   test_caught_nested_exceptionz0ProfileHookTestCase.test_caught_nested_exception   r}   r   c                 f    d }t          |          }|                     |dd|fdd|fg           d S )Nc                     ddz   d S rt   r   rj   s    r   rl   z4ProfileHookTestCase.test_nested_exception.<locals>.f   ru   r   r5   r*   r+   rm   rn   s      r   test_nested_exceptionz)ProfileHookTestCase.test_nested_exception   sZ    	 	 	((!q&'2  !(G4	  	! 	! 	! 	! 	!r   c                     d f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                     ddz   d S rt   r   rj   s    r   rl   z>ProfileHookTestCase.test_exception_in_except_clause.<locals>.f   ru   r   c                 X    	  |            d S #   |            Y d S #  Y Y d S xY wxY wr   r   rk   rl   s    r   gz>ProfileHookTestCase.test_exception_in_except_clause.<locals>.g   sD    !QqTTTTTTs    ) &))r5   r*   r?   r+      rm   r   r   ro   g_identrl   s       @r   test_exception_in_except_clausez3ProfileHookTestCase.test_exception_in_except_clause   s    	 	 		 	 	 	 	 ((((!q&'2 &'2 (G4 &'2 (G4 (G4  	! 	! 	! 	! 	!r   c           
          d fd}t                    }t          |          }|                     |dd|fdd|fdd|fdd|fdd|fg           d S )Nc                     ddz   d S rt   r   rj   s    r   rl   z9ProfileHookTestCase.test_exception_propagation.<locals>.f   ru   r   c                 |     |            |                      d           d S # |                      d           w xY w)Nfalling throughr-   r   s    r   r   z9ProfileHookTestCase.test_exception_propagation.<locals>.g   sB    1[[!233333Q[[!23333s   $ ;r5   r*   r?   r+   r   rm   r   s       @r   test_exception_propagationz.ProfileHookTestCase.test_exception_propagation   s    	 	 		4 	4 	4 	4 	4 ((((!q&'2 &'2 (G4 "3W= (G4	  	! 	! 	! 	! 	!r   c                 f    d }t          |          }|                     |dd|fdd|fg           d S )Nc                 *    ddz   d S #  ddz   Y d S xY wrt   r   rj   s    r   rl   z/ProfileHookTestCase.test_raise_twice.<locals>.f   s$    1AaCCCCCs    r5   r*   r+   rm   rn   s      r   test_raise_twicez$ProfileHookTestCase.test_raise_twice   sZ    	 	 	 ((!q&'2 (G4  	! 	! 	! 	! 	!r   c                 f    d }t          |          }|                     |dd|fdd|fg           d S )Nc                     ddz   d S #   xY wrt   r   rj   s    r   rl   z1ProfileHookTestCase.test_raise_reraise.<locals>.f   s    1Es    r5   r*   r+   rm   rn   s      r   test_raise_reraisez&ProfileHookTestCase.test_raise_reraise   sZ    	 	 	 ((!q&'2 (G4  	! 	! 	! 	! 	!r   c                 f    d }t          |          }|                     |dd|fdd|fg           d S )Nc                     t                      r   )	Exceptionrj   s    r   rl   z)ProfileHookTestCase.test_raise.<locals>.f   s    ++r   r5   r*   r+   rm   rn   s      r   
test_raisezProfileHookTestCase.test_raise   sX    	 	 	((!q&'2 (G4  	! 	! 	! 	! 	!r   c                 N  	
 d fdfd		fd

f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                      ddz   d S rt   r   r   r   r   rl   z5ProfileHookTestCase.test_distant_exception.<locals>.f   ru   r   c                                     d S r   r   rl   s   r   r   z5ProfileHookTestCase.test_distant_exception.<locals>.g       ACCCCCr   c                                     d S r   r   r   s   r   hz5ProfileHookTestCase.test_distant_exception.<locals>.h   r   r   c                                     d S r   r   r   s   r   iz5ProfileHookTestCase.test_distant_exception.<locals>.i   r   r   c                                   d S r   r   rk   r   s    r   jz5ProfileHookTestCase.test_distant_exception.<locals>.j   r   r   r5   r*   r?   r         r+   rm   r   r   ro   r   h_identi_identj_identrl   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          |          }|                     |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               3   6   K   t          d          D ]} | V  d S Nr?   ranger   s    r   rl   z-ProfileHookTestCase.test_generator.<locals>.f   0      1XX   r   c                 "                 D ]}d S r   r   rk   r   rl   s     r   r   z-ProfileHookTestCase.test_generator.<locals>.g   %    QSS   r   r5   r*   r?   r+   rm   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          |          }|                     |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               3   6   K   t          d          D ]} | V  d S r   r   r   s    r   rl   z2ProfileHookTestCase.test_stop_iteration.<locals>.f  r   r   c                 "                 D ]}d S r   r   r   s     r   r   z2ProfileHookTestCase.test_stop_iteration.<locals>.g  r   r   r5   r*   r?   r+   rm   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_   rp   rv   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   rf   rf   d   s          ! ! !! ! !! ! !! ! !	! 	! 	!! ! !&! ! !! ! !! ! !! ! !! ! !8! ! !,! ! ! ! !r   rf   c                   J    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S )ProfileSimulatorTestCasec                      t          |           S r   )rG   r   s    r   r_   z$ProfileSimulatorTestCase.new_watcher  s    %%%r   c                 f    d }t          |          }|                     |dd|fdd|fg           d S )Nc                     d S r   r   rj   s    r   rl   z/ProfileSimulatorTestCase.test_simple.<locals>.f  r   r   r5   r*   r+   rm   rn   s      r   rp   z$ProfileSimulatorTestCase.test_simple  rq   r   c                 f    d }t          |          }|                     |dd|fdd|fg           d S )Nc                     ddz   d S rt   r   rj   s    r   rl   z8ProfileSimulatorTestCase.test_basic_exception.<locals>.f&  ru   r   r5   r*   r+   rm   rn   s      r   test_basic_exceptionz-ProfileSimulatorTestCase.test_basic_exception%  rw   r   c                 f    d }t          |          }|                     |dd|fdd|fg           d S )Nc                      ddz   d S #  Y d S xY wrt   r   rj   s    r   rl   z9ProfileSimulatorTestCase.test_caught_exception.<locals>.f.  rz   r{   r5   r*   r+   rm   rn   s      r   r|   z.ProfileSimulatorTestCase.test_caught_exception-  r}   r   c                 N  	
 d fdfd		fd

f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                      ddz   d S rt   r   r   r   r   rl   z:ProfileSimulatorTestCase.test_distant_exception.<locals>.f7  ru   r   c                                     d S r   r   r   s   r   r   z:ProfileSimulatorTestCase.test_distant_exception.<locals>.g9  r   r   c                                     d S r   r   r   s   r   r   z:ProfileSimulatorTestCase.test_distant_exception.<locals>.h;  r   r   c                                     d S r   r   r   s   r   r   z:ProfileSimulatorTestCase.test_distant_exception.<locals>.i=  r   r   c                                   d S r   r   r   s    r   r   z:ProfileSimulatorTestCase.test_distant_exception.<locals>.j?  r   r   r5   r*   r?   r   r   r   r+   rm   r   s          @@@@r   r   z/ProfileSimulatorTestCase.test_distant_exception6  r   r   c                 p    i fd}t          |          }|                     |dd|fdd|fg           d S )Nc                 .    t          j        i dfi  d S N*   dictgetrk   kwargss    r   rl   z7ProfileSimulatorTestCase.test_unbound_method.<locals>.fU  s#    HR&&v&&&&&r   r5   r*   r+   rm   r   rl   ro   r   s      @r   test_unbound_methodz,ProfileSimulatorTestCase.test_unbound_methodS  sh    	' 	' 	' 	' 	'((!q&'2 (G46 	7 	7 	7 	7 	7r   c                 f    d }t          |          }|                     |dd|fdd|fg           d S )Nc                 8    t                                            d S r   r   rj   s    r   rl   z?ProfileSimulatorTestCase.test_unbound_method_no_args.<locals>.f]  s    HHJJJJJr   r5   r*   r+   rm   rn   s      r   test_unbound_method_no_argsz4ProfileSimulatorTestCase.test_unbound_method_no_args\  sX    	 	 	((!q&'2 (G46 	7 	7 	7 	7 	7r   c                 f    d }t          |          }|                     |dd|fdd|fg           d S )Nc                 F    t                               t          d           d S r   r   r   printrj   s    r   rl   zDProfileSimulatorTestCase.test_unbound_method_invalid_args.<locals>.fe  s    HHUBr   r5   r*   r+   rm   rn   s      r    test_unbound_method_invalid_argsz9ProfileSimulatorTestCase.test_unbound_method_invalid_argsd  sX    	  	  	 ((!q&'2 (G46 	7 	7 	7 	7 	7r   c                 p    i fd}t          |          }|                     |dd|fdd|fg           d S )Nc                 *    t          j        di  d S )Nr   r   r   s    r   rl   zGProfileSimulatorTestCase.test_unbound_method_no_keyword_args.<locals>.fn  s    Hvr   r5   r*   r+   rm   r   s      @r   #test_unbound_method_no_keyword_argsz<ProfileSimulatorTestCase.test_unbound_method_no_keyword_argsl  sh    	 	 	 	 	((!q&'2 (G46 	7 	7 	7 	7 	7r   c                 p    i fd}t          |          }|                     |dd|fdd|fg           d S )Nc                 8    t          j        t          dfi  d S r   r   r   s    r   rl   zLProfileSimulatorTestCase.test_unbound_method_invalid_keyword_args.<locals>.fw  s#    HUB))&)))))r   r5   r*   r+   rm   r   s      @r   (test_unbound_method_invalid_keyword_argszAProfileSimulatorTestCase.test_unbound_method_invalid_keyword_argsu  sh    	* 	* 	* 	* 	*((!q&'2 (G46 	7 	7 	7 	7 	7r   N)r    r!   r"   r_   rp   r   r|   r   r   r   r   r   r   r   r   r   r   r     s        & & &! ! !! ! !! ! !! ! !:7 7 77 7 77 7 77 7 7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                 ,     | |           d S #  Y d S xY wr   r   )rl   rk   s     r   protectr     s     	
1DDDs    c                    |t                      }t          j                    }t          j                     	 t	          j        |j                   t          | |           t	          j        d            |rt          j                     n# |rt          j                     w w xY w|	                                dd         S )Nr5   )
r$   gc	isenableddisabler	   r
   r3   r   enablerE   )rb   rk   old_gcs      r   r^   r^     s    yMM \^^FJLLLqz"""!t 	IKKK  	IKKKK	<<>>!B$s   =B B#c                 H    dd l } |j         t          |                      d S )Nr   )r`   r^   )rb   r`   s     r   show_eventsr     s+    MMMFM.**+++++r   c                        e Zd Zd Zd Zd ZdS )TestEdgeCasesc                     |                      t          j        t          j                               t          j        d            d S r   )
addCleanupr	   r
   r   r   s    r   r   zTestEdgeCases.setUp  s6    (8(8999tr   c                    d }d  G fdd          }t          j         |                       t          j                    5 }t          j        |           |                     |j        j        |j                   |                     |j        j	        t                     d d d            n# 1 swxY w Y   |                     t          j                    |           d S )Nc                      d S r   r   r   s    r   fooz*TestEdgeCases.test_reentrancy.<locals>.foo      Cr   c                      d S r   r   r   s    r   barz*TestEdgeCases.test_reentrancy.<locals>.bar  r  r   c                        e Zd Zd Z fdZdS )(TestEdgeCases.test_reentrancy.<locals>.Ac                     d S r   r   )r   r   s     r   __call__z1TestEdgeCases.test_reentrancy.<locals>.A.__call__  s    r   c                 0    t          j                   d S 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
   r   catch_unraisable_exceptionassertEqual
unraisableobjectr  assertIsInstance	exc_valueRuntimeErrorr   )r   r  r  cmr  s       @r   test_reentrancyzTestEdgeCases.test_reentrancy  sJ   	 	 		 	 		$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	qqss/11 	IRN3R]119===!!"-"9<HHH	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I
 	))3/////s   AB11B58B5c                     d }t          j        |           ~t          j        t          j                               d S )Nc                      d S 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  sD    	 	 	 	ss~''(((((r   N)r    r!   r"   r   r  r  r   r   r   r   r     sA          0 0 0.) ) ) ) )r   r   __main__r   )r   r`   r	   unittesttestr   TestCaser   r$   rG   r\   rf   r   r;   r   protect_identr^   r   r   r    mainr   r   r   <module>r      s   				  



       , , , , ,X& , , ,"K K K K K K K K@ 
  
  
  
  
{  
  
  
FL L L L L8$ L L Lr! r! r! r! r!, r! r! r!jb7 b7 b7 b7 b7| b7 b7 b7J- - -   g       ", , ,
#) #) #) #) #)H% #) #) #)L zHMOOOOO r   