U
    ,1]DU                  2   @   s  d Z ddlZddlZddlZddlZddlZddlZejd dkZejdd dkZ	ejdd dkZ
ejdd dkZejd dkZejdd dkZejdd d	kZeed
Zdd Zdd Zerdd Zdd Zdd ZefZefZefZeZeZn8dd Zdd Zdd ZefZee fZeej!fZe"ZeZerDdd Z#ndd Z#de#_ erxdddZ$ddd Z%dd!d"Z&ndd#dZ$dd$d Z%dd&d"Z&d'e$_ erd(d) Z'd*d+ Z(d,d- Z)d.d/ Z*n ddl+Z+e+j,Z'e+j-Z(e+j.Z)e+j/Z*dd1d2Z0d3d4 Z1d5d6 Z2d7d8 Z3d9d: Z4d;d< Z5d=d> Z6d?d@ Z7dAdB Z8dCdD Z9dEdF Z:erddGdH Z;ddIdJZ<e=fdKdLZ>ndMdH Z;e?dN@  dOe>_ e<ZAdPdQ ZBerdRdS ZCndTdS ZCdUdV ZDdWdX ZEeZFeZGdYdZ ZHd[d\ ZId]d^ ZJd_d` ZKdadb ZLerddlMZMeNeMdcZOn
ddddeZOdfdg ZPddhdiZQz
eRj4 W n& eSk
rH   djdk ZTdldm ZUY nX dndk ZTdodm ZUerjdpdq ZVndrdq ZVdsdtdudvdidwd@ddd dxdVdqdedydBdQdzdXd\d2d`d^dZd:d<d>d/dmdkd-d)d+dbd{d|ddgddJdLd}d~d"ddd4d6d8dg2ZWdS )a  
A selection of cross-compatible functions for Python 2 and 3.

This module exports useful functions for 2/3 compatible code:

    * bind_method: binds functions to classes
    * ``native_str_to_bytes`` and ``bytes_to_native_str``
    * ``native_str``: always equal to the native platform string object (because
      this may be shadowed by imports from future.builtins)
    * lists: lrange(), lmap(), lzip(), lfilter()
    * iterable method compatibility:
        - iteritems, iterkeys, itervalues
        - viewitems, viewkeys, viewvalues

        These use the original method if available, otherwise they use items,
        keys, values.

    * types:

        * text_type: unicode in Python 2, str in Python 3
        * string_types: basestring in Python 2, str in Python 3
        * binary_type: str in Python 2, bytes in Python 3
        * integer_types: (int, long) in Python 2, int in Python 3
        * class_types: (type, types.ClassType) in Python 2, type in Python 3

    * bchr(c):
        Take an integer and make a 1-character byte string
    * bord(c)
        Take the result of indexing on a byte string and make an integer
    * tobytes(s)
        Take a text string, a byte string, or a sequence of characters taken
        from a byte string, and make a byte string.

    * raise_from()
    * raise_with_traceback()

This module also defines these decorators:

    * ``python_2_unicode_compatible``
    * ``with_metaclass``
    * ``implements_iterator``

Some of the functions in this module come from the following sources:

    * Jinja2 (BSD licensed: see
      https://github.com/mitsuhiko/jinja2/blob/master/LICENSE)
    * Pandas compatibility module pandas.compat
    * six.py by Benjamin Peterson
    * Django
    N      )r      )r      )r      )r   r   )r      Zpypy_translation_infoc                 C   s   t s| j| _dd | _| S )u  
    A decorator that defines __unicode__ and __str__ methods under Python
    2. Under Python 3, this decorator is a no-op.

    To support Python 2 and 3 with a single code base, define a __str__
    method returning unicode text and apply this decorator to the class, like
    this::

    >>> from future.utils import python_2_unicode_compatible

    >>> @python_2_unicode_compatible
    ... class MyClass(object):
    ...     def __str__(self):
    ...         return u'Unicode string: 孔子'

    >>> a = MyClass()

    Then, after this import:

    >>> from future.builtins import str

    the following is ``True`` on both Python 3 and 2::

    >>> str(a) == a.encode('utf-8').decode('utf-8')
    True

    and, on a Unicode-enabled terminal with the right fonts, these both print the
    Chinese characters for Confucius::

    >>> print(a)
    >>> print(str(a))

    The implementation comes from django.utils.encoding.
    c                 S   s   |   dS Nutf-8)__unicode__encode)self r   7/usr/lib/python3/dist-packages/future/utils/__init__.py<lambda>k       z-python_2_unicode_compatible.<locals>.<lambda>)PY3__str__r
   clsr   r   r   python_2_unicode_compatibleF   s    #
r   c                    s"   G  fddd}|ddi S )a  
    Function from jinja2/_compat.py. License: BSD.

    Use it like this::

        class BaseForm(object):
            pass

        class FormType(type):
            pass

        class Form(with_metaclass(FormType, BaseForm)):
            pass

    This requires a bit of explanation: the basic idea is to make a
    dummy metaclass for one level of class instantiation that replaces
    itself with the actual metaclass.  Because of internal type checks
    we also need to make sure that we downgrade the custom metaclass
    for one level to something closer to type (that's why __call__ and
    __init__ comes back from type etc.).

    This has the advantage over six.with_metaclass of not introducing
    dummy classes into the final MRO.
    c                       s&   e Zd ZejZejZ fddZdS )z!with_metaclass.<locals>.metaclassc                    s$   |d krt | |d|S | |S )Nr   )type__new__)r   nameZ
this_basesdbasesmetar   r   r      s    z)with_metaclass.<locals>.metaclass.__new__N)__name__
__module____qualname__r   __call____init__r   r   r   r   r   	metaclass   s   r"   Ztemporary_classNr   )r   r   r"   r   r   r   with_metaclasso   s    r#   c                 C   s
   t | gS N)bytessr   r   r   bchr   s    r(   c                 C   s    t | trt| dS t| S d S Nlatin-1)
isinstancestrr%   r&   r   r   r   bstr   s    

r-   c                 C   s   | S r$   r   r&   r   r   r   bord   s    r.   c                 C   s   t | S r$   )chrr&   r   r   r   r(      s    c                 C   s   t | S r$   )r,   r&   r   r   r   r-      s    c                 C   s   t | S r$   )ordr&   r   r   r   r.      s    c                 C   s.   t | tr| S t | tr"| dS t| S d S r)   )r+   r%   r,   r   r&   r   r   r   tobytes   s
    


r1   c                 C   s"   t | tr| dS d| S d S )Nr*    )r+   unicoder   joinr&   r   r   r   r1      s    

zS
    Encodes to latin-1 (where the first 256 chars are the same as
    ASCII.)
    r	   c                 C   s
   |  |S r$   r   )r'   encodingr   r   r   native_str_to_bytes   s    r7   c                 C   s
   |  |S r$   )decodebr6   r   r   r   bytes_to_native_str   s    r;   c                 C   s   | S r$   r   tr6   r   r   r   text_to_native_str   s    r>   c                 C   s   ddl m} || S )Nr   newbytes)Zfuture.typesr@   )r'   r6   r@   r   r   r   r7      s    c                 C   s   t | S r$   )nativer9   r   r   r   r;      s    asciic                 C   s   t | |S )z}
        Use this to create a Py2 native string when "from __future__ import
        unicode_literals" is in effect.
        )r3   r   r<   r   r   r   r>      s    zu
    On Py3, returns an encoded string.
    On Py2, returns a newbytes type, ignoring the ``encoding`` argument.
    c                  O   s   t t| |S r$   )listrangeargskwargsr   r   r   lrange   s    rH   c                  O   s   t t| |S r$   )rC   ziprE   r   r   r   lzip   s    rJ   c                  O   s   t t| |S r$   )rC   maprE   r   r   r   lmap   s    rL   c                  O   s   t t| |S r$   )rC   filterrE   r   r   r   lfilter   s    rN   Fc                 C   sL   |rt dd | dD S tr(|  S ddl}|d}t|| S dS )zE
    A function equivalent to the str.isidentifier method on Py3
    c                 s   s   | ]}t |V  qd S r$   )isidentifier).0ar   r   r   	<genexpr>  s     zisidentifier.<locals>.<genexpr>.r   Nz[a-zA-Z_][a-zA-Z0-9_]*$)allsplitr   rO   recompileboolmatch)r'   ZdottedrV   Z_name_rer   r   r   rO      s    
rO   c                 K   s    t | dd}|s| j}|f |S )z
    Function for iterating over dictionary items with the same set-like
    behaviour on Py2.7 as on Py3.

    Passes kwargs to method.	viewitemsNgetattritemsobjrG   funcr   r   r   rZ     s    rZ   c                 K   s    t | dd}|s| j}|f |S )z
    Function for iterating over dictionary keys with the same set-like
    behaviour on Py2.7 as on Py3.

    Passes kwargs to method.viewkeysNr\   keysr^   r   r   r   ra     s    ra   c                 K   s    t | dd}|s| j}|f |S )z
    Function for iterating over dictionary values with the same set-like
    behaviour on Py2.7 as on Py3.

    Passes kwargs to method.
viewvaluesNr\   valuesr^   r   r   r   rd   %  s    rd   c                 K   s    t | dd}|s| j}|f |S )zsUse this only if compatibility with Python versions before 2.7 is
    required. Otherwise, prefer viewitems().
    	iteritemsNr[   r^   r   r   r   rg   1  s    rg   c                 K   s    t | dd}|s| j}|f |S )zrUse this only if compatibility with Python versions before 2.7 is
    required. Otherwise, prefer viewkeys().
    iterkeysNrb   r^   r   r   r   rh   ;  s    rh   c                 K   s    t | dd}|s| j}|f |S )ztUse this only if compatibility with Python versions before 2.7 is
    required. Otherwise, prefer viewvalues().
    
itervaluesNre   r^   r   r   r   ri   E  s    ri   c                 C   s,   t st| |t|d|  nt| || dS )a/  Bind a method to class, python 2 and python 3 compatible.

    Parameters
    ----------

    cls : type
        class to receive bound method
    name : basestring
        name of method on class instance
    func : function
        function to be bound as method

    Returns
    -------
    None
    N)r   setattrtypes
MethodType)r   r   r`   r   r   r   bind_methodO  s    rm   c                   C   s   t  d S )N   )sysexc_infor   r   r   r   getexceptiong  s    rq   c                  C   s(   t  d } | d j}| d j}||fS )zr
    Returns the globals and locals of the calling frame.

    Is there an alternative to frame hacking here?
    r   r   )inspectstack	f_globalsf_locals)Zcaller_frame	myglobalsmylocalsr   r   r   _get_caller_globals_and_localsk  s    

