B
    `_4                 @   s  d 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
 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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lmZ ddlmZ ddlmZ ddlmZ er&ddl m!Z! ddl"m#Z# e$ee$e$f dddZ%eddddZ&edddd Z'G d!d" d"Z(G d#d$ d$Z)G d%d& d&Z*d'd( Z+d)d* Z,eee- eed+d,d-Z.ee- ej/d.d/d0Z0ej/dd1d2d3Z1dS )4z4Interactive debugging with PDB, the Python Debugger.    N)Any)Callable)	Generator)List)Optional)Tuple)Type)TYPE_CHECKING)Union)outcomes)ExceptionInfo)Config)ConftestImportFailure)hookimpl)PytestPluginManager)Parser)
UsageError)Node)
BaseReport)CaptureManager)CallInfo)valuereturnc          
   C   sP   y|  d\}}W n4 tk
rF } zt| d|W dd}~X Y nX ||fS )z#Validate syntax of --pdbcls option.:z) is not in the format 'modname:classname'N)split
ValueErrorargparseArgumentTypeError)r   modname	classnamee r!   Y/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/_pytest/debugging.py_validate_usepdb_cls    s    
r#   )parserr   c             C   sF   |  d}|jddddd |jddd	td
d |jddddd d S )NZgeneralz--pdbusepdb
store_truezEstart the interactive Python debugger on errors or KeyboardInterrupt.)destactionhelpz--pdbcls
usepdb_clszmodulename:classnamezqstart a custom interactive Python debugger on errors. For example: --pdbcls=IPython.terminal.debugger:TerminalPdb)r'   metavartyper)   z--tracetracez)Immediately break when running each test.)ZgetgroupZ
_addoptionr#   )r$   groupr!   r!   r"   pytest_addoption+   s"    
r/   )configr   c                s   dd l  | dr"| jt d | dr<| jt d tj j	tj
tjf tj	 _	| jt_
| t_d d fdd}| j| d S )	Nr   r-   Zpdbtracer%   Z	pdbinvoke)r   c                  s   t j \ _t _t _d S )N)	pytestPDB_savedpop	set_trace_pluginmanager_configr!   )pdbr!   r"   finT   s    zpytest_configure.<locals>.fin)r7   getvaluepluginmanagerregisterPdbTrace	PdbInvoker1   r2   appendr4   r5   r6   _cleanup)r0   r8   r!   )r7   r"   pytest_configureC   s    

r@   c               @   s   e Zd ZU dZdZee ed< dZee	 ed< g Z
eeed ee ee	 f  ed< dZdZeeee ee f  ed< eed	 eeef d
ddZeed	 dddZeed	 dddZedd ZeddddZdS )r1   z'Pseudo PDB that defers to the real pdb.Nr5   r6   ).Nr2   r   _wrapped_pdb_clsr   )capmanr   c             C   s   |r|  S dS )NF)Zis_capturing)clsrB   r!   r!   r"   _is_capturingi   s    zpytestPDB._is_capturing)rB   c          
   C   s
  | j sdd l}|jS | j d}| jr>| jd |kr>| jd S |r|\}}yNt| tj| }|d}t	||d }x|dd  D ]}	t	||	}qW W q t
k
r }
 z(d||f}td|d|
 |
W d d }
~
X Y qX ndd l}|j}| ||}||f| _|S )Nr   r*      .r   z--pdbcls: could not import z: )r6   r7   ZPdbr9   rA   
__import__sysmodulesr   getattr	Exceptionjoinr   _get_pdb_wrapper_class)rC   rB   r7   r*   r   r   modpartspdb_clspartexcr   Zwrapped_clsr!   r!   r"   _import_pdb_clso   s2    



zpytestPDB._import_pdb_clsc                s$   dd l  G  fddd|}|S )Nr   c                   sl   e Zd ZZdZ fddZ fddZe ZZ fddZ	e	Z
e	Z fdd	Z fd
dZ  ZS )z:pytestPDB._get_pdb_wrapper_class.<locals>.PytestPdbWrapperFc                s,    j d7  _ t |} j d8  _ |S )NrE   )_recursive_debugsuperdo_debug)selfargret)	__class__rC   r!   r"   rV      s    zCpytestPDB._get_pdb_wrapper_class.<locals>.PytestPdbWrapper.do_debugc                s   t  |}jdkrjd k	s$tjj}|  | j}t	
|}|r|dkrd|dd n|dd|  |d k	st|  n|dd jd k	stjjjj| d d| _|S )	Nr   global>z#PDB continue (IO-capturing resumed)z*PDB continue (IO-capturing resumed for %s)zPDB continue)r0   r7   T)rU   do_continuerT   r6   AssertionErrorr0   create_terminal_writerline_pytest_capmanr1   rD   sepresumer5   hookZpytest_leave_pdb
_continued)rW   rX   rY   twrB   	capturing)rZ   _pytestrC   r!   r"   r]      s*    


zFpytestPDB._get_pdb_wrapper_class.<locals>.PytestPdbWrapper.do_continuec                s$   t  |}jdkr td |S )a  Raise Exit outcome when quit command is used in pdb.

                This is a bit of a hack - it would be better if BdbQuit
                could be handled, but this would require to wrap the
                whole pytest run, and adjust the report etc.
                r   zQuitting debugger)rU   do_quitrT   r   exit)rW   rX   rY   )rZ   rC   r!   r"   ri      s    

zBpytestPDB._get_pdb_wrapper_class.<locals>.PytestPdbWrapper.do_quitc                s0   t  ||}|s,| jr,| jr,| jjdd |S )zSuspend on setup().

                Needed after do_continue resumed, and entering another
                breakpoint again.
                T)in_)rU   setupre   ra   suspend_global_capture)rW   ftbrY   )rZ   r!   r"   rl      s
    
z@pytestPDB._get_pdb_wrapper_class.<locals>.PytestPdbWrapper.setupc                s\   t  ||\}}|d krTtdt|d }x&|rR|| d jddrR|d8 }q.W ||fS )Nr   rE   Z__tracebackhide__F)rU   	get_stackmaxlenf_localsget)rW   rn   tstacki)rZ   r!   r"   rp      s    zDpytestPDB._get_pdb_wrapper_class.<locals>.PytestPdbWrapper.get_stack)__name__
__module____qualname__ra   re   rV   r]   Zdo_cZdo_contri   Zdo_qZdo_exitrl   rp   __classcell__r!   )rh   rB   rC   )rZ   r"   PytestPdbWrapper   s   r|   )_pytest.config)rC   rP   rB   r|   r!   )rh   rB   rC   r"   rM      s    Oz pytestPDB._get_pdb_wrapper_classc       
      O   s   ddl }| jdkrd}n| jd}|r4|jdd | jr|j| j}|  | jdkr|	dd}|dk	r||
d| nT| |}|dkr|
dd	| d
 n,|r|
dd||f  n|
dd	|  | |f |}	| jr| jjj| j|	d |	S )z4Initialize PDB debugging, dropping any IO capturing.r   NcapturemanagerT)rk   headerr\   r[   zPDB z (IO-capturing turned off)z'PDB %s (IO-capturing turned off for %s))r0   r7   )r}   r5   	getpluginZsuspendr6   r0   r_   r`   rT   r3   rb   rD   rS   rd   Zpytest_enter_pdb)
rC   methodargskwargsrh   rB   rf   r   rg   _pdbr!   r!   r"   	_init_pdb   s4    


