B ÍAÒ[eOã-@sÂdZddlZddlZddlZddlZddlZddlZejddkZejdd…dkZ ejdd…dkZ ejddkZ ejdd…dkZ ejdd…dkZ eed ƒZd d „Zd d „Zerêdd„Zdd„Zdd„ZefZefZefZeZeZn8dd„Zdd„Zdd„ZefZeefZeej fZe!ZeZe�r2dd„Z"ndd„Z"de"_e�rfdzdd„Z#d{dd„Z$d|d d!„Z%nd}d"d„Z#d~d#d„Z$dd%d!„Z%d&e#_e�r²d'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)d€d0d1„Z/d2d3„Z0d4d5„Z1d6d7„Z2d8d9„Z3d:d;„Z4dd?„Z6d@dA„Z7dBdC„Z8dDdE„Z9e�rRdFdG„Z:d�dHdI„Z;edM ?¡ƒdNe=_e;Z@dOdP„ZAe�rˆdQdR„ZBndSdR„ZBdTdU„ZCdVdW„ZDeZEeZFdXdY„ZGdZd[„ZHd\d]„ZId^d_„ZJd`da„ZKe�rêddlLZLeMeLdbƒZNn d‚dcdd„ZNdedf„ZOdƒdgdh„ZPy eQj3Wn&eRk �r6didj„ZSdkdl„ZTYnXdmdj„ZSdndl„ZTe�rXdodp„ZUndqdp„ZUdrdsdtdudhd?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)réZpypy_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| ¡ d¡S)Nzutf-8)Ú __unicode__Úencode)Úself©r úF/opt/alt/python37/lib/python3.7/site-packages/future/utils/__init__.pyÚhóz-python_2_unicode_compatible..)ÚPY3Ú__str__r)Úclsr r r Úpython_2_unicode_compatibleCs# rcs"G‡‡fdd„dˆƒ}|ddiƒ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. cs&eZdZejZejZ‡‡fdd„ZdS)z!with_metaclass..metaclasscs$|dkrt ||d|¡Sˆ|ˆ|ƒS)Nr )ÚtypeÚ__new__)rÚnameZ this_basesÚd)ÚbasesÚmetar r rˆsz)with_metaclass..metaclass.__new__N)Ú__name__Ú __module__Ú __qualname__rÚ__call__Ú__init__rr )rrr r Ú metaclass…srZtemporary_classNr )rrrr )rrr Úwith_metaclasslsrcCs t|gƒS)N)Úbytes)Úsr r r Úbchr‘sr!cCs t|tƒrt|dƒSt|ƒSdS)Nzlatin-1)Ú isinstanceÚstrr)r r r r Úbstr“s  r$cCs|S)Nr )r r r r Úbord˜sr%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|tƒr|St|tƒr"| d¡St|ƒSdS)Nzlatin-1)r"rr#r)r r r r Útobytes³s    r(cCs"t|tƒr| d¡Sd |¡SdS)Nzlatin-1Ú)r"ÚunicoderÚjoin)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_bytesÉsr.cCs | |¡S)N)Údecode)Úbr-r r r Úbytes_to_native_strÌsr1cCs|S)Nr )Útr-r r r Útext_to_native_strÏsr3cCsddlm}||ƒS)Nr)Únewbytes)Z future.typesr4)r r-r4r r r r.Ós cCst|ƒS)N)Únative)r0r-r r r r1×sÚasciicCst|ƒ |¡S)z} Use this to create a Py2 native string when "from __future__ import unicode_literals" is in effect. )r*r)r2r-r r r r3Úszu On Py3, returns an encoded string. On Py2, returns a newbytes type, ignoring the ``encoding`` argument. cOstt||ŽƒS)N)ÚlistÚrange)ÚargsÚkwargsr r r Úlrangeèsr;cOstt||ŽƒS)N)r7Úzip)r9r:r r r Úlzipësr=cOstt||ŽƒS)N)r7Úmap)r9r:r r r Úlmapîsr?cOstt||ŽƒS)N)r7Úfilter)r9r:r r r ÚlfilterñsrAFcCsL|rtdd„| d¡DƒƒStr(| ¡Sddl}| d¡}t| |¡ƒSdS)zE A function equivalent to the str.isidentifier method on Py3 css|]}t|ƒVqdS)N)Ú isidentifier)Ú.0Úar r r ú szisidentifier..Ú.rNz[a-zA-Z_][a-zA-Z0-9_]*$)ÚallÚsplitrrBÚreÚcompileÚboolÚmatch)r ZdottedrIZ_name_rer r r rBüs rBcKs 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)ÚgetattrÚitems)Úobjr:Úfuncr r r rM s rMcKs 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)rNÚkeys)rPr:rQr r r rRs rRcKs 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)rNÚvalues)rPr:rQr r r rT"s rTcKs t|ddƒ}|s|j}|f|ŽS)zsUse this only if compatibility with Python versions before 2.7 is required. Otherwise, prefer viewitems(). Ú iteritemsN)rNrO)rPr:rQr r r rV.s rVcKs t|ddƒ}|s|j}|f|ŽS)zrUse this only if compatibility with Python versions before 2.7 is required. Otherwise, prefer viewkeys(). ÚiterkeysN)rNrS)rPr:rQr r r rW8s rWcKs t|ddƒ}|s|j}|f|ŽS)ztUse this only if compatibility with Python versions before 2.7 is required. Otherwise, prefer viewvalues(). Ú itervaluesN)rNrU)rPr:rQr r r rXBs rXcCs,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)rÚsetattrÚtypesÚ MethodType)rrrQr r r Ú bind_methodLsr\cCs t ¡dS)Né)ÚsysÚexc_infor r r r Ú getexceptiondsr`cCs(t ¡d}|dj}|dj}||fS)zr Returns the globals and locals of the calling frame. Is there an alternative to frame hacking here? rr)ÚinspectÚstackÚ f_globalsÚf_locals)Z caller_frameÚ myglobalsÚmylocalsr r r Ú_get_caller_globals_and_localshs   rgcCs0t|ƒ}| d¡r(| d¡r(|dd…S|SdS)zA Returns the string without any initial or final quotes. ú'r]éÿÿÿÿN)ÚreprÚ startswithÚendswith)ZmystringÚrr r r Ú _repr_stripts rncCs6tƒ\}}| ¡}||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)rgÚcopyÚexec)ÚexcÚcausererfZexecstrr r r Ú raise_from€s  rscCsL|dk rt|tƒrtdƒ‚|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)ÚtpÚvalueÚtbrqr r r Úraise_’s   r{cCs$|tkrt ¡\}}}| |¡‚dS)N)ÚEllipsisr^r_rw)rqÚ tracebackÚ_r r r Úraise_with_traceback¢srcCs–t|tƒrt|tƒr|ƒ}n|}d|_t|tƒrJt|tƒrJ|ƒ|_d|_n6|dkr`d|_d|_n t|tƒrx||_d|_ntdƒ‚t  ¡d|_ |‚dS)zn Equivalent to: raise EXCEPTION from CAUSE on Python 3. (See PEP 3134). FTNz/exception causes must derive from BaseExceptionr]) r"rÚ issubclassrtÚ__suppress_context__Ú __cause__Ú BaseExceptionrur^r_Ú __context__)rqrrÚer r r rs¨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.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_iteratorßs rˆcCs|jS)N)r‡)Úxr r r r ÷r r cCs|jS)N)r†)r‰r r r r ùr cCs$tr|St|tƒr| d¡S|SdS)Nzutf-8)rr"r*r)Úfilenamer r r Úencode_filenameüs   r‹cCs t|dƒodt|ƒkpt|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Údir)rr r r Ú is_new_stylesr‘cCst|tdƒƒS)z Deprecated. Use:: >>> isinstance(obj, str) after this import: >>> from future.builtins import str r))r"r)rPr r r Úistextsr’cCst|tdƒƒS)zƒ Deprecated. Use:: >>> isinstance(obj, bytes) after this import: >>> from future.builtins import bytes r )r"r)rPr r r Úisbytessr“cCsddlm}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)r4)Úfuture.types.newbytesr4r)rPr4r r r Ú isnewbytes)s r•cCs t|tjƒ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"ÚnumbersÚIntegral)rPr r r Úisint6sr˜cCst|dƒr| ¡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)r�r™)rPr r r r5Is r5rpcCsB|dkr*t d¡}|j}|dkr&|j}~n |dkr6|}tdƒdS)zExecute code in a namespace.Nr]zexec code in globs, locs)r^Ú _getframercrdrp)ÚcodeZglobsZlocsÚframer r r Úexec_qs r�cCs,t|tjƒr t|tjƒr ||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"r–r—)rDr0r r r Úold_divs ržcs tr dd„S‡fdd„}|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..cst ˆ¡‡‡fdd„ƒ}|S)Ncsˆ||Žjˆd�S)N)r-)r)r9r:)r-rŸr r Úwrapper sz/as_native_str..encoder..wrapper)Ú functoolsÚwraps)rŸr )r-)rŸr ÚencoderŸszas_native_str..encoderN)r)r-r£r )r-r Ú as_native_strŽs r¤cCs t| ¡ƒS)N)r7rU)rr r r Ú listvalues¬sr¥cCs t| ¡ƒS)N)r7rO)rr r r Ú listitems®sr¦cCs| ¡S)N)rU)rr r r r¥²scCs| ¡S)N)rO)rr r r r¦´scCs|S)Nr )rPr r r Úensure_new_type¸sr§cCs¼ddlm}ddlm}ddlm}ddlm}tt |ƒƒ}t |t|ƒƒr |t krZ||ƒS|t krj||ƒS|t krz||ƒS|tkrŠ||ƒS|tkrš||ƒS|Snt|ƒ||gks´t‚|SdS)Nr)r4)Únewstr)Únewint)Únewdict)r”r4Zfuture.types.newstrr¨Zfuture.types.newintr©Zfuture.types.newdictrªrr5r€r#r*ÚintÚlongÚdictÚAssertionError)rPr4r¨r©rªZ native_typer r r r§»s&     ÚPY2ÚPY26rÚPYPYÚget_nextÚ native_bytesÚ native_strÚreraise)r,)r,)N)N)N)r6)F)NN)NN)r,)WÚ__doc__rZr^r–r¡roraÚ version_inforZ PY35_PLUSZ PY36_PLUSr¯r°ZPY27r�r±rrr!r$r%r#Z string_typesr«Z integer_typesrZ class_typesZ text_typerZ binary_typeZ basestringr¬Z ClassTyper*r(r.r1r3r;r=r?rAZ __builtin__r8r<r>r@rBrMrRrTrVrWrXr\r`rgrnrsr{r|rrpÚstriprµrˆr²r‹r‘r´r³r’r“r•r˜r5ÚbuiltinsrNr�ržr¤r­ÚAttributeErrorr¥r¦r§Ú__all__r r r r Ú0sè )$                    "       $