o
    gq                     @   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 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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/d0 Z,d1d2 Z-d3d4 Z.d5d6 Z/d7d8 Z0d9d: Z1d;d< Z2d=d> Z3d?d@ Z4dAdB Z5dCdD Z6G dEdF dFej7Z8G dGdH dHej7Z9dIdJ Z:e;dKkre<  dS dS )L    N)	ExitStack)StringIO)	os_helper)
_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   C/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/test/test_pdb.py__init__   s   
zPdbTestInput.__init__c                 C   s6   t j| _t| jt _tt drt  | _d S 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 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      d S r   r   r   r   r   r   test_pdb_displayhook'      r   c                   C   r   r   r   r   r   r   r   test_pdb_basic_commandsD   r   r   c                  C   s   dd l } | j  d S )Nr   )bdbZ
BreakpointZclearBreakpoints)r    r   r   r   reset_Breakpoint   s   r!   c                   C   r   r   r   r   r   r   r   test_pdb_breakpoint_commands   r   r"   c                   C   r   r   r   r   r   r   r   :test_pdb_breakpoints_preserved_across_interactive_sessionsG  r   r#   c                   C   r   r   r   r   r   r   r   test_pdb_pp_repr_exc  r   r$   c                   C   r   r   r   r   r   r   r   
do_nothing  r   r%   c                   C   s   t d d S )N*   )printr   r   r   r   do_something     r(   c                   C   r   r   r   r   r   r   r   test_list_commands  r   r*   c                   C   r   r   r   r   r   r   r   test_pdb_whatis_command  r   r+   c                   C   r   r   r   r   r   r   r   test_post_mortem5  r   r,   c                   C   r   r   r   r   r   r   r   test_pdb_skip_moduless  r   r-   Zmodule_to_skipz6def foo_pony(callback): x = 1; callback(); return Nonec                   C   r   r   r   r   r   r   r   #test_pdb_skip_modules_with_callback  r   r.   c                   C   r   r   r   r   r   r   r    test_pdb_continue_in_bottomframe  r   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   r5   c                   C   r   r   r   r   r   r   r   $test_pdb_run_with_incorrect_argument  r   r6   c                   C   r   r   r   r   r   r   r   test_pdb_run_with_code_object  r   r7   c                   C   r   r   r   r   r   r   r   &test_next_until_return_at_return_event  r   r8   c                   C   r   r   r   r   r   r   r   #test_pdb_next_command_for_generatorO  r   r9   c                   C   r   r   r   r   r   r   r   #test_pdb_next_command_for_coroutine  r   r:   c                   C   r   r   r   r   r   r   r   "test_pdb_next_command_for_asyncgen  r   r;   c                   C   r   r   r   r   r   r   r   %test_pdb_return_command_for_generator  r   r<   c                   C   r   r   r   r   r   r   r   %test_pdb_return_command_for_coroutine6  r   r=   c                   C   r   r   r   r   r   r   r   $test_pdb_until_command_for_generator`  r   r>   c                   C   r   r   r   r   r   r   r   $test_pdb_until_command_for_coroutine  r   r?   c                   C   r   r   r   r   r   r   r   +test_pdb_next_command_in_generator_for_loop  r   r@   c                   C   r   r   r   r   r   r   r   !test_pdb_next_command_subiterator  r   rA   c                   C   r   r   r   r   r   r   r   test_pdb_issue_20766  r   rB   c                   C   r   r   r   r   r   r   r   test_pdb_issue_43318-  r   rC   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 e!j"d=d> Z#d?d@ Z$dAS )BPdbTestCasec                 C      t t j d S r   r   unlinkTESTFNr   r   r   r   tearDownL     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 s8w   Y  |oCt|}|oJt|}||fS )N__pycache__-mr1   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procrP   rQ   r   r   r   _run_pdbO  s   zPdbTestCase._run_pdbc                 C   s\   	 d}t |d}|t| W d    n1 sw   Y  | tj| | |g|S )Nmain.pyw)openwritetextwrapdedentrS   r   rG   rd   )r
   scriptra   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 s*w   Y  t|d}|t| W d    n1 sGw   Y  | 	tj| j  | 
