B A[eO-@sdZddlZddlZddlZddlZddlZddlZejddkZejdddkZ ejdddkZ ejddkZ ejdddkZ ejdddkZ eed Zd d Zd d ZerddZddZddZefZefZefZeZeZn8ddZddZddZefZeefZeej fZe!ZeZer2ddZ"nddZ"de"_erfdzddZ#d{ddZ$d|d d!Z%nd}d"dZ#d~d#dZ$dd%d!Z%d&e#_erd'd(Z&d)d*Z'd+d,Z(d-d.Z)n ddl*Z*e*j+Z&e*j,Z'e*j-Z(e*j.Z)dd0d1Z/d2d3Z0d4d5Z1d6d7Z2d8d9Z3d:d;Z4dd?Z6d@dAZ7dBdCZ8dDdEZ9erRdFdGZ:ddHdIZ;edM?dNe=_e;Z@dOdPZAerdQdRZBndSdRZBdTdUZCdVdWZDeZEeZFdXdYZGdZd[ZHd\d]ZId^d_ZJd`daZKerddlLZLeMeLdbZNn ddcddZNdedfZOddgdhZPy eQj3Wn&eRk r6didjZSdkdlZTYnXdmdjZSdndlZTerXdodpZUndqdpZUdrdsdtdudhd?ddddUdpdddvdAdPdWd[d1d_d]dYd9d;d=d.dldjd,d(d*dadwdxddfd dIdKdyd!dd3d5d7d g-ZVdS)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 * binary_type: str in Python 2, bytes in Python 3 * string_types: basestring in Python 2, str 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)rr)rZpypy_translation_infocCsts|j|_dd|_|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. cSs|dS)Nzutf-8) __unicode__encode)selfr F/opt/alt/python37/lib/python3.7/site-packages/future/utils/__init__.pyhz-python_2_unicode_compatible..)PY3__str__r)clsr r r python_2_unicode_compatibleCs# rcs"Gfddd}|ddiS)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. cs&eZdZejZejZfddZdS)z!with_metaclass..metaclasscs$|dkrt||d|S||S)Nr )type__new__)rname this_basesd)basesmetar r rsz)with_metaclass..metaclass.__new__N)__name__ __module__ __qualname__r__call____init__rr )rrr r metaclasssrtemporary_classNr )rrrr )rrr with_metaclasslsr cCs t|gS)N)bytes)sr r r bchrsr#cCs t|trt|dSt|SdS)Nzlatin-1) isinstancestrr!)r"r r r bstrs  r&cCs|S)Nr )r"r r r bordsr'cCst|S)N)chr)r"r r r r#scCst|S)N)r%)r"r r r r&scCst|S)N)ord)r"r r r r'scCs.t|tr|St|tr"|dSt|SdS)Nzlatin-1)r$r!r%r)r"r r r tobytess    r*cCs"t|tr|dSd|SdS)Nzlatin-1)r$unicoderjoin)r"r r r r*s  zS Encodes to latin-1 (where the first 256 chars are the same as ASCII.) utf-8cCs ||S)N)r)r"encodingr r r native_str_to_bytessr0cCs ||S)N)decode)br/r r r bytes_to_native_strsr3cCs|S)Nr )tr/r r r text_to_native_strsr5cCsddlm}||S)Nr)newbytes)Z future.typesr6)r"r/r6r r r r0s cCst|S)N)native)r2r/r r r r3sasciicCst||S)z} Use this to create a Py2 native string when "from __future__ import unicode_literals" is in effect. )r,r)r4r/r r r r5szu On Py3, returns an encoded string. On Py2, returns a newbytes type, ignoring the ``encoding`` argument. cOstt||S)N)listrange)argskwargsr r r lrangesr=cOstt||S)N)r9zip)r;r<r r r lzipsr?cOstt||S)N)r9map)r;r<r r r lmapsrAcOstt||S)N)r9filter)r;r<r r r lfiltersrCFcCsL|rtdd|dDStr(|Sddl}|d}t||SdS)zE A function equivalent to the str.isidentifier method on Py3 css|]}t|VqdS)N) isidentifier).0ar r r szisidentifier...rNz[a-zA-Z_][a-zA-Z0-9_]*$)allsplitrrDrecompileboolmatch)r"ZdottedrKZ_name_rer r r rDs rDcKs t|dd}|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)getattritems)objr<funcr r r rO s rOcKs t|dd}|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)rPkeys)rRr<rSr r r rTs rTcKs t|dd}|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)rPvalues)rRr<rSr r r rV"s rVcKs t|dd}|s|j}|f|S)zsUse this only if compatibility with Python versions before 2.7 is required. Otherwise, prefer viewitems(). iteritemsN)rPrQ)rRr<rSr r r rX.s rXcKs t|dd}|s|j}|f|S)zrUse this only if compatibility with Python versions before 2.7 is required. Otherwise, prefer viewkeys(). iterkeysN)rPrU)rRr<rSr r r rY8s rYcKs t|dd}|s|j}|f|S)ztUse this only if compatibility with Python versions before 2.7 is required. Otherwise, prefer viewvalues(). itervaluesN)rPrW)rRr<rSr r r rZBs rZcCs,tst||t|d|n t|||dS)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)rsetattrtypes MethodType)rrrSr r r bind_methodLsr^cCs tdS)N)sysexc_infor r r r getexceptiondsrbcCs(td}|dj}|dj}||fS)zr Returns the globals and locals of the calling frame. Is there an alternative to frame hacking here? rr)inspectstack f_globalsf_locals)Z caller_frame myglobalsmylocalsr r r _get_caller_globals_and_localshs   ricCs0t|}|dr(|dr(|ddS|SdS)zA Returns the string without any initial or final quotes. 'r_N)repr startswithendswith)Zmystringrr r r _repr_stripts rpcCs6t\}}|}||d<||d<d}t|||dS)zn 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)ricopyexec)exccausergrhZexecstrr r r raise_froms  rucCsL|dk rt|trtd|dk r,||}n|}|j|k rD|||dS)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 value)r$ Exception TypeError __traceback__with_traceback)tpvaluetbrsr r r raise_s   r}cCs$|tkrt\}}}||dS)N)Ellipsisr`rary)rs traceback_r r r raise_with_tracebacksrcCst|trt|tr|}n|}d|_t|trJt|trJ||_d|_n6|dkr`d|_d|_n t|trx||_d|_ntdt d|_ |dS)zn Equivalent to: raise EXCEPTION from CAUSE on Python 3. (See PEP 3134). FTNz/exception causes must derive from BaseExceptionr_) r$r issubclassrv__suppress_context__ __cause__ BaseExceptionrwr`ra __context__)rsrter r r rus  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.cCstr|S|j|_|`|SdS)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__next)rr r r implements_iterators rcCs|jS)N)r)xr r r r r r cCs|jS)N)r)rr r r r r cCs$tr|St|tr|dS|SdS)Nzutf-8)rr$r,r)filenamer r r encode_filenames   rcCs t|dodt|kpt|dS)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__)hasattrdir)rr r r is_new_stylesrcCst|tdS)z Deprecated. Use:: >>> isinstance(obj, str) after this import: >>> from future.builtins import str r+)r$r)rRr r r istextsrcCst|tdS)z Deprecated. Use:: >>> isinstance(obj, bytes) after this import: >>> from future.builtins import bytes r )r$r)rRr r r isbytessrcCsddlm}t||kS)z Equivalent to the result of ``isinstance(obj, newbytes)`` were ``__instancecheck__`` not overridden on the newbytes subclass. In other words, it is REALLY a newbytes instance, not a Py2 native str object? r)r6)future.types.newbytesr6r)rRr6r r r isnewbytes)s rcCs t|tjS)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$numbersIntegral)rRr r r isint6srcCst|dr|S|SdS)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)rr)rRr r r r7Is r7rrcCsB|dkr*td}|j}|dkr&|j}~n |dkr6|}tddS)zExecute code in a namespace.Nr_zexec code in globs, locs)r` _getframererfrr)codeZglobsZlocsframer r r exec_qs rcCs,t|tjr t|tjr ||S||SdS)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.) N)r$rr)rFr2r r r old_divs rcs tr ddSfdd}|SdS)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() cSs|S)Nr )fr r r r r zas_native_str..cstfdd}|S)Ncs||jdS)N)r/)r)r;r<)r/rr r wrappersz/as_native_str..encoder..wrapper) functoolswraps)rr)r/)rr encoderszas_native_str..encoderN)r)r/rr )r/r as_native_strs rcCs t|S)N)r9rW)rr r r listvaluessrcCs t|S)N)r9rQ)rr r r listitemssrcCs|S)N)rW)rr r r rscCs|S)N)rQ)rr r r rscCs|S)Nr )rRr r r ensure_new_typesrcCsddlm}ddlm}ddlm}ddlm}tt |}t |t|r|t krZ||S|t krj||S|t krz||S|tkr||S|tkr||S|Snt|||gkst|SdS)Nr)r6)newstr)newint)newdict)rr6Zfuture.types.newstrrZfuture.types.newintrZfuture.types.newdictrrr7rr%r,intlongdictAssertionError)rRr6rrrZ native_typer r r rs&     PY2PY26rPYPYget_next native_bytes native_strreraise)r.)r.)N)N)N)r8)F)NN)NN)r.)W__doc__r\r`rrrqrc version_inforZ PY35_PLUSZ PY36_PLUSrrZPY27rrrr r#r&r'r% string_typesrZ integer_typesrZ class_typesZ text_typer!Z binary_type basestringrZ ClassTyper,r*r0r3r5r=r?rArCZ __builtin__r:r>r@rBrDrOrTrVrXrYrZr^rbrirprur}r~rrrstriprrrrrrrrrrrr7builtinsrPrrrrAttributeErrorrrr__all__r r r r 0s )$                    "       $