a
    ze7                     @   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Zd dl	Z	d dl
mZm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 dd	 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!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.d0d1 Z/d2d3 Z0d4d5 Z1d6d7 Z2d8d9 Z3d:d; Z4d<d= Z5d>d? Z6d@dA Z7dBdC Z8G dDdE dEej9Z:G dFdG dGej9Z;dHdI Z<e=dJkre>  dS )K    N)	ExitStackredirect_stdout)StringIO)support)
_FakeInput)patch)
Breakpointc                   C   s   dt _i t _d gt _d S )N   )r   nextZbplistZ
bpbynumber r   r   #/usr/lib/python3.9/test/test_pdb.pyreset_Breakpoint   s    r   c                   @   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   r   r   __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 r   )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 r   r   r   r   r   r   test_pdb_displayhook.   s    r!   c                   C   s   d S r   r   r   r   r   r   test_pdb_basic_commandsK   s    r"   c                   C   s   d S r   r   r   r   r   r   test_pdb_breakpoint_commands   s    r#   c                   C   s   d S r   r   r   r   r   r   test_pdb_pp_repr_excL  s    r$   c                   C   s   d S r   r   r   r   r   r   
do_nothingh  s    r%   c                   C   s   t d d S )N*   )printr   r   r   r   do_somethingk  s    r(   c                   C   s   d S r   r   r   r   r   r   test_list_commandsn  s    r)   c                   C   s   d S r   r   r   r   r   r   test_pdb_whatis_command  s    r*   c                   C   s   d S r   r   r   r   r   r   test_post_mortem  s    r+   c                   C   s   d S r   r   r   r   r   r   test_pdb_skip_modules5  s    r,   Zmodule_to_skipz6def foo_pony(callback): x = 1; callback(); return Nonec                   C   s   d S r   r   r   r   r   r   #test_pdb_skip_modules_with_callbackQ  s    r-   c                   C   s   d S r   r   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_invoke  s    r4   c                   C   s   d S r   r   r   r   r   r   $test_pdb_run_with_incorrect_argument  s    r5   c                   C   s   d S r   r   r   r   r   r   test_pdb_run_with_code_object  s    r6   c                   C   s   d S r   r   r   r   r   r   &test_next_until_return_at_return_event  s    r7   c                   C   s   d S r   r   r   r   r   r   #test_pdb_next_command_for_generator  s    r8   c                   C   s   d S r   r   r   r   r   r   #test_pdb_next_command_for_coroutineL  s    r9   c                   C   s   d S r   r   r   r   r   r   "test_pdb_next_command_for_asyncgen  s    r:   c                   C   s   d S r   r   r   r   r   r   %test_pdb_return_command_for_generator  s    r;   c                   C   s   d S r   r   r   r   r   r   %test_pdb_return_command_for_coroutine  s    r<   c                   C   s   d S r   r   r   r   r   r   $test_pdb_until_command_for_generator"  s    r=   c                   C   s   d S r   r   r   r   r   r   $test_pdb_until_command_for_coroutineN  s    r>   c                   C   s   d S r   r   r   r   r   r   +test_pdb_next_command_in_generator_for_loop}  s    r?   c                   C   s   d S r   r   r   r   r   r   !test_pdb_next_command_subiterator  s    r@   c                   C   s   d S r   r   r   r   r   r   test_pdb_issue_20766  s    rA   c                   C   s   d S r   r   r   r   r   r   test_pdb_issue_43318  s    rB   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-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!e"j#d?d@ Z$dAdB Z%dCS )DPdbTestCasec                 C   s   t t j d S r   r   unlinkTESTFNr   r   r   r   tearDown  s    zPdbTestCase.tearDownc                 C   s   |  tjd tjddg| }tj|tjtjtji t	j
ddid$}|t|\}}W d    n1 sp0    Y  |ot|}|ot|}||fS )N__pycache__-mr0   PYTHONIOENCODINGutf-8stdoutr   stderrenv)
addCleanupr   rmtreer   
executable
subprocessPopenPIPESTDOUTosenvironcommunicatestrencodebytesdecode)r   Zpdb_argscommandscmdprocrM   rN   r   r   r   _run_pdb  s    2zPdbTestCase._run_pdbc                 C   sZ   d}t |d }|t| W d    n1 s40    Y  | tj| | |g|S )Nmain.pyw)openwritetextwrapdedentrP   r   rE   ra   )r   scriptr^   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    n1 sR0    Y  t|d }|t| W d    n1 s0    Y  | 	tj| j  | 
d| j g|S )Nt_main/__main__.py/__init__.pyrc   rI   )module_namer   rQ   rW   mkdirrd   re   rf   rg   rP   ra   )r   rh   r^   	main_file	init_filerj   r   r   r   run_pdb_module'  s    

