B
    u9asÂ  ã               @   s’  d dl Z d dlZd dlZd dl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 d dlmZ d dlmZ G dd„ deƒ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e d¡Zedejƒ 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/d0„ Z*d1d2„ Z+d3d4„ Z,d5d6„ Z-d7d8„ Z.d9d:„ Z/G d;d<„ d<ej0ƒZ1d=d>„ Z2e3d?krŽe 4¡  dS )@é    N)Ú	ExitStack)ÚStringIO)Úsupport)Ú
_FakeInput)Úpatchc               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚPdbTestInputc             C   s
   || _ d S )N)Úinput)Úselfr   © r
   ú/usr/lib/python3.7/test_pdb.pyÚ__init__   s    zPdbTestInput.__init__c             C   s0   t j| _t| jƒt _tt dƒr&t  ¡ nd | _d S )NÚgettrace)ÚsysÚstdinÚ
real_stdinr   r   Úhasattrr   Ú
orig_trace)r	   r
   r
   r   Ú	__enter__   s    zPdbTestInput.__enter__c             G   s   | j t_| jrt | j¡ d S )N)r   r   r   r   Úsettrace)r	   Úexcr
   r
   r   Ú__exit__   s    zPdbTestInput.__exit__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r
   r
   r
   r   r      s   r   c               C   s   d S )Nr
   r
   r
   r
   r   Útest_pdb_displayhook%   s    r   c               C   s   d S )Nr
   r
   r
   r
   r   Útest_pdb_basic_commandsB   s    |r   c               C   s   d S )Nr
   r
   r
   r
   r   Útest_pdb_breakpoint_commandsÁ   s    kr   c               C   s   d S )Nr
   r
   r
   r
   r   Ú
do_nothing/  s    r   c               C   s   t dƒ d S )Né*   )Úprintr
   r
   r
   r   Údo_something2  s    r    c               C   s   d S )Nr
   r
   r
   r
   r   Útest_list_commands5  s    ]r!   c               C   s   d S )Nr
   r
   r
   r
   r   Útest_post_mortem•  s    ;r"   c               C   s   d S )Nr
   r
   r
   r
   r   Útest_pdb_skip_modulesÓ  s    r#   Zmodule_to_skipz6def foo_pony(callback): x = 1; callback(); return Nonec               C   s   d S )Nr
   r
   r
   r
   r   Ú#test_pdb_skip_modules_with_callbackï  s    (r$   c               C   s   d S )Nr
   r
   r
   r
   r   Ú test_pdb_continue_in_bottomframe  s    'r%   c             C   s   t tjddd| ƒ|ƒ d S )NTF)ZnosigintZreadrc)ÚgetattrÚpdbÚPdb)ÚmethodÚargr
   r
   r   Ú
pdb_invokeD  s    r+   c               C   s   d S )Nr
   r
   r
   r
   r   Ú$test_pdb_run_with_incorrect_argumentI  s    r,   c               C   s   d S )Nr
   r
   r
   r
   r   Útest_pdb_run_with_code_objectY  s    r-   c               C   s   d S )Nr
   r
   r
   r
   r   Ú&test_next_until_return_at_return_evento  s    ?r.   c               C   s   d S )Nr
   r
   r
   r
   r   Ú#test_pdb_next_command_for_generator°  s    9r/   c               C   s   d S )Nr
   r
   r
   r
   r   Ú#test_pdb_next_command_for_coroutineë  s    5r0   c               C   s   d S )Nr
   r
   r
   r
   r   Ú"test_pdb_next_command_for_asyncgen"  s    :r1   c               C   s   d S )Nr
   r
   r
   r
   r   Ú%test_pdb_return_command_for_generator^  s    5r2   c               C   s   d S )Nr
   r
   r
   r
   r   Ú%test_pdb_return_command_for_coroutine•  s    'r3   c               C   s   d S )Nr
   r
   r
   r
   r   Ú$test_pdb_until_command_for_generator¾  s    *r4   c               C   s   d S )Nr
   r
   r
   r
   r   Ú$test_pdb_until_command_for_coroutineê  s    ,r5   c               C   s   d S )Nr
   r
   r
   r
   r   Ú+test_pdb_next_command_in_generator_for_loop  s    'r6   c               C   s   d S )Nr
   r
   r
   r
   r   Ú!test_pdb_next_command_subiteratorA  s    -r7   c               C   s   d S )Nr
   r
   r
   r
   r   Útest_pdb_issue_20766p  s    r8   c               @   s¼   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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d-S ).ÚPdbTestCasec             C   s   t  t j¡ d S )N)r   ÚunlinkÚTESTFN)r	   r
   r
   r   ÚtearDownŠ  s    zPdbTestCase.tearDownc          	   C   sz   |   tjd¡ tjddg| }tj|tjtjtjd}| 	t
 |¡¡\}}W d Q R X |obt |¡}|opt |¡}||fS )NÚ__pycache__z-mr'   )Ústdoutr   Ústderr)Ú
