o ,1]DU@svdZddlZddlZddlZddlZddlZddlZejddkZejdddkZ ejdddkZ ejdddkZ ejddkZ ejdddkZ ejddd kZeed Zd d Zd dZer~ddZddZddZefZefZefZeZeZnddZddZddZefZee fZeej!fZe"ZeZerddZ#nddZ#de#_erdtddZ$dtdd Z%dud!d"Z&ndud#dZ$dud$d Z%dvd&d"Z&d'e$_erd(d)Z'd*d+Z(d,d-Z)d.d/Z*nddl+Z+e+j,Z'e+j-Z(e+j.Z)e+j/Z*dwd1d2Z0d3d4Z1d5d6Z2d7d8Z3d9d:Z4d;d<Z5d=d>Z6d?d@Z7dAdBZ8dCdDZ9dEdFZ:er/dGdHZ;dxdIdJZn dMdHZ;e?dN@dOe>_e>> 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|dSNutf-8) __unicode__encode)selfr7/usr/lib/python3/dist-packages/future/utils/__init__.pyksz-python_2_unicode_compatible..)PY3__str__r clsrrrpython_2_unicode_compatibleFs# 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$|dur t||d|S||S)Nr)type__new__)rname this_basesdbasesmetarrrs z)with_metaclass..metaclass.__new__N)__name__ __module__ __qualname__r__call____init__rrrrr metaclasssr#temporary_classNr)rrr#rrrwith_metaclassos r%cCs t|gSN)bytessrrrbchr r*cCst|tr t|dSt|SNlatin-1) isinstancestrr'r(rrrbstrs  r0cC|Sr&rr(rrrbordr2cCt|Sr&)chrr(rrrr*cCr4r&)r/r(rrrr0r6cCr4r&)ordr(rrrr2r6cCs*t|tr|St|tr|dSt|Sr,)r.r'r/r r(rrrtobytess   r8cCst|tr |dSd|S)Nr-)r.unicoder joinr(rrrr8s   zS Encodes to latin-1 (where the first 256 chars are the same as ASCII.) r cC ||Sr&r )r)encodingrrrnative_str_to_bytesr+r?cCr<r&)decodebr>rrrbytes_to_native_strr+rCcCr1r&rtr>rrrtext_to_native_strr3rFcCsddlm}||S)Nrnewbytes) future.typesrH)r)r>rHrrrr?s cCr4r&)nativerArrrrCr6asciicCst||S)z} Use this to create a Py2 native string when "from __future__ import unicode_literals" is in effect. )r:r rDrrrrFszu On Py3, returns an encoded string. On Py2, returns a newbytes type, ignoring the ``encoding`` argument. cOtt|i|Sr&)listrangeargskwargsrrrlrangerRcOrLr&)rMziprOrrrlziprSrUcOrLr&)rMmaprOrrrlmaprSrWcOrLr&)rMfilterrOrrrlfilterrSrYFcCsH|rtdd|dDStr|Sddl}|d}t||S)zE A function equivalent to the str.isidentifier method on Py3 css|]}t|VqdSr&) isidentifier).0arrr szisidentifier...rNz[a-zA-Z_][a-zA-Z0-9_]*$)allsplitrrZrecompileboolmatch)r)dottedra_name_rerrrrZs rZcK$t|dd}|s |j}|di|S)z Function for iterating over dictionary items with the same set-like behaviour on Py2.7 as on Py3. Passes kwargs to method. viewitemsNrgetattritemsobjrQfuncrrrrh  rhcKrg)z Function for iterating over dictionary keys with the same set-like behaviour on Py2.7 as on Py3. Passes kwargs to method.viewkeysNrrjkeysrlrrrrprorpcKrg)z Function for iterating over dictionary values with the same set-like behaviour on Py2.7 as on Py3. Passes kwargs to method. viewvaluesNrrjvaluesrlrrrrs%rorscKrg)zsUse this only if compatibility with Python versions before 2.7 is required. Otherwise, prefer viewitems(). iteritemsNrrirlrrrrv1 rvcKrg)zrUse this only if compatibility with Python versions before 2.7 is required. Otherwise, prefer viewkeys(). iterkeysNrrqrlrrrrx;rwrxcKrg)ztUse this only if compatibility with Python versions before 2.7 is required. Otherwise, prefer viewvalues(). itervaluesNrrtrlrrrryErwrycCs.tst||t|d|dSt|||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)rrrnrrr bind_methodOsr}cCs tdS)N)sysexc_inforrrr getexceptiong rcCs(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) caller_frame myglobalsmylocalsrrr_get_caller_globals_and_localsks   rcCs,t|}|dr|dr|ddS|S)zA Returns the string without any initial or final quotes. 'r~)repr startswithendswith)mystringrrrr _repr_stripws rcCs6t\}}|}||d<||d<d}t|||dS)n Equivalent to: raise EXCEPTION from CAUSE on Python 3. (See PEP 3134). __python_future_raise_from_exc __python_future_raise_from_causezJraise __python_future_raise_from_exc from __python_future_raise_from_causeN)rcopyexec)exccauserrexecstrrrr raise_froms rcCst|tr|dur td|}n/t|tr!t|ts!td|jt||r)|}nt|tr3||}n |dur;|}n||}|j|urI|||)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 issubclassrtuple __traceback__with_traceback)tpvaluetbrrrrraise_s      rcCs |tkr t\}}}||r&)Ellipsisrrr)r traceback_rrrraise_with_tracebacks rcCst|trt|tr|}n|}d|_t|tr-t|tr-||_td|j_d|_n'|dur8d|_d|_nt|t rP||_t |jdtdd|_nt dtd|_ |)rFrTNrz/exception causes must derive from BaseExceptionr~)r.rr Exception__suppress_context__ __cause__rrrrobject __setattr__r __context__)rrerrrrs$  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|_|`|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() )r__next__nextrrrrimplements_iterators rcC|jSr&)rxrrrrrcCrr&)rrrrrrrcCs tr|St|tr|dS|Sr )rr.r:r )filenamerrrencode_filenames   rcCs t|dodt|vpt|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__)hasattrdirrrrr is_new_style srcCt|tdS)z Deprecated. Use:: >>> isinstance(obj, str) after this import: >>> from future.builtins import str r9r.rrmrrristext0rcCr)z Deprecated. Use:: >>> isinstance(obj, bytes) after this import: >>> from future.builtins import bytes rrrrrisbytes:rrcCst|jdkS)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) rH)rrrrrr isnewbytesDs 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.numbersIntegralrrrrisintPs rcCst|dr |S|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__)rrrrrrrJcs rJrcCsB|durtd}|j}|dur|j}~n|dur|}tddS)zExecute code in a namespace.Nr~zexec code in globs, locs)r _getframerrr)codeglobslocsframerrrexec_s  rcCs(t|tjrt|tjr||S||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.) r)r\rBrrrold_divs rcstrddSfdd}|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() cSr1r&rfrrrrszas_native_str..cstfdd}|S)Ncs|i|jdS)Nr>r=rO)r>rrrwrappersz/as_native_str..encoder..wrapper) functoolswraps)rrrrrencoderszas_native_str..encoder)r)r>rrrr as_native_strs rcC t|Sr&)rMrurrrr listvaluesrrcCrr&)rMrkrrrr listitemsrrcC|Sr&)rurrrrrr6cCrr&)rkrrrrrr6cCr1r&rrrrrensure_new_typer3rcCsddlm}ddlm}ddlm}ddlm}tt |}t |t|rO|t kr-||S|t kr5||S|t kr=||S|tkrE||S|tkrM||S|St|||fvsYJ|S)NrrG)newstr)newint)newdict)future.types.newbytesrHfuture.types.newstrrfuture.types.newintrfuture.types.newdictrrrJrr/r:intlongdict)rmrHrrr native_typerrrrs&     )2PY2PY26rPYPYr binary_typer}r2r0rC class_typesrrrget_nextrr integer_typesrrrZrrrrvrxryrYrrrWrRrUrJ native_bytes native_strr?rrrrreraise string_typesrF text_typer8rhrprsr%)r r&)rK)F)NN)X__doc__r{rrrrr version_infor PY34_PLUS PY35_PLUS PY36_PLUSrrPY27rrrr%r*r0r2r/rrrrrrr'r basestringr ClassTyper:r8r?rCrFrRrUrWrY __builtin__rNrTrVrXrZrhrprsrvrxryr}rrrrrrrrstriprrrrrrrrrrrrJbuiltinsrjrrrrAttributeErrorrrr__all__rrrrs3 )$                    &$       $