a
    *ñ»j{6  ã                   @  sš  d Z ddlmZ ddlZddlmZ ddlmZ ddlZddlZddl	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)ÚCallable)Ú	Generator)ÚAny)Úoutcomes)ÚExceptionInfo)ÚCaptureManager)ÚConfig)ÚConftestImportFailure)Úhookimpl)ÚPytestPluginManager)ÚParser)Ú
UsageError)ÚNode)Ú
BaseReport)ÚCallInfoÚstrztuple[str, str])Ú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ÚargparseZArgumentTypeError)r   ÚmodnameÚ	classnameÚe© r   ú7E:\soccer\ml_env\Lib\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)r6   ÚgetvalueÚpluginmanagerÚregisterÚPdbTraceÚ	PdbInvoker/   r0   Úappendr2   r3   r4   Zadd_cleanup)r,   r7   r   r5   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 )r/   z'Pseudo PDB that defers to the real pdb.NzPytestPluginManager | Noner3   zConfig | Noner4   zKlist[tuple[Callable[..., None], PytestPluginManager | None, Config | None]]r0   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)Úclsr@   r   r   r   Ú_is_capturingg   s    zpytestPDB._is_capturing)r@   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: )r4   r6   ZPdbr8   r?   Ú
__import__ÚsysÚmodulesr   ÚgetattrÚ	ExceptionÚjoinr   Ú_get_pdb_wrapper_class)rA   r@   r6   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                      s¨   e Zd Z”ZdZ‡ ‡fdd„Ze”dƒr2”jje_‡ ‡‡fdd„Ze”dƒrV”jje_e Z	Z
‡ ‡fdd	„Ze”d
ƒr€”jje_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 )NrC   )Ú_recursive_debugÚsuperÚdo_debug©ÚselfÚargÚret©Ú	__class__rA   r   r   rT   œ   s    zCpytestPDB._get_pdb_wrapper_class.<locals>.PytestPdbWrapper.do_debugrT   c                   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�¡ |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 ú)zPDB continue©r,   r6   T)rS   Údo_continuerR   r4   r,   Úcreate_terminal_writerÚlineÚ_pytest_capmanr/   rB   ÚsepÚresumer3   ÚhookZpytest_leave_pdbÚ
_continued)rV   rW   rX   Útwr@   Ú	capturing)rZ   Ú_pytestrA   r   r   r_   ¥   s*    


þ
zFpytestPDB._get_pdb_wrapper_class.<locals>.PytestPdbWrapper.do_continuer_   c                   s$   t ƒ  |¡}ˆjdkr t d¡ |S )Nr   úQuitting debugger)rS   Údo_quitrR   r   ÚexitrU   rY   r   r   rk   Ä   s    

zBpytestPDB._get_pdb_wrapper_class.<locals>.PytestPdbWrapper.do_quitrk   c                   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_)rS   Úsetuprf   rb   Úsuspend_global_capture)rV   ÚfÚtbrX   ©rZ   r   r   rn   ×   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   rC   Z__tracebackhide__F)rS   Ú	get_stackÚmaxÚlenÚf_localsÚget)rV   rp   ÚtÚstackÚirr   r   r   rs   å   s    
zDpytestPDB._get_pdb_wrapper_class.<locals>.PytestPdbWrapper.get_stack)Ú__name__Ú
__module__Ú__qualname__rb   rf   rT   ÚhasattrÚ__doc__r_   Zdo_cZdo_contrk   Zdo_qZdo_exitrn   rs   Ú__classcell__r   ©ri   r@   rA   rN   rr   r   ÚPytestPdbWrapper˜   s    