rx   c                 C   s0   t | }|dr(|dr(|dd S |S dS )zA
    Returns the string without any initial or final quotes.
    'rn   N)repr
startswithendswith)Zmystringrr   r   r   _repr_stripw  s    r   c                 C   s6   t  \}}| }| |d< ||d< d}t||| dS )n
        Equivalent to:

            raise EXCEPTION from CAUSE

        on Python 3. (See PEP 3134).
        Z__python_future_raise_from_excZ __python_future_raise_from_causezJraise __python_future_raise_from_exc from __python_future_raise_from_causeN)rx   copyexec)exccauserv   rw   Zexecstrr   r   r   
raise_from  s    
r   c                 C   s   t | tr |dk	rtd| }n`t | trDt| tsDtd| j n<t || rT|}n,t |trh| | }n|dkrx|  }n| |}|j|k	r|||dS )z
        A function that matches the Python 2.x ``raise`` statement. This
        allows re-raising exceptions with the cls value and traceback on
        Python 2 and 3.
        Nz0instance exception may not have a separate valuez,class must derive from BaseException, not %s)	r+   BaseException	TypeErrorr   
issubclassr   tuple__traceback__with_traceback)tpvaluetbr   r   r   r   raise_  s     
	




r   c                 C   s$   |t krt \}}}| |d S r$   )Ellipsisro   rp   r   )r   	traceback_r   r   r   raise_with_traceback  s    r   c                 C   s   t | trt| tr|  }n| }d|_t |trZt|trZ| |_t d |j_d|_nN|dkrpd|_d|_n8t |t	r||_t
|jdt d  d|_ntdt d |_|dS )r   Fr   TNr   z/exception causes must derive from BaseExceptionrn   )r+   r   r   	Exception__suppress_context__	__cause__ro   rp   r   r   object__setattr__r   __context__)r   r   er   r   r   r     s$    
z
def raise_(tp, value=None, tb=None):
    raise tp, value, tb

def raise_with_traceback(exc, traceback=Ellipsis):
    if traceback == Ellipsis:
        _, _, traceback = sys.exc_info()
    raise exc, None, traceback
zjRaise exception with existing traceback.
If traceback is not passed, uses sys.exc_info() to get traceback.c                 C   s   t r| S | j| _| `| S dS )a  
    From jinja2/_compat.py. License: BSD.

    Use as a decorator like this::

        @implements_iterator
        class UppercasingIterator(object):
            def __init__(self, iterable):
                self._iter = iter(iterable)
            def __iter__(self):
                return self
            def __next__(self):
                return next(self._iter).upper()

    N)r   __next__nextr   r   r   r   implements_iterator  s
    r   c                 C   s   | j S r$   )r   xr   r   r   r     r   r   c                 C   s   | j S r$   )r   r   r   r   r   r     r   c                 C   s$   t r| S t| tr| dS | S d S r   )r   r+   r3   r   )filenamer   r   r   encode_filename  s
    