d| j g|S )Nt_main/__main__.py/__init__.pyrf   rL   )module_namer   rT   rZ   mkdirrg   rh   ri   rj   rS   rd   )r
   rk   ra   	main_file	init_filerm   r   r   r   run_pdb_modulef  s   

zPdbTestCase.run_pdb_modulec                 C   sl   t tjd}|| W d    n1 sw   Y  |sd n	|d tj|d f}| |t|tj d S )Nwbr      )rg   r   rH   rh   assertEqualr1   Zfind_function)r
   Zfile_content	func_nameexpectedrm   r   r   r   _assert_find_functiont  s   z!PdbTestCase._assert_find_functionc                 C   s   |  ddd  d S )N    Zfoo)r|   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      r|   r^   r   r   r   r   test_find_function_found  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_cookie  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   rx   )r|   codecsBOM_UTF8r^   r   r   r   r   !test_find_function_found_with_bom  s   z-PdbTestCase.test_find_function_found_with_bomc                 C   s   t tjd}|d W d    n1 sw   Y  tjddtjg}tj|tjtjtj	d}| 
|jj |d\}}| d|d d S )	Nrw   s   print("testing my pdb")
rL   r1   rP   r   rQ   s   quit
s   SyntaxErrorz0Got a syntax error running test script under PDB)rg   r   rH   rh   r   rU   rV   rW   rX   rY   rS   rP   closer\   assertNotInr
   rm   rb   rc   rP   rQ   r   r   r   test_issue7964  s   zPdbTestCase.test_issue7964c                 C   s   d}d}d}t dd}|t| W d    n1 sw   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.pyrf   c                 s       | ]}d |v V  qdS )zmain.py(5)foo()->NoneNr   .0lr   r   r   	<genexpr>      z.PdbTestCase.test_issue13183.<locals>.<genexpr>z+Fail to step into the caller after a return)rg   rh   ri   rj   rS   r   rG   rn   
assertTrueany
splitlines)r
   rk   ra   Zbarrm   rP   rQ   r   r   r   test_issue13183  s   zPdbTestCase.test_issue13183c                 C      t tjd}|tdd W d    n1 sw   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 )Nrw   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-urM   rN   rO   s   cont
   Error*Got an error running test script under PDBrg   r   rH   rh   ri   rj   r^   r   rU   rV   rW   rX   rY   rZ   r[   rS   rP   r   r\   r   r   r   r   r   test_issue13120  s"   
zPdbTestCase.test_issue13120c                 C   r   )Nrw   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   rM   rN   rO   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
 zSyntaxError:zE

Expected:
{}
Got:
{}
Fail to handle a syntax error in the debuggee.)rn   assertInformat)r
   rk   ra   r{   rP   rQ   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>-  s    z/PdbTestCase.test_issue26053.<locals>.<listcomp>z#Restarting .* with arguments:
a b cz#Restarting .* with arguments:
d e f)rn   joinr   ZassertRegex)r
   rk   ra   rP   rQ   resr   r   r   test_issue26053#  s   zPdbTestCase.test_issue26053c              	   C   s:  t d}tjdd }zt l tdd}|d W d    n1 s'w   Y  tdd}|| W d    n1 sAw   Y  t	j
dg}tj|tjtjtjd}| |d\}}| d	| W d    n1 sqw   Y  W d    n1 sw   Y  W |d ur|tjd< d S d S |d ur|tjd< w )
Nz_
            import pdb; pdb.Pdb(readrc=False).set_trace()

            print('hello')
        HOME.pdbrcrf   zinvalid
re   r   s   q
s(   NameError: name 'invalid' is not defined)ri   rj   rZ   r[   popr   temp_cwdrg   rh   r   rU   rV   rW   rX   r\   r   )r
   rk   	save_homerm   rb   rc   rP   rQ   r   r   r   test_readrc_kwarg1  s<   


zPdbTestCase.test_readrc_kwargc                 C   s  t jdd }t l}tdO t j|d}|t jj_	z0t
|d}|d W d    n1 s3w   Y  | t jd d W |d urM|t jd< n
|d urW|t jd< w W d    n1 sbw   Y  W d    d S W d    d S 1 szw   Y  d S )Nr   zos.path.expanduserr   rf   invalidr   )rZ   r[   r   r   temp_dirr   pathr   
expanduserZreturn_valuerg   rh   ry   r1   r2   ZrcLines)r
   r   r   Zrc_pathrm   r   r   r   test_readrc_homedirQ  s    

