
    daf"                        d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
mZmZmZ ddlmZ ddlZddlmZmZ ddlmZmZ  G d	 d
ej.                        Zd Zed        Zd Zi Zded<   ded<   ded<   edk(  r e        yy)z"Test suite for the profile module.    N)unified_diff)StringIO)TESTFNunlinktemp_dir
change_cwd)contextmanager)testfunctimer)assert_python_failureassert_python_okc                   t    e Zd Zej                  ZeZg dZdZd Z	d Z
ed        Zd Zd Zd Zd	 Zd
 Zd Zy)ProfileTest)print_statsprint_callersprint_calleesz:0(max)c                 "    t        t               y N)r   r   selfs    ,/root/Python-3.12.4/Lib/test/test_profile.pytearDownzProfileTest.tearDown   s	    v    c                     t         S r   )_ProfileOutputr   s    r   get_expected_outputzProfileTest.get_expected_output   s    r   c                    g }| j                  t        d      }t               }|j                  dt               t	                      |j                  t               |z
         | j                  D ]  }t               }t        j                  ||      }|j                         j                  d        t        ||              |j                         j                         }t        j                   j#                  dd      d   }|D 	cg c]  }	||	v s|	j%                          }}	|j                  dj'                  |              |S c c}	w )NMbP?
testfunc()streamstdname.   
)profilerclassr   runctxglobalslocalsappendmethodnamesr   pstatsStats
strip_dirs
sort_statsgetattrgetvalue
splitlinesr
   
__module__rsplitrstripjoin)
clsresultsprofstart_timer
methodnamesstatsoutputmod_namelines
             r   do_profilingzProfileTest.do_profiling   s     .gL')VX6uw,-// 	.J
ALLa0E)))4&GE:&(ZZ\,,.F**11#q9!<H 17K(d:JdkkmKFKNN499V,-	.  Ls   	E	E	c           	         | j                         }| j                         }| j                  |d   d       g }t        | j                        D ]  \  }}||   }||dz      }||k7  s|j                  d| d| j                  j                   d       |j                  t        |j                  d      |j                  d      d	              |r!| j                  dj                  |             y y )
Nr   i  r$   z
Stats.z output for z does not fit expectation:r%    )lineterm)rA   r   assertEqual	enumerater+   r*   r&   __name__extendr   splitfailr6   )r   r8   expectedrJ   imethodabs           r   test_cprofilezProfileTest.test_cprofile3   s    ##%++-T*"4#3#34 	*IAv A!AAvhvhl#11::; <99 : L%') *	* IIdiio& r   c           	      h   g d}|D ]  }t               }| j                  t        d      }|j                  |t	               t                      t        j                  ||      }|j                          |j                         }| j                  | j                  |dj                  ||              y )N)zmax([0])zmax([0], key=int)zmax([0], **dict(key=int))zmax(*([0],))zmax(*([0],), key=int)zmax(*([0],), **dict(key=int))r   r    z&Profiling {0!r} didn't report max:
{1})r   r&   r   r'   r(   r)   r,   r-   r   r1   assertInexpected_max_outputformat)r   stmtsstmtr<   r9   r=   ress          r   test_calling_conventionsz$ProfileTest.test_calling_conventionsD   s    
  	MD
A%%eU3DKKgi2LLa0E**,CMM$22C9@@sKM	Mr   c                    t               5  | j                  j                  d       d d d        | j                  j                  dt               | j	                  t
        j                  j                  t                     y # 1 sw Y   ]xY w)Nzint('1')filename)silentprofilermodulerunr   
assertTrueospathexistsr   s    r   test_runzProfileTest.test_runY   sb    X 	0##J/	0
V<v./	0 	0s   BBc                 f   t               5  | j                  j                  dt               t	                      d d d        | j                  j                  dt               t	               t
               | j                  t        j                  j                  t
                     y # 1 sw Y   oxY w)Nr   rZ   )
