o
    NEg2P                     @   s6  d dl Z d dlZd dlZd dlmZmZmZmZmZm	Z	m
Z
 ddlmZ ddlmZmZmZ ddlmZ zd dlmZ W n eyK   d dlmZ Y nw dZe
d	ZG d
d deZG dd dee ZG dd dZG dd dZG dd deZG dd deZe ZG dd dee Z G dd de e Z!dS )    N)	AwaitableCallableGenericListOptionalTupleTypeVar   )BaseProtocol)BaseTimerContextset_exception
set_result)internal_logger)Deque)EMPTY_PAYLOAD	EofStreamStreamReader	DataQueueFlowControlDataQueue_Tc                   @   s   e Zd ZdZdS )r   zeof stream indication.N)__name__
__module____qualname____doc__ r   r   i/var/www/eduai.edurigo.com/doc_train/edurigo_ai/Puru/venv/lib/python3.10/site-packages/aiohttp/streams.pyr      s    r   c                   @   sB   e Zd Zdeg ee f ddfddZdddZdefd	d
ZdS )AsyncStreamIterator	read_funcreturnNc                 C   
   || _ d S N)r   )selfr   r   r   r   __init__      
zAsyncStreamIterator.__init__AsyncStreamIterator[_T]c                 C      | S r    r   r!   r   r   r   	__aiter__"      zAsyncStreamIterator.__aiter__c                    s8   z	|   I d H }W n	 ty   tw |dkrt|S N    )r   r   StopAsyncIterationr!   rvr   r   r   	__anext__%   s   zAsyncStreamIterator.__anext__)r   r$   )	r   r   r   r   r   r   r"   r'   r.   r   r   r   r   r      s    
r   c                   @   s6   e Zd ZdddZdddZdeeef fd	d
ZdS )ChunkTupleAsyncStreamIteratorstreamr   r   Nc                 C   r   r    )_stream)r!   r0   r   r   r   r"   0   r#   z&ChunkTupleAsyncStreamIterator.__init__c                 C   r%   r    r   r&   r   r   r   r'   3   r(   z'ChunkTupleAsyncStreamIterator.__aiter__c                    s"   | j  I d H }|dkrt|S )Nr*   F)r1   	readchunkr+   r,   r   r   r   r.   6   s
   z'ChunkTupleAsyncStreamIterator.__anext__)r0   r   r   N)r   r/   )	r   r   r   r"   r'   r   bytesboolr.   r   r   r   r   r/   /   s    

r/   c                   @   sT   e Zd Zdee fddZdedee fddZdee fddZde	fd	d
Z
dS )AsyncStreamReaderMixinr   c                 C   
   t | jS r    )r   readliner&   r   r   r   r'   >   r#   z AsyncStreamReaderMixin.__aiter__nc                    s   t  fddS )zzReturns an asynchronous iterator that yields chunks of size n.

        Python-3.5 available for Python 3.5+ only
        c                      s
     S r    )readr   r9   r!   r   r   <lambda>F   s   
 z5AsyncStreamReaderMixin.iter_chunked.<locals>.<lambda>)r   r!   r9   r   r;   r   iter_chunkedA   s   z#AsyncStreamReaderMixin.iter_chunkedc                 C   r7   )zReturns an asynchronous iterator that yields all the available
        data as soon as it is received

        Python-3.5 available for Python 3.5+ only
        )r   readanyr&   r   r   r   iter_anyH   s   
zAsyncStreamReaderMixin.iter_anyc                 C   s   t | S )a  Returns an asynchronous iterator that yields chunks of data
        as they are received by the server. The yielded objects are tuples
        of (bytes, bool) as returned by the StreamReader.readchunk method.

        Python-3.5 available for Python 3.5+ only
        )r/   r&   r   r   r   iter_chunksP   s   z"AsyncStreamReaderMixin.iter_chunksN)r   r   r   r   r4   r'   intr>   r@   r/   rA   r   r   r   r   r6   =   s
    r6   c                   @   s  e Zd ZdZdZddddededee dee	j
 d	df
d
dZd	efddZd	eeef fddZd	ee fddZded	dfddZdeg df d	dfddZd?ddZd	efddZd	efddZd?ddZd ed	dfd!d"Zd@d ed#ed	dfd$d%Zd?d&d'Zd?d(d)Zd*ed	dfd+d,Zd	efd-d.Z dAd0ed	efd1d2Z!d	efd3d4Z"d	eeef fd5d6Z#d0ed	efd7d8Z$dAd0ed	efd9d:Z%d0ed	efd;d<Z&d0ed	efd=d>Z'dS )Br   a*  An enhancement of asyncio.StreamReader.

    Supports asynchronous iteration by line, chunk or as available::

        async for line in reader:
            ...
        async for chunk in reader.iter_chunked(1024):
            ...
        async for slice in reader.iter_any():
            ...

    r   N)timerloopprotocollimitrC   rD   r   c                C   sv   || _ || _|d | _|d u rt }|| _d| _d| _d | _t	
 | _d| _d| _d | _d | _d | _|| _g | _d S )N   r   F)	_protocol