r   c                 C   s    t | dodt| kpt | dS )a  
    Python 2.7 has both new-style and old-style classes. Old-style classes can
    be pesky in some circumstances, such as when using inheritance.  Use this
    function to test for whether a class is new-style. (Python 3 only has
    new-style classes.)
    	__class____dict__	__slots__)hasattrdirr   r   r   r   is_new_style   s    r   c                 C   s   t | tdS )z
    Deprecated. Use::
        >>> isinstance(obj, str)
    after this import:
        >>> from future.builtins import str
    r2   r+   r   r_   r   r   r   istext0  s    r   c                 C   s   t | tdS )z
    Deprecated. Use::
        >>> isinstance(obj, bytes)
    after this import:
        >>> from future.builtins import bytes
    r   r   r   r   r   r   isbytes:  s    r   c                 C   s   t | jdkS )a  
    Equivalent to the result of ``type(obj)  == type(newbytes)``
    in other words, it is REALLY a newbytes instance, not a Py2 native str
    object?

    Note that this does not cover subclasses of newbytes, and it is not
    equivalent to ininstance(obj, newbytes)
    r@   )r   r   r   r   r   r   
isnewbytesD  s    	r   c                 C   s   t | tjS )a_  
    Deprecated. Tests whether an object is a Py3 ``int`` or either a Py2 ``int`` or
    ``long``.

    Instead of using this function, you can use:

        >>> from future.builtins import int
        >>> isinstance(obj, int)

    The following idiom is equivalent:

        >>> from numbers import Integral
        >>> isinstance(obj, Integral)
    r+   numbersZIntegralr   r   r   r   isintP  s    r   c                 C   s   t | dr|  S | S dS )aO  
    On Py3, this is a no-op: native(obj) -> obj

    On Py2, returns the corresponding native Py2 types that are
    superclasses for backported objects from Py3:

    >>> from builtins import str, bytes, int

    >>> native(str(u'ABC'))
    u'ABC'
    >>> type(native(str(u'ABC')))
    unicode

    >>> native(bytes(b'ABC'))
    b'ABC'
    >>> type(native(bytes(b'ABC')))
    bytes

    >>> native(int(10**20))
    100000000000000000000L
    >>> type(native(int(10**20)))
    long

    Existing native types on Py2 will be returned unchanged:

    >>> type(native(u'ABC'))
    unicode
    
