o
    gO                     @   s  d dl Z d dlZd dlmZ d dl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/d0 d0ej!Z'G d1d2 d2ej!Z(G d3d4 d4ej!Z)e*d5kre+  dS dS )6    N)captured_stdout)TESTFNrmtreeunlinkassert_python_okassert_python_failure)Trace)testmodc                 C   s   |  dr| dd } | S )z8Given a .pyc filename converts it to the appropriate .pyz.pycN)endswith)filename r   E/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/test/test_trace.py
fix_ext_py   s   
r   c                  C   s$   t jt jtd } tt| fS )z4The .py file and module name of this file (__file__)r   N)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      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_importing1   s   r,   c                 C   s   t | | }||  S r   r$   )r   r#   r   r   r   traced_func_simple_caller4   s   
r.   c                 C   s   t | }|t|| 7 }|S r   )r.   r,   )r   kr   r   r   traced_func_importing_caller8   s   r0   c                 c   s"    d}t | D ]}|| V  qd S r%   r'   )numr#   r)   r   r   r   traced_func_generator=   s
   r2   c                  C   s   d} t dD ]}| |7 } qd S )Nr   
   )r2   )r/   r)   r   r   r   traced_func_calling_generatorB   s   
r4   c                 C      | d S N   r   )r1   r   r   r   traced_doublerG   r   r8   c                  O   s   | |fS r   r   )argskwargsr   r   r   traced_capturerJ   r   r;   c                  C   s   d} dd t | D }|S )Nr3   c                 S   s   g | ]}t |qS r   )r8   ).0r)   r   r   r   
<listcomp>O   s    z4traced_caller_list_comprehension.<locals>.<listcomp>r'   )r/   Zmylistr   r   r    traced_caller_list_comprehensionM   s   r>   c                  C   s,   dd } dd }| | dd }|  d S )Nc                 S      | S r   r   fr   r   r   
decorator1S      z-traced_decorated_function.<locals>.decorator1c                  S   s   dd } | S )Nc                 S   r?   r   r   r@   r   r   r   
decorator2V   rC   zGtraced_decorated_function.<locals>.decorator_fabric.<locals>.decorator2r   rD   r   r   r   decorator_fabricU   s   z3traced_decorated_function.<locals>.decorator_fabricc                   S   s   d S r   r   r   r   r   r   r   Y   s   z'traced_decorated_function.<locals>.funcr   )rB   rF   r   r   r   r   traced_decorated_functionR   s   
rG   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__a      
zTracedClass.__init__c                 C   s
   | j | S r   rI   )rJ   r    r   r   r   inst_method_lineard   rL   TracedClass.inst_method_linearc                 C   s   |  |}|t|| S r   )rM   r$   )rJ   r   r#   r   r   r   inst_method_callingg      
