B
    u9aÑE  ã               @   s†  d dl Z d dlZd dl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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 d.d/„ d/ejƒZ&G d0d1„ d1ejƒZ'e(d2kr‚e )¡  dS )3é    N)ÚTESTFNÚ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.7/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 )Nr   )ÚxÚyÚaÚbÚcr   r   r   Útraced_func_linear$   s    r    c             C   s"   | }xt dƒD ]}||7 }qW |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 )N)r    )r   r   r   r   r   Útraced_func_simple_caller3   s    
r'   c             C   s   t | ƒ}|t|| ƒ7 }|S )N)r'   r&   )r   Úkr   r   r   Útraced_func_importing_caller7   s    r)   c             c   s$   d}xt | ƒD ]}|| V  qW d S )Nr!   )r"   )Únumr   r#   r   r   r   Útraced_func_generator<   s    r+   c              C   s"   d} xt dƒD ]}| |7 } qW d S )Nr   é
   )r+   )r(   r#   r   r   r   Útraced_func_calling_generatorA   s    r-   c             C   s   | d S )Né   r   )r*   r   r   r   Útraced_doublerF   s    r/   c              O   s   | |fS )Nr   )ÚargsÚkwargsr   r   r   Útraced_capturerI   s    r2   c              C   s   d} dd„ t | ƒD ƒ}|S )Nr,   c             S   s   g | ]}t |ƒ‘qS r   )r/   )Ú.0r#   r   r   r   ú
<listcomp>N   s    z4traced_caller_list_comprehension.<locals>.<listcomp>)r"   )r(   Zmylistr   r   r   Ú traced_caller_list_comprehensionL   s    r5   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 )N)r   )Úselfr   r   r   r   Ú__init__S   s    zTracedClass.__init__c             C   s
   | j | S )N)r   )r7   r   r   r   r   Úinst_method_linearV   s    zTracedClass.inst_method_linearc             C   s   |   |¡}|t||ƒ S )N)r9   r    )r7   r   r   r   r   r   Úinst_method_callingY   s    
zTracedClass.inst_method_callingc             C   s   |d S )Nr.   r   )Úclsr   r   r   r   Úclass_method_linear]   s    zTracedClass.class_method_linearc             C   s   | d S )Nr.   r   )r   r   r   r   Ústatic_method_lineara   s    z TracedClass.static_method_linearN)
Ú__name__Ú
__module__Ú__qualname__r8   r9   r:   Úclassmethodr<   Ústaticmethodr=   r   r   r   r   r6   R   s
   r6   c               @   sD   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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)r7   r   r   r   ÚsetUpk   s    zTestLineCounts.setUpc             C   sf   | j  tdd¡}|  |d¡ i }ttƒ}x$tddƒD ]}d|| j|| f< q4W |  | j  ¡ j|¡ d S )Nr.   r!   é   r%   )	rL   Úrunfuncr    ÚassertEqualr   r"   rM   ÚresultsÚcounts)r7   ÚresultÚexpectedÚfirstlinenor#   r   r   r   Útest_traced_func_linearp   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 )Nr.   é   r%   é   r!   é   )rL   rP   r$   r   rM   rQ   rR   rS   )r7   rV   rU   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 )Nr.   r!   r%   rX   )rL   rP   r&   r   rM   r   r	   r   rQ   rR   rS   )r7   rV   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%   r.   é   rX   r,   )	rL   rP   r-   r   r+   rM   rQ   rR   rS   )r7   Úfirstlineno_callingZfirstlineno_genrU   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%   r.   é   rX   r,   )	rL   rP   r5   r   r/   rM   rQ   rR   rS   )r7   r^   Zfirstlineno_calledrU   r   r   r   Útest_trace_list_comprehension£   s    z,TestLineCounts.test_trace_list_comprehensionc             C   sl   xfdD ]^}t ddddd}tdƒ}t||ƒ}| |d¡ t|ƒ}| j|d fdi}|  | ¡ j|¡ qW d S )N)r9   r%   r   )rD   rE   rF   rG   é   é   )	r   r6   ÚgetattrrP   r   rM   rQ   rR   rS   )r7   ZmethnamerL   Z
traced_objÚmethodrV   rU   r   r   r   Útest_linear_methods³   s    

