a
    ,]=                     @   s   d Z ddlmZ ddlmZmZmZmZ ddlm	Z	m
Z
 ddlmZ erZeZddlmZ nddlmZ G dd deZG d	d
 d
eeeZd
gZdS )a  
This module redefines ``str`` on Python 2.x to be a subclass of the Py2
``unicode`` type that behaves like the Python 3.x ``str``.

The main differences between ``newstr`` and Python 2.x's ``unicode`` type are
the stricter type-checking and absence of a `u''` prefix in the representation.

It is designed to be used together with the ``unicode_literals`` import
as follows:

    >>> from __future__ import unicode_literals
    >>> from builtins import str, isinstance

On Python 3.x and normally on Python 2.x, these expressions hold

    >>> str('blah') is 'blah'
    True
    >>> isinstance('blah', str)
    True

However, on Python 2.x, with this import:

    >>> from __future__ import unicode_literals

the same expressions are False:

    >>> str('blah') is 'blah'
    False
    >>> isinstance('blah', str)
    False

This module is designed to be imported together with ``unicode_literals`` on
Python 2 to bring the meaning of ``str`` back into alignment with unprefixed
string literals (i.e. ``unicode`` subclasses).

Note that ``str()`` (and ``print()``) would then normally call the
``__unicode__`` method on objects in Python 2. To define string
representations of your objects portably across Py3 and Py2, use the
:func:`python_2_unicode_compatible` decorator in  :mod:`future.utils`.

    )Number)PY3istextwith_metaclass
isnewbytes)noissubset)	newobject)Iterablec                   @   s   e Zd Zdd ZdS )
BaseNewStrc                 C   s"   | t krt|tS t|j| S d S N)newstr
isinstanceunicode
issubclass	__class__)clsinstance r   d/home/tom/ab/renpy-build/tmp/install.linux-x86_64/lib/python3.9/site-packages/future/types/newstr.py__instancecheck__;   s    
zBaseNewStr.__instancecheck__N)__name__
__module____qualname__r   r   r   r   r   r   :   s   r   c                       s"  e Zd ZdZdZ fddZ fddZ fddZd	d
 Ze	d fddZ
e	ddd Z fddZ fddZ fddZe	d fddZe	d fddZe	dd fddZdd ZdT fd!d"	Ze	dd# fd$d%Ze	dd# fd&d'Ze	dd#dU fd*d+	Ze	dd#dV fd,d-	Ze	dd# fd.d/Ze	dd# fd0d1Ze	dd#d2d3 ZdW fd5d6	Z fd7d8Z fd9d:Z fd;d<Zd=Z fd>d?Z  fd@dAZ! fdBdCZ" fdDdEZ# fdFdGZ$dHdI Z%e&dXdJdKZ'dLdM Z(dNdO Z)dPdQ Z*dRdS Z+  Z,S )Yr   z6
    A backport of the Python 3 str object to Py2
    z,Can't convert '{0}' object to str implicitlyc                    s   t |dkrtt| | S t|d tkr<| tkr<|d S t|d trT|d }nVt|d trd|v svt |dkr|d j|dd i |}q|d 	 }n|d }tt| | |S )a/  
        From the Py3 str docstring:

          str(object='') -> str
          str(bytes_or_buffer[, encoding[, errors]]) -> str

          Create a new string object from the given object. If encoding or
          errors is specified, then the object must expose a data buffer
          that will be decoded using the given encoding and error handler.
          Otherwise, returns the result of object.__str__() (if defined)
          or repr(object).
          encoding defaults to sys.getdefaultencoding().
          errors defaults to 'strict'.

        r   encoding   N)
