a
    zeO                     @   s  d dl Z d dlZd dlmZmZmZmZmZmZ d dl	m
Z
m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 Zdd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! ZG d"d# d#e Z!G d$d% d%ej"Z#G d&d' d'ej"Z$G d(d) d)ej"Z%G d*d+ d+ej"Z&G d,d- d-ej"Z'G d.d/ d/ej"Z(G d0d1 d1ej"Z)G d2d3 d3ej"Z*e+d4kre,  dS )5    N)TESTFNTESTFN_UNICODEFS_NONASCIIrmtreeunlinkcaptured_stdoutassert_python_okassert_python_failure)Trace)testmodc                 C   s   |  dr| d d } | S )Nz.pyc)endswith)filename r   %/usr/lib/python3.9/test/test_trace.py
fix_ext_py   s    
r   c                  C   s$   t jt jtd } tt| fS )Nr   )ospathsplitextbasename__file__r   )modnamer   r   r   my_file_and_modname   s    r   c                 C   s   | j jS N)__code__co_firstlinenofuncr   r   r   get_firstlineno   s    r   c                 C   s   | }|}|| }|S r   r   )xyabcr   r   r   traced_func_linear$   s    r%   c                 C   s   | }t dD ]}||7 }q|S N   range)r    r!   r$   ir   r   r   traced_func_loop*   s    
r+   c                 C   s   | | t d S )N   )r   r   )r    r!   r   r   r   traced_func_importing0   s    r-   c                 C   s   t | | }||  S r   r%   )r    r$   r   r   r   traced_func_simple_caller3   s    
r/   c                 C   s   t | }|t|| 7 }|S r   )r/   r-   )r    kr   r   r   traced_func_importing_caller7   s    r1   c                 c   s    d}t | D ]}|| V  qd S r&   r(   )numr$   r*   r   r   r   traced_func_generator<   s    r3   c                  C   s   d} t dD ]}| |7 } qd S )Nr   
   )r3   )r0   r*   r   r   r   traced_func_calling_generatorA   s    r5   c                 C   s   | d S N   r   )r2   r   r   r   traced_doublerF   s    r8   c                  O   s   | |fS r   r   )argskwargsr   r   r   traced_capturerI   s    r;   c                  C   s   d} dd t | D }|S )Nr4   c                 S   s   g | ]}t |qS r   )r8   ).0r*   r   r   r   
<listcomp>N       z4traced_caller_list_comprehension.<locals>.<listcomp>r(   )r0   Zmylistr   r   r    traced_caller_list_comprehensionL   s    r?   c                  C   s,   dd } dd }| | dd }|  d S )Nc                 S   s   | S r   r   fr   r   r   
decorator1R   s    z-traced_decorated_function.<locals>.decorator1c                  S   s   dd } | S )Nc                 S   s   | S r   r   r@   r   r   r   
decorator2U   s    zGtraced_decorated_function.<locals>.decorator_fabric.<locals>.decorator2r   rC   r   r   r   decorator_fabricT   s    z3traced_decorated_function.<locals>.decorator_fabricc                   S   s   d S r   r   r   r   r   r   r   X   s    z'traced_decorated_function.<locals>.funcr   )rB   rE   r   r   r   r   traced_decorated_functionQ   s    rF   c                   @   s<   e Zd Zdd Zdd Zdd Zedd Zed	d
 Z	dS )TracedClassc                 C   s
   || _ d S r   r"   )selfr    r   r   r   __init__`   s    zTracedClass.__init__c                 C   s
   | j | S r   rH   )rI   r!   r   r   r   inst_method_linearc   s    TracedClass.inst_method_linearc                 C   s   |  |}|t|| S r   )rK   r%   )rI   r    r$   r   r   r   inst_method_callingf   s    