r\   r]   r'   r(   r)   r   r_   r`   ra   rb   r   s    r   test_runctxzProfileTest.test_runctx_   s{    X 	J&&|WYI	J""<FH+1 	# 	3v./		J 	Js   .B''B0c                     t        d| j                  j                  d       t        d| j                  j                  dd       t        d| j                  j                  dddd       y )N-mrandom_module_xyztimeitz-n1)r   r]   rG   r   r   s    r   test_run_profile_as_modulez&ProfileTest.test_run_profile_as_modulef   s]    dD$7$7$@$@$G 	dD$7$7$@$@"$7	9 	t22;;xs	4r   c           	         t               5 }t        |      5  t        j                  d       t	        ddd      5 }|j                  d       d d d        t        d| j                  j                  dd	d       | j                  t        j                  j                  d	             d d d        d d d        y # 1 sw Y   kxY w# 1 sw Y   xY w# 1 sw Y   y xY w)
Ndestzdemo.pywzutf-8)encodingzimport os; os.chdir("dest")rg   z-oz
out.pstats)r   r   r`   mkdiropenwriter   r]   rG   r_   ra   rb   )r   tmpdirfs      r   (test_output_file_when_changing_directoryz4ProfileTest.test_output_file_when_changing_directoryr   s    Z 	:6:f#5 	:HHViw7 71567 d))22l OOBGGNN<89	: 	: 	:7 7	: 	: 	: 	:s:   C$CB7AC&C7C <CC	CCN)rG   r3   __qualname__profileProfiler&   r]   r+   rS   r   r   classmethodrA   rP   rX   rc   re   rk   ru    r   r   r   r      sX    OOMNCK#  ('"M*00
4:r   r   c           
         | j                  d      } t        d| z         |j                         }g }t        | d      5 }|D ]&  }|j	                  |       |j                  d      s& n d d d        t        | d      5 }|j                  |       |j                  d       t        |j                        D ]#  \  }}|j                  d|d||d	z      d
       % |j                  d       d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)NcozRegenerating %s...rz#--cutrn   z_ProfileOutput = {}
z_ProfileOutput[z	] = """\
r$   z"""
z'
if __name__ == "__main__":
    main()
)
r5   printrA   rq   r*   
startswith
writelinesrr   rF   r+   )r[   r7   r8   newfilert   r@   rL   rM   s           r   regenerate_expected_outputr      s   t$H	

)* GG	h	  	DNN4 x(	 
h	 >	W	'("3??3 	+IAvGGGAaCL* +	+ 	
<=> > > >s   (C6'C6>A/D6C?Dc               #      K   t         j                  } 	 t               t         _        d  | t         _        y # | t         _        w xY wwr   )sysstdoutr   )r   s    r   r\   r\      s.     ZZFZ

V
s   A7 AAAc                  z    dt         j                  vrt        j                          y t	        t
        t               y )Nz-r)r   argvunittestmainr   __file__r   rz   r   r   r   r      s!    388"8[9r   a         28   27.972    0.999   27.972    0.999 profilee.py:110(__getattr__)
        1  269.996  269.996  999.769  999.769 profilee.py:25(testfunc)
     23/3  149.937    6.519  169.917   56.639 profilee.py:35(factorial)
       20   19.980    0.999   19.980    0.999 profilee.py:48(mul)
        2   39.986   19.993  599.830  299.915 profilee.py:55(helper)
        4  115.984   28.996  119.964   29.991 profilee.py:73(helper1)
        2   -0.006   -0.003  139.946   69.973 profilee.py:84(helper2_indirect)
        8  311.976   38.997  399.912   49.989 profilee.py:88(helper2)
        8   63.976    7.997   79.960    9.995 profilee.py:98(subhelper)r   a  :0(append)                        <- profilee.py:73(helper1)(4)  119.964
:0(exception)                     <- profilee.py:73(helper1)(4)  119.964
:0(hasattr)                       <- profilee.py:73(helper1)(4)  119.964
                                     profilee.py:88(helper2)(8)  399.912
profilee.py:110(__getattr__)      <- :0(hasattr)(12)   11.964
                                     profilee.py:98(subhelper)(16)   79.960
profilee.py:25(testfunc)          <- <string>:1(<module>)(1)  999.767
profilee.py:35(factorial)         <- profilee.py:25(testfunc)(1)  999.769
                                     profilee.py:35(factorial)(20)  169.917
                                     profilee.py:84(helper2_indirect)(2)  139.946
profilee.py:48(mul)               <- profilee.py:35(factorial)(20)  169.917
profilee.py:55(helper)            <- profilee.py:25(testfunc)(2)  999.769
profilee.py:73(helper1)           <- profilee.py:55(helper)(4)  599.830
profilee.py:84(helper2_indirect)  <- profilee.py:55(helper)(2)  599.830
profilee.py:88(helper2)           <- profilee.py:55(helper)(6)  599.830
                                     profilee.py:84(helper2_indirect)(2)  139.946
profilee.py:98(subhelper)         <- profilee.py:88(helper2)(8)  399.912r   a  :0(hasattr)                       -> profilee.py:110(__getattr__)(12)   27.972
<string>:1(<module>)              -> profilee.py:25(testfunc)(1)  999.769
profilee.py:110(__getattr__)      ->
profilee.py:25(testfunc)          -> profilee.py:35(factorial)(1)  169.917
                                     profilee.py:55(helper)(2)  599.830
profilee.py:35(factorial)         -> profilee.py:35(factorial)(20)  169.917
                                     profilee.py:48(mul)(20)   19.980
profilee.py:48(mul)               ->
profilee.py:55(helper)            -> profilee.py:73(helper1)(4)  119.964
                                     profilee.py:84(helper2_indirect)(2)  139.946
                                     profilee.py:88(helper2)(6)  399.912
profilee.py:73(helper1)           -> :0(append)(4)   -0.004
profilee.py:84(helper2_indirect)  -> profilee.py:35(factorial)(2)  169.917
                                     profilee.py:88(helper2)(2)  399.912
profilee.py:88(helper2)           -> :0(hasattr)(8)   11.964
                                     profilee.py:98(subhelper)(8)   79.960
profilee.py:98(subhelper)         -> profilee.py:110(__getattr__)(16)   27.972r   __main__)__doc__r   r,   r   r`   difflibr   ior   test.support.os_helperr   r   r   r   
contextlibr	   rw   test.profileer
   r   test.support.script_helperr   r   TestCaser   r   r\   r   r   rG   rz   r   r   <module>r      s    ( 
   	    G G %  ) Nm:(## m:`>(  : 	!K} #L $#R & zF r   