lensuperr   __new__typer   r   bytesdecode__str__)r   argskwargsvaluer   r   r   r   H   s    
znewstr.__new__c                    s   t t|  }|dd S )z&
        Without the u prefix
        r   N)r   r   __repr__)selfr%   r&   r   r   r'   j   s    znewstr.__repr__c                    s   t tt | |S )z
        Warning: Python <= 2.7.6 has a bug that causes this method never to be called
        when y is a slice object. Therefore the type of newstr()[:2] is wrong
        (unicode instead of newstr).
        )r   r   __getitem__)r(   yr&   r   r   r)   s   s    znewstr.__getitem__c                 C   s`   d}t |tkr|}n8t|ts2t|tr<t|s<t|}nt|t |tt	|t	| S )Nz6'in <string>' requires string as left operand, not {0})
r   r   r   r   r    r   	TypeErrorformatr   list)r(   keyerrmsgZnewkeyr   r   r   __contains__{   s    
znewstr.__contains__newbytesc                    s   t tt | |S r   )r   r   __add__r(   otherr&   r   r   r2      s    znewstr.__add__c                 C   s$   zt ||  W S    t Y S 0 dS )z left + self N)r   NotImplemented)r(   leftr   r   r   __radd__   s    znewstr.__radd__c                    s   t tt | |S r   )r   r   __mul__r3   r&   r   r   r8      s    znewstr.__mul__c                    s   t tt | |S r   )r   r   __rmul__r3   r&   r   r   r9      s    znewstr.__rmul__c                    sh   d}t |D ]\}}t|rt||qt| tkrLttt| |S tttt| |S d S )Nz7sequence item {0}: expected unicode string, found bytes)	enumerater   r+   r,   r   r   r   join)r(   iterabler/   iitemr&   r   r   r;      s    znewstr.joinc                    s   t t| j|g|R  S r   )r   r   findr(   subr#   r&   r   r   r?      s    znewstr.findc                    s   t t| j|g|R  S r   )r   r   rfindr@   r&   r   r   rB      s    znewstr.rfind)r      c                    s   t tt | j||g|R  S r   )r   r   replace)r(   oldnewr#   r&   r   r   rD      s    znewstr.replacec                 G   s   t dd S )N)decode method has been disabled in newstr)AttributeError)r(   r#   r   r   r   r!      s    znewstr.decodeutf-8strictc                    s   ddl m} |dkr|dkr$tdg }| D ]L}t|}d|  krLdkrfn n|||d g q,||j|d	 q,|d
|S |tt| ||S )a  
        Returns bytes

        Encode S 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 UnicodeEncodeError. Other possible values are 'ignore', 'replace' and
        'xmlcharrefreplace' as well as any other name registered with
        codecs.register_error that can handle UnicodeEncodeErrors.
        r   )r1   surrogateescapezutf-16z?FIXME: surrogateescape handling is not yet implemented properlyi   i  i   )r       )	Zfuture.types.newbytesr1   NotImplementedErrorordappendencoder;   r   r   )r(   r   errorsr1   Zmybytesccoder&   r   r   rP      s    znewstr.encoder   c                    sH   t |tr0|D ] }t|rt| jt|qtt| j	|g|R  S r   )
r   r
   r   r+   no_convert_msgr,   r   r   r   
startswithr(   prefixr#   thingr&   r   r   rU      s
    
znewstr.startswithc                    sH   t |tr0|D ] }t|rt| jt|qtt| j	|g|R  S r   )
r   r
   r   r+   rT   r,   r   r   r   endswithrV   r&   r   r   rY      s
    
znewstr.endswithNc                    s    t t| ||}dd |D S )Nc                 S   s   g | ]}t |qS r   r   .0partr   r   r   
<listcomp>   rL   z newstr.split.<locals>.<listcomp>)r   r   splitr(   sepmaxsplitpartsr&   r   r   r`      s    znewstr.splitc                    s    t t| ||}dd |D S )Nc                 S   s   g | ]}t |qS r   r[   r\   r   r   r   r_      rL   z!newstr.rsplit.<locals>.<listcomp>)r   r   rsplitra   r&   r   r   re      s    znewstr.rsplitc                    s"   t t| |}tdd |D S )Nc                 s   s   | ]}t |V  qd S r   r[   r\   r   r   r   	<genexpr>  rL   z#newstr.partition.<locals>.<genexpr>)r   r   	partitiontupler(   rb   rd   r&   r   r   rg     s    znewstr.partitionc                    s"   t t| |}tdd |D S )Nc                 s   s   | ]}t |V  qd S r   r[   r\   r   r   r   rf   	  rL   z$newstr.rpartition.<locals>.<genexpr>)r   r   
rpartitionrh   ri   r&   r   r   rj     s    znewstr.rpartitionc                 G   s&   | j |g|R  }|dkr"td|S )zb
        Like newstr.find() but raise ValueError when the substring is not
        found.
        rZ   zsubstring not found)r?   
