a
    /§»a_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   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!   úK/home/yuhw/wc/pochoir/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_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)r:   ÚgetvalueÚpluginmanagerÚregisterÚPdbTraceÚ	PdbInvoker3   r4   Úappendr6   r7   r8   Ú_cleanup)r0   r;   r!   r9   r"   Úpytest_configureC   s    

ÿrC   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)ÚclsrE   r!   r!   r"   Ú_is_capturingi   s    zpytestPDB._is_capturing)rE   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	||	ƒ}q‚W qî t
yÜ }
 z0d ||f¡}td|›d|
› �ƒ|
‚W Y d }
~
qîd }
~
0 0 ndd l}|j}|  ||¡}||f| _|S )Nr   r*   é   Ú.r   z--pdbcls: could not import z: )r8   r:   ZPdbr<   rD   Ú
__import__ÚsysÚmodulesr   ÚgetattrÚ	ExceptionÚjoinr   Ú_get_pdb_wrapper_class)rF   rE   r:   r*   r   r   ÚmodÚpartsÚpdb_clsÚpartÚexcr   Zwrapped_clsr!   r!   r"   Ú_import_pdb_clso   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 )NrH   )Ú_recursive_debugÚsuperÚdo_debug©ÚselfÚargÚret©Ú	__class__rF   r!   r"   rY       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s€J ‚| ¡  n| 
dd¡ ˆjd us¤J ‚ˆ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)rX   Údo_continuerW   r8   r0   Úcreate_terminal_writerÚlineÚ_pytest_capmanr3   rG   ÚsepÚresumer7   ÚhookZpytest_leave_pdbÚ
_continued)r[   r\   r]   ÚtwrE   Ú	capturing)r_   Ú_pytestrF   r!   r"   rc   ¦   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)rX   Údo_quitrW   r   ÚexitrZ   r^   r!   r"   ro   Ã   s    

zBpytestPDB._get_pdb_wrapper_class.<locals>.PytestPdbWrapper.do_quitc                    s0   t ƒ  ||¡}|s,| jr,| jr,| jjdd� |S )zŽSuspend on setup().

                Needed after do_continue resumed, and entering another
                breakpoint again.
                T©Zin_)rX   Úsetuprj   rf   Úsuspend_global_capture)r[   ÚfÚtbr]   ©r_   r!   r"   rr   Ô   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   rH   Z__tracebackhide__F)rX   Ú	get_stackÚmaxÚlenÚf_localsÚget)r[   rt   ÚtÚstackÚirv   r!   r"   rw   â   s    
zDpytestPDB._get_pdb_wrapper_class.<locals>.PytestPdbWrapper.get_stack)Ú__name__Ú
__module__Ú__qualname__rf   rj   rY   rc   Zdo_cZdo_contro   Zdo_qZdo_exitrr   rw   Ú__classcell__r!   ©rm   rE   rF   rv   r"   ÚPytestPdbWrapperœ   s   r„   )Ú_pytest.config)rF   rS   rE   r„   r!   rƒ   r"   rP   –   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ÚcapturemanagerTrq   Úheaderra   r`   zPDB z (IO-capturing turned off)z'PDB %s (IO-capturing turned off for %s)rb   )r…   r7   Ú	getpluginZsuspendr8   r0   rd   re   rW   r5   rg   rG   rV   ri   Zpytest_enter_pdb)
rF   ÚmethodÚargsÚkwargsrm   rE   rk   r‡   rl   Ú_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)rK   Ú	_getframeÚf_backr�   r6   )rF   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   rW   rD   r   r   Úclassmethodr
   ÚstrÚboolrG   rV   rP   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   sb   |j j d¡}|rB|jdd� | ¡ \}}tj |¡ tj |¡ |jd usPJ ‚t	||j|ƒ d S )Nr†   Trq   )
r0   r=   rˆ   rs   Zread_global_capturerK   ÚstdoutÚwriteÚexcinfoÚ
_enter_pdb)r[   r–   r—   r˜   rE   Ú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)r[   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�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)r[   Ú
pyfuncitemr!   r!   r"   Úpytest_pyfunc_call0  s    zPdbTrace.pytest_pyfunc_callN)r   r€   r�   r   r   r§   r!   r!   r!   r"   r?   /  s   r?   c                    s2   t  d¡‰ | j‰t ˆ¡‡ ‡fdd„ƒ}|| _dS )zñChange 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"   ÚwrapperA  s    z1wrap_pytest_function_for_tracing.<locals>.wrapperN)r3   r�   Úobjr©   Úwraps)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   r<   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}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Úallra   z	captured éÿÿÿÿÚ
Ú	tracebackzentering PDBT)r0   r=   rˆ   Z_twre   ÚoptionÚshowcaptureZ	capstdoutZ	capstderrZcaplogrg   Z
toterminalr¡   Z	_pdbshownr¢   )r–   r›   r±   rk   r¹   ZsectionnameÚcontentru   r!   r!   r"   rœ   P  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»   Ú
isinstancer   Úexc_infor   r›   Z_excinfo)r›   r»   r!   r!   r"   r¡   o  s    r¡   )r|   r   c                 C   s2   t  d¡}| ¡  | d | ¡ |jr.t d¡ d S )Nr¢   rn   )r3   r�   ÚresetZinteractionZquittingr   rp   )r|   Úpr!   r!   r"   r¢     s
    
r¢   )2r‘   r   r©   rK   ÚtypesÚtypingr   r   r   r   r   r   r   r	   r
   rm   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/   rC   r3   r@   r?   r¥   r°   r¤   rœ   ÚTracebackTyper¡   r¢   r!   r!   r!   r"   Ú<module>   sP    @
þ