z"TestLineCounts.test_linear_methodsN)
r>   r?   r@   rN   rW   r[   r\   r_   ra   rf   r   r   r   r   rC   i   s   rC   c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚTestRunExecCountsc             C   s    t tƒ| _|  tjt ¡ ¡ d S )N)r   r   rM   rH   rI   rJ   rK   )r7   r   r   r   rN   Å   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}x,| 
¡ D ] }|  | j ¡ j| || ¡ q„W d S )Nr%   r   )rD   rE   rF   rG   ztraced_func_loop(2, 5)Úexecr.   rY   rX   r!   rZ   )r   rL   Úcompiler   ZrunctxÚglobalsÚvarsr   r$   rM   ÚkeysrQ   rR   rS   )r7   ÚcoderV   rU   r(   r   r   r   Útest_exec_countsÉ   s    z"TestRunExecCounts.test_exec_countsN)r>   r?   r@   rN   rn   r   r   r   r   rg   Ã   s   rg   c               @   sV   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S )Ú	TestFuncsc             C   s8   |   tjt ¡ ¡ tdddd| _tƒ | _t ¡ | _d S )Nr   r%   )rD   rE   rF   )	rH   rI   rJ   rK   r   rL   r   ÚfilemodÚ_saved_tracefunc)r7   r   r   r   rN   á   s    zTestFuncs.setUpc             C   s   | j d k	rt | j ¡ d S )N)rq   rI   rJ   )r7   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    )rL   rP   r'   rp   rQ   rR   Úcalledfuncs)r7   rU   r   r   r   Útest_simple_callerë   s    
zTestFuncs.test_simple_callerc          	   C   st   | j jtddddd}|  |ddddœf¡ | j jtdd}|  |dd	dif¡ |  t¡ | j  ¡  W d Q R X d S )
Nr%   r.   rX   rZ   )r7   r   )r%   r.   )r   Úargr   ru   )rL   rP   r2   rQ   ZassertRaisesÚ	TypeError)r7   Z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    )r)   )r&   r	   r   )
rL   rP   r)   rp   r   r	   r   rQ   rR   rs   )r7   rU   r   r   r   Útest_loop_caller_importingü   s    



z$TestFuncs.test_loop_caller_importingrK   z3pre-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 )Nrc   r%   )zTracedClass.inst_method_calling)zTracedClass.inst_method_linear)r    )r6   rL   rP   r:   rp   rQ   rR   rs   )r7   ÚobjrU   r   r   r   Útest_inst_method_calling  s    

z"TestFuncs.test_inst_method_callingN)r>   r?   r@   rN   rr   rt   rw   rx   ÚunittestÚskipIfÚhasattrrI   rK   rz   r   r   r   r   ro   ß   s   	ro   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%   )rD   rE   rG   )rH   rI   rJ   rK   r   rL   r   rp   )r7   r   r   r   rN     s    zTestCallers.setUprK   z3pre-existing trace function throws off measurementsc             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   z.pyrE   zTrace.runfunc)r)   )r'   )r    )r&   r	   r   )rL   rP   r)   r   r   r   rE   r   rp   r   r	   rQ   rR   Zcallers)r7   rU   r   r   r   rx     s    z&TestCallers.test_loop_caller_importingN)
r>   r?   r@   rN   r{   r|   r}   rI   rK   rx   r   r   r   r   r~     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 )N)rH   rI   rJ   rK   )r7   r   r   r   rN   3  s    zTestCoverage.setUpc             C   s   t tƒ ttƒ d S )N)r   r   r   )r7   r   r   r   rr   6  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)ÚrunrR   Zwrite_resultsr   )r7   rL   ÚcmdÚrr   r   r   Ú	_coverage:  s    
zTestCoverage._coveragec          	   C   sp   t jddd}tƒ }|  |¡ W d Q R X | ¡ }|  d|¡ |  d|¡ t t¡}|  d|¡ |  d|¡ d S )Nr   r%   )rE   rD   z	pprint.pyzcase.pyzpprint.coverzunittest.case.cover)	rE   r   r   r„   ÚgetvalueÚassertInr   Úlistdirr   )r7   rL   ÚstdoutÚfilesr   r   r   Útest_coverageA  s    
zTestCoverage.test_coveragec          	   C   st   t j t j t j¡¡}tjtjtj	|gddd}t
ƒ }|  |¡ W d Q R X t j t¡rpt  t¡}|  |dg¡ d S )Nr   r%   )Z
ignoredirsrE   rD   z_importlib.cover)r   r   ÚnormpathÚdirnamer   rE   r   rI   Úbase_prefixÚbase_exec_prefixr   r„   Úexistsr   r‡   rQ   )r7   ZlibpathrL   rˆ   r‰   r   r   r   Útest_coverage_ignoreL  s    
z!TestCoverage.test_coverage_ignorec       
   	   C   sÎ   t jddd}d}|tjkr$tj|= d}tƒ }|  ||¡ W d Q R X | d¡ | ¡  i }x>|D ]6}| ¡ d d… \}}}	t	|ƒt	|d d… ƒf||	< qbW t  
tj| j¡}|  ||¡ |  || d¡ d S )	Nr   r%   )rE   rD   ztest.tracedmodules.testmodz<import test.tracedmodules.testmod as t;t.func(0); t.func2();rX   r
   )r!   éd   )rE   r   rI   Úmodulesr   r„   ÚseekÚreadlineÚsplitÚintZ_fullmodnamer   r†   rQ   )