addCleanupr   Úrmtreer   Ú
executableÚ
subprocessÚPopenÚPIPEÚSTDOUTÚcommunicateÚstrÚencodeÚbytesÚdecode)r	   Zpdb_argsÚcommandsÚcmdÚprocr>   r?   r
   r
   r   Ú_run_pdb  s    
zPdbTestCase._run_pdbc          	   C   sF   d}t |dƒ}| t |¡¡ W d Q R X |  tj|¡ |  |g|¡S )Nzmain.pyÚw)ÚopenÚwriteÚtextwrapÚdedentr@   r   r:   rO   )r	   ÚscriptrL   ÚfilenameÚfr
   r
   r   Úrun_pdb_script›  s
    zPdbTestCase.run_pdb_scriptc          	   C   s   d| _ t | j ¡ | j d }| j d }t | j ¡ t|dƒ}W d Q R X t|dƒ}| t |¡¡ W d Q R X |  	tj| j ¡ |  
d| j g|¡S )NÚt_mainz/__main__.pyz/__init__.pyrP   z-m)Úmodule_namer   rA   ÚosÚmkdirrQ   rR   rS   rT   r@   rO   )r	   rU   rL   Ú	main_fileÚ	init_filerW   r
   r
   r   Úrun_pdb_module£  s    


zPdbTestCase.run_pdb_modulec          	   C   sb   t  |¡}ttjdƒ}| |¡ W d Q R X |s4d n|d tj|d f}|  |t |tj¡¡ d S )NrP   r   é   )	rS   rT   rQ   r   r;   rR   ÚassertEqualr'   Zfind_function)r	   Zfile_contentZ	func_nameÚexpectedrW   r
   r
   r   Ú_assert_find_function±  s    
z!PdbTestCase._assert_find_functionc             C   s   |   ddd ¡ d S )NÚ Zfoo)rc   )r	   r
   r
   r   Útest_find_function_empty_file¼  s    z)PdbTestCase.test_find_function_empty_filec             C   s   |   ddd¡ d S )Nz“            def foo():
                pass

            def bar():
                pass

            def quux():
                pass
            Úbar)rf   é   )rc   )r	   r
   r
   r   Útest_find_function_found¿  s    
z$PdbTestCase.test_find_function_foundc          	   C   sx   t tjdƒ}| d¡ W d Q R X tjddtjg}tj|tjtjtj	d}|  
|jj¡ | d¡\}}|  d|d¡ d S )	NÚwbs   print("testing my pdb")
z-mr'   )r>   r   r?   s   quit
s   SyntaxErrorz0Got a syntax error running test script under PDB)rQ   r   r;   rR   r   rB   rC   rD   rE   rF   r@   r>   ÚcloserG   ÚassertNotIn)r	   rW   rM   rN   r>   r?   r
   r
   r   Útest_issue7964Ï  s    