_low_water_high_waterasyncioget_event_loop_loop_size_cursor_http_chunk_splitscollectionsdeque_buffer_buffer_offset_eof_waiter_eof_waiter
_exception_timer_eof_callbacks)r!   rE   rF   rC   rD   r   r   r   r"   j   s"   


zStreamReader.__init__c                 C   s   | j jg}| jr|d| j  | jr|d | jdkr(|d| j| jf  | jr3|d| j  | jr>|d| j  dd	| S )	Nz%d byteseofi   zlow=%d high=%dzw=%rze=%rz<%s> )
	__class__r   rN   appendrU   rI   rJ   rV   rX   join)r!   infor   r   r   __repr__   s   


zStreamReader.__repr__c                 C   s   | j | jfS r    )rI   rJ   r&   r   r   r   get_read_buffer_limits   s   z#StreamReader.get_read_buffer_limitsc                 C      | j S r    rX   r&   r   r   r   	exception      zStreamReader.exceptionexcc                 C   sT   || _ | j  | j}|d urd | _t|| | j}|d ur(d | _t|| d S d S r    )rX   rZ   clearrV   r   rW   r!   rg   waiterr   r   r   r      s   

zStreamReader.set_exceptioncallbackc                 C   sB   | j rz|  W d S  ty   td Y d S w | j| d S NException in eof callback)rU   	Exceptionr   re   rZ   r^   r!   rk   r   r   r   on_eof   s   zStreamReader.on_eofc              	   C   s   d| _ | j}|d urd | _t|d  | j}|d ur!d | _t|d  | jD ]}z|  W q$ ty9   td Y q$w | j  d S )NTrm   )	rU   rV   r   rW   rZ   rn   r   re   rh   )r!   rj   cbr   r   r   feed_eof   s    



zStreamReader.feed_eofc                 C   rc   )z&Return True if  'feed_eof' was called.rU   r&   r   r   r   is_eof   s   zStreamReader.is_eofc                 C      | j o| j S )z=Return True if the buffer is empty and 'feed_eof' was called.rU   rS   r&   r   r   r   at_eof   s   zStreamReader.at_eofc                    sH   | j rd S | jd u sJ | j | _z| jI d H  W d | _d S d | _w r    )rU   rW   rM   create_futurer&   r   r   r   wait_eof   s   zStreamReader.wait_eofdatac                 C   sx   t jdtdd |sdS | jr| jd | jd | jd< d| _|  jt|7  _|  jt|8  _| j| d| _	dS )zDrollback reading some data from stream, inserting it to buffer head.zJunread_data() is deprecated and will be removed in future releases (#3260)rG   )
stacklevelNr   )
warningswarnDeprecationWarningrT   rS   rN   lenrO   
appendleft_eof_counter)r!   rz   r   r   r   unread_data   s   
zStreamReader.unread_datasizec                 C   s   | j rJ d|sd S |  jt|7  _| j| |  jt|7  _| j}|d ur2d | _t|d  | j| jkrC| j	j
sE| j	  d S d S d S )Nzfeed_data after feed_eof)rU   rN   r   rS   r^   total_bytesrV   r   rJ   rH   _reading_pausedpause_readingr!   rz   r   rj   r   r   r   	feed_data   s   
zStreamReader.feed_datac                 C   s&   | j d u r| jrtdg | _ d S d S )Nz?Called begin_http_chunk_receiving whensome data was already fed)rP   r   RuntimeErrorr&   r   r   r   begin_http_chunk_receiving   s   

z'StreamReader.begin_http_chunk_receivingc                 C   sh   | j d u r	td| j r| j d nd}| j|krd S | j | j | j}|d ur2d | _t|d  d S d S )NzFCalled end_chunk_receiving without calling begin_chunk_receiving firstr   )rP   r   r   r^   rV   r   )r!   posrj   r   r   r   end_http_chunk_receiving  s   


z%StreamReader.end_http_chunk_receiving	func_namec                    s   | j d urtd| | j  }| _ z-| jr1| j |I d H  W d    n1 s+w   Y  n|I d H  W d | _ d S W d | _ d S d | _ w )NzH%s() called while another coroutine is already waiting for incoming data)rV   r   rM   rx   rY   )r!   r   rj   r   r   r   _wait#  s$   

 zStreamReader._waitc                    s   | j d ur	| j g }d}d}|r\| jrL|rL| j}| jd d|d }| |r,|| nd}|| |t|7 }|r>d}|| jkrGtd| jrL|s| j	rPn|rZ| 