.zPdbTestCase.run_pdb_modulec                 C   sl   t tjd}|| W d    n1 s,0    Y  |s>d n|d tj|d f}| |t|tj d S )Nwbr   r	   )rd   r   rF   re   assertEqualr0   Zfind_function)r   Zfile_content	func_nameexpectedrj   r   r   r   _assert_find_function5  s    (z!PdbTestCase._assert_find_functionc                 C   s   |  ddd  d S )N    Zfoo)rx   r   r   r   r   test_find_function_empty_file>  s    z)PdbTestCase.test_find_function_empty_filec                 C   s   |  d dd d S )Nu@   def foo():
    pass

def bœr():
    pass

def quux():
    pass
   bœr)r{      rx   r[   r   r   r   r   test_find_function_foundA  s
    
z$PdbTestCase.test_find_function_foundc                 C   s   |  dddd d S )NuV   # coding: iso-8859-15
def foo():
    pass

def bœr():
    pass

def quux():
    pass
ziso-8859-15r{   )r{      r}   r   r   r   r   -test_find_function_found_with_encoding_cookieQ  s    
z9PdbTestCase.test_find_function_found_with_encoding_cookiec                 C   s   |  tjd  dd d S )Nu   def bœr():
    pass
r{   )r{   r	   )rx   codecsBOM_UTF8r[   r   r   r   r   !test_find_function_found_with_bomb  s
    z-PdbTestCase.test_find_function_found_with_bomc                 C   s   t tjd}|d W d    n1 s,0    Y  tjddtjg}tj|tjtjtj	d}| 
|jj |d\}}| d|d d S )	Nrt   s   print("testing my pdb")
rI   r0   rM   r   rN   s   quit
s   SyntaxErrorz0Got a syntax error running test script under PDB)rd   r   rF   re   r   rR   rS   rT   rU   rV   rP   rM   closerY   assertNotInr   rj   r_   r`   rM   rN   r   r   r   test_issue7964l  s    (zPdbTestCase.test_issue7964c                 C   sH   d}d}|  ||\}}|pd|p$d }| d|d | d|d d S )Nz
            def do_testcmdwithnodocs(self, arg):
                pass

            import pdb
            pdb.Pdb.do_testcmdwithnodocs = do_testcmdwithnodocs
        zA
            continue
            help testcmdwithnodocs
         AttributeErrorzCCalling help on a command with no docs should be handled gracefullyz;*** No help for 'testcmdwithnodocs'; __doc__ string missingz<Calling help on a command with no docs should print an error)rk   r   assertIn)r   rh   r^   rM   rN   outputr   r   r   test_issue46434{  s    zPdbTestCase.test_issue46434c                 C   s   d}d}d}t dd }|t| W d    n1 s<0    Y  | 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.pyrc   c                 s   s   | ]}d |v V  qdS )zmain.py(5)foo()->NoneNr   .0lr   r   r   	<genexpr>  ry   z.PdbTestCase.test_issue13183.<locals>.<genexpr>z+Fail to step into the caller after a return)rd   re   rf   rg   rP   r   rE   rk   
assertTrueany
splitlines)r   rh   r^   Zbarrj   rM   rN   r   r   r   test_issue13183  s    .zPdbTestCase.test_issue13183c                 C   s   t tjd&}|tdd W d    n1 s80    Y  tjdtjg}t	j
|t	jt	jt	ji tjddid}| |jj |d\}}| d	|d
 d S )Nrt   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()ascii-urJ   rK   rL   s   cont
   Error*Got an error running test script under PDBrd   r   rF   re   rf   rg   r[   r   rR   rS   rT   rU   rV   rW   rX   rP   rM   r   rY   r   r   r   r   r   test_issue13120  s     
$zPdbTestCase.test_issue13120c                 C   s   t tjd&}|tdd W d    n1 s80    Y  tjdtjg}t	j
|t	jt	jt	ji tjddid}| |jj |d\}}| d	|d
 d S )Nrt   a  
                import threading
                import pdb

                evt = threading.Event()

                def start_pdb():
                    evt.wait()
                    pdb.Pdb(readrc=False).set_trace()

                t = threading.Thread(target=start_pdb)
                t.start()
                pdb.Pdb(readrc=False).set_trace()
                evt.set()
                t.join()r   r   rJ   rK   rL   s
   cont
cont
r   r   r   r   r   r   r   test_issue36250  s     $zPdbTestCase.test_issue36250c                 C   s6   d}d}d}|  ||\}}| ||d|| d S )Nzdef f: pass
r   zSyntaxError:zE

Expected:
{}
Got:
{}
Fail to handle a syntax error in the debuggee.)rk   r   format)r   rh   r^   rw   rM   rN   r   r   r   test_issue16180  s    zPdbTestCase.test_issue16180c                 C   sL   d}d}|  ||\}}ddd | D }| |d | |d d S )Nzprint('hello')z[
            continue
            run a b c
            run d e f
            quit
        
c                 S   s   g | ]}|  qS r   )strip)r   xr   r   r   
<listcomp>  ry   z/PdbTestCase.test_issue26053.<locals>.<listcomp>z#Restarting .* with arguments:
a b cz#Restarting .* with arguments:
d e f)rk   joinr   ZassertRegex)r   rh   r^   rM   rN   resr   r   r   test_issue26053  s    zPdbTestCase.test_issue26053c              	   C   s@  t d}tjdd }z
t  tdd}|d W d    n1 sP0    Y  tdd}|| W d    n1 s0    Y  t	j
dg}tj|tjtjtjd}|* |d\}}| d	| W d    n1 s0    Y  W d    n1 s0    Y  W |d ur<|tjd< n|d ur:|tjd< 0 d S )
Nz_
            import pdb; pdb.Pdb(readrc=False).set_trace()

            print('hello')
        HOME.pdbrcrc   zinvalid
rb   r   s   q
s(   NameError: name 'invalid' is not defined)rf   rg   rW   rX   popr   temp_cwdrd   re   r   rR   rS   rT   rU   rY   r   )r   rh   	save_homerj   r_   r`   rM   rN   r   r   r   test_readrc_kwarg  s0    