PzPdbTestCase.test_readrc_homedirc                 C   sx   t  }d}t  }|td| |ttjd tj|d W d    n1 s+w   Y  | |	 |d  d S )Nz"Nobody expects... blah, blah, blahz
sys.stdout	set_trace)headerr   )
r   r   enter_contextr   objectr1   r2   r   ry   getvalue)r
   rP   r   Z	resourcesr   r   r   test_header^  s   zPdbTestCase.test_headerc                 C   :   d}d}|  ||\}}| tdd | D | d S )Nzprint("SUCCESS")/
            continue
            quit
        c                 s   r   SUCCESSNr   r   r   r   r   r   n  r   z.PdbTestCase.test_run_module.<locals>.<genexpr>rv   r   r   r   r
   rk   ra   rP   rQ   r   r   r   test_run_moduleg  s   "zPdbTestCase.test_run_modulec                 C   r   )NzQ
            if __name__ == '__main__':
                print("SUCCESS")
        r   c                 s   r   r   r   r   r   r   r   r   z  r   z9PdbTestCase.test_module_is_run_as_main.<locals>.<genexpr>r   r   r   r   r   test_module_is_run_as_mainp  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   r   )zBreakpoint 1 atNr   r   r   r   r   r     r   z.PdbTestCase.test_breakpoint.<locals>.<genexpr>c                 s   s    | ]}d |vV  qdS r   r   r   r   r   r   r     r   )rv   r   r   r   allr   r   r   r   test_breakpoint|  s
   "zPdbTestCase.test_breakpointc                 C   s2   d}|  ddg|\}}| tj|dd d S )Nz(
            c
            quit
        rL   r1   r   )rd   r   r1   Z_usagereplace)r
   ra   rP   rQ   r   r   r   test_run_pdb_with_pdb  s   
z!PdbTestCase.test_run_pdb_with_pdbc                 C   s   d}t | |d }t| t|d}W d    n1 s w   Y  | t j| | d|gd\}}| d|  d S )Nro   rq   rf   rL   r   z,ImportError: No module named t_main.__main__)	r   rT   rZ   rs   rg   rS   rd   r   r   )r
   rr   ru   rm   rP   rQ   r   r   r   test_module_without_a_main  s   

z&PdbTestCase.test_module_without_a_mainc                 C   s   d}d}t | |d | }t| t|d d}W d    n1 s&w   Y  | t j| | d|ddgd\}}| d	| d S )
NZt_pkgro   /rq   rf   rL   .r   z;'t_pkg.t_main' is a package and cannot be directly executed)	r   rT   rZ   makedirsrg   rS   rd   r   r   )r
   pkg_namerr   modpathrm   rP   rQ   r   r   r   test_package_without_a_main  s   

z'PdbTestCase.test_package_without_a_mainc                 C   r   )NzY
                #This is a comment, on line 2

                print("SUCCESS")
        z
            quit
        c                 s   r   )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>w   Y  t|d}|t	d W d    n1 s[w   Y  t|d}|t	d W d    n1 sxw   Y  d	}| 
d
| j g|\}}| tdd | D | | tdd | D  | tdd | D  d S )Nro   rp   rq   
/module.pyrf   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
        rL   c                 s   r   zVAR from moduleNr   r   r   r   r   r     r   z4PdbTestCase.test_relative_imports.<locals>.<genexpr>c                 s   r   )zVAR from topNr   r   r   r   r   r     r   c                 s   r   )z
second varNr   r   r   r   r   r     r   rr   r   rT   rS   rZ   rs   rg   rh   ri   rj   rd   r   r   r   r
   rt   ru   Zmodule_filerm   ra   rP   _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>w   Y  t|d}|t	d W d    n1 s[w   Y  t|d}|t	d W d    n1 sxw   Y  d	}| 
d
| j d g|\}}| tdd | D | d S )Nro   z	/runme.pyrq   r   rf   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
        rL   z.runmec                 s   r   r   r   r   r   r   r   r     r   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cpassrx   )