TracedClass.inst_method_callingc                 C   s   |d S r6   r   )clsr!   r   r   r   class_method_linearj   s    zTracedClass.class_method_linearc                 C   s   | d S r6   r   )r!   r   r   r   static_method_linearn   s    z TracedClass.static_method_linearN)
__name__
__module____qualname__rJ   rK   rM   classmethodrP   staticmethodrQ   r   r   r   r   rG   _   s   
rG   c                   @   sL   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dS )TestLineCountsc                 C   s2   |  tjt  tddddd| _tt| _d S )Nr,   r   counttrace
countfuncscountcallers)	
addCleanupsyssettracegettracer   tracerr   r   my_py_filenamerI   r   r   r   setUpx   s    zTestLineCounts.setUpc                 C   sb   | j tdd}| |d i }tt}tddD ]}d|| j|| f< q2| | j  j| d S )Nr7   r'      r,   )	ra   runfuncr%   assertEqualr   r)   rb   resultscounts)rI   resultexpectedfirstlinenor*   r   r   r   test_traced_func_linear}   s    z&TestLineCounts.test_traced_func_linearc              	   C   sl   | j tdd tt}| j|d fd| j|d fd| j|d fd| j|d fdi}| | j  j| d S )Nr7      r,      r'      )ra   rf   r+   r   rb   rg   rh   ri   rI   rl   rk   r   r   r   test_traced_func_loop   s    z$TestLineCounts.test_traced_func_loopc                 C   s^   | j tdd tt}| j|d fdttjdfdttjdfdi}| | j 	 j
| d S )Nr7   r'   r,   rn   )ra   rf   r-   r   rb   r   r   r   rg   rh   ri   rq   r   r   r   test_traced_func_importing   s    z)TestLineCounts.test_traced_func_importingc                 C   s   | j t tt}tt}| j|d fd| j|d fd| j|d fd| j|d fd| j|d fd| j|d fdi}| | j  j| d S )Nr,   r7      rn   r4   )	ra   rf   r5   r   r3   rb   rg   rh   ri   )rI   firstlineno_callingZfirstlineno_genrk   r   r   r   test_trace_func_generator   s    z(TestLineCounts.test_trace_func_generatorc              	   C   sp   | j t tt}tt}| j|d fd| j|d fd| j|d fd| j|d fdi}| | j  j| d S )Nr,   r7      rn   r4   )	ra   rf   r?   r   r8   rb   rg   rh   ri   )rI   ru   Zfirstlineno_calledrk   r   r   r   test_trace_list_comprehension   s    z,TestLineCounts.test_trace_list_comprehensionc                 C   s   | j t tt}| j|d fd| j|d fd| j|d fd| j|d fd| j|d fd| j|d fd| j|d fd| j|d fd| j|d	 fd| j|d
 fd| j|d fdi}| | j  j| d S )Nr,   r7   rn   rp   r'   ro   re      	   r4   rt   )ra   rf   rF   r   rb   rg   rh   ri   rq   r   r   r   test_traced_decorated_function   s    z-TestLineCounts.test_traced_decorated_functionc                 C   sh   dD ]^}t ddddd}td}t||}||d t|}| j|d fdi}| | j| qd S )N)rK   r,   r   rX         )	r   rG   getattrrf   r   rb   rg   rh   ri   )rI   Zmethnamera   Z
traced_objmethodrl   rk   r   r   r   test_linear_methods   s    
z"TestLineCounts.test_linear_methodsN)rR   rS   rT   rd   rm   rr   rs   rv   rx   r{   r   r   r   r   r   rW   v   s   rW   c                   @   s   e Zd Zdd Zdd ZdS )TestRunExecCountsc                 C   s    t t| _| tjt  d S r   )r   r   rb   r]   r^   r_   r`   rc   r   r   r   rd      s    
zTestRunExecCounts.setUpc              	   C   s   t ddddd| _d}t|td}| j|t t  tt}| j	|d fd| j	|d fd| j	|d fd	| j	|d
 fdi}|
 D ] }| | j j| ||  qd S )Nr,   r   rX   ztraced_func_loop(2, 5)execr7   ro   rn   r'   rp   )r   ra   compiler   Zrunctxglobalsvarsr   r+   rb   keysrg   rh   ri   )rI   coderl   rk   r0   r   r   r   test_exec_counts   s    z"TestRunExecCounts.test_exec_countsN)rR   rS   rT   rd   r   r   r   r   r   r      s   r   c                   @   s^   e Zd Zdd Zdd Zdd Zdd Zd	d
 Ze	e
