a
    ,]?                     @   s   d Z ddlmZ ddlZddlZddlmZmZmZm	Z	m
Z
 ddlmZmZ ddlmZ erjddlmZ nddlmZ eZe	reZG dd	 d	eZd
d ZG dd de
eeZdgZdS )z
Pure-Python implementation of a Python 3-like bytes object for Python 2.

Why do this? Without it, the Python 2 bytes object is a very, very
different beast to the Python 3 bytes object.
    )IntegralN)istextisbytesPY2PY3with_metaclass)noissubset)	newobject)Iterablec                   @   s   e Zd Zdd ZdS )BaseNewBytesc                 C   s"   | t krt|tS t|j| S d S N)newbytes
isinstance_builtin_bytes
issubclass	__class__)clsinstance r   f/home/tom/ab/renpy-build/tmp/install.linux-x86_64/lib/python3.9/site-packages/future/types/newbytes.py__instancecheck__   s    
zBaseNewBytes.__instancecheck__N)__name__
__module____qualname__r   r   r   r   r   r      s   r   c                 C   s    t | tr| dS t| S d S )Nascii)r   strencodechr)xr   r   r   _newchr%   s    

r    c                       s  e Zd ZdZ fddZ fddZ fddZ fdd	Zd
d Zdd Z	e
e fddZe
edd Ze
e fddZe
e fddZdd Zdd Z fddZedd Ze
e fddZe
e fd d!Ze
ed" fd#d$Zd%d& Zd\ fd)d*	Ze
e fd+d,Ze
e fd-d.Ze
ed] fd1d2	Zd^ fd4d5	Ze
ed_ fd6d7	Ze
e fd8d9Ze
e fd:d;Z e
ed<d=d> Z!e
e fd?d@Z" fdAdBZ# fdCdDZ$dEZ% fdFdGZ& fdHdIZ' fdJdKZ( fdLdMZ) fdNdOZ* fdPdQZ+e
ed` fdRdS	Z,e
eda fdTdU	Z- fdVdWZ.e
e fdXdYZ/ee
edZd[ Z0  Z1S )br   z8
    A backport of the Python 3 bytes object to Py2
    c                    s  d}d}t |dkr$tt| | S t |dkrTt|}t |dkrL| }| }t|d tkrl|d S t|d tr|d }nXt|d t	r*z<d|v r|du sJ |d }d|v r|du sJ |d }W n t
y   tdY n0 |du r td|g}|dur|| |d j| }nt|d d	rH|d  }nt|d trt |d dkrpd
}n0ztdd |d D }W n   tdY n0 n<t|d tr|d dk rtdd|d  }n|d }t|tkrt|S tt| | |S dS )aK  
        From the Py3 bytes docstring:

        bytes(iterable_of_ints) -> bytes
        bytes(string, encoding[, errors]) -> bytes
        bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
        bytes(int) -> bytes object of size given by the parameter initialized with null bytes
        bytes() -> empty bytes object

        Construct an immutable array of bytes from:
          - an iterable yielding integers in range(256)
          - a text string encoded using the specified encoding
          - any object implementing the buffer API.
          - an integer
        Nr         encodingerrorsz#Argument given by name and positionz+unicode string argument without an encoding	__bytes__    c                 S   s   g | ]}t |qS r   )r    ).0r   r   r   r   
<listcomp>w   r&   z$newbytes.__new__.<locals>.<listcomp>zbytes must be in range(0, 256)znegative count    )lensuperr   __new__listpoptyper   r   unicodeAssertionError	TypeErrorappendr   hasattrr%   r   	bytearray
ValueErrorr   copy)r   argskwargsr#   r$   valueZnewargsr   r   r   r,   0   s\    



znewbytes.__new__c                    s   dt t|   S )Nb)r+   r   __repr__selfr;   r   r   r=      s    znewbytes.__repr__c                    s   dd tt|   S )Nr<   z'{0}')formatr+   r   __str__r>   r;   r   r   rA      s    znewbytes.__str__c                    s.   t t| |}t|tr"t|S t|S d S r   )r+   r   __getitem__r   r   ord)r?   yr:   r;   r   r   rB      s    
znewbytes.__getitem__c                 G   s   |  t| S r   )rB   slicer?   r8   r   r   r   __getslice__   s    znewbytes.__getslice__c                 C   sB   t |trt|g}nt|tkr(|}nt|}tt|t| S r   )r   intr   r/   r	   r-   )r?   keyZnewbyteskeyr   r   r   __contains__   s    
znewbytes.__contains__c                    s   t tt | |S r   )r   r+   __add__r?   otherr;   r   r   rK      s    znewbytes.__add__c                 C   s   t ||  S r   r   )r?   leftr   r   r   __radd__   s    znewbytes.__radd__c                    s   t tt | |S r   )r   r+   __mul__rL   r;   r   r   rQ      s    znewbytes.__mul__c                    s   t tt | |S r   )r   r+   __rmul__rL   r;   r   r   rR      s    znewbytes.__rmul__c                 C   s   t |trt|}nt |trVg }|D ]"}t |tr@t|}|| q(t|}nBt|jdrt|jdr| D ] \}}t |trvt|||< qvt	| |S )NrB   	iteritems)
r   r   r   rA   tupler3   r4   r   rS   __mod__)r?   valsZnewvalsvkr   r   r   rU      s     






znewbytes.__mod__c                 C   s
   |  |S r   )rU   rL   r   r   r   __imod__   s    znewbytes.__imod__c                    sj   d}t |st|r(t|dt|t|D ]$\}}t|r0t||t|q0ttt| |S )Nz,sequence item {0}: expected bytes, {1} foundr   )	r   r   r2   r@   r/   	enumerater   r+   join)r?   Ziterable_of_byteserrmsgiitemr;   r   r   r[      s    znewbytes.joinc                 C   s   | | dddS )N  hex)replacedecode)r   stringr   r   r   fromhex   s    znewbytes.fromhexc                    s   t t| j|g|R  S r   )r+   r   findr?   subr8   r;   r   r   rf      s    znewbytes.findc                    s   t t| j|g|R  S r   )r+   r   rfindrg   r;   r   r   ri      s    znewbytes.rfind)   r!   c                    s   t tt | j||g|R  S r   )r   r+   rb   )r?   oldnewr8   r;   r   r   rb      s    znewbytes.replacec                 G   s   t dd S )N+encode method has been disabled in newbytes)AttributeErrorrF   r   r   r   r      s    znewbytes.encodeutf-8strictc                    s<   ddl m} |dkr&ddlm} |  |tt| ||S )a  
        Returns a newstr (i.e. unicode subclass)

        Decode B using the codec registered for encoding. Default encoding
        is 'utf-8'. errors may be given to set a different error
        handling scheme.  Default is 'strict' meaning that encoding errors raise
        a UnicodeDecodeError.  Other possible values are 'ignore' and 'replace'
        as well as any other name registered with codecs.register_error that is
        able to handle UnicodeDecodeErrors.
        r   )newstrsurrogateescape)register_surrogateescape)Zfuture.types.newstrrq   Zfuture.utils.surrogateescapers   r+   r   rc   )r?   r#   r$   rq   rs   r;   r   r   rc      s
    znewbytes.decodec                    s   t t| j|g|R  S r   )r+   r   
startswithr?   prefixr8   r;   r   r   rt     s    znewbytes.startswithc                    s   t t| j|g|R  S r   )r+   r   endswithru   r;   r   r   rw     s    znewbytes.endswithNc                    s    t t| ||}dd |D S )Nc                 S   s   g | ]}t |qS r   rN   r'   partr   r   r   r(   '  r&   z"newbytes.split.<locals>.<listcomp>)r+   r   splitr?   sepmaxsplitpartsr;   r   r   r{   "  s    znewbytes.splitFc                    s   t t| |}dd |D S )z
        B.splitlines([keepends]) -> list of lines

        Return a list of the lines in B, breaking at line boundaries.
        Line breaks are not included in the resulting list unless keepends
        is given and true.
        c                 S   s   g | ]}t |qS r   rN   ry   r   r   r   r(   4  r&   z'newbytes.splitlines.<locals>.<listcomp>)r+   r   
splitlines)r?   keependsr   r;   r   r   r   )  s    
znewbytes.splitlinesc                    s    t t| ||}dd |D S )Nc                 S   s   g | ]}t |qS r   rN   ry   r   r   r   r(   ;  r&   z#newbytes.rsplit.<locals>.<listcomp>)r+   r   rsplitr|   r;   r   r   r   6  s    znewbytes.rsplitc                    s"   t t| |}tdd |D S )Nc                 s   s   | ]}t |V  qd S r   rN   ry   r   r   r   	<genexpr>@  r&   z%newbytes.partition.<locals>.<genexpr>)r+   r   	partitionrT   r?   r}   r   r;   r   r   r   =  s    znewbytes.partitionc                    s"   t t| |}tdd |D S )Nc                 s   s   | ]}t |V  qd S r   rN   ry   r   r   r   r   E  r&   z&newbytes.rpartition.<locals>.<genexpr>)r+   r   
rpartitionrT   r   r;   r   r   r   B  s    znewbytes.rpartition)rj   c                 G   s&   | j |g|R  }|dkr"tddS )z
        S.rindex(sub [,start [,end]]) -> int

        Like S.rfind() but raise ValueError when the substring is not found.
        rx   substring not foundN)ri   r6   )r?   rh   r8   posr   r   r   rindexG  s    znewbytes.rindexc              	      s   t |trpt|dkr&dt|  }}n4t|dkr<|d }nt|dkrR|\}}ntdt| || |S t |tsz| |}W n ttfy   tdY n0 zt	t
| j|g|R  W S  ty   tdY n0 dS )z
        Returns index of sub in bytes.
        Raises ValueError if byte is not in bytes and TypeError if can't
        be converted bytes or its length is not 1.
        r   rj   r!   ztakes at most 3 argumentszcan't convert sub to bytesr   N)r   rH   r*   r2   r-   indexbytesr   r6   r+   r   )r?   rh   r8   startendr;   r   r   r   R  s$    



znewbytes.indexc                    s&   t |ttfrtt| |S dS d S )NF)r   r   r5   r+   r   __eq__rL   r;   r   r   r   m  s    znewbytes.__eq__c                    s"   t |trtt| |S dS d S )NT)r   r   r+   r   __ne__rL   r;   r   r   r   s  s    
znewbytes.__ne__z"unorderable types: bytes() and {0}c                    s2   t |trtt| |S t| jt|d S r   )	r   r   r+   r   __lt__r2   unorderable_errr@   r/   rL   r;   r   r   r   {  s    
znewbytes.__lt__c                    s2   t |trtt| |S t| jt|d S r   )	r   r   r+   r   __le__r2   r   r@   r/   rL   r;   r   r   r     s    
znewbytes.__le__c                    s2   t |trtt| |S t| jt|d S r   )	r   r   r+   r   __gt__r2   r   r@   r/   rL   r;   r   r   r     s    
znewbytes.__gt__c                    s2   t |trtt| |S t| jt|d S r   )	r   r   r+   r   __ge__r2   r   r@   r/   rL   r;   r   r   r     s    
znewbytes.__ge__c                    s   t t|  S r   )r+   r   rA   r>   r;   r   r   
__native__  s    znewbytes.__native__c                    s    |dv rt dtt| |S )zu
        A trick to cause the ``hasattr`` builtin-fn to return False for
        the 'encode' method on Py2.
        )r   r   rm   )rn   r+   r   __getattribute__)r?   namer;   r   r   r     s    znewbytes.__getattribute__c                    s   t tt | |S )z
        Strip trailing bytes contained in the argument.
        If the argument is omitted, strip trailing ASCII whitespace.
        )r   r+   rstripr?   Zbytes_to_stripr;   r   r   r     s    znewbytes.rstripc                    s   t tt | |S )z
        Strip leading and trailing bytes contained in the argument.
        If the argument is omitted, strip trailing ASCII whitespace.
        )r   r+   stripr   r;   r   r   r     s    znewbytes.stripc                    s   t tt |  S )zv
        b.lower() -> copy of b

        Return a copy of b with all ASCII characters converted to lowercase.
        )r   r+   lowerr>   r;   r   r   r     s    znewbytes.lowerc                    s   t tt |  S )zv
        b.upper() -> copy of b

        Return a copy of b with all ASCII characters converted to uppercase.
        )r   r+   upperr>   r;   r   r   r     s    znewbytes.upperc                 C   s   t t||S )aT  
        B.maketrans(frm, to) -> translation table

        Return a translation table (a bytes object of length 256) suitable
        for use in the bytes or bytearray translate method where each byte
        in frm is mapped to the byte at the same position in to.
        The bytes objects frm and to must be of the same length.
        )r   rd   	maketrans)r   Zfrmtor   r   r   r     s    znewbytes.maketrans)ro   rp   )Nrx   )F)Nrx   )N)N)2r   r   r   __doc__r,   r=   rA   rB   rG   rJ   r   r0   rK   rP   rQ   rR   rU   rY   r[   classmethodre   rf   ri   rb   r   rc   rt   rw   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   r   r;   r   r   ,   s   Z
	
1

	r   )r   numbersr   rd   r7   Zfuture.utilsr   r   r   r   r   Zfuture.typesr   r	   Zfuture.types.newobjectr
   collectionsr   collections.abcr   r   r   r0   r/   r   r    r   __all__r   r   r   r   <module>   s&      #