((
D

zPdbTestCase.test_readrc_kwargc                 C   s   t jdd }t }td t j|d}|t jj_	z`t
|d}|d W d    n1 sf0    Y  | t jd d W |d ur|t jd< n|d ur|t jd< 0 W d    n1 s0    Y  W d    n1 s0    Y  d S )Nr   zos.path.expanduserr   rc   invalidr   )rW   rX   r   r   temp_dirr   pathr   
expanduserZreturn_valuerd   re   ru   r0   r1   ZrcLines)r   r   r   Zrc_pathrj   r   r   r   test_readrc_homedir'  s    
(zPdbTestCase.test_readrc_homedirc                 C   sx   t  }d}t @}|td| |ttjd tj|d W d    n1 sV0    Y  | |	 |d  d S )Nz"Nobody expects... blah, blah, blahz
sys.stdout	set_trace)headerr   )
r   r   enter_contextr   objectr0   r1   r   ru   getvalue)r   rM   r   Z	resourcesr   r   r   test_header4  s    *zPdbTestCase.test_headerc                 C   s:   d}d}|  ||\}}| tdd | D | d S )Nzprint("SUCCESS")/
            continue
            quit
        c                 s   s   | ]}d |v V  qdS SUCCESSNr   r   r   r   r   r   D  ry   z.PdbTestCase.test_run_module.<locals>.<genexpr>rs   r   r   r   r   rh   r^   rM   rN   r   r   r   test_run_module=  s    zPdbTestCase.test_run_modulec                 C   s:   d}d}|  ||\}}| tdd | D | d S )NzQ
            if __name__ == '__main__':
                print("SUCCESS")
        r   c                 s   s   | ]}d |v V  qdS r   r   r   r   r   r   r   P  ry   z9PdbTestCase.test_module_is_run_as_main.<locals>.<genexpr>r   r   r   r   r   test_module_is_run_as_mainF  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 |v V  qdS )zBreakpoint 1 atNr   r   r   r   r   r   ^  ry   z.PdbTestCase.test_breakpoint.<locals>.<genexpr>c                 s   s   | ]}d |vV  qdS r   r   r   r   r   r   r   _  ry   )rs   r   r   r   allr   r   r   r   test_breakpointR  s
    zPdbTestCase.test_breakpointc                 C   s2   d}|  ddg|\}}| tj|dd d S )Nz(
            c
            quit
        rI   r0   r   )ra   r   r0   Z_usagereplace)r   r^   rM   rN   r   r   r   test_run_pdb_with_pdba  s    
z!PdbTestCase.test_run_pdb_with_pdbc                 C   s   d}t | |d }t| t|d W d    n1 s@0    Y  | t j| | d|gd\}}| d|  d S )Nrl   rn   rc   rI   r   z,ImportError: No module named t_main.__main__)	r   rQ   rW   rp   rd   rP   ra   r   r   )r   ro   rr   rM   rN   r   r   r   test_module_without_a_mainl  s    

z&PdbTestCase.test_module_without_a_mainc                 C   s   d}d}t | |d | }t| t|d d W d    n1 sL0    Y  | t j| | d|ddgd\}}| d	| d S )
NZt_pkgrl   /rn   rc   rI   .r   z;'t_pkg.t_main' is a package and cannot be directly executed)	r   rQ   rW   makedirsrd   rP   ra   r   r   )r   Zpkg_namero   modpathrM   rN   r   r   r   test_package_without_a_mainx  s    

z'PdbTestCase.test_package_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 |v V  qdS )z__main__.py(4)<module>()Nr   r   r   r   r   r     s   z=PdbTestCase.test_blocks_at_first_code_line.<locals>.<genexpr>r   r   r   r   r   test_blocks_at_first_code_line  s    z*PdbTestCase.test_blocks_at_first_code_linec                 C   sn  d| _ t| j  | j d }| j d }| j d }| tj| j  t| j  t|d }|t	d W d    n1 s|0    Y  t|d }|t	d W d    n1 s0    Y  t|d }|t	d W d    n1 s0    Y  d	}| 
d
| j g|\}}| tdd | D | | tdd | D  | tdd | D  d S )Nrl   rm   rn   
/module.pyrc   6
                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
        rI   c                 s   s   | ]}d |v V  qdS zVAR from moduleNr   r   r   r   r   r     ry   z4PdbTestCase.test_relative_imports.<locals>.<genexpr>c                 s   s   | ]}d |v V  qdS )zVAR from topNr   r   r   r   r   r     ry   c                 s   s   | ]}d |v V  qdS )z
second varNr   r   r   r   r   r     ry   ro   r   rQ   rP   rW   rp   rd   re   rf   rg   ra   r   r   r   r   rq   rr   Zmodule_filerj   r^   rM   _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    n1 s|0    Y  t|d }|t	d W d    n1 s0    Y  t|d }|t	d W d    n1 s0    Y  d	}| 
d
| j d g|\}}| tdd | D | d S )Nrl   z	/runme.pyrn   r   rc   r   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
        rI   z.runmec                 s   s   | ]}d |v V  qdS r   r   r   r   r   r   r     ry   zDPdbTestCase.test_relative_imports_on_plain_module.<locals>.<genexpr>r   r   r   r   r   %test_relative_imports_on_plain_module  s     


...z1PdbTestCase.test_relative_imports_on_plain_modulec                 C   sB   d g d}| d|d \}}| | dd  g d d S )Nr   )zprint(zdebug print(zdebug doesnotexistcr   r	   )	z3(Pdb) *** SyntaxError: unexpected EOF while parsing!(Pdb) ENTERING RECURSIVE DEBUGGERz-*** SyntaxError: unexpected EOF while parsingLEAVING RECURSIVE DEBUGGERr   z> <string>(1)<module>()z9((Pdb)) *** NameError: name 'doesnotexist' is not definedr   (Pdb) r   rk   ru   r   )r   r^   rM   r   r   r   r   test_errors_in_command  s    z"PdbTestCase.test_errors_in_commandc                    s$    fdd}|dd |dd d S )Nc                    sV   d d|  dg} d|d \}} | dd  dd|  d| d	g d S )
Nr   zrun qpassr	   z-> passz(Pdb) *** Cannot run z: r   r   )Zbad_argmsgr^   rM   r   r   r   r   check  s    z*PdbTestCase.test_issue34266.<locals>.check\zNo escaped character"zNo closing quotationr   )r   r   r   r   r   test_issue34266  s    
zPdbTestCase.test_issue34266c                 C   sx   t d}d}t N}dtj| }| ||\}}| |	dd 
d| W d    n1 sj0    Y  d S )NQ
            import sys
            print('sys.path[0] is', sys.path[0])
        c
q(Pdb) sys.path[0] is r      r   )rf   rg   r   r   rW   r   realpathrk   ru   splitrstrip)r   rh   r^   cwdrw   rM   rN   r   r   r   test_issue42384  s    

zPdbTestCase.test_issue42384c           
   	   C   s  t d}d}t }tj|}tj|d}tj|d}d| }t| t	tj|dd}|
| W d    n1 s0    Y  t| ttj|dtj|d | tjddg|\}}	| |dd	 d
| W d    n1 s
0    Y  d S )Nr   r   dir_onedir_twor   foo.pyrc   r   r   r   )rf   rg   r   r   rW   r   r   r   rp   rd   re   symlinkra   ru   r   r   )
r   rh   r^   r   r   r   rw   rj   rM   rN   r   r   r   test_issue42384_symlink  s    



(
 z#PdbTestCase.test_issue42384_symlinkc           	   	   C   s
  t  }tdd$}td}|| W d    n1 s>0    Y  tj|d}t	| t	tj|d tj|d}t|d}|d W d    n1 s0    Y  | 
dgd\}}d}| |dd	 d
| W d    n1 s0    Y  d S )Nr   rc   z
                    print('The correct file was executed')

                    import os
                    os.chdir("subdir")
                subdirz$print("The wrong file was executed")zc
c
qz#(Pdb) The correct file was executedr      r   )r   r   rd   rf   rg   re   rW   r   r   rp   ra   ru   r   r   )	r   r   rj   sr   Z
wrong_filerM   rN   rw   r   r   r   test_issue42383&  s    

(
(zPdbTestCase.test_issue42383N)&r   r   r    rG   ra   rk   rs   rx   rz   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zskip_unless_symlinkr   r   r   r   r   r   rC     sD   	
$ 		%
rC   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )ChecklineTestsc                 C   s   t   d S r   )	linecache
clearcacher   r   r   r   setUp?  s    zChecklineTests.setUpc                 C   s   t t j d S r   rD   r   r   r   r   rG   B  s    zChecklineTests.tearDownc                 C   sX   t tjd}|d W d    n1 s,0    Y  t }| |tjdd d S Nrc   z
print(123)r	   )rd   r   rF   re   r0   r1   ru   	checkliner   rj   dbr   r   r   test_checkline_before_debuggingE  s    (z.ChecklineTests.test_checkline_before_debuggingc                 C   s`   t tjd}|d W d    n1 s,0    Y  t }|  | |tjdd d S r   )	rd   r   rF   re   r0   r1   resetru   r   r   r   r   r   test_checkline_after_resetK  s
    (z)ChecklineTests.test_checkline_after_resetc                 C   s   t d}ttjd}|| W d    n1 s60    Y  t| d }tt	 : t
 }t|D ]}| |tj| qlW d    n1 s0    Y  d S )Nz\
            # Comment
            """ docstring """
            ''' docstring '''

        rc   r   )rf   rg   rd   r   rF   re   lenr   r   r   r0   r1   rangeZassertFalser   )r   r   rj   Z	num_linesr   linenor   r   r    test_checkline_is_not_executableR  s    
(z/ChecklineTests.test_checkline_is_not_executableN)r   r   r    r   rG   r   r   r   r   r   r   r   r   >  s
   r   c                  G   s2   ddl m} ttttt|g}t|S )Nr   )test_pdb)	testr   unittestZ	makeSuiterC   r   doctestZDocTestSuiteZ	TestSuite)argsr   Zsuitesr   r   r   
load_testsc  s    r  __main__)?r  rW   r0   r   typesr   r  rS   rf   r   
contextlibr   r   ior   r  r   Ztest.test_doctestr   Zunittest.mockr   Zbdbr   r   r   r   r!   r"   r#   r$   r%   r(   r)   r*   r+   r,   
ModuleTypemodexec__dict__r-   r.   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   ZTestCaserC   r   r  r   mainr   r   r   r   <module>   sp    k_*>
+*@;8=7*,/*/    6%