-> passz+(Pdb) *** SyntaxError: '(' was never closed!(Pdb) ENTERING RECURSIVE DEBUGGERz%*** SyntaxError: '(' was never closedLEAVING RECURSIVE DEBUGGERr   z> <string>(1)<module>()z9((Pdb)) *** NameError: name 'doesnotexist' is not definedr   (Pdb) r   rn   ry   r   )r
   ra   rP   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 qr   rx   r   z(Pdb) *** Cannot run z: r   r   )Zbad_argmsgra   rP   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   s|   	 t d}d}t (}dtj| }| ||\}}| |	dd 
d| W d    d S 1 s7w   Y  d S )NQ
            import sys
            print('sys.path[0] is', sys.path[0])
        c
q(Pdb) sys.path[0] is r      r   )ri   rj   r   r   rZ   r   realpathrn   ry   splitrstrip)r
   rk   ra   cwdr{   rP   rQ   r   r   r   test_issue42384)  s   

"zPdbTestCase.test_issue42384c           
   	   C   s  	 t d}d}t w}tj|}tj|d}tj|d}d| }t| t	tj|dd}|
| W d    n1 sEw   Y  t| ttj|dtj|d | tjddg|\}}	| |dd	 d
| W d    d S 1 sw   Y  d S )Nr   r   dir_onedir_twor   foo.pyrf   r   r   r   )ri   rj   r   r   rZ   r   r   r   rs   rg   rh   symlinkrd   ry   r   r   )
r
   rk   ra   r   r   r   r{   rm   rP   rQ   r   r   r   test_issue42384_symlink8  s"   




 "z#PdbTestCase.test_issue42384_symlinkc           	   	   C   s  t  x}tdd}td}|| W d    n1 sw   Y  tj|d}t	| t	tj|d tj|d}t|d}|d W d    n1 sVw   Y  | 
dgd\}}d}| |dd	 d
| W d    d S 1 sw   Y  d S )Nr   rf   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   rg   ri   rj   rh   rZ   r   r   rs   rd   ry   r   r   )	r
   r   rm   sr   Z
wrong_filerP   rQ   r{   r   r   r   test_issue42383Q  s    

	
"zPdbTestCase.test_issue42383N)%r   r   r   rI   rd   rn   rv   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   r   Zskip_unless_symlinkr   r   r   r   r   r   rD   K  sD    	
$ 		%
rD   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   setUpj  r)   zChecklineTests.setUpc                 C   rE   r   rF   r   r   r   r   rI   m  rJ   zChecklineTests.tearDownc                 C   sX   t tjd}|d W d    n1 sw   Y  t }| |tjdd d S Nrf   z
print(123)rx   )rg   r   rH   rh   r1   r2   ry   	checkliner
   rm   dbr   r   r   test_checkline_before_debuggingp  s
   z.ChecklineTests.test_checkline_before_debuggingc                 C   s`   t tjd}|d W d    n1 sw   Y  t }|  | |tjdd d S r   )	rg   r   rH   rh   r1   r2   resetry   r   r   r   r   r   test_checkline_after_resetv  s   z)ChecklineTests.test_checkline_after_resetc                 C   s~   t tjd}td}|| W d    n1 sw   Y  t }t|	 d }t
|D ]}| |tj| q0d S )Nrf   zl
                # Comment
                """ docstring """
                ''' docstring '''

            r   )rg   r   rH   ri   rj   rh   r1   r2   lenr   rangeZassertFalser   )r
   rm   r   r   Z	num_lineslinenor   r   r    test_checkline_is_not_executable}  s   
	z/ChecklineTests.test_checkline_is_not_executableN)r   r   r   r   rI   r   r  r  r   r   r   r   r   i  s    r   c                  G   s2   ddl m} ttttt|g}t|S )Nr   )test_pdb)	testr  unittestZ	makeSuiterD   r   doctestZDocTestSuiteZ	TestSuite)argsr  Zsuitesr   r   r   
load_tests  s   
r  __main__)=r
  rZ   r1   r   typesr   r	  rV   ri   r   
contextlibr   ior   Ztest.supportr   Ztest.test_doctestr   Zunittest.mockr   r   r   r   r   r!   r"   r#   r$   r%   r(   r*   r+   r,   r-   
ModuleTypemodexec__dict__r.   r/   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   ZTestCaserD   r   r  r   mainr   r   r   r   <module>   st    jC_*>
+*@;8=7*,/*/    "$