dI d H  |sd	|S )
Nr   T   
r	   r   FzLine is too longr8   r*   )rX   rS   rT   find_read_nowait_chunkr^   r   rJ   
ValueErrorrU   r   r_   )r!   line	line_size
not_enoughoffseticharrz   r   r   r   r8   8  s0   





zStreamReader.readliner   r9   c                    s   | j d ur	| j 	 | jr%| js%t| ddd | _| jdkr%tjddd |s)dS |dk rEg }	 |  I d H }|s:n|| q0d	|S | jsY| jsY| 
d	I d H  | jsY| jrK| |S )
NTr   r   r	      zEMultiple access to StreamReader in eof state, might be infinite loop.)
stack_infor*   r:   )rX   rU   rS   getattrr   r   warningr?   r^   r_   r   _read_nowait)r!   r9   blocksblockr   r   r   r:   V  s4   




zStreamReader.readc                    sD   | j d ur	| j | js| js| dI d H  | js| jr| dS )Nr?   r   )rX   rS   rU   r   r   r&   r   r   r   r?     s   

zStreamReader.readanyc                    s   	 | j dur
| j | jr1| jd}|| jkrdS || jkr)| || j dfS td | js| jr;| ddfS | j	r@dS | 
d	I dH  q)
zReturns a tuple of (data, end_of_http_chunk). When chunked transfer
        encoding is used, end_of_http_chunk is a boolean indicating if the end
        of the data corresponds to the end of a HTTP chunk , otherwise it is
        always False.
        TNr   r*   TzESkipping HTTP chunk end due to data consumption beyond chunk boundaryr   Fr2   r3   )rX   rP   poprO   r   r   r   rS   r   rU   r   )r!   r   r   r   r   r3     s(   


zStreamReader.readchunkc                    sx   | j d ur	| j g }|dkr7| |I d H }|s(d|}t|t|| || |t|8 }|dksd|S )Nr   r*   )rX   r:   r_   rK   IncompleteReadErrorr   r^   )r!   r9   r   r   partialr   r   r   readexactly  s   



zStreamReader.readexactlyc                 C   s2   | j d ur| j | jr| j std| |S )Nz9Called while some coroutine is waiting for incoming data.)rX   rV   doner   r   r=   r   r   r   read_nowait  s   

zStreamReader.read_nowaitc                 C   s   | j d }| j}|dkr$t|| |kr$||||  }|  j|7  _n|r5| j   ||d  }d| _n| j  }|  jt|8  _|  jt|7  _| j}|rf|d | jk rf|d |rf|d | jk sX| j| jk ru| j	j
ru| j	  |S )Nr   r   )rS   rT   r   popleftrN   rO   rP   r   rI   rH   r   resume_reading)r!   r9   first_bufferr   rz   chunk_splitsr   r   r   r     s&   




zStreamReader._read_nowait_chunkc                 C   sT   g }| j r!| |}|| |dkr|t|8 }|dkrn| j s|r(d|S dS )z8 Read not more than n bytes, or whole buffer if n == -1 r   r   r*   )rS   r   r^   r   r_   )r!   r9   chunkschunkr   r   r   r     s   

zStreamReader._read_nowaitr   Nr   r   )(r   r   r   r   r   r
   rB   r   r   rK   AbstractEventLoopr"   strra   r   rb   BaseExceptionre   r   r   rp   rr   r5   rt   rw   ry   r4   r   r   r   r   r   r8   r:   r?   r3   r   r   r   r   r   r   r   r   r   Z   sL    

	


* r   c                   @   s   e Zd Zdee fddZdeddfddZdeg df ddfd	d
Zd%ddZ	de
fddZde
fddZd%ddZd&dededdfddZdefddZd'dedefddZdefddZdeee
f fdd Zdedefd!d"Zdefd#d$ZdS )(EmptyStreamReaderr   c                 C      d S r    r   r&   r   r   r   re     r(   zEmptyStreamReader.exceptionrg   Nc                 C   r   r    r   )r!   rg   r   r   r   r     r(   zEmptyStreamReader.set_exceptionrk   c                 C   s,   z|  W d S  t y   td Y d S w rl   )rn   r   re   ro   r   r   r   rp     s
   zEmptyStreamReader.on_eofc                 C   r   r    r   r&   r   r   r   rr     r(   zEmptyStreamReader.feed_eofc                 C      dS NTr   r&   r   r   r   rt     r(   zEmptyStreamReader.is_eofc                 C   r   r   r   r&   r   r   r   rw     r(   zEmptyStreamReader.at_eofc                    s   d S r    r   r&   r   r   r   ry   
     zEmptyStreamReader.wait_eofr   rz   r9   c                 C   r   r    r   )r!   rz   r9   r   r   r   r     r(   zEmptyStreamReader.feed_datac                       dS r)   r   r&   r   r   r   r8     r   zEmptyStreamReader.readliner   c                    r   r)   r   r=   r   r   r   r:     r   zEmptyStreamReader.readc                    r   r)   r   r&   r   r   r   r?     r   zEmptyStreamReader.readanyc                    r   )Nr   r   r&   r   r   r   r3     r   zEmptyStreamReader.readchunkc                    s   t d|r)   )rK   r   r=   r   r   r   r     s   zEmptyStreamReader.readexactlyc                 C   r   r)   r   r&   r   r   r   r     r(   zEmptyStreamReader.read_nowaitr   r   r   )r   r   r   r   r   re   r   r   rp   rr   r5   rt   rw   ry   r4   rB   r   r8   r:   r?   r   r3   r   r   r   r   r   r   r     s    

