B
    \ c                 @   s:   d Z ddlZejZG dd dejZG dd dejZdS )z7Internal classes used by the gzip, lzma and bz2 modules    Nc               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )
BaseStreamzMode-checking helper functions.c             C   s   | j rtdd S )NzI/O operation on closed file)closed
ValueError)self r   lib/python3.7/_compression.py_check_not_closed   s    zBaseStream._check_not_closedc             C   s   |   stdd S )NzFile not open for reading)readableioUnsupportedOperation)r   r   r   r   _check_can_read   s    zBaseStream._check_can_readc             C   s   |   stdd S )NzFile not open for writing)writabler
   r   )r   r   r   r   _check_can_write   s    zBaseStream._check_can_writec             C   s(   |   std|  s$tdd S )Nz3Seeking is only supported on files open for readingz3The underlying file object does not support seeking)r	   r
   r   seekable)r   r   r   r   _check_can_seek   s    
zBaseStream._check_can_seekN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   	   s
   r   c                   sj   e Zd ZdZdd ZdddZ fddZd	d
 Zdd ZdddZ	dd Z
ejfddZdd Z  ZS )DecompressReaderz5Adapts the decompressor API to a RawIOBase reader APIc             C   s   dS )NTr   )r   r   r   r   r	   $   s    zDecompressReader.readabler   c             K   s>   || _ d| _d| _d| _|| _|| _| jf | j| _|| _d S )NFr   )_fp_eof_pos_size_decomp_factory_decomp_args_decompressor_trailing_error)r   fpZdecomp_factoryZtrailing_errorZdecomp_argsr   r   r   __init__'   s    zDecompressReader.__init__c                s   d | _ t  S )N)r   superclose)r   )	__class__r   r   r"   ;   s    zDecompressReader.closec             C   s
   | j  S )N)r   r   )r   r   r   r   r   ?   s    zDecompressReader.seekablec          
   C   sP   t |:}|d$}| t|}||d t|< W d Q R X W d Q R X t|S )NB)
memoryviewcastreadlen)r   bZviewZ	byte_viewdatar   r   r   readintoB   s    $zDecompressReader.readintor   c             C   s   |dk r|   S |r| jrdS d }x| jjr| jjp>| jt}|sFP | jf | j	| _y| j
||}W q | jk
r   P Y qX n4| jjr| jt}|stdnd}| j
||}|r$P q$W |sd| _| j| _dS |  jt|7  _|S )Nr       zACompressed file ended before the end-of-stream marker was reachedT)readallr   r   eofZunused_datar   r'   BUFFER_SIZEr   r   Z
decompressr   Zneeds_inputEOFErrorr   r   r(   )r   sizer*   Zrawblockr   r   r   r'   H   s>    



zDecompressReader.readc             C   s,   | j d d| _d| _| jf | j| _d S )Nr   F)r   seekr   r   r   r   r   )r   r   r   r   _rewindr   s    zDecompressReader._rewindc             C   s   |t jkrnV|t jkr"| j| }n@|t jkrT| jdk rHx| t jrFq8W | j| }ntd	||| jk rv| 
  n
|| j8 }x0|dkr| tt j|}|sP |t|8 }qW | jS )Nr   zInvalid value for whence: {})r
   SEEK_SETSEEK_CURr   SEEK_ENDr   r'   DEFAULT_BUFFER_SIZEr   formatr3   minr(   )r   offsetwhencer*   r   r   r   r2   x   s&    







zDecompressReader.seekc             C   s   | j S )z!Return the current file position.)r   )r   r   r   r   tell   s    zDecompressReader.tell)r   )r   )r   r   r   r   r	   r    r"   r   r+   r'   r3   r
   r4   r2   r<   __classcell__r   r   )r#   r   r   !   s   

*r   )r   r
   r7   r/   BufferedIOBaser   	RawIOBaser   r   r   r   r   <module>   s   