__native__N)r   r   r   r   r   r   rA   c  s    
rA   r   c                 C   sB   |dkr*t d}|j}|dkr&|j}~n|dkr6|}td dS )zExecute code in a namespace.Nrn   zexec code in globs, locs)ro   	_getframert   ru   r   )codeZglobsZlocsframer   r   r   exec_  s    
r   c                 C   s,   t | tjr t |tjr | | S | | S dS )z
    DEPRECATED: import ``old_div`` from ``past.utils`` instead.

    Equivalent to ``a / b`` on Python 2 without ``from __future__ import
    division``.

    TODO: generalize this to other objects (like arrays etc.)
    Nr   )rQ   r:   r   r   r   old_div  s    	r   c                    s    t rdd S  fdd}|S dS )a~  
    A decorator to turn a function or method call that returns text, i.e.
    unicode, into one that returns a native platform str.

    Use it as a decorator like this::

        from __future__ import unicode_literals

        class MyClass(object):
            @as_native_str(encoding='ascii')
            def __repr__(self):
                return next(self._iter).upper()
    c                 S   s   | S r$   r   fr   r   r   r     r   zas_native_str.<locals>.<lambda>c                    s   t   fdd}|S )Nc                     s   | |j  dS )Nr6   r5   rE   )r6   r   r   r   wrapper  s    z/as_native_str.<locals>.encoder.<locals>.wrapper)	functoolswraps)r   r   r   r   r   encoder  s    zas_native_str.<locals>.encoderN)r   )r6   r   r   r   r   as_native_str  s    r   c                 C   s   t |  S r$   )rC   rf   r   r   r   r   