r7   rL   r   r‚   rˆ   ZcoverageÚlineÚlinesZcovÚmoduler   r   r   Útest_issue9936X  s     


 zTestCoverage.test_issue9936N)r€   )	r>   r?   r@   rN   rr   r„   rŠ   r   rš   r   r   r   r   r   2  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   ÚjoinrE   Z_IgnoreÚ
assertTrueÚnamesÚassertFalse)r7   ZjnÚignorer   r   r   Útest_ignoredq  s    zTest_Ignore.test_ignoredN)r>   r?   r@   r¢   r   r   r   r   r›   p  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   s,   t | jdƒ}| t d¡¡ W d Q R X d S )NÚwzc                x = 42
                if []:
                    print('unreachable')
            )ÚopenÚcodefileÚwriteÚtextwrapÚdedent)r7   Úfr   r   r   rN   ‚  s    z#TestCoverageCommandLineOutput.setUpc             C   s   t | jƒ t | jƒ d S )N)r   r¦   Ú	coverfile)r7   r   r   r   rr   Š  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¡ W d Q R X d S )Nztrace.coverz-m trace --countó    z<    1: x = 42
    1: if []:
           print('unreachable')
)r   r   rŒ   ÚabspathrE   r   r   r   r•   r¦   r   rQ   r    r   rž   r«   r¥   Úread)r7   ZtracedirZtracecoverpathÚargvÚstatusrˆ   Ústderrrª   r   r   r   Ú%test_cover_files_written_no_highlightŽ  s    
zCTestCoverageCommandLineOutput.test_cover_files_written_no_highlightc          	   C   sb   d  ¡ | jg }t|Ž \}}}|  tj | j¡¡ t| jƒ}|  	| 
¡ t d¡¡ W d Q R X d S )Nz-m trace --count --missingzx                    1: x = 42
                    1: if []:
                >>>>>>     print('unreachable')
            )r•   r¦   r   rž   r   r   r   r«   r¥   rQ   r®   r¨   r©   )r7   r¯   r°   rˆ   r±   rª   r   r   r   Ú'test_cover_files_written_with_highlight¡  s    zETestCoverageCommandLineOutput.test_cover_files_written_with_highlightN)	r>   r?   r@   r¦   r«   rN   rr   r²   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	S )
ÚTestCommandLinec             C   s8   d}x.|D ]&^}}t d|žŽ ^ }}|  ||¡ q
W d S )N))s3   filename is missing: required with the main optionsz-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ú-mrE   )rµ   rE   )r   r†   )r7   Ú_errorsÚmessager0   Ú_r±   r   r   r   Útest_failures®  s    zTestCommandLine.test_failuresc          	   C   sP   t tdƒ<}|  tt¡ | d¡ tdddtƒ\}}}|  d|¡ W d Q R X d S )Nr¤   za = 1
z-mrE   z-ls   functions called:)r¥   r   rH   r   r§   r   r†   )r7   Úfdr°   rˆ   r±   r   r   r   Útest_listfuncs_flag_success»  s
    
z+TestCommandLine.test_listfuncs_flag_successc          	   C   sl   t tdƒ&}|  tt¡ | d¡ | d¡ W d Q R X ttƒ\}}}tdddtƒ\}}}|  | ¡ |¡ d S )Nr¤   zimport sys
zprint(type(sys.argv))
z-mrE   z-l)r¥   r   rH   r   r§   r   r†   Ústrip)r7   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|dƒ.}|  t|¡ |  t|¡ | t d¡¡ W d Q R X tddd|ƒ\}}}| ¡ }|  	|d¡ |  
d	|¡ |  
d
t › d|› d|¡ d S )Nz.pyz.coverr¤   z°                x = 1
                y = 2

                def f():
                    return x + y

                for i in range(10):
                    f()
            z-mrE   z-csr   zlines   cov%   module   (path)z6   100%   z   (ú))r   r¥   rH   r   r§   r¨   r©   r   ÚdecoderQ   r†   )r7   r   Zcoverfilenamerº   r°   rˆ   r¸   r   r   r   Útest_count_and_summaryÌ  s    

	z&TestCommandLine.test_count_and_summaryN)r>   r?   r@   r¹   r»   r½   rÀ   r   r   r   r   r´   ¬  s   
r´   Ú__main__)*r   rI   Ztest.supportr   r   r   r   Ztest.support.script_helperr   r   r¨   r{   rE   r   Ztest.tracedmodulesr	   r   r   r   r    r$   r&   r'   r)   r+   r-   r/   r2   r5   Úobjectr6   ZTestCaserC   rg   ro   r~   r   r›   r£   r´   r>   Úmainr   r   r   r   Ú<module>   s@   
Z7>/6