zpytestPDB._init_pdb)r   c             O   s(   t  j}| jd||}|| dS )zBInvoke debugging via ``Pdb.set_trace``, dropping any IO capturing.r4   N)r4   )rH   	_getframef_backr   r4   )rC   r   r   framer   r!   r!   r"   r4     s    
zpytestPDB.set_trace)rx   ry   rz   __doc__r5   r   r   __annotations__r6   r   r2   r   r   r   rT   rA   r   r   classmethodr
   strboolrD   rS   rM   r   r4   r!   r!   r!   r"   r1   ^   s   
& &V(r1   c               @   s4   e Zd ZededdddZee ddddZdS )	r=   zCallInfo[Any]N)nodecallreportr   c             C   sb   |j jd}|rB|jdd | \}}tj| tj| |jd k	sPt	t
||j| d S )Nr~   T)rk   )r0   r:   r   rm   Zread_global_capturerH   stdoutwriteexcinfor^   
_enter_pdb)rW   r   r   r   rB   outerrr!   r!   r"   pytest_exception_interact  s    z#PdbInvoke.pytest_exception_interact)r   r   c             C   s   t |}t| d S )N)_postmortem_tracebackpost_mortem)rW   r   ro   r!   r!   r"   pytest_internalerror*  s    zPdbInvoke.pytest_internalerror)	rx   ry   rz   r   r   r   r   BaseExceptionr   r!   r!   r!   r"   r=     s   
r=   c               @   s(   e Zd Zedded dddZdS )r<   T)Zhookwrapper)NNN)r   c             c   s   t | d V  d S )N) wrap_pytest_function_for_tracing)rW   
pyfuncitemr!   r!   r"   pytest_pyfunc_call0  s    zPdbTrace.pytest_pyfunc_callN)rx   ry   rz   r   r   r   r!   r!   r!   r"   r<   /  s   r<   c                s2   t d | jt fdd}|| _dS )zChange the Python function object of the given Function item by a
    wrapper which actually enters pdb before calling the python function
    itself, effectively leaving the user in the pdb prompt in the first
    statement of the function.runcallc                 s    t jf| |} | d S )N)	functoolspartialr   )r   r   func)r   testfunctionr!   r"   wrapperA  s    z1wrap_pytest_function_for_tracing.<locals>.wrapperN)r1   r   objr   wraps)r   r   r!   )r   r   r"   r   6  s    
