o
    h{6                     @  sj  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Z"d;ddZ#d<d d!Z$G d"d# d#Z%G d$d% d%Z&G d&d' d'Z'd=d(d)Z(d=d*d+Z)d>d2d3Z*d?d5d6Z+d@d8d9Z,dS )Az4Interactive debugging with PDB, the Python Debugger.    )annotationsN)Callable)	Generator)Any)outcomes)ExceptionInfo)CaptureManager)Config)ConftestImportFailure)hookimpl)PytestPluginManager)Parser)
UsageError)Node)
BaseReport)CallInfovaluestrreturntuple[str, str]c              
   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   R/var/www/html/openai_agents/venv/lib/python3.10/site-packages/_pytest/debugging.py_validate_usepdb_cls   s   r    parserr   Nonec                 C  sF   |  d}|jddddd |jddd	td
d |jddddd d S )N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)getgroup	addoptionr    )r!   groupr   r   r   pytest_addoption)   s(   

r0   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
 fdd	}| | d S )Nr   r,   pdbtracer$   	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r   r"   )r<   getvaluepluginmanagerregisterPdbTrace	PdbInvoker5   r6   appendr8   r9   r:   add_cleanup)r1   r=   r   r;   r   pytest_configureA   s   

rF   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Ze
dddZe
dddZe
dd Ze
dddZdS )r5   z'Pseudo PDB that defers to the real pdb.NzPytestPluginManager | Noner9   zConfig | Noner:   zKlist[tuple[Callable[..., None], PytestPluginManager | None, Config | None]]r6   r   z"tuple[type[Any], type[Any]] | None_wrapped_pdb_clscapmanCaptureManager | Noner   
str | boolc                 C  s   |r|  S dS )NF)is_capturing)clsrH   r   r   r   _is_capturingg   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: )r:   r<   Pdbr?   rG   
__import__sysmodulesr   getattr	Exceptionjoinr   _get_pdb_wrapper_class)rL   rH   r<   r)   r   r   modpartspdb_clspartexcr   wrapped_clsr   r   r   _import_pdb_clsm   s<   



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rjje_ fddZedr+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 )NrN   )_recursive_debugsuperdo_debugselfargret	__class__rL   r   r   ra      s   zCpytestPDB._get_pdb_wrapper_class.<locals>.PytestPdbWrapper.do_debugra   c                   s   t  |}jdkrMjd usJ jj}|  | j}t	|}|rG|dkr2|
dd n
|
dd| d |d usBJ |  n|
dd jd usTJ 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r1   r<   T)r`   do_continuer_   r:   r1   create_terminal_writerline_pytest_capmanr5   rM   sepresumer9   hookpytest_leave_pdb
_continued)rc   rd   re   twrH   	capturing)rg   _pytestrL   r   r   rl      s*   



zFpytestPDB._get_pdb_wrapper_class.<locals>.PytestPdbWrapper.do_continuerl   c                   s$   t  |}jdkrtd |S )Nr   Quitting debugger)r`   do_quitr_   r   exitrb   rf   r   r   ry      s   

zBpytestPDB._get_pdb_wrapper_class.<locals>.PytestPdbWrapper.do_quitry   c                   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_)r`   setuprt   ro   suspend_global_capture)rc   ftbre   rg   r   r   r}      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   rN   __tracebackhide__F)r`   	get_stackmaxlenf_localsget)rc   r   tstackir   r   r   r      s   zDpytestPDB._get_pdb_wrapper_class.<locals>.PytestPdbWrapper.get_stack)__name__
__module____qualname__ro   rt   ra   hasattr__doc__rl   do_cdo_contry   do_qdo_exitr}   r   __classcell__r   rw   rH   rL   rZ   r   r   PytestPdbWrapper   s"    





r   )_pytest.config)rL   rZ   rH   r   r   r   r   rW      s   Vz 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capturemanagerTr{   headerri   rh   zPDB z (IO-capturing turned off)z (IO-capturing turned off for rj   rk   r   )r   r9   	getpluginsuspendr:   r1   rm   rn   r_   r7   rp   rM   r^   rr   pytest_enter_pdb)
rL   methodargskwargsrw   rH   ru   r   rv   _pdbr   r   r   	_init_pdb   s4   