zPdbTestCase.test_issue7964c          	   C   sr   d}d}d}t ddƒ}| t |¡¡ W d Q R X |  tjd¡ |  ||¡\}}|  t	dd„ | 
¡ D ƒƒd¡ d S )	Nzã
            from bar import bar

            def foo():
                bar()

            def nope():
                pass

            def foobar():
                foo()
                nope()

            foobar()
        z‡
            from bar import bar
            break bar
            continue
            step
            step
            quit
        z5
            def bar():
                pass
        zbar.pyrP   c             s   s   | ]}d |kV  qdS )zmain.py(5)foo()->NoneNr
   )Ú.0Úlr
   r
   r   ú	<genexpr>ÿ  s    z.PdbTestCase.test_issue13183.<locals>.<genexpr>z+Fail to step into the caller after a return)rQ   rR   rS   rT   r@   r   r:   rX   Ú
assertTrueÚanyÚ
splitlines)r	   rU   rL   rf   rW   r>   r?   r
   r
   r   Útest_issue13183Þ  s    zPdbTestCase.test_issue13183c          	   C   s†   t tjdƒ}| t d¡ d¡¡ W d Q R X tjdtjg}t	j
|t	jt	jt	jd}|  |jj¡ | d¡\}}|  d| ¡ d¡ d S )	Nri   a  
                import threading
                import pdb

                def start_pdb():
                    pdb.Pdb(readrc=False).set_trace()
                    x = 1
                    y = 1

                t = threading.Thread(target=start_pdb)
                t.start()Úasciiz-u)r>   r   r?   s   cont
ÚErrorz*Got an error running test script under PDB)rQ   r   r;   rR   rS   rT   rI   r   rB   rC   rD   rE   rF   r@   r>   rj   rG   rk   rK   )r	   rW   rM   rN   r>   r?   r
   r
   r   Útest_issue13120  s    

zPdbTestCase.test_issue13120c             C   s6   d}d}d}|   ||¡\}}|  ||d ||¡¡ d S )Nzdef f: pass
rd   zSyntaxError:zE

Expected:
{}
Got:
{}
Fail to handle a syntax error in the debuggee.)rX   ÚassertInÚformat)r	   rU   rL   rb   r>   r?   r
   r
   r   Útest_issue16180  s    zPdbTestCase.test_issue16180c          
   C   sÚ   t  d¡}tj dd ¡}z¨t ¡ – tddƒ}| d¡ W d Q R X tddƒ}| |¡ W d Q R X t	j
dg}tj|tjtjtjd}|$ | d¡\}}|  d	| ¡ ¡ W d Q R X W d Q R X W d |d k	rÔ|tjd< X d S )
Nz_
            import pdb; pdb.Pdb(readrc=False).set_trace()

            print('hello')
        ÚHOMEz.pdbrcrP   zinvalid
zmain.py)r>   r   r?   s   q
z(NameError: name 'invalid' is not defined)rS   rT   r[   ÚenvironÚpopr   Ztemp_cwdrQ   rR   r   rB   rC   rD   rE   rG   rk   rK   )r	   rU   Z	save_homerW   rM   rN   r>   r?   r
   r
   r   Útest_readrc_kwarg)  s*    


