U
    /e                     @   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                   @   s6   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 kst| | j |< td|| || _|| _|| _tddd| _	|d krZd | _
nt|j| _
|j}|d ksx|dkr|d}zt|| _W n2 tk
r   tjd||dd td| _Y nX 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)	instancesAssertionErrorr   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$   ;/tmp/pip-unpacked-wheel-dg3irmqq/debugpy/launcher/output.py__init__   s4    
zCaptureOutput.__init__c                 C   s6   | j }|d k	r2zt| W n tk
r0   Y nX d S )N)r   oscloser   )r!   r"   r$   r$   r%   __del__7   s    zCaptureOutput.__del__c                 C   s^   | j d k	rLzt| j d}W n tk
r4   Y qLY nX t|s@qL| | q | 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k
rV   Y nX | j	d krfd S zl| 
|d\}}t|}d}||k r| j	||d  }| j	  |dkrt| j d | _q||7 }qW n$ tk
r   td|| j Y nX d S )	Nr+   r   outputz

)r   r1   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!   r0   r,   _sizeiZwrittenr$   r$   r%   r/   L   s4     

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    rA   )
r   r'   r   Zdebugpyr   Zdebugpy.commonr   objectr   rA   r$   r$   r$   r%   <module>   s   `