a
    ,]3                     @   s   d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	m
Z
mZmZmZmZ e	rfe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 )z
Backport of Python 3's int, based on Py2's long.

They are very similar. The most notable difference is:

- representation: trailing L in Python 2 removed in Python 3
    )divisionN)newbytes)	newobject)PY3isintistextisbyteswith_metaclassnative)Iterablec                   @   s   e Zd Zdd ZdS )
BaseNewIntc                 C   s&   | t krt|ttfS t|j| S d S N)newint
isinstanceintlong
issubclass	__class__)clsinstance r   d/home/tom/ab/renpy-build/tmp/install.linux-x86_64/lib/python3.9/site-packages/future/types/newint.py__instancecheck__   s    zBaseNewInt.__instancecheck__N)__name__
__module____qualname__r   r   r   r   r   r      s   r   c                       s  e Zd ZdZdR fdd	Z fddZ fdd	Z fd
dZ fddZ fddZ	 fddZ
 fddZdd Zdd Zdd Z fddZ fddZdd Z fd d!Z fd"d#Zd$d% Z fd&d'Z fd(d)Z fd*d+Z fd,d-Z fd.d/Z fd0d1Z fd2d3Z fd4d5Z fd6d7Z fd8d9Z fd:d;Z fd<d=Z  fd>d?Z! fd@dAZ" fdBdCZ#dDdE Z$dFdG Z% fdHdIZ&dJdK Z'dSdNdOZ(e)dTdPdQZ*  Z+S )Ur   z6
    A backport of the Python 3 int object to Py2
    r   
   c                    s  z|  }W n ty"   |}Y n0 t|s>tdt||dkrt|sht|sht|t	shtdzt
t| | ||W S  ty   t
t| | t|| Y S 0 zt
t| | |W S  ty   zt
t| | t|W  Y S    tdt|Y n0 Y n0 dS )a.  
        From the Py3 int docstring:

        |  int(x=0) -> integer
        |  int(x, base=10) -> integer
        |
        |  Convert a number or string to an integer, or return 0 if no
        |  arguments are given.  If x is a number, return x.__int__().  For
        |  floating point numbers, this truncates towards zero.
        |
        |  If x is not a number or if base is given, then x must be a string,
        |  bytes, or bytearray instance representing an integer literal in the
        |  given base.  The literal can be preceded by '+' or '-' and be
        |  surrounded by whitespace.  The base defaults to 10.  Valid bases are
        |  0 and 2-36. Base 0 means to interpret the base from the string as an
        |  integer literal.
        |  >>> int('0b100', base=0)
        |  4

        z__int__ returned non-int ({0})r   z1int() can't convert non-string with explicit basez6newint argument must be a string or a number,not '{0}'N)__int__AttributeErrorr   	TypeErrorformattyper   r   r   	bytearraysuperr   __new__r   )r   xbasevalr   r   r   r$   %   s4    
znewint.__new__c                    s*   t t|  }|d dksJ |dd S )z&
        Without the L suffix
        LN)r#   r   __repr__)selfvaluer(   r   r   r+   [   s    znewint.__repr__c                    s,   t t| |}|tu r$t| | S t|S r   )r#   r   __add__NotImplementedr   r,   otherr-   r(   r   r   r.   c   s    znewint.__add__c                    s,   t t| |}|tu r$|t|  S t|S r   )r#   r   __radd__r/   r   r0   r(   r   r   r2   i   s    znewint.__radd__c                    s,   t t| |}|tu r$t| | S t|S r   )r#   r   __sub__r/   r   r0   r(   r   r   r3   o   s    znewint.__sub__c                    s,   t t| |}|tu r$|t|  S t|S r   )r#   r   __rsub__r/   r   r0   r(   r   r   r4   u   s    znewint.__rsub__c                    s8   t t| |}t|r t|S |tu r4t| | S |S r   )r#   r   __mul__r   r/   r   r0   r(   r   r   r5   {   s    znewint.__mul__c                    s8   t t| |}t|r t|S |tu r4|t|  S |S r   )r#   r   __rmul__r   r/   r   r0   r(   r   r   r6      s    znewint.__rmul__c                 C   s*   t | | }t|tt fr"t|S |S d S r   r   r   r   r   r0   r   r   r   __div__   s    znewint.__div__c                 C   s*   |t |  }t|tt fr"t|S |S d S r   r7   r0   r   r   r   __rdiv__   s    znewint.__rdiv__c                 C   s(   |  |}t|ttfr t|S |S d S r   )__itruediv__r   r   r   r   r0   r   r   r   __idiv__   s    