listvalues  s    r   c                 C   s   t |  S r$   )rC   r]   r   r   r   r   	listitems  s    r   c                 C   s   |   S r$   )rf   r   r   r   r   r     s    c                 C   s   |   S r$   )r]   r   r   r   r   r     s    c                 C   s   | S r$   r   r   r   r   r   ensure_new_type  s    r   c                 C   s   ddl m} ddlm} ddlm} ddlm} tt	| }t
|t| r|tkrZ|| S |tkrj|| S |tkrz|| S |tkr|| S |tkr|| S | S nt| ||fkst| S d S )Nr   r?   )newstr)newint)newdict)Zfuture.types.newbytesr@   Zfuture.types.newstrr   Zfuture.types.newintr   Zfuture.types.newdictr   r   rA   r   r,   r3   intlongdictAssertionError)r_   r@   r   r   r   Znative_typer   r   r   r     s&    PY2PY26r   PYPYbinary_typeclass_typesget_nextinteger_typesnative_bytes
native_strreraisestring_types	text_type)r	   )r	   )N)N)N)rB   )F)NN)NN)r	   )X__doc__rk   ro   r   r   r   rr   version_infor   Z	PY34_PLUSZ	PY35_PLUSZ	PY36_PLUSr   r   ZPY27r   r   r   r#   r(   r-   r.   r,   r   r   r   r   r   r   r%   r   Z
basestringr   Z	ClassTyper3   r1   r7   r;   r>   rH   rJ   rL   rN   Z__builtin__rD   rI   rK   rM   rO   rZ   ra   rd   rg   rh   ri   rm   rq   rx   r   r   r   r   r   r   stripr   r   r   r   r   r   r   r   r   r   r   rA   builtinsr\   r   r   r   r   AttributeErrorr   r   r   __all__r   r   r   r   <module>   s0  3
)$













&$
	

$



                                  