edoDe ddd Zdd ZdS )	TestFuncsc                 C   s8   |  tjt  tdddd| _t | _t | _d S )Nr   r,   )rY   rZ   r[   )	r]   r^   r_   r`   r   ra   r   filemod_saved_tracefuncrc   r   r   r   rd     s    zTestFuncs.setUpc                 C   s   | j d urt| j  d S r   )r   r^   r_   rc   r   r   r   tearDown  s    
zTestFuncs.tearDownc                 C   s>   | j td | jd d| jd di}| | j  j| d S )Nr,   r/   r.   )ra   rf   r/   r   rg   rh   calledfuncsrI   rk   r   r   r   test_simple_caller  s
    

zTestFuncs.test_simple_callerc                 C   s   | j jtddddd}| |ddddf | t  | j jtdd W d    n1 s\0    Y  | t | j   W d    n1 s0    Y  d S )Nr,   r7   rn   rp   )rI   r   )r,   r7   )r   arg)ra   rf   r;   rg   ZassertRaises	TypeError)rI   resr   r   r   test_arg_errors  s    .zTestFuncs.test_arg_errorsc                 C   sb   | j td | jd d| jd d| jd d| jd dttjddfdi}| | j  j	| d S )Nr,   r   r.   r1   r-   r   r   )
ra   rf   r1   r   r   r   r   rg   rh   r   r   r   r   r   test_loop_caller_importing  s    



z$TestFuncs.test_loop_caller_importingr`   3pre-existing trace function throws off measurementsc                 C   sR   t d}| j|jd | jd d| jd d| jd di}| | j j| d S )Nr}   r,   )rN   )rL   r.   )rG   ra   rf   rM   r   rg   rh   r   )rI   objrk   r   r   r   test_inst_method_calling(  s    


z"TestFuncs.test_inst_method_callingc              
   C   sZ   | j t | jd d| jd d| jd d| jd d| jd di}| | j  j| d S )N)rF   r,   )rE   rD   )rB   r   )ra   rf   rF   r   rg   rh   r   r   r   r   r   r{   5  s    




z(TestFuncs.test_traced_decorated_functionN)rR   rS   rT   rd   r   r   r   r   unittestskipIfhasattrr^   r`   r   r{   r   r   r   r   r      s   	
r   c                   @   s6   e Zd Zdd Zeeedo$e ddd Z	dS )TestCallersc                 C   s.   |  tjt  tdddd| _t | _d S )Nr   r,   )rY   rZ   r\   )r]   r^   r_   r`   r   ra   r   r   rc   r   r   r   rd   D  s    zTestCallers.setUpr`   r   c                 C   s   | j td tjtjd d ddf| jd fd| jd | jd fd| jd | jd fd| jd | jd	 fd| jd	 t	t
jd
dffdi}| | j  j| d S )Nr,   r   .pyrZ   zTrace.runfuncr   r   r.   r   r   r   )ra   rf   r1   r   r   r   rZ   r   r   r   r   rg   rh   Zcallersr   r   r   r   r   I  s.    z&TestCallers.test_loop_caller_importingN)
rR   rS   rT   rd   r   r   r   r^   r`   r   r   r   r   r   r   B  s
   r   c                   @   s>   e Zd Zdd Zdd ZdddZdd	 Zd
d Zdd ZdS )TestCoveragec                 C   s   |  tjt  d S r   )r]   r^   r_   r`   rc   r   r   r   rd   _  s    zTestCoverage.setUpc                 C   s   t t tt d S r   )r   r   r   rc   r   r   r   r   b  s    zTestCoverage.tearDown_import test.support, test.test_pprint;test.support.run_unittest(test.test_pprint.QueryTestCase)c                 C   s&   | | | }|jddtd d S )NT)Zshow_missingZsummaryZcoverdir)runrh   Zwrite_resultsr   )rI   ra   cmdrr   r   r   	_coveragef  s    
zTestCoverage._coveragec                 C   s   t jddd}t }| | W d    n1 s40    Y  | }| d| | d| tt}| d| | d| d S )Nr   r,   rZ   rY   z	pprint.pyzcase.pyzpprint.coverzunittest.case.cover)	rZ   r   r   r   getvalueassertInr   listdirr   )rI   ra   stdoutfilesr   r   r   test_coveragem  s    (
zTestCoverage.test_coveragec                 C   s   t jt jt j}tjtjtj	|gddd}t
 }| | W d    n1 sV0    Y  t jtrt t}| |dg d S )Nr   r,   )Z
ignoredirsrZ   rY   z_importlib.cover)r   r   normpathdirnamer   rZ   r   r^   base_prefixbase_exec_prefixr   r   existsr   r   rg   )rI   Zlibpathra   r   r   r   r   r   test_coverage_ignorex  s    (
z!TestCoverage.test_coverage_ignorec           
      C   s   t jddd}d}|tjv r$tj|= d}t }| || W d    n1 sP0    Y  |d |  i }|D ]6}| d d \}}}	t	|t	|d d f||	< qtt 
tj| j}| || | || d d S )	Nr   r,   r   ztest.tracedmodules.testmodz<import test.tracedmodules.testmod as t;t.func(0); t.func2();rn   r   )r'   d   )rZ   r   r^   modulesr   r   seekreadlinesplitintZ_fullmodnamer   r   rg   )
rI   ra   r   r   r   ZcoveragelinelinesZcovmoduler   r   r   test_issue9936  s     
*
zTestCoverage.test_issue9936N)r   )	rR   rS   rT   rd   r   r   r   r   r   r   r   r   r   r   ^  s    
r   c                   @   s   e Zd Zdd ZdS )Test_Ignorec                 C   s   t jj}tddg|ddg}| |dd | |dd | |dd	 | ||ddd
d | ||ddd | ||dd
d d S )Nr    zy.zZfooZbarzx.pyzxy.pyZxyzy.pyr!   zbaz.pyZbazzz.pyz)r   r   joinrZ   Z_Ignore
assertTruenamesassertFalse)rI   Zjnignorer   r   r   test_ignored  s    zTest_Ignore.test_ignoredN)rR   rS   rT   r   r   r   r   r   r     s   r   c                   @   s4   e Zd ZdZdZdd Zdd Zdd Zd	d
 ZdS )TestCoverageCommandLineOutputztmp.pyz	tmp.coverc                 C   sD   t | jddd }|td W d    n1 s60    Y  d S )Nwiso-8859-15encodingu                   # coding: iso-8859-15
                x = 'spœm'
                if []:
                    print('unreachable')
            )opencodefilewritetextwrapdedent)rI   rA   r   r   r   rd     s    z#TestCoverageCommandLineOutput.setUpc                 C   s   t | j t | j d S r   )r   r   	coverfilerc   r   r   r   r     s    
z&TestCoverageCommandLineOutput.tearDownc                 C   s   t jt jtj}t j|d}t| d | j	g }t
| \}}}| |d | t j| | t j| j t| jdd }| | d W d    n1 s0    Y  d S )Nztrace.coverz-m trace --countr>   r   r   u^          # coding: iso-8859-15
    1: x = 'spœm'
    1: if []:
           print('unreachable')
)r   r   r   abspathrZ   r   r   r   r   r   r	   rg   r   r   r   r   r   read)rI   ZtracedirZtracecoverpathargvstatusr   stderrrA   r   r   r   %test_cover_files_written_no_highlight  s    
zCTestCoverageCommandLineOutput.test_cover_files_written_no_highlightc                 C   sz   d  | jg }t| \}}}| tj| j t| jdd&}| 	|
 td W d    n1 sl0    Y  d S )Nz-m trace --count --missingr   r   u                          # coding: iso-8859-15
                    1: x = 'spœm'
                    1: if []:
                >>>>>>     print('unreachable')
            )r   r   r	   r   r   r   r   r   r   rg   r   r   r   )rI   r   r   r   r   rA   r   r   r   'test_cover_files_written_with_highlight  s
    zETestCoverageCommandLineOutput.test_cover_files_written_with_highlightN)	rR   rS   rT   r   r   rd   r   r   r   r   r   r   r   r     s   	r   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestCommandLinec                 C   s:   d}|D ],^}}t ddg|R  ^ }}| || qd S )N))s3   progname is missing: required with the main options-lz-T)s8   cannot specify both --listfuncs and (--trace or --count)z-lc)s>   argument -R/--no-report: not allowed with argument -r/--reportz-rR)sL   must specify one of --trace, --count, --report, --listfuncs, or --trackcallsz-g)s   -r/--report requires -f/--filez-r)s3   --summary can only be used with --count or --reportz-sT)s   unrecognized arguments: -yz-y-mrZ   )r
   r   )rI   _errorsmessager9   _r   r   r   r   test_failures  s    zTestCommandLine.test_failuresc                 C   s   t d }tjt }t|dddl}| t| |d tddd|dd	\}}}| 	d
| d| d| d}| 	|
 | W d    n1 s0    Y  d S )Nr   r   utf-8r   za = 1
r   rZ   r   ZPYTHONIOENCODINGs   functions called:z
filename: z, modulename: z, funcname: <module>)r   r   r   r   r   r]   r   r   r	   r   encode)rI   r   
modulenamefdr   r   r   rk   r   r   r   test_listfuncs_flag_success  s    

z+TestCommandLine.test_listfuncs_flag_successc                 C   s   t tddd0}| tt |d |d W d    n1 sD0    Y  tt\}}}tdddtdd	\}}}| | | d S )
Nr   r   r   zimport sys
zprint(type(sys.argv))
r   rZ   r   r   )r   r   r]   r   r   r	   r   strip)rI   r   r   Zdirect_stdoutr   Ztrace_stdoutr   r   r   test_sys_argv_list  s    
(
z"TestCommandLine.test_sys_argv_listc                 C   s   t  d}t  d}tjt }t|ddd8}| t| | t| |t	d W d    n1 sl0    Y  t
ddd	|dd
\}}}| }| |d | d| | d| d| d| d S )Nr   z.coverr   r   r   z                x = 1
                y = 2

                def f():
                    return x + y

                for i in range(10):
                    f()
            r   rZ   z-csr   r   zlines   cov%   module   (path)z6   100%   z   ())r   r   r   r   r   r]   r   r   r   r   r	   decoderg   r   )rI   r   Zcoverfilenamer   r   r   r   r   r   r   r   test_count_and_summary  s    

.

z&TestCommandLine.test_count_and_summaryc                 C   s(   t ddddddd tddddd d S )	Nr   rZ   r   z--moduleZtimeitz-n1Znot_a_module_zzzr   rc   r   r   r   test_run_as_module  s    z"TestCommandLine.test_run_as_moduleN)rR   rS   rT   r   r   r   r   r   r   r   r   r   r     s
   r   __main__)-r   r^   Ztest.supportr   r   r   r   r   r   Ztest.support.script_helperr	   r
   r   r   rZ   r   Ztest.tracedmodulesr   r   r   r   r%   r+   r-   r/   r1   r3   r5   r8   r;   r?   rF   objectrG   ZTestCaserW   r   r   r   r   r   r   r   rR   mainr   r   r   r   <module>   sB    
mC>2C