r   c             C   s   | j drt|  dS )zaWrap the given pytestfunct item for tracing support if --trace was given in
    the command line.r-   N)r0   r9   r   )r   r!   r!   r"   &maybe_wrap_pytest_function_for_tracingI  s    r   )r   r   repr   c             C   s   | j jdj}|  | j jj}xnd|jfd|jfd|j	ffD ]N\}}||dfkr@|r@|
dd|  |dd  d	kr|d d }|| q@W |
dd
 || |
dd t|}d|_t| |S )NZterminalreporterr   stderrlogallr\   z	captured 
	tracebackzentering PDBT)r0   r:   r   Z_twr`   optionshowcaptureZ	capstdoutZ	capstderrZcaplogrb   Z
toterminalr   Z	_pdbshownr   )r   r   r   rf   r   Zsectionnamecontentro   r!   r!   r"   r   P  s&    

r   )r   r   c             C   sX   ddl m} t| j|r$| jjd S t| jtr<| jjd S | jd k	sJt| jd S d S )Nr   )UnexpectedException   )	doctestr   
isinstancer   exc_infor   r   Z_excinfor^   )r   r   r!   r!   r"   r   o  s    r   )ru   r   c             C   s2   t d}|  |d |  |jr.td d S )Nr   zQuitting debugger)r1   r   resetZinteractionZquittingr   rj   )ru   pr!   r!   r"   r     s
    
r   )2r   r   r   rH   typestypingr   r   r   r   r   r   r   r	   r
   rh   r   Z_pytest._coder   r}   r   r   r   r   Z_pytest.config.argparsingr   Z_pytest.config.exceptionsr   Z_pytest.nodesr   Z_pytest.reportsr   Z_pytest.capturer   Z_pytest.runnerr   r   r#   r/   r@   r1   r=   r<   r   r   r   r   TracebackTyper   r   r!   r!   r!   r"   <module>   sN    @