TracedClass.inst_method_callingc                 C   s   |d S r6   r   )clsr    r   r   r   class_method_lineark      zTracedClass.class_method_linearc                 C   r5   r6   r   )r    r   r   r   static_method_linearo   rT   z TracedClass.static_method_linearN)
__name__
__module____qualname__rK   rM   rO   classmethodrS   staticmethodrU   r   r   r   r   rH   `   s    
rH   c                   @   sP   e Zd 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 )TestLineCountsz/White-box testing of line-counting, via runfuncc                 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rJ   r   r   r   setUpy   s   zTestLineCounts.setUpc                 C   sb   | j tdd}| |d i }tt}tddD ]}d|| j|| f< q| | j  j| d S )Nr7   r&      r+   )	re   runfuncr$   assertEqualr   r(   rf   resultscounts)rJ   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&      )re   rj   r*   r   rf   rk   rl   rm   rJ   rp   ro   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+   rr   )re   rj   r,   r   rf   r   r
   r   rk   rl   rm   ru   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      rr   r3   )	re   rj   r4   r   r2   rf   rk   rl   rm   )rJ   firstlineno_callingZfirstlineno_genro   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      rr   r3   )	re   rj   r>   r   r8   rf   rk   rl   rm   )rJ   ry   Zfirstlineno_calledro   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   rr   rt   r&   rs   ri      	   r3   rx   )re   rj   rG   r   rf   rk   rl   rm   ru   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)rM   r+   r   r\         )	r	   rH   getattrrj   r   rf   rk   rl   rm   )rJ   Zmethnamere   Z
traced_objmethodrp   ro   r   r   r   test_linear_methods   s   
z"TestLineCounts.test_linear_methodsN)rV   rW   rX   __doc__rh   rq   rv   rw   rz   r|   r   r   r   r   r   r   r[   w   s    r[   c                   @   s    e Zd ZdZdd Zdd ZdS )TestRunExecCountsz8A simple sanity test of line-counting, via runctx (exec)c                 C   s    t t| _| tjt  d S r   )r   r   rf   ra   rb   rc   rd   rg   r   r   r   rh      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| ||  qAd S )Nr+   r   r\   ztraced_func_loop(2, 5)execr7   rs   rr   r&   rt   )r	   re   compiler   Zrunctxglobalsvarsr   r*   rf   keysrk   rl   rm   )rJ   coderp   ro   r/   r   r   r   test_exec_counts   s   z"TestRunExecCounts.test_exec_countsN)rV   rW   rX   r   rh   r   r   r   r   r   r      s    r   c                   @   sb   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Ze	
eedo$e ddd Zdd ZdS )	TestFuncsz"White-box testing of funcs tracingc                 C   s8   |  tjt  tdddd| _t | _t | _d S )Nr   r+   )r]   r^   r_   )	ra   rb   rc   rd   r	   re   r   filemod_saved_tracefuncrg   r   r   r   rh     s   zTestFuncs.setUpc                 C   s   | j d urt| j  d S d S r   )r   rb   rc   rg   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-   )re   rj   r.   r   rk   rl   calledfuncsrJ   ro   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.w   Y  | t | j   W d    d S 1 sIw   Y  d S )Nr+   r7   rr   rt   )rJ   r   )r+   r7   )r   arg)re   rj   r;   rk   ZassertRaises	TypeError)rJ   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-   r0   r,   r
   r   )
re   rj   r0   r   r   r
   r   rk   rl   r   r   r   r   r   test_loop_caller_importing  s   



z$TestFuncs.test_loop_caller_importingrd   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+   )rQ   )rN   r-   )rH   re   rj   rO   r   rk   rl   r   )rJ   objro   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)rG   r+   )rF   rE   )rB   r   )re   rj   rG   r   rk   rl   r   r   r   r   r   r   6  s   




z(TestFuncs.test_traced_decorated_functionN)rV   rW   rX   r   rh   r   r   r   r   unittestskipIfhasattrrb   rd   r   r   r   r   r   r   r      s    	
r   c                   @   s:   e Zd ZdZdd Zeeedoe	 ddd Z
dS )	TestCallersz$White-box testing of callers tracingc                 C   s.   |  tjt  tdddd| _t | _d S )Nr   r+   )r]   r^   r`   )ra   rb   rc   rd   r	   re   r   r   rg   r   r   r   rh   E  s   zTestCallers.setUprd   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   .pyr^   zTrace.runfuncr   r   r-   r   r
   r   )re   rj   r0   r   r   r   r^   r   r   r   r
   rk   rl   Zcallersr   r   r   r   r   J  s.   z&TestCallers.test_loop_caller_importingN)rV   rW   rX   r   rh   r   r   r   rb   rd   r   r   r   r   r   r   C  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   )ra   rb   rc   rd   rg   r   r   r   rh   `  s   zTestCoverage.setUpc                 C   s   t t tt d S r   )r   r   r   rg   r   r   r   r   c  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)runrl   Zwrite_resultsr   )rJ   re   cmdrr   r   r   	_coverageg  s   
zTestCoverage._coveragec                 C   s   t jddd}t }| | W d    n1 sw   Y  | }| d| | d| tt}| d| | d| d S )Nr   r+   r^   r]   z	pprint.pyzcase.pyzpprint.coverzunittest.case.cover)	r^   r	   r   r   getvalueassertInr   listdirr   )rJ   re   stdoutfilesr   r   r   test_coveragen  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 s+w   Y  t jtrDt t}| |dg d S d S )Nr   r+   )Z
ignoredirsr^   r]   z_importlib.cover)r   r   normpathdirnamer   r^   r	   rb   base_prefixbase_exec_prefixr   r   existsr   r   rk   )rJ   Zlibpathre   r   r   r   r   r   test_coverage_ignorey  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 s(w   Y  |d |  i }|D ]}| d d \}}}	t	|t	|d d f||	< q:t 
tj| j}| || | || d d S )	Nr   r+   r   ztest.tracedmodules.testmodz<import test.tracedmodules.testmod as t;t.func(0); t.func2();rr   r   )r&   d   )r^   r	   rb   modulesr   r   seekreadlinesplitintZ_fullmodnamer   r   rk   )
rJ   re   r   r   r   ZcoveragelinelinesZcovmoduler   r   r   test_issue9936  s"   

zTestCoverage.test_issue9936N)r   )	rV   rW   rX   rh   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   joinr^   Z_Ignore
assertTruenamesassertFalse)rJ   Zjnignorer   r   r   test_ignored  s   zTest_Ignore.test_ignoredN)rV   rW   rX   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   sF   t | jddd}|td W d    d S 1 sw   Y  d S )Nwiso-8859-15encodingu                   # coding: iso-8859-15
                x = 'spœm'
                if []:
                    print('unreachable')
            )opencodefilewritetextwrapdedent)rJ   rA   r   r   r   rh     s   "z#TestCoverageCommandLineOutput.setUpc                 C   s   t | j t | j d S r   )r   r   	coverfilerg   r   r   r   r     rP   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    d S 1 sYw   Y  d S )Nztrace.coverz-m trace --count    r   r   u^          # coding: iso-8859-15
    1: x = 'spœm'
    1: if []:
           print('unreachable')
)r   r   r   abspathr^   r   r   r   r   r   r   rk   r   r   r   r   r   read)rJ   ZtracedirZtracecoverpathargvstatusr   stderrrA   r   r   r   %test_cover_files_written_no_highlight  s   
"zCTestCoverageCommandLineOutput.test_cover_files_written_no_highlightc                 C   s|   d  | jg }t| \}}}| tj| j t| jdd}| 	|
 td W d    d S 1 s7w   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   rk   r   r   r   )rJ   r   r   r   r   rA   r   r   r   'test_cover_files_written_with_highlight  s   "zETestCoverageCommandLineOutput.test_cover_files_written_with_highlightN)	rV   rW   rX   r   r   rh   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-mr^   )r   r   )rJ   Z_errorsmessager9   _r   r   r   r   test_failures  s
   zTestCommandLine.test_failuresc                 C   s   t d }tjt }t|ddd7}| t| |d tddd|dd	\}}}| 	d
| d| d| d}| 	|
 | W d    d S 1 sKw   Y  d S )Nr   r   utf-8r   za = 1
r   r^   r   ZPYTHONIOENCODINGs   functions called:z
filename: z, modulename: z, funcname: <module>)r   r   r   r   r   ra   r   r   r   r   encode)rJ   r   
modulenamefdr   r   r   ro   r   r   r   test_listfuncs_flag_success  s   

"z+TestCommandLine.test_listfuncs_flag_successc                 C   s   t tddd}| tt |d |d W d    n1 s"w   Y  tt\}}}tdddtdd	\}}}| | | d S )
Nr   r   r   zimport sys
zprint(type(sys.argv))
r   r^   r   r   )r   r   ra   r   r   r   r   strip)rJ   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}| t| | t| |t	d W d    n1 s6w   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   r^   z-csr   r   zlines   cov%   module   (path)z6   100%   z   ())r   r   r   r   r   ra   r   r   r   r   r   decoderk   r   )rJ   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   r^   r   z--moduleZtimeitz-n1Znot_a_module_zzzr   rg   r   r   r   test_run_as_module  s   z"TestCommandLine.test_run_as_moduleN)rV   rW   rX   r   r   r   r   r   r   r   r   r   r     s    r   __main__),r   rb   Ztest.supportr   Ztest.support.os_helperr   r   r   Ztest.support.script_helperr   r   r   r   r^   r	   Ztest.tracedmodulesr
   r   r   r   r$   r*   r,   r.   r0   r2   r4   r8   r;   r>   rG   objectrH   ZTestCaser[   r   r   r   r   r   r   r   rV   mainr   r   r   r   <module>   sH    
mC>2C