a
    !f4                     @   s  d 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 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j0d.d/d0Z1ej0dd1d2d3Z2d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   sR   z|  d\}}W n6 tyH } zt| d|W Y d}~n
d}~0 0 ||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!   R/var/www/html/python-backend/venv/lib/python3.9/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_truezDStart the interactive Python debugger on errors or KeyboardInterrupt)destactionhelpz--pdbcls
usepdb_clszmodulename:classnamez{Specify a custom interactive Python debugger for use with --pdb.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}| | 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"   finU   s    zpytest_configure.<locals>.fin)r:   getvaluepluginmanagerregisterPdbTrace	PdbInvoker3   r4   appendr6   r7   r8   Zadd_cleanup)r0   r;   r!   r9   r"   pytest_configureD   s    

rB   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 )r3   z'Pseudo PDB that defers to the real pdb.Nr7   r8   ).Nr4   r   _wrapped_pdb_clsr   )capmanr   c                 C   s   |r|  S dS )NF)Zis_capturing)clsrD   r!   r!   r"   _is_capturingj   s    zpytestPDB._is_capturing)rD   c              
   C   s  | j sdd l}|jS | j d}| jr>| jd |kr>| jd S |r|\}}zJt| tj| }|d}t	||d }|dd  D ]}	t	||	}qW q t
y }
 z0d||f}td|d|
 |
W Y d }
~
qd }
~
0 0 ndd l}|j}| ||}||f| _|S )Nr   r*      .r   z--pdbcls: could not import z: )r8   r:   ZPdbr<   rC   
__import__sysmodulesr   getattr	Exceptionjoinr   _get_pdb_wrapper_class)rE   rD   r:   r*   r   r   modpartspdb_clspartexcr   Zwrapped_clsr!   r!   r"   _import_pdb_clsp   s6    



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 )NrG   )_recursive_debugsuperdo_debugselfargret	__class__rE   r!   r"   rX      s    zCpytestPDB._get_pdb_wrapper_class.<locals>.PytestPdbWrapper.do_debugc                    s   t  |}jdkrjd us$J jj}|  | j}t	|}|r|dkrd|
dd n|
dd|  |d usJ |  n|
dd jd usJ 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   r:   T)rW   do_continuerV   r8   r0   create_terminal_writerline_pytest_capmanr3   rF   sepresumer7   hookZpytest_leave_pdb
_continued)rZ   r[   r\   twrD   	capturing)r^   _pytestrE   r!   r"   rb      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   Quitting debugger)rW   do_quitrV   r   exitrY   r]   r!   r"   rn      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Zin_)rW   setupri   re   suspend_global_capture)rZ   ftbr\   r^   r!   r"   rq      s
    
z@pytestPDB._get_pdb_wrapper_class.<locals>.PytestPdbWrapper.setupc                    sX   t  ||\}}|d u rPtdt|d }|rP|| d jddrP|d8 }q,||fS )Nr   rG   Z__tracebackhide__F)rW   	get_stackmaxlenf_localsget)rZ   rs   tstackiru   r!   r"   rv      s    
zDpytestPDB._get_pdb_wrapper_class.<locals>.PytestPdbWrapper.get_stack)__name__
__module____qualname__re   ri   rX   rb   Zdo_cZdo_contrn   Zdo_qZdo_exitrq   rv   __classcell__r!   rl   rD   rE   ru   r"   PytestPdbWrapper   s   r   )_pytest.config)rE   rR   rD   r   r!   r   r"   rO      s    Oz pytestPDB._get_pdb_wrapper_classc           
      O   s  ddl }| jdu rd}n| jd}|r4|jdd | jr|j| j}|  | jdkr|	dd}|dur||
d| nT| |}|dkr|
dd	| d
 n,|r|
dd||f  n|
dd	|  | |f i |}	| jr| jjj| j|	d |	S )z4Initialize PDB debugging, dropping any IO capturing.r   NcapturemanagerTrp   headerr`   r_   zPDB z (IO-capturing turned off)z'PDB %s (IO-capturing turned off for %s)ra   )r   r7   	getpluginZsuspendr8   r0   rc   rd   rV   r5   rf   rF   rU   rh   Zpytest_enter_pdb)
rE   methodargskwargsrl   rD   rj   r   rk   _pdbr!   r!   r"   	_init_pdb   s8    


zpytestPDB._init_pdbr1   c                 O   s0   t  j}| jdg|R i |}|| dS )zBInvoke debugging via ``Pdb.set_trace``, dropping any IO capturing.r6   N)rJ   	_getframef_backr   r6   )rE   r   r   framer   r!   r!   r"   r6     s    
zpytestPDB.set_trace)r~   r   r   __doc__r7   r   r   __annotations__r8   r   r4   r   r   r   rV   rC   r   r   classmethodr
   strboolrF   rU   rO   r   r6   r!   r!   r!   r"   r3   _   s&   
 &V
'r3   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   sr   |j jd}|rB|jdd | \}}tj| tj| |jd usPJ t	|jj
tjsnt||j| d S )Nr   Trp   )r0   r=   r   rr   Zread_global_capturerJ   stdoutwriteexcinfo
isinstancer   unittestZSkipTest
_enter_pdb)rZ   r   r   r   rD   outerrr!   r!   r"   pytest_exception_interact  s    z#PdbInvoke.pytest_exception_interactr   r   c                 C   s   t |}t| d S r2   )_postmortem_tracebackpost_mortem)rZ   r   rt   r!   r!   r"   pytest_internalerror-  s    zPdbInvoke.pytest_internalerror)	r~   r   r   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)NNNr1   c                 c   s   t | d V  d S r2   ) wrap_pytest_function_for_tracing)rZ   
pyfuncitemr!   r!   r"   pytest_pyfunc_call3  s    zPdbTrace.pytest_pyfunc_callN)r~   r   r   r   r   r   r!   r!   r!   r"   r?   2  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g| R i |} | d S r2   )	functoolspartialr   )r   r   funcr   Ztestfunctionr!   r"   wrapperD  s    z1wrap_pytest_function_for_tracing.<locals>.wrapperN)r3   r   objr   wraps)r   r   r!   r   r"   r   9  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   r<   r   )r   r!   r!   r"   &maybe_wrap_pytest_function_for_tracingL  s    r   )r   r   repr   c                 C   s   | j jdj}|  | j jj}d|jfd|jfd|j	ffD ]N\}}||dfv r>|r>|
dd|  |dd  d	kr|d d }|| q>|
dd
 || |
dd t|}d|_t| |S )NZterminalreporterr   stderrlogallr`   z	captured 
	tracebackzentering PDBT)r0   r=   r   Z_twrd   optionshowcaptureZ	capstdoutZ	capstderrZcaplogrf   Z
toterminalr   Z	_pdbshownr   )r   r   r   rj   r   Zsectionnamecontentrt   r!   r!   r"   r   S  s&    

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 usJJ | jd S d S )Nr   )UnexpectedException   )doctestr   r   r   exc_infor   r   Z_excinfo)r   r   r!   r!   r"   r   r  s    r   )r{   r   c                 C   s2   t d}|  |d |  |jr.td d S )Nr   rm   )r3   r   resetZinteractionZquittingr   ro   )r{   pr!   r!   r"   r     s
    
r   )3r   r   r   rJ   typesr   typingr   r   r   r   r   r   r   r	   r
   rl   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/   rB   r3   r@   r?   r   r   r   r   TracebackTyper   r   r!   r!   r!   r"   <module>   sR    @
