B
    2*™\F  ã               @   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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 d.d/„ d/ejƒZ&e'd0krze (¡  dS )1é    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   ú:C:\ALexclude\prg\programme\Python37\Lib\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 )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              C   s   d} dd„ t | ƒD ƒ}|S )Nr,   c             S   s   g | ]}t |ƒ‘qS r   )r/   )Ú.0r#   r   r   r   ú
<listcomp>K   s    z4traced_caller_list_comprehension.<locals>.<listcomp>)r"   )r(   Zmylistr   r   r   Ú traced_caller_list_comprehensionI   s    r2   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__P   s    zTracedClass.__init__c             C   s
   | j | S )N)r   )r4   r   r   r   r   Úinst_method_linearS   s    zTracedClass.inst_method_linearc             C   s   |   |¡}|t||ƒ S )N)r6   r    )r4   r   r   r   r   r   Úinst_method_callingV   s    
zTracedClass.inst_method_callingc             C   s   |d S )Nr.   r   )Úclsr   r   r   r   Úclass_method_linearZ   s    zTracedClass.class_method_linearc             C   s   | d S )Nr.   r   )r   r   r   r   Ústatic_method_linear^   s    z TracedClass.static_method_linearN)
Ú__name__Ú
__module__Ú__qualname__r5   r6   r7   Úclassmethodr9   Ústaticmethodr:   r   r   r   r   r3   O   s
   r3   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)r4   r   r   r   ÚsetUph   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%   )	rI   Úrunfuncr    ÚassertEqualr   r"   rJ   ÚresultsÚcounts)r4   ÚresultÚexpectedÚfirstlinenor#   r   r   r   Útest_traced_func_linearm   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!   é   )rI   rM   r$   r   rJ   rN   rO   rP   )r4   rS   rR   r   r   r   Útest_traced_func_loopy   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%   rU   )rI   rM   r&   r   rJ   r   r	   r   rN   rO   rP   )r4   rS   rR   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.   é   rU   r,   )	rI   rM   r-   r   r+   rJ   rN   rO   rP   )r4   Úfirstlineno_callingZfirstlineno_genrR   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.   é   rU   r,   )	rI   rM   r2   r   r/   rJ   rN   rO   rP   )r4   r[   Zfirstlineno_calledrR   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)r6   r%   r   )rA   rB   rC   rD   é   é   )	r   r3   ÚgetattrrM   r   rJ   rN   rO   rP   )r4   ZmethnamerI   Z
traced_objÚmethodrS   rR   r   r   r   Útest_linear_methods°   s    

z"TestLineCounts.test_linear_methodsN)
r;   r<   r=   rK   rT   rX   rY   r\   r^   rc   r   r   r   r   r@   f   s   r@   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   rJ   rE   rF   rG   rH   )r4   r   r   r   rK   Â   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   )rA   rB   rC   rD   ztraced_func_loop(2, 5)Úexecr.   rV   rU   r!   rW   )r   rI   Úcompiler   ZrunctxÚglobalsÚvarsr   r$   rJ   ÚkeysrN   rO   rP   )r4   ÚcoderS   rR   r(   r   r   r   Útest_exec_countsÆ   s    z"TestRunExecCounts.test_exec_countsN)r;   r<   r=   rK   rk   r   r   r   r   rd   À   s   rd   c               @   sN   e Z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S )Ú	TestFuncsc             C   s8   |   tjt ¡ ¡ tdddd| _tƒ | _t ¡ | _d S )Nr   r%   )rA   rB   rC   )	rE   rF   rG   rH   r   rI   r   ÚfilemodÚ_saved_tracefunc)r4   r   r   r   rK   Þ   s    zTestFuncs.setUpc             C   s   | j d k	rt | j ¡ d S )N)rn   rF   rG   )r4   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    )rI   rM   r'   rm   rN   rO   Úcalledfuncs)r4   rR   r   r   r   Útest_simple_callerè   s    
zTestFuncs.test_simple_callerc             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   )
rI   rM   r)   rm   r   r	   r   rN   rO   rp   )r4   rR   r   r   r   Útest_loop_caller_importingñ   s    



