
    <e"                        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             dS dS )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                   r    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dS )ProfileTest)print_statsprint_callersprint_calleesz:0(max)c                 .    t          t                     d S N)r   r   selfs    (/usr/lib/python3.11/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 }|                      t          d          }t                      }|                    dt                      t	                                 |                    t                      |z
             | j        D ]}t                      }t          j	        ||          }|
                                                    d            t          ||                       |                                                                }t          j                            dd          d         fd|D             }|                    d                    |                     |S )	NMbP?
testfunc()streamstdname.   c                 @    g | ]}|v |                                 S  )rstrip).0linemod_names     r   
<listcomp>z,ProfileTest.do_profiling.<locals>.<listcomp>/   s+    KKK(d:J:Jdkkmm:J:J:Jr   
)profilerclassr   runctxglobalslocalsappendmethodnamesr   pstatsStats
strip_dirs
sort_statsgetattrgetvalue
splitlinesr
   
__module__rsplitjoin)	clsresultsprofstart_timer
methodnamesstatsoutputr*   s	           @r   do_profilingzProfileTest.do_profiling   s=     ..ggL'))VXX666uww,---/ 	. 	.J

ALa000E)))444&GE:&&(((ZZ\\,,..F*11#q99!<H LKKKKKKFNN499V,,----r   c           	      2   |                                  }|                                 }|                     |d         d           g }t          | j                  D ]\  }}||         }||dz            }||k    rq|                    d| d| j        j         d           |                    t          |
                    d          |
                    d          d	                     |r*|                     d                    |                     d S d S )
Nr   i  r$   z
Stats.z output for z does not fit expectation:r,    )lineterm)rE   r   assertEqual	enumerater2   r1   r-   __name__extendr   splitfailr<   )r   r>   expectedrN   imethodabs           r   test_cprofilezProfileTest.test_cprofile3   s9   ##%%++--T***"4#344 	* 	*IAv A!AAvv 9v 9 9#1:9 9 9 : : : L%') ) ) * * * 	'IIdiioo&&&&&	' 	'r   c           	         g d}|D ]}t                      }|                     t          d          }|                    |t	                      t                                 t          j        ||          }|                                 |	                                }| 
                    | j        |d                    ||                     d S )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/   r0   r3   r4   r   r8   assertInexpected_max_outputformat)r   stmtsstmtrB   r?   rC   ress          r   test_calling_conventionsz$ProfileTest.test_calling_conventionsD   s    
 
 
  	M 	MD

A%%eU33DKKgii222La000E**,,CMM$2C9@@sKKM M M M	M 	Mr   c                 6   t                      5  | j                            d           d d d            n# 1 swxY w Y   | j                            dt                     |                     t
          j                            t                               d S )Nzint('1')filename)silentprofilermodulerunr   
assertTrueospathexistsr   s    r   test_runzProfileTest.test_runY   s    XX 	0 	0##J///	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0
V<<<v../////s   6::c                    t                      5  | j                            dt                      t	                                 d d d            n# 1 swxY w Y   | j                            dt                      t	                      t
                     |                     t          j        	                    t
                               d S )Nr   r^   )
r`   ra   r.   r/   r0   r   rc   rd   re   rf   r   s    r   test_runctxzProfileTest.test_runctx_   s    XX 	J 	J&&|WYYIII	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J""<FHH+1 	# 	3 	3 	3v../////s   5AAAc                     t          d| j        j        d           t          d| j        j        dd           t          d| j        j        dddd           d S )N-mrandom_module_xyztimeitz-n1)r   ra   rK   r   r   s    r   test_run_profile_as_modulez&ProfileTest.test_run_profile_as_modulef   so    dD$7$@$GGG 	dD$7$@"$7	9 	9 	9 	t2;xs	4 	4 	4 	4 	4r   c           	         t                      5 }t          |          5  t          j        d           t	          ddd          5 }|                    d           d d d            n# 1 swxY w Y   t          d| j        j        dd	d           | 	                    t          j
                            d	                     d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )
Ndestzdemo.pywzutf-8)encodingzimport os; os.chdir("dest")rk   z-oz
out.pstats)r   r   rd   mkdiropenwriter   ra   rK   rc   re   rf   )r   tmpdirfs      r   (test_output_file_when_changing_directoryz4ProfileTest.test_output_file_when_changing_directoryr   s   ZZ 	:6:f#5#5 	: 	:HViw777 7156667 7 7 7 7 7 7 7 7 7 7 7 7 7 7 d)2l   OOBGNN<88999	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	:sY   C&'CA(C(A,,C/A,0ACC&C	C&C	C&&C*-C*N)rK   r:   __qualname__profileProfiler-   ra   r2   rW   r   r   classmethodrE   rT   r\   rg   ri   ro   ry   r&   r   r   r   r      s        OMNCCCK#       [(' ' '"M M M*0 0 00 0 0
4 
4 
4: : : : :r   r   c           
         |                      d          } t          d| z             |                                }g }t          | d          5 }|D ].}|                    |           |                    d          r n/d d d            n# 1 swxY w Y   t          | d          5 }|                    |           |                    d           t          |j	                  D ]*\  }}|                    d|d||d	z            d
           +|                    d           d d d            d S # 1 swxY w Y   d S )NcozRegenerating %s...rz#--cutrr   z_ProfileOutput = {}
z_ProfileOutput[z	] = """\
r$   z"""
z'
if __name__ == "__main__":
    main()
)
r'   printrE   ru   r1   
startswith
writelinesrv   rJ   r2   )r_   r=   r>   newfilerx   r)   rP   rQ   s           r   regenerate_expected_outputr      s   t$$H	

)***  GG	h		  	 	DNN4   x((                
h		 >	W	'((("3?33 	+ 	+IAvGGGFFGAaCLLL* + + + +	<===> > > > > > > > > > > > > > > > > >s%   2BBB'A?D33D7:D7c               #      K   t           j        } 	 t                      t           _        d V  | t           _        d S # | t           _        w xY wr   )sysstdoutr   )r   s    r   r`   r`      sD      ZFZZ



V
s	   : Ac                      dt           j        vrt          j                     d S t	          t
          t                     d S )Nz-r)r   argvunittestmainr   __file__r   r&   r   r   r   r      s4    38"8[9999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(exc_info)                      <- 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   r3   r   rd   difflibr   ior   test.support.os_helperr   r   r   r   
contextlibr	   r{   test.profileer
   r   test.support.script_helperr   r   TestCaser   r   r`   r   r   rK   r&   r   r   <module>r      s   ( ( 



   				                   G G G G G G G G G G G G % % % % % %  ) ) ) ) ) ) ) ) N N N N N N N Nm: m: m: m: m:(# m: m: m:`> > >(   : : : 	!K} #L $#R & zDFFFFF r   