o
    Zh5                     @   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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rddl!m"Z" ddl#m$Z$ de%dee%e%f fddZ&deddfddZ'deddfd d!Z(G d"d# d#Z)G d$d% d%Z*G d&d' d'Z+d(d) Z,d*d+ Z-d,ed-ee. d.e de fd/d0Z/d-ee. dej0fd1d2Z1d3ej0ddfd4d5Z2dS )6z4Interactive 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   sD   z|  d\}}W ||fS  ty! } z	t| d|d}~ww )z#Validate syntax of --pdbcls option.:z) is not in the format 'modname:classname'N)split
ValueErrorargparseArgumentTypeError)r   modname	classnamee r!   H/var/www/html/lang_env/lib/python3.10/site-packages/_pytest/debugging.py_validate_usepdb_cls#   s   r#   parserc                 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/   configc                    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	 fdd}| | d S )
Nr   r-   Zpdbtracer%   Z	pdbinvoker   c                      s   t j \ _t _t _d S N)	pytestPDB_savedpop	set_trace_pluginmanager_configr!   pdbr!   r"   finW   s
   zpytest_configure.<locals>.finr   N)r9   getvaluepluginmanagerregisterPdbTrace	PdbInvoker2   r3   appendr5   r6   r7   Zadd_cleanup)r0   r:   r!   r8   r"   pytest_configureF   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d	ed
 deeef fddZed	ed
 fddZed	ed
 fddZedd ZedddZdS )r2   z'Pseudo PDB that defers to the real pdb.Nr6   r7   ).Nr3   r   _wrapped_pdb_clscapmanr   r   c                 C   s   |r|  S dS )NF)Zis_capturing)clsrD   r!   r!   r"   _is_capturingl   s   zpytestPDB._is_capturingc              
   C   s   | j s
dd l}|jS | j d}| jr| jd |kr| jd S |ri|\}}z%t| tj| }|d}t	||d }|dd  D ]}	t	||	}qAW n% t
yh }
 zd||f}td|d|
 |
d }
~
ww dd l}|j}| ||}||f| _|S )Nr   r*      .r   z--pdbcls: could not import z: )r7   r9   ZPdbr<   rC   
__import__sysmodulesr   getattr	Exceptionjoinr   _get_pdb_wrapper_class)rE   rD   r9   r*   r   r   modpartspdb_clspartexcr   Zwrapped_clsr!   r!   r"   _import_pdb_clsr   s<   



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rKjd usJ jj}|  | j}t	|}|rE|dkr2|
dd n|
dd|  |d us@J |  n|
dd jd usRJ 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   r9   T)rW   do_continuerV   r7   r0   create_terminal_writerline_pytest_capmanr2   rF   sepresumer6   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in_)rW   setupri   re   suspend_global_capture)rZ   ftbr\   r^   r!   r"   rr      s
   
z@pytestPDB._get_pdb_wrapper_class.<locals>.PytestPdbWrapper.setupc                    sp   t  ||\}}|d u r4tdt|d }|r4|| d jddr4|d8 }|r4|| d jdds#||fS )Nr   rG   Z__tracebackhide__F)rW   	get_stackmaxlenf_localsget)rZ   rt   tstackirv   r!   r"   rw      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_exitrr   rw   __classcell__r!   rl   rD   rE   rv   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}|r|jdd | jrk|j| j}|  | jdkrk|	dd}|dur>|
d| n-| |}|dkrR|
dd	| d
 n|rb|
dd	| d| d n	|
dd	|  | |d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 (IO-capturing turned off for )ra   r!   )r   r6   	getpluginZsuspendr7   r0   rc   rd   rV   r4   rf   rF   rU   rh   Zpytest_enter_pdb)
rE   methodargskwargsrl   rD   rj   r   rk   _pdbr!   r!   r"   	_init_pdb   s4   


zpytestPDB._init_pdbc                 O   s0   t  j}| jdg|R i |}|| dS )zBInvoke debugging via ``Pdb.set_trace``, dropping any IO capturing.r5   N)rJ   	_getframef_backr   r5   )rE   r   r   framer   r!   r!   r"   r5     s   
zpytestPDB.set_tracer;   )r   r   r   __doc__r6   r   r   __annotations__r7   r   r3   r   r   r   rV   rC   r   r   classmethodr
   strboolrF   rU   rO   r   r5   r!   r!   r!   r"   r2   a   s(   
   &V
&r2   c                   @   s<   e Zd ZdedddeddfddZd	ee ddfd
dZdS )r@   nodecallzCallInfo[Any]reportr   Nc                 C   sv   |j jd}|r!|jdd | \}}tj| tj| |jd us(J t	|jj
tjs9t||j| d S d S )Nr   Trp   )r0   r=   r   rs   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_interactr   c                 C   s   t |}t| d S r1   )_postmortem_tracebackpost_mortem)rZ   r   ru   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dedeef fddZdS )r?   T)wrapperr   Nc                 c   s    t | d V S r1   ) wrap_pytest_function_for_tracing)rZ   
pyfuncitemr!   r!   r"   pytest_pyfunc_call4  s   zPdbTrace.pytest_pyfunc_call)r   r   r   r   r   objectr   r!   r!   r!   r"   r?   3  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 r1   )	functoolspartialr   )r   r   funcr   Ztestfunctionr!   r"   r   E  s   z1wrap_pytest_function_for_tracing.<locals>.wrapperN)r2   r   objr   wraps)r   r   r!   r   r"   r   :  s
   

r   c                 C   s   | j drt|  dS 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_tracingM  s   r   r   r   repc                 C   s   | j jdj}|  | j jj}d|jfd|jfd|j	ffD ]'\}}||dfv rF|rF|
dd|  |dd  d	krA|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contentru   r!   r!   r"   r   T  s(   


r   c                 C   sd   ddl m} t| j|r| jjd S t| jtr&| jjjd us!J | jjjS | jd us-J | jd S )Nr   )UnexpectedException   )	doctestr   r   r   exc_infor   cause__traceback__Z_excinfo)r   r   r!   r!   r"   r   s  s   

r   r|   c                 C   s6   t d}|  |d |  |jrtd d S d S )Nr   rm   )r2   r   resetZinteractionZquittingr   ro   )r|   pr!   r!   r"   r     s   
r   )3r   r   r   rJ   typestypingr   r   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   r2   r@   r?   r   r   r   r   TracebackTyper   r   r!   r!   r!   r"   <module>   s`    ?
