o
    [Zh                     @   sL   d dl Z d dlZd dlZd dlmZ d dlmZ G dd deZdd Z	dS )    N)launcher)logc                   @   s8   e Zd ZdZi Z	 dd Zdd Zdd Zdd	d
ZdS )CaptureOutputzCaptures output from the specified file descriptor, and tees it into another
    file descriptor while generating DAP "output" events for it.
    c              	   C   s   || j vsJ | | j |< td|| || _|| _|| _tddd| _|d u r-d | _	n9|j
| _	|j}|d u s<|dkr>d}zt|| _W n ty^   tjd||dd td| _Y nw td	|| tj| j|d
| _| j  d S )NzCapturing {0} of {1}.zutf-8surrogateescape)errorscp65001z6Unsupported {0} encoding {1!r}; falling back to UTF-8.warning)levelzUsing encoding {0!r} for {1})targetname)	instancesr   infocategory_whose_fdcodecsgetincrementaldecoder_decoder_streambufferencoding
getencoder_encode	Exceptionswallow_exception	threadingThread_worker_worker_threadstart)selfZwhoser   fdstreamr    r#   N/var/www/html/lang_env/lib/python3.10/site-packages/debugpy/launcher/output.py__init__   s6   
	zCaptureOutput.__init__c                 C   s8   | j }|d urzt| W d S  ty   Y d S w d S )N)r   oscloser   )r    r!   r#   r#   r$   __del__7   s   zCaptureOutput.__del__c                 C   s`   | j d ur'z	t| j d}W n	 ty   Y nw t|sn
| | | j d us| jddd d S )Ni       Tfinal)r   r&   readr   len_process_chunk)r    sr#   r#   r$   r   ?   s   



zCaptureOutput._workerFc                 C   s  | j j||d}t|dkrd S ztjd| j|ddd W n	 ty*   Y nw | j	d u r2d S z?| 
|d\}}t|}d}||k ro| j	||d  }| j	  |dkrdt| j d | _W d S ||7 }||k sEW d S W d S  ty   td|| j Y d S w )	Nr*   r   outputz

)r   r0   r   zError printing {0!r} to {1})r   decoder-   r   ZchannelZ
send_eventr   replacer   r   r   writeflushr&   r'   r   r   r   )r    r/   r+   _sizeiZwrittenr#   r#   r$   r.   L   s8   


zCaptureOutput._process_chunkN)F)	__name__
__module____qualname____doc__r   r%   r(   r   r.   r#   r#   r#   r$   r      s    "r   c                  C   s2   t j D ]\} }td| |j |j  qdS )z=Waits for all remaining output to be captured and propagated.z!Waiting for remaining {0} of {1}.N)r   r   itemsr   r   r   r   join)r   instancer#   r#   r$   wait_for_remaining_outputm   s   r@   )
r   r&   r   Zdebugpyr   Zdebugpy.commonr   objectr   r@   r#   r#   r#   r$   <module>   s   `