o
    HXh"                     @   s   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
 d
ejZdd Zedd Zdd Zi Zded< ded< ded< edkrre  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                   @   sn   e Zd ZejZeZg dZdZdd Z	dd Z
edd Zd	d
 Zdd Zdd Zdd Zdd Zdd ZdS )ProfileTest)print_statsprint_callersprint_calleesz:0(max)c                 C   s   t t d S N)r   r   self r   ;/opt/python-3.10.19/usr/lib/python3.10/test/test_profile.pytearDown   s   zProfileTest.tearDownc                 C   s   t S r   )_ProfileOutputr   r   r   r   get_expected_output   s   zProfileTest.get_expected_outputc                    s   g }|  td}t }|dt t  |t |  | jD ]9}t }tj	||d}|
 d t||  |  }tjddd   fdd|D }|d	| q|S )
NMbP?
testfunc()streamZstdname.   c                    s   g | ]
} |v r|  qS r   )rstrip).0lineZmod_namer   r   
<listcomp>/   s    z,ProfileTest.do_profiling.<locals>.<listcomp>
)profilerclassr
   runctxglobalslocalsappendmethodnamesr   pstatsStatsZ
strip_dirsZ
sort_statsgetattrgetvalue
splitlinesr	   
__module__rsplitjoin)clsresultsprofZstart_timerZ
methodnamesstatsoutputr   r"   r   do_profiling   s   
zProfileTest.do_profilingc                 C   s   |   }|  }| |d d g }t| jD ]0\}}|| }||d  }||krG|d| d| jj d |t	|
d|
ddd	 q|rT| d| d S d S )
Nr   i  r   z
Stats.z output for z does not fit expectation:r$    )Zlineterm)r9   r   ZassertEqual	enumerater*   r)   r%   __name__extendr   splitfailr2   )r   r4   Zexpectedr?   imethodabr   r   r   test_cprofile3   s$   
zProfileTest.test_cprofilec              	   C   sp   g d}|D ]/}t  }| td}||t t  tj||d}|  |	 }| 
| j|d|| q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'   r(   r+   r,   r   r.   ZassertInexpected_max_outputformat)r   ZstmtsZstmtr6   r5   r7   resr   r   r   test_calling_conventionsD   s   

z$ProfileTest.test_calling_conventionsc                 C   sX   t   | jd W d    n1 sw   Y  | jjdtd | tjt d S )Nzint('1')filename)silentprofilermodulerunr   
assertTrueospathexistsr   r   r   r   test_runY   s
   zProfileTest.test_runc                 C   sh   t   | jdt t  W d    n1 sw   Y  | jjdt t td | tj	t d S )Nr   rI   )
rK   rL   r&   r'   r(   r   rN   rO   rP   rQ   r   r   r   r   test_runctx_   s   zProfileTest.test_runctxc                 C   s<   t d| jjd t d| jjdd td| jjdddd d S )N-mZrandom_module_xyzZtimeitz-n1)r   rL   r<   r   r   r   r   r   test_run_profile_as_modulef   s   

z&ProfileTest.test_run_profile_as_modulec              
   C   s   t  Y}t|< td tdddd}|d W d    n1 s%w   Y  td| jjdd	d | 	tj
d	 W d    n1 sGw   Y  W d    d S W d    d S 1 s_w   Y  d S )
Ndestzdemo.pywzutf-8)encodingzimport os; os.chdir("dest")rT   z-oz
out.pstats)r   r   rO   mkdiropenwriter   rL   r<   rN   rP   rQ   )r   Ztmpdirfr   r   r   (test_output_file_when_changing_directoryr   s   
Pz4ProfileTest.test_output_file_when_changing_directoryN)r<   r0   __qualname__profileZProfiler%   rL   r*   rE   r   r   classmethodr9   rD   rH   rR   rS   rV   r^   r   r   r   r   r      s    
r   c              	   C   s   |  d} td|   | }g }t| d}|D ]}|| |dr' nqW d    n1 s2w   Y  t| d/}|| |d t|j	D ]\}}|d|||d  f  qL|d	 W d    d S 1 snw   Y  d S )
NcozRegenerating %s...rz#--cutrX   z_ProfileOutput = {}
z _ProfileOutput[%r] = """\
%s"""
r   z'
if __name__ == "__main__":
    main()
)
r   printr9   r[   r)   
startswith
writelinesr\   r;   r*   )rJ   r3   r4   newfiler]   r!   r@   rA   r   r   r   regenerate_expected_output   s*   





"rh   c                  c   s,    t j} zt t _d V  W | t _d S | t _w r   )sysstdoutr   )rj   r   r   r   rK      s   rK   c                   C   s$   dt jvrt  d S ttt d S )Nz-r)ri   argvunittestmainrh   __file__r   r   r   r   r   rm      s   
rm   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__ri   r+   rl   rO   Zdifflibr   ior   Ztest.support.os_helperr   r   r   r   
contextlibr   r`   Ztest.profileer	   r
   Ztest.support.script_helperr   r   ZTestCaser   rh   rK   rm   r   r<   r   r   r   r   <module>   s0    p
		