zpytestPDB._init_pdbr"   c                 O  s0   t  j}| jdg|R i |}|| dS )zBInvoke debugging via ``Pdb.set_trace``, dropping any IO capturing.r8   N)rR   	_getframef_backr   r8   )rL   r   r   framer   r   r   r   r8     s   
zpytestPDB.set_trace)rH   rI   r   rJ   )rH   rI   r>   )r   r   r   r   r9   __annotations__r:   r6   r_   rG   classmethodrM   r^   rW   r   r8   r   r   r   r   r5   \   s$   
 
&[
&r5   c                   @  s    e Zd Zdd	d
ZdddZdS )rC   noder   callCallInfo[Any]reportr   r   r"   c                 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   Tr{   )r1   r@   r   r~   read_global_capturerR   stdoutwriteexcinfo
isinstancer   unittestSkipTest
_enter_pdb)rc   r   r   r   rH   outerrr   r   r   pytest_exception_interact   s   z#PdbInvoke.pytest_exception_interactr   ExceptionInfo[BaseException]c                 C  s   t |}t| d S r4   )_postmortem_exc_or_tbpost_mortem)rc   r   	exc_or_tbr   r   r   pytest_internalerror.  s   zPdbInvoke.pytest_internalerrorN)r   r   r   r   r   r   r   r"   )r   r   r   r"   )r   r   r   r   r   r   r   r   r   rC     s    
rC   c                   @  s    e Zd ZedddddZdS )	rB   T)wrapperr   Generator[None, object, object]c                 c  s    t | d V S r4   ) wrap_pytest_function_for_tracing)rc   
pyfuncitemr   r   r   pytest_pyfunc_call4  s   zPdbTrace.pytest_pyfunc_callN)r   r   )r   r   r   r   r   r   r   r   r   rB   3  s    rB   c                   s4   t d | jtd 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.runcallr   r"   c                    s&   t jg| R i |} | d S r4   )	functoolspartialr   )r   r   funcr   testfunctionr   r   r   E  s   z1wrap_pytest_function_for_tracing.<locals>.wrapperNr>   )r5   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)r1   r?   r   )r   r   r   r   &maybe_wrap_pytest_function_for_tracingM  s   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 ]'\}}||dfv rF|rF|
dd|  |dd  d	krA|d d }|| q|
dd
 || |
dd t|}d|_t| |S )Nterminalreporterr   stderrlogallri   z	captured 
	tracebackzentering PDBT)r1   r@   r   _twrn   optionshowcapture	capstdout	capstderrcaplogrp   
toterminalr   	_pdbshownr   )r   r   r   ru   r   sectionnamecontent	tb_or_excr   r   r   r   T  s(   


r   #types.TracebackType | BaseExceptionc                 C  s   ddl m} tjdk}t| j|r | j}|r|jd S |jd S t| jtr8| jj}|r.|S |j	d us5J |j	S | j
d us?J |rF| j
d S | j
d S )Nr   )UnexpectedException)      rN      )doctestr   rR   version_infor   r   exc_infor
   cause__traceback___excinfo)r   r   get_excunderlying_excr   r   r   r   r   s  s"   




r   r   c                 C  s6   t d}|  |d |  |jrtd d S d S )Nr   rx   )r5   r   resetinteractionquittingr   rz   )r   pr   r   r   r     s   
r   )r   r   r   r   )r!   r   r   r"   )r1   r	   r   r"   r>   )r   r   r   r   r   r   r   r   )r   r   r   r   )r   r   r   r"   )-r   
__future__r   r   collections.abcr   r   r   rR   typestypingr   r   rw   r   _pytest._coder   _pytest.capturer   r   r	   r
   r   r   _pytest.config.argparsingr   _pytest.config.exceptionsr   _pytest.nodesr   _pytest.reportsr   _pytest.runnerr   r    r0   rF   r5   rC   rB   r   r   r   r   r   r   r   r   r   <module>   sD   


 D