ValueError)r(   rA   r#   posr   r   r   index  s    znewstr.indexFc                    s   t t| |}dd |D S )z
        S.splitlines(keepends=False) -> list of strings

        Return a list of the lines in S, 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   r[   r\   r   r   r   r_   !  rL   z%newstr.splitlines.<locals>.<listcomp>)r   r   
splitlines)r(   keependsrd   r&   r   r   rn     s    
znewstr.splitlinesc                    s4   t |tst |tr,t|s,tt| |S tS d S r   )r   r   r    r   r   r   __eq__r5   r3   r&   r   r   rp   #  s    
znewstr.__eq__c                    s4   t | tst | tr*t| s*tt|  S t d S r   )r   r   r    r   r   r   __hash__rM   r(   r&   r   r   rq   *  s    
znewstr.__hash__c                    s4   t |tst |tr,t|s,tt| |S dS d S )NT)r   r   r    r   r   r   __ne__r3   r&   r   r   rs   1  s    
znewstr.__ne__z unorderable types: str() and {0}c                    sD   t |tst |tr,t|s,tt| |S t| j	t
|d S r   )r   r   r    r   r   r   __lt__r+   unorderable_errr,   r   r3   r&   r   r   rt   :  s    
znewstr.__lt__c                    sD   t |tst |tr,t|s,tt| |S t| j	t
|d S r   )r   r   r    r   r   r   __le__r+   ru   r,   r   r3   r&   r   r   rv   @  s    
znewstr.__le__c                    sD   t |tst |tr,t|s,tt| |S t| j	t
|d S r   )r   r   r    r   r   r   __gt__r+   ru   r,   r   r3   r&   r   r   rw   F  s    
znewstr.__gt__c                    sD   t |tst |tr,t|s,tt| |S t| j	t
|d S r   )r   r   r    r   r   r   __ge__r+   ru   r,   r   r3   r&   r   r   rx   L  s    
znewstr.__ge__c                    s    |dv rt dtt| |S )zu
        A trick to cause the ``hasattr`` builtin-fn to return False for
        the 'decode' method on Py2.
        )r!   r!   rG   )rH   r   r   __getattribute__)r(   namer&   r   r   ry   R  s    znewstr.__getattribute__c                 C   s   t | S )z@
        A hook for the future.utils.native() function.
        )r   rr   r   r   r   
__native__[  s    znewstr.__native__c           	      C   s   |du r^|du sJ t | ts&tdi }|  D ](\}}t|dkrNtd||t|< q2npt | tszt |trztdt| t|kstdi }t| |D ],\}}t|dkrtdt||t|< q|dur|D ]}d|t|< q|S )a_  
        Return a translation table usable for str.translate().

        If there is only one argument, it must be a dictionary mapping Unicode
        ordinals (integers) or characters to Unicode ordinals, strings or None.
        Character keys will be then converted to ordinals.
        If there are two arguments, they must be strings of equal length, and
        in the resulting dictionary, each character in x will be mapped to the
        character at the same position in y. If there is a third argument, it
        must be a string, whose characters will be mapped to None in the result.
        Nz<if you give only one argument to maketrans it must be a dictr   z3keys in translate table must be strings or integerszx and y must be unicode stringsz8the first two maketrans arguments must have equal length)	r   dictr+   itemsr   rk   rN   r   zip)	xr*   zresultr.   r%   Zxiyicharr   r   r   	maketransa  s,    
znewstr.maketransc                 C   sl   g }| D ]X}t ||v rV|t | }|du r0qq`t|trF|| q`|t| q|| qd|S )a`  
        S.translate(table) -> str

        Return a copy of the string S, where all characters have been mapped
        through the given translation table, which must be a mapping of
        Unicode ordinals to Unicode ordinals, strings, or None.
        Unmapped characters are left untouched. Characters mapped to None
        are deleted.
        N )rN   r   r   rO   chrr;   )r(   tablelrR   valr   r   r   	translate  s    

znewstr.translatec                 C   s   t dd S NZfixmerM   rr   r   r   r   isprintable  s    znewstr.isprintablec                 C   s   t dd S r   r   rr   r   r   r   isidentifier  s    znewstr.isidentifierc                 C   s   t dd S r   r   rr   r   r   r   
format_map  s    znewstr.format_map)rI   rJ   )NrZ   )NrZ   )F)NN)-r   r   r   __doc__rT   r   r'   r)   r0   r   r2   r7   r8   r9   r;   r?   rB   rD   r!   rP   rU   rY   r`   re   rg   rj   rm   rn   rp   rq   rs   ru   rt   rv   rw   rx   ry   r{   staticmethodr   r   r   r   r   __classcell__r   r   r&   r   r   B   sh   "	
)

	&r   N)r   numbersr   Zfuture.utilsr   r   r   r   Zfuture.typesr   r   Zfuture.types.newobjectr	   strr   collections.abcr
   collectionsr   r   r   __all__r   r   r   r   <module>   s   *  j