r   c                   @   s   e Zd ZdZdejddfddZdefddZde	fd	d
Z
de	fddZdee fddZdeddfddZddededdfddZdddZdefddZdee fddZdS )r   z>DataQueue is a general-purpose blocking queue with one reader.rD   r   Nc                 C   s,   || _ d| _d | _d | _d| _t | _d S )NFr   )rM   rU   rV   rX   rN   rQ   rR   rS   )r!   rD   r   r   r   r"   )  s   zDataQueue.__init__c                 C   r7   r    )r   rS   r&   r   r   r   __len__1  r#   zDataQueue.__len__c                 C   rc   r    rs   r&   r   r   r   rt   4  rf   zDataQueue.is_eofc                 C   ru   r    rv   r&   r   r   r   rw   7  s   zDataQueue.at_eofc                 C   rc   r    rd   r&   r   r   r   re   :  rf   zDataQueue.exceptionrg   c                 C   s2   d| _ || _| j}|d urd | _t|| d S d S r   )rU   rX   rV   r   ri   r   r   r   r   =  s   zDataQueue.set_exceptionr   rz   r   c                 C   sD   |  j |7  _ | j||f | j}|d ur d | _t|d  d S d S r    )rN   rS   r^   rV   r   r   r   r   r   r   F  s   zDataQueue.feed_datac                 C   s,   d| _ | j}|d urd | _t|d  d S d S r   )rU   rV   r   )r!   rj   r   r   r   rr   O  s   zDataQueue.feed_eofc              	      s   | j s*| js*| jrJ | j | _z| jI d H  W n tjtjfy)   d | _ w | j r=| j  \}}|  j	|8  _	|S | j
d urE| j
tr    )rS   rU   rV   rM   rx   rK   CancelledErrorTimeoutErrorr   rN   rX   r   r!   rz   r   r   r   r   r:   W  s"   

zDataQueue.readc                 C   r7   r    )r   r:   r&   r   r   r   r'   k  r#   zDataQueue.__aiter__r   r   )r   r   r   r   rK   r   r"   rB   r   r5   rt   rw   r   r   re   r   r   r   rr   r:   r   r'   r   r   r   r   r   &  s    	
	r   c                       sb   e Zd ZdZdededejddf fddZdd
e	deddf fddZ
de	f fddZ  ZS )r   zgFlowControlDataQueue resumes and pauses an underlying stream.

    It is a destination for parsed data.rE   rF   rD   r   Nc                   s"   t  j|d || _|d | _d S )N)rD   rG   )superr"   rH   _limit)r!   rE   rF   rD   r]   r   r   r"   t  s   zFlowControlDataQueue.__init__r   rz   r   c                    s8   t  || | j| jkr| jjs| j  d S d S d S r    )r   r   rN   r   rH   r   r   r   r   r   r   r   |  s   zFlowControlDataQueue.feed_datac                    s\   zt   I d H W | j| jk r| jjr| j  S S S | j| jk r,| jjr-| j  w w w r    )r   r:   rN   r   rH   r   r   r&   r   r   r   r:     s   zFlowControlDataQueue.readr   )r   r   r   r   r
   rB   rK   r   r"   r   r   r:   __classcell__r   r   r   r   r   o  s    r   )"rK   rQ   r|   typingr   r   r   r   r   r   r   base_protocolr
   helpersr   r   r   logr   r   ImportErrortyping_extensions__all__r   rn   r   r   r/   r6   r   r   r   r   r   r   r   r   r   <module>   s4    $   /I