z$TestFuncs.test_loop_caller_importingrH   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 )Nr`   r%   )zTracedClass.inst_method_calling)zTracedClass.inst_method_linear)r    )r3   rI   rM   r7   rm   rN   rO   rp   )r4   ÚobjrR   r   r   r   Útest_inst_method_callingý   s    

z"TestFuncs.test_inst_method_callingN)r;   r<   r=   rK   ro   rq   rr   ÚunittestÚskipIfÚhasattrrF   rH   rt   r   r   r   r   rl   Ü   s   	rl   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%   )rA   rB   rD   )rE   rF   rG   rH   r   rI   r   rm   )r4   r   r   r   rK     s    zTestCallers.setUprH   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.pyrB   zTrace.runfunc)r)   )r'   )r    )r&   r	   r   )rI   rM   r)   r   r   r   rB   r   rm   r   r	   rN   rO   Zcallers)r4   rR   r   r   r   rr     s    z&TestCallers.test_loop_caller_importingN)
r;   r<   r=   rK   ru   rv   rw   rF   rH   rr   r   r   r   r   rx     s   rx   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)rE   rF   rG   rH   )r4   r   r   r   rK   (  s    zTestCoverage.setUpc             C   s   t tƒ ttƒ d S )N)r   r   r   )r4   r   r   r   ro   +  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)ÚrunrO   Zwrite_resultsr   )r4   rI   Ú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%   )rB   rA   z	pprint.pyzcase.pyzpprint.coverzunittest.case.cover)	rB   r   r   r~   ÚgetvalueÚassertInr   Úlistdirr   )r4   rI   ÚstdoutÚfilesr   r   r   Útest_coverage6  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
ignoredirsrB   rA   z_importlib.cover)r   r   ÚnormpathÚdirnamer   rB   r   rF   Úbase_prefixÚbase_exec_prefixr   r~   Úexistsr   r   rN   )r4   ZlibpathrI   r‚   rƒ   r   r   r   Útest_coverage_ignoreA  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%   )rB   rA   ztest.tracedmodules.testmodz<import test.tracedmodules.testmod as t;t.func(0); t.func2();rU   r
   )r!   éd   )rB   r   rF   Úmodulesr   r~   ÚseekÚreadlineÚsplitÚintZ_fullmodnamer   r€   rN   )
r4   rI   r   r|   r‚   ZcoverageÚlineÚlinesZcovÚmoduler   r   r   Útest_issue9936M  s     


 zTestCoverage.test_issue9936N)rz   )	r;   r<   r=   rK   ro   r~   r„   rŠ   r”   r   r   r   r   ry   '  s   
ry   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   ÚjoinrB   Z_IgnoreÚ
assertTrueÚnamesÚassertFalse)r4   ZjnÚignorer   r   r   Útest_ignoredf  s    zTest_Ignore.test_ignoredN)r;   r<   r=   rœ   r   r   r   r   r•   e  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)r4   Úfr   r   r   rK   w  s    z#TestCoverageCommandLineOutput.setUpc             C   s   t | jƒ t | jƒ d S )N)r   r    Ú	coverfile)r4   r   r   r   ro     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†   ÚabspathrB   r   r—   r   r   r    r   rN   rš   r‰   r˜   r¥   rŸ   Úread)r4   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Ÿ   rN   r¨   r¢   r£   )r4   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¥   rK   ro   r¬   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ú-mrB   )r¯   rB   )r   r€   )r4   Ú_errorsÚmessageÚargsÚ_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-mrB   z-ls   functions called:)rŸ   r   rE   r   r¡   r   r€   )r4   Ú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-mrB   z-l)rŸ   r   rE   r   r¡   r   r€   Ústrip)r4   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-mrB   z-csr   zlines   cov%   module   (path)z6   100%   z   (ú))r   rŸ   rE   r   r¡   r¢   r£   r   ÚdecoderN   r€   )r4   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   rF   Ztest.supportr   r   r   r   Ztest.support.script_helperr   r   r¢   ru   rB   r   Ztest.tracedmodulesr	   r   r   r   r    r$   r&   r'   r)   r+   r-   r/   r2   Úobjectr3   ZTestCaser@   rd   rl   rx   ry   r•   r   r®   r;   Úmainr   r   r   r   Ú<module>   s>   
Z/>/6