znewint.__idiv__c                    s(   t t| |}|tu r$t| | }|S r   )r#   r   __truediv__r/   r   r0   r(   r   r   r<      s    znewint.__truediv__c                    s   t t| |S r   )r#   r   __rtruediv__r,   r1   r(   r   r   r=      s    znewint.__rtruediv__c                 C   s   t | }|| }|S r   r   r,   r1   mylongr   r   r   r:      s    znewint.__itruediv__c                    s   t tt | |S r   )r   r#   __floordiv__r>   r(   r   r   rB      s    znewint.__floordiv__c                    s   t tt | |S r   )r   r#   __rfloordiv__r>   r(   r   r   rC      s    znewint.__rfloordiv__c                 C   s   t | }|| }t|S r   )r   r   r@   r   r   r   __ifloordiv__   s    znewint.__ifloordiv__c                    s,   t t| |}|tu r$t| | S t|S r   )r#   r   __mod__r/   r   r0   r(   r   r   rE      s    znewint.__mod__c                    s,   t t| |}|tu r$|t|  S t|S r   )r#   r   __rmod__r/   r   r0   r(   r   r   rF      s    znewint.__rmod__c                    sH   t t| |}|tu r0t| }|| || fS t|d t|d fS Nr      )r#   r   
__divmod__r/   r   r,   r1   r-   rA   r(   r   r   rI      s
    znewint.__divmod__c                    sH   t t| |}|tu r0t| }|| || fS t|d t|d fS rG   )r#   r   __rdivmod__r/   r   rJ   r(   r   r   rK      s
    znewint.__rdivmod__c                    s,   t t| |}|tu r$t| | S t|S r   )r#   r   __pow__r/   r   r0   r(   r   r   rL      s    znewint.__pow__c                    s,   t t| |}|tu r$|t|  S t|S r   )r#   r   __rpow__r/   r   r0   r(   r   r   rM      s    znewint.__rpow__c                    s8   t |s$tdt| jt|jf ttt| |S )Nz1unsupported operand type(s) for <<: '%s' and '%s')r   r   r!   r   r   r#   
__lshift__r>   r(   r   r   rN      s    znewint.__lshift__c                    s8   t |s$tdt| jt|jf ttt| |S )Nz1unsupported operand type(s) for >>: '%s' and '%s')r   r   r!   r   r   r#   
__rshift__r>   r(   r   r   rO      s    znewint.__rshift__c                    s8   t |s$tdt| jt|jf ttt| |S )Nz0unsupported operand type(s) for &: '%s' and '%s')r   r   r!   r   r   r#   __and__r>   r(   r   r   rP      s    znewint.__and__c                    s8   t |s$tdt| jt|jf ttt| |S )Nz0unsupported operand type(s) for |: '%s' and '%s')r   r   r!   r   r   r#   __or__r>   r(   r   r   rQ      s    znewint.__or__c                    s8   t |s$tdt| jt|jf ttt| |S )Nz0unsupported operand type(s) for ^: '%s' and '%s')r   r   r!   r   r   r#   __xor__r>   r(   r   r   rR     s    znewint.__xor__c                    s   t tt |  S r   )r   r#   __neg__r,   r(   r   r   rS   	  s    znewint.__neg__c                    s   t tt |  S r   )r   r#   __pos__rT   r(   r   r   rU     s    znewint.__pos__c                    s   t tt |  S r   )r   r#   __abs__rT   r(   r   r   rV     s    znewint.__abs__c                    s   t tt |  S r   )r   r#   
__invert__rT   r(   r   r   rW     s    znewint.__invert__c                 C   s   | S r   r   rT   r   r   r   r     s    znewint.__int__c                 C   s   |   S r   )__bool__rT   r   r   r   __nonzero__  s    znewint.__nonzero__c                    s   t t|  S )z<
        So subclasses can override this, Py3-style
        )r#   r   rY   rT   r(   r   r   rX     s    znewint.__bool__c                 C   s   t | S r   r?   rT   r   r   r   