"zPdbTestCase.test_readrc_kwargc          	   C   sd   t ƒ }d}tƒ 6}| td|ƒ¡ | t tjd¡¡ tj|d W d Q R X |  | 	¡ |d ¡ d S )Nz"Nobody expects... blah, blah, blahz
sys.stdoutÚ	set_trace)ÚheaderÚ
)
r   r   Úenter_contextr   Úobjectr'   r(   r~   ra   Úgetvalue)r	   r>   r   Z	resourcesr
   r
   r   Útest_headerI  s    zPdbTestCase.test_headerc             C   s:   d}d}|   ||¡\}}|  tdd„ | ¡ D ƒƒ|¡ d S )Nzprint("SUCCESS")z/
            continue
            quit
        c             s   s   | ]}d |kV  qdS )ÚSUCCESSNr
   )rm   rn   r
   r
   r   ro   Y  s    z.PdbTestCase.test_run_module.<locals>.<genexpr>)r_   rp   rq   rr   )r	   rU   rL   r>   r?   r
   r
   r   Útest_run_moduleR  s    zPdbTestCase.test_run_modulec             C   s:   d}d}|   ||¡\}}|  tdd„ | ¡ D ƒƒ|¡ d S )NzQ
            if __name__ == '__main__':
                print("SUCCESS")
        z/
            continue
            quit
        c             s   s   | ]}d |kV  qdS )r…   Nr
   )rm   rn   r
   r
   r   ro   e  s    z9PdbTestCase.test_module_is_run_as_main.<locals>.<genexpr>)r_   rp   rq   rr   )r	   rU   rL   r>   r?   r
   r
   r   Útest_module_is_run_as_main[  s    z&PdbTestCase.test_module_is_run_as_mainc             C   sX   d}d}|   ||¡\}}|  tdd„ | ¡ D ƒƒ|¡ |  tdd„ | ¡ D ƒƒ|¡ d S )Nz{
            if __name__ == '__main__':
                pass
                print("SUCCESS")
                pass
        z*
            b 3
            quit
        c             s   s   | ]}d |kV  qdS )zBreakpoint 1 atNr
   )rm   rn   r
   r
   r   ro   s  s    z.PdbTestCase.test_breakpoint.<locals>.<genexpr>c             s   s   | ]}d |kV  qdS )r…   Nr
   )rm   rn   r
   r
   r   ro   t  s    )r_   rp   rq   rr   Úall)r	   rU   rL   r>   r?   r
   r
   r   Útest_breakpointg  s
    zPdbTestCase.test_breakpointc             C   s2   d}|   ddg|¡\}}|  tj| dd¡¡ d S )Nz(
            c
            quit
        z-mr'   úrd   )rO   rw   r'   Z_usageÚreplace)r	   rL   r>   r?   r
   r
   r   Útest_run_pdb_with_pdbv  s
    z!PdbTestCase.test_run_pdb_with_pdbc          	   C   sl   d}t  |¡ |d }t |¡ t|dƒ}W d Q R X |  t j|¡ |  d|gd¡\}}|  d| ¡ ¡ d S )NrY   z/__init__.pyrP   z-mrd   z,ImportError: No module named t_main.__main__)	r   rA   r[   r\   rQ   r@   rO   rw   rr   )r	   rZ   r^   rW   r>   r?   r
   r
   r   Útest_module_without_a_main  s    


z&PdbTestCase.test_module_without_a_mainc             C   s:   d}d}|   ||¡\}}|  tdd„ | ¡ D ƒƒ|¡ d S )NzY
                #This is a comment, on line 2

                print("SUCCESS")
        z
            quit
        c             s   s   | ]}d |kV  qdS )z__main__.py(4)<module>()Nr
   )rm   rn   r
   r
   r   ro   —  s   z=PdbTestCase.test_blocks_at_first_code_line.<locals>.<genexpr>)r_   rp   rq   rr   )r	   rU   rL   r>   r?   r
   r
   r   Útest_blocks_at_first_code_line  s
    z*PdbTestCase.test_blocks_at_first_code_linec          	   C   s2  d| _ t | j ¡ | j d }| j d }| j d }|  tj| j ¡ t | j ¡ t|dƒ}| t 	d¡¡ W d Q R X t|dƒ}| t 	d¡¡ W d Q R X t|dƒ}| t 	d¡¡ W d Q R X d	}|  
