U
    `»{jÌ3  ã                   @  sš  d Z ddlm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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dœdd„Z!dddœdd„Z"dddœdd „Z#G d!d"„ d"ƒZ$G d#d$„ d$ƒZ%G d%d&„ d&ƒZ&dd'œd(d)„Z'dd'œd*d+„Z(d,d-d.d.d/œd0d1„Z)d-d2d3œd4d5„Z*d2dd6œd7d8„Z+dS )9z4Interactive debugging with PDB, the Python Debugger.é    )ÚannotationsN)ÚAny)ÚCallable)Ú	Generator)Úoutcomes)ÚExceptionInfo)ÚCaptureManager)ÚConfig)ÚConftestImportFailure)Úhookimpl)ÚPytestPluginManager)ÚParser)Ú
UsageError)ÚNode)Ú
BaseReport)ÚCallInfoÚstrztuple[str, str])ÚvalueÚreturnc              
   C  sP   z|   d¡\}}W n4 tk
rF } zt | ›d�¡|‚W 5 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   úP/var/www/html/bwave/python/venv/lib/python3.8/site-packages/_pytest/debugging.pyÚ_validate_usepdb_cls   s    ÿþr   r   ÚNone)Ú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,   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	pdbinvoker    ©r   c                     s   t j ¡ \ˆ _t _t _d S ©N)Ú	pytestPDBÚ_savedÚpopÚ	set_traceÚ_pluginmanagerÚ_configr   ©Úpdbr   r   ÚfinR   s    üýzpytest_configure.<locals>.fin)r7   ÚgetvalueÚpluginmanagerÚregisterÚPdbTraceÚ	PdbInvoker0   r1   Úappendr3   r4   r5   Zadd_cleanup)r-   r8   r   r6   r   Úpytest_configureA   s    

ÿr?   c                   @  sœ   e Zd ZU dZdZded< dZded< g Zded< d	ZdZ	d
ed< e
dddœdd„ƒZe
ddœdd„ƒZe
ddœdd„ƒZe
dd„ ƒZe
ddœdd„ƒZdS )r0   z'Pseudo PDB that defers to the real pdb.NzPytestPluginManager | Noner4   zConfig | Noner5   zRlist[tuple[Callable[(Ellipsis, None)], PytestPluginManager | None, Config | None]]r1   r   z"tuple[type[Any], type[Any]] | NoneÚ_wrapped_pdb_clszCaptureManager | Nonez
str | bool)Úcapmanr   c                 C  s   |r|  ¡ S dS )NF)Zis_capturing)ÚclsrA   r   r   r   Ú_is_capturingg   s    zpytestPDB._is_capturing)rA   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
k
rÚ }
 z(d ||f¡}td|›d|
› �ƒ|
‚W 5 d }
~
X Y qìX ndd l}|j}|  ||¡}||f| _|S )Nr   r'   é   Ú.r   z--pdbcls: could not import z: )r5   r7   ZPdbr9   r@   Ú
__import__ÚsysÚmodulesr   ÚgetattrÚ	ExceptionÚjoinr   Ú_get_pdb_wrapper_class)rB   rA   r7   r'   r   r   ÚmodÚpartsÚpdb_clsÚpartÚexcr   Zwrapped_clsr   r   r   Ú_import_pdb_clsm   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 )NrD   )Ú_recursive_debugÚsuperÚdo_debug©ÚselfÚargÚret©Ú	__class__rB   r   r   rU   œ   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�¡ |d k	s„t‚| ¡  n| dd¡ ˆjd k	s¨t‚ˆjjjˆj| d� d	| _|S )
Nr   Úglobalú>z#PDB continue (IO-capturing resumed)z'PDB continue (IO-capturing resumed for ú)zPDB continue©r-   r7   T)rT   Údo_continuerS   r5   ÚAssertionErrorr-   Úcreate_terminal_writerÚlineÚ_pytest_capmanr0   rC   ÚsepÚresumer4   ÚhookZpytest_leave_pdbÚ
_continued)rW   rX   rY   ÚtwrA   Ú	capturing)r[   Ú_pytestrB   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   úQuitting debugger)rT   Údo_quitrS   r   ÚexitrV   rZ   r   r   rm   ¾   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_)rT   Úsetuprh   rd   Úsuspend_global_capture)rW   ÚfÚtbrY   ©r[   r   r   rp   Ï   s
    
z@pytestPDB._get_pdb_wrapper_class.<locals>.PytestPdbWrapper.setupc                   sX   t ƒ  ||¡\}}|d krPtdt|ƒd ƒ}|rP|| d j dd¡rP|d8 }q,||fS )Nr   rD   Z__tracebackhide__F)rT   Ú	get_stackÚmaxÚlenÚf_localsÚget)rW   rr   ÚtÚstackÚirt   r   r   ru   Ý   s    
zDpytestPDB._get_pdb_wrapper_class.<locals>.PytestPdbWrapper.get_stack)Ú__name__Ú
__module__Ú__qualname__rd   rh   rU   r`   Zdo_cZdo_contrm   Zdo_qZdo_exitrp   ru   Ú__classcell__r   ©rk   rA   rB   rt   r   ÚPytestPdbWrapper˜   s   r‚   )Ú_pytest.config)rB   rO   rA   r‚   r   r�   r   rL   ”   s    Nz 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|¡ nZ|  |¡}|dkr¤| 
dd	|› d
�¡ n2|rÄ| 
dd	|› d|› d�¡ n| 
dd	|› �¡ |  |¡f |Ž}	| j�r| jjj| j|	d� |	S )z4Initialize PDB debugging, dropping any IO capturing.r   NÚcapturemanagerTro   Úheaderr]   r\   zPDB z (IO-capturing turned off)z (IO-capturing turned off for r^   r_   )rƒ   r4   Ú	getpluginZsuspendr5   r-   rb   rc   rS   r2   re   rC   rR   rg   Zpytest_enter_pdb)
rB   ÚmethodÚargsÚkwargsrk   rA   ri   r…   rj   Ú_pdbr   r   r   Ú	_init_pdbè   s4    