__native__!  s    znewint.__native__bigFc           	      C   s  |dk rt d|dkr&| dkr&t S |rX| dk rX|d }d| |  }|dkrltdn| dk rhtd| }|dvr|t dd	| }td
t|d  | |d d}|r|d d@ }| dkr|rtd| dk r|stdt||krtd|dkr|S |ddd S )aG  
        Return an array of bytes representing an integer.

        The integer is represented using length bytes.  An OverflowError is
        raised if the integer is not representable with the given number of
        bytes.

        The byteorder argument determines the byte order used to represent the
        integer.  If byteorder is 'big', the most significant byte is at the
        beginning of the byte array.  If byteorder is 'little', the most
        significant byte is at the end of the byte array.  To request the native
        byte order of the host system, use `sys.byteorder' as the byte order value.

        The signed keyword-only argument determines whether two's complement is
        used to represent the integer.  If signed is False and a negative integer
        is given, an OverflowError is raised.
        r   z$length argument must be non-negative      zint too smal to convertz&can't convert negative int to unsignedlittler[   *byteorder must be either 'little' or 'big's   %x   0hex   zint too big to convertzint too small to convertr[   Nr)   )
ValueErrorr   OverflowErrorlenzfilldecode)	r,   length	byteordersignedbitsnumhsZhigh_setr   r   r   to_bytes$  s2    
(znewint.to_bytesc                 C   s   |dvrt dt|tr$tdnt|tr6t|}|dkrB|n|ddd }t|dkr`d}tt|	d	d
}|r|d d@ r|dt|d   }| |S )a'  
        Return the integer represented by the given array of bytes.

        The mybytes argument must either support the buffer protocol or be an
        iterable object producing bytes.  Bytes and bytearray are examples of
        built-in objects that support the buffer protocol.

        The byteorder argument determines the byte order used to represent the
        integer.  If byteorder is 'big', the most significant byte is at the
        beginning of the byte array.  If byteorder is 'little', the most
        significant byte is at the end of the byte array.  To request the native
        byte order of the host system, use `sys.byteorder' as the byte order value.

        The signed keyword-only argument indicates whether two's complement is
        used to represent the integer.
        r^   r`   z'cannot convert unicode objects to bytesr[   Nr)   r       rb      rc   r]   r\   )
rd   r   unicoder   r   r   rf   r   r
   encode)r   Zmybytesrj   rk   brm   r   r   r   
from_bytesQ  s    


znewint.from_bytes)r   r   )r[   F)r[   F),r   r   r   __doc__r$   r+   r.   r2   r3   r4   r5   r6   r8   r9   r;   r<   r=   r:   rB   rC   rD   rE   rF   rI   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rU   rV   rW   r   rY   rX   rZ   rp   classmethodrv   __classcell__r   r   r(   r   r   !   sP   6
	
-r   )rw   
__future__r   structZfuture.types.newbytesr   Zfuture.types.newobjectr   Zfuture.utilsr   r   r   r   r	   r
   r   r   collections.abcr   collectionsr!   r   r   __all__r   r   r   r   <module>   s    	  ^