d
| j g|¡\}}|  tdd„ | ¡ D ƒƒ|¡ |  tdd„ | ¡ D ƒƒ¡ |  tdd„ | ¡ D ƒƒ¡ d S )NrY   z/__main__.pyz/__init__.pyz
/module.pyrP   z6
                top_var = "VAR from top"
            zº
                from . import top_var
                from .module import var
                from . import module
                pass # We'll stop here and print the vars
            zY
                var = "VAR from module"
                var2 = "second var"
            zz
            b 5
            c
            p top_var
            p var
            p module.var2
            quit
        z-mc             s   s   | ]}d |kV  qdS )zVAR from moduleNr
   )rm   rn   r
   r
   r   ro   »  s    z4PdbTestCase.test_relative_imports.<locals>.<genexpr>c             s   s   | ]}d |kV  qdS )zVAR from topNr
   )rm   rn   r
   r
   r   ro   ¼  s    c             s   s   | ]}d |kV  qdS )z
second varNr
   )rm   rn   r
   r
   r   ro   ½  s    )rZ   r   rA   r@   r[   r\   rQ   rR   rS   rT   rO   rp   rq   rr   )r	   r]   r^   Úmodule_filerW   rL   r>   Ú_r
   r
   r   Útest_relative_importsš  s*    


z!PdbTestCase.test_relative_importsc          	   C   sþ   d| _ t | j ¡ | j d }| j d }| j d }|  tj| j ¡ t | j ¡ t|dƒ}| t 	d¡¡ W d Q R X t|dƒ}| t 	d¡¡ W d Q R X t|dƒ}| t 	d¡¡ W d Q R X d	}|  
d
| j d g|¡\}}|  tdd„ | ¡ D ƒƒ|¡ d S )NrY   z	/runme.pyz/__init__.pyz
/module.pyrP   z6
                top_var = "VAR from top"
            zl
                from . import module
                pass # We'll stop here and print the vars
            z5
                var = "VAR from module"
            zQ
            b 3
            c
            p module.var
            quit
        z-mz.runmec             s   s   | ]}d |kV  qdS )zVAR from moduleNr
   )rm   rn   r
   r
   r   ro   Ü  s    zDPdbTestCase.test_relative_imports_on_plain_module.<locals>.<genexpr>)rZ   r   rA   r@   r[   r\   rQ   rR   rS   rT   rO   rp   rq   rr   )r	   r]   r^   r   rW   rL   r>   r   r
   r
   r   Ú%test_relative_imports_on_plain_module¿  s&    


z1PdbTestCase.test_relative_imports_on_plain_modulec             C   sT   d  ddddg¡}|  d|d ¡\}}|  | ¡ dd … dd	d
dd	ddddg	¡ d S )Nr€   zprint(zdebug print(zdebug doesnotexistÚcrd   r`   z3(Pdb) *** SyntaxError: unexpected EOF while parsingz!(Pdb) ENTERING RECURSIVE DEBUGGERz-*** SyntaxError: unexpected EOF while parsingzLEAVING RECURSIVE DEBUGGERz> <string>(1)<module>()z9((Pdb)) *** NameError: name 'doesnotexist' is not definedz(Pdb) )ÚjoinrX   ra   rr   )r	   rL   r>   r   r
   r
   r   Útest_errors_in_commandÞ  s     z"PdbTestCase.test_errors_in_commandN)r   r   r   r<   rO   rX   r_   rc   re   rh   rl   rs   rv   ry   r}   r„   r†   r‡   r‰   rŒ   r   rŽ   r‘   r’   r•   r
   r
   r
   r   r9   ‰  s,   $ 		%r9   c              G   s*   ddl m} t t¡t |¡g}t |¡S )Nr   )Útest_pdb)Útestr–   ÚunittestZ	makeSuiter9   ÚdoctestZDocTestSuiteZ	TestSuite)Úargsr–   Zsuitesr
   r
   r   Ú
load_testsõ  s    r›   Ú__main__)5r™   r[   r'   r   Útypesr˜   rC   rS   Ú
contextlibr   Úior   r—   r   Ztest.test_doctestr   Zunittest.mockr   r‚   r   r   r   r   r   r    r!   r"   r#   Ú
ModuleTypeÚmodÚexecÚ__dict__r$   r%   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   ZTestCaser9   r›   r   Úmainr
   r
   r
   r   Ú<module>   sZ   n`>
+*A;7<7),.)/  n	