r‚   )Ú_pytest.config)rA   rN   r@   r‚   r   r�   r   rK   ”   s    Vz 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|¡ nZ|  |¡}|dkr¤| 
dd	|› d
�¡ n2|rÄ| 
dd	|› d|› d�¡ n| 
dd	|› �¡ |  |¡f i |¤Ž}	| j�r| jjj| j|	d� |	S )z4Initialize PDB debugging, dropping any IO capturing.r   NÚcapturemanagerTrm   Úheaderr\   r[   zPDB z (IO-capturing turned off)z (IO-capturing turned off for r]   r^   )rƒ   r3   Ú	getpluginZsuspendr4   r,   r`   ra   rR   r1   rc   rB   rQ   re   Zpytest_enter_pdb)
rA   ÚmethodÚargsÚkwargsri   r@   rg   r…   rh   Ú_pdbr   r   r   Ú	_init_pdbð   s4    


þzpytestPDB._init_pdbr   r-   c                 O  s0   t  ¡ j}| jdg|¢R i |¤Ž}| |¡ dS )zBInvoke debugging via ``Pdb.set_trace``, dropping any IO capturing.r2   N)rF   Ú	_getframeÚf_backr‹   r2   )rA   rˆ   r‰   ÚframerŠ   r   r   r   r2     s    
zpytestPDB.set_trace)r{   r|   r}   r   r3   Ú__annotations__r4   r0   rR   r?   ÚclassmethodrB   rQ   rK   r‹   r2   r   r   r   r   r/   \   s"   
þ
&[
&r/   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 usPJ ‚t	|jj
tjƒsnt||j|ƒ d S )Nr„   Trm   )r,   r9   r†   ro   Zread_global_capturerF   ÚstdoutÚwriteÚexcinfoÚ
isinstancer   ÚunittestZSkipTestÚ
_enter_pdb)rV   r‘   r’   r“   r@   Ú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_exc_or_tbÚpost_mortem)rV   r–   Z	exc_or_tbr   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)rV   Ú
pyfuncitemr   r   r   Úpytest_pyfunc_call4  s    zPdbTrace.pytest_pyfunc_callN)r{   r|   r}   r   r¥   r   r   r   r   r;   3  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ˆg| ¢R i |¤Ž}ˆ  |¡ d S r.   )Ú	functoolsÚpartialr¦   )rˆ   r‰   Úfunc©rŠ   Ztestfunctionr   r   r¢   E  s    z1wrap_pytest_function_for_tracing.<locals>.wrapperN)r/   r‹   Úobjr§   Úwraps)r¤   r¢   r   rª   r   r£   :  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,   r8   r£   )r¤   r   r   r   Ú&maybe_wrap_pytest_function_for_tracingM  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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)r,   r9   r†   Z_twra   ÚoptionÚshowcaptureZ	capstdoutZ	capstderrZcaplogrc   Z
toterminalrŸ   Z	_pdbshownr    )r‘   r–   r®   rg   r¶   ZsectionnameÚcontentÚ	tb_or_excr   r   r   r™   T  s&    
ý
r™   z#types.TracebackType | BaseExceptionrž   c                 C  sš   ddl m} tjdk}t| j|ƒr@| j}|r6|jd S |jd S t| jtƒrp| jj}|r\|S |j	d usjJ ‚|j	S | j
d us~J ‚|rŒ| j
d S | j
d S d S )Nr   )ÚUnexpectedException)é   é   rC   é   )Údoctestr¹   rF   Úversion_infor—   r   Úexc_infor
   ÚcauseÚ__traceback__Z_excinfo)r–   r¹   Zget_excZunderlying_excrÀ   r   r   r   rŸ   s  s"    



rŸ   )r¸   r   c                 C  s2   t  d¡}| ¡  | d | ¡ |jr.t d¡ d S )Nr    rj   )r/   r‹   ÚresetZinteractionZquittingr   rl   )r¸   Úpr   r   r   r    ’  s
    
r    )-r   Ú
__future__r   r   Úcollections.abcr   r   r§   rF   ÚtypesÚtypingr   r˜   ri   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>   r/   r<   r;   r£   r­   r™   rŸ   r    r   r   r   r   Ú<module>   sB    D