þzpytestPDB._init_pdbr    r.   c                 O  s(   t  ¡ j}| jd|ž|Ž}| |¡ dS )zBInvoke debugging via ``Pdb.set_trace``, dropping any IO capturing.r3   N)r3   )rG   Ú	_getframeÚf_backr‹   r3   )rB   rˆ   r‰   ÚframerŠ   r   r   r   r3     s    
zpytestPDB.set_trace)r}   r~   r   Ú__doc__r4   Ú__annotations__r5   r1   rS   r@   ÚclassmethodrC   rR   rL   r‹   r3   r   r   r   r   r0   \   s"   
þ
&S
&r0   c                   @  s0   e Zd Zdddddœdd„Zddd	œd
d„ZdS )r=   r   zCallInfo[Any]r   r    )ÚnodeÚcallÚreportr   c                 C  sr   |j j d¡}|rB|jdd� | ¡ \}}tj |¡ tj |¡ |jd k	sPt	‚t
|jjtjƒsnt||j|ƒ d S )Nr„   Tro   )r-   r:   r†   rq   Zread_global_capturerG   ÚstdoutÚwriteÚexcinfora   Ú
isinstancer   ÚunittestZSkipTestÚ
_enter_pdb)rW   r’   r“   r”   rA   ÚoutÚerrr   r   r   Úpytest_exception_interact  s    z#PdbInvoke.pytest_exception_interactúExceptionInfo[BaseException]©r—   r   c                 C  s   t |ƒ}t|ƒ d S r/   )Ú_postmortem_tracebackÚpost_mortem)rW   r—   rs   r   r   r   Úpytest_internalerror&  s    zPdbInvoke.pytest_internalerrorN)r}   r~   r   r�   r¢   r   r   r   r   r=     s   r=   c                   @  s$   e Zd Zedd�ddœdd„ƒZdS )r<   T)ÚwrapperzGenerator[None, object, object]r.   c                 c  s   t |ƒ d V S r/   )Ú wrap_pytest_function_for_tracing)rW   Ú
pyfuncitemr   r   r   Úpytest_pyfunc_call,  s    zPdbTrace.pytest_pyfunc_callN)r}   r~   r   r   r¦   r   r   r   r   r<   +  s   r<   r.   c                   s8   t  d¡‰ | j‰t ˆ¡ddœ‡ ‡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.Úruncallr    r.   c                    s    t jˆf| ž|Ž}ˆ  |¡ d S r/   )Ú	functoolsÚpartialr§   )rˆ   r‰   Úfunc©rŠ   Ztestfunctionr   r   r£   =  s    z1wrap_pytest_function_for_tracing.<locals>.wrapperN)r0   r‹   Úobjr¨   Úwraps)r¥   r£   r   r«   r   r¤   2  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)r-   r9   r¤   )r¥   r   r   r   Ú&maybe_wrap_pytest_function_for_tracingE  s    r®   r   rž   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k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)r-   r:   r†   Z_twrc   ÚoptionÚshowcaptureZ	capstdoutZ	capstderrZcaplogre   Z
toterminalr    Z	_pdbshownr¡   )r’   r—   r¯   ri   r·   ZsectionnameÚcontentrs   r   r   r   rš   L  s&    
ý
rš   ztypes.TracebackTyperŸ   c                 C  sh   ddl m} t| j|ƒr$| jjd S t| jtƒrL| jjjd k	sBt‚| jjjS | j	d k	sZt‚| j	d S d S )Nr   )ÚUnexpectedExceptioné   )
Údoctestr¹   r˜   r   Úexc_infor
   ÚcauseÚ__traceback__ra   Z_excinfo)r—   r¹   r   r   r   r    k  s    
r    )rz   r   c                 C  s2   t  d¡}| ¡  | d | ¡ |jr.t d¡ d S )Nr¡   rl   )r0   r‹   ÚresetZinteractionZquittingr   rn   )rz   Úpr   r   r   r¡   |  s
    
r¡   ),r�   Ú
__future__r   r   r¨   rG   ÚtypesÚtypingr   r   r   r™   rk   r   Z_pytest._coder   Z_pytest.capturer   rƒ   r	   r
   r   r   Z_pytest.config.argparsingr   Z_pytest.config.exceptionsr   Z_pytest.nodesr   Z_pytest.reportsr   Z_pytest.runnerr   r   r,   r?   r0   r=   r<   r¤   r®   rš   r    r¡   r   r   r   r   Ú<module>   sB    <