a
    ˆÏ?eº4  ã                   @   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!   úT/home/yuhw/wc/wire-cell-python/wcpy/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	||	ƒ}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<   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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)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 )zŽSuspend 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 )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"   Ú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    @
þ