U  W[Z)@sdZddlmZmZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddlZddlmZmZe jdkrdZndZe jd krdZndZe jd krdZndZed krdZndZd d ZepeZd}ddZddZddZz ejWnek rde_YnXzeejdWn*eeej fk rRee_ee_YnXe!Z"erd["[[e#Z#e$Z$zddl%m&Z&Wne'k re&Z&YnXd~ddZ(ze)Z)Wnek rddZ)YnXddZ*ere+Z,e-Z.ne,Z,e.Z.e,fddZ/dd Z0d!d"Z1erd#d$Z2ne3d%d&e2_er8dd'lm4Z5n dd(lm6Z5ernd)d*Z7d+d,Z8dd-d.Z9d/d0Z:nd1d*Z7d2d,Z8e;Z9d3d0Z:d4e7_d5e8_d6e:_zeZ>Wne'k re?Z>YnXzdd8l m@Z@Wne'k reZ@YnXer\ddlAmBZCdd9lDmEZEdd:lAmFZGdd;lAmHZIddZPd?d@ZQdAdBZReSZSeSZTeUZVn0dCd>ZPdDd@ZQdEdBZReTZSeTZTddFlWmVZVeVdGeP_dHeQ_dIeR_dJdKZXdLdMZYdNdOZZddPl[m\Z\ddQl]m^Z^ddRl_m`Z`e^e\dSdTdUddVdWdXerrddYlambZcddZlamdZendd[lamfZcdd\lamgZed]d^Zhzdd_l miZiWne'k reiZiYnXd`daZjerekZlemZnnelZlenZndbdcZoere j Zpne jqZpzdddlrmsZsWn"e'k r,dddl_msZsYnXddedfZtd$ddgdhdidddXd djd0dkd*d,d.dldmdndBd>d@dodpdqdMdrdsdtdudKdvdwd^dadcdxdydzd{dfd|g)ZudS)a) Compatibility module to provide backwards compatibility for useful Python features. This is mainly for use of internal Twisted code. We encourage you to use the latest version of Python directly from your code, if possible. @var unicode: The type of Unicode strings, C{unicode} on Python 2 and C{str} on Python 3. @var NativeStringIO: An in-memory file-like object that operates on the native string type (bytes in Python 2, unicode in Python 3). @var urllib_parse: a URL-parsing module (urlparse on Python 2, urllib.parse on Python 3) )absolute_importdivisionN) MethodType) TextIOBaseIOBase)rFT)rr)rrZPyPycCs"tjdd}|dkrdSdSdS)a- Returns whether or not we should enable the new-style conversion of old-style classes. It inspects the environment for C{TWISTED_NEWSTYLE}, accepting an empty string, C{no}, C{false}, C{False}, and C{0} as falsey values and everything else as a truthy value. @rtype: L{bool} ZTWISTED_NEWSTYLE)r ZnoZfalseFalse0FTN)osenvironget)valuer7/usr/lib/python3/dist-packages/twisted/python/compat.py_shouldEnableNewStyle=s rcCs$t}t|dD] }|j}q|S)a In Python 3, L{inspect.currentframe} does not take a stack-level argument. Restore that functionality from Python 2 so we don't have to re-implement the C{f_back}-walking loop in places where it's called. @param n: The number of stack levels above the caller to walk. @type n: L{int} @return: a frame, n levels up the stack from the caller. @rtype: L{types.FrameType} )inspect currentframerangef_back)nfxrrrrQs rc Cs|s td|tjkr t|S|ttddkrd|kr^|ddksV|ddkr^td|dd}|d}|d}d|d k}t|d |ks|d krtd |d krd S|r@dgd t|||}| dr||dd<n4| dr ||dd<n|d}||||d<t|d |krZtd nt|d |krZtd |r|d dd kr|td t|d }t d|}dd|D|d d<dd|D}t j d|StdddS)a, Emulator of L{socket.inet_pton}. @param af: An address family to parse; C{socket.AF_INET} or C{socket.AF_INET6}. @type af: L{int} @param addr: An address. @type addr: native L{str} @return: The binary packed version of the passed address. @rtype: L{bytes} z-illegal IP address string passed to inet_ptonAF_INET6%rr:r .rzSyntactically invalid addresszr ::Nz!HHcSsg|]}t|ddqSr#Nhex.0rrrr szinet_pton..cSsg|]}t|dqS))intr(rrrr*s!8Ha(Address family not supported by protocol)r-) ValueErrorsocketAF_INETZ inet_atongetattrcountindexsplitlen startswithendswithstructunpackZpackerror) afaddrpartsZelidedZ ipv4ComponentZzerosidxZrawipv4Z unpackedipv4rrr inet_ptondsH  $        rAcCs>|tjkrt|S|tjkr.t|dkr4tdtd|}d}}tdD]N}||sv|dkrl|}d}|d7}qP|dk rPd}|dks||kr|}|}d}qP|dk r|dks||kr|}|}dd|D}|dk rd g||||<|dd kr| dd |d d kr$| t|dd d |St d d dS)Nr+zaddress length incorrectr-r!rrcSsg|]}t|ddqSr%r&r(rrrr*szinet_ntop..r r rr.r/) r1r2Z inet_ntoarr7r0r:r;rinsertjoinr<)r=r>r?ZcurBaseZbestBaseiZcurLenZbestLenrrr inet_ntops>         rErr")reducec CsF|dkr |}t|d}|}W5QRXt||d}t|||dS)az Execute a Python script in the given namespaces. Similar to the execfile builtin, but a namespace is mandatory, partly because that's a sensible thing to require, and because otherwise we'd have to do some frame hacking. This is a compatibility implementation for Python 3 porting, to avoid the use of the deprecated builtin C{execfile} function. Nrbexec)openreadcompilerH)filenameglobalslocalsZfinsourcecoderrrexecfiles   rQcCs ||kr dS||krdSdSdS)z Compare two objects. Returns a negative number if C{a < b}, zero if they are equal, and a positive number if C{a > b}. r rrNr)abrrrcmps rTcCs`ts|Sdd}dd}dd}dd}d d }d d }||_||_||_||_||_||_|S) z Class decorator that ensures support for the special C{__cmp__} method. On Python 2 this does nothing. On Python 3, C{__eq__}, C{__lt__}, etc. methods are added to the class, relying on C{__cmp__} to implement their comparisons. cSs||}|tkr|S|dkSNrZ__cmp__NotImplementedselfothercrrr__eq__s zcomparable..__eq__cSs||}|tkr|S|dkSrUrVrXrrr__ne__"s zcomparable..__ne__cSs||}|tkr|S|dkSrUrVrXrrr__lt__)s zcomparable..__lt__cSs||}|tkr|S|dkSrUrVrXrrr__le__0s zcomparable..__le__cSs||}|tkr|S|dkSrUrVrXrrr__gt__7s zcomparable..__gt__cSs||}|tkr|S|dkSrUrVrXrrr__ge__>s zcomparable..__ge__)_PY3r^r`r_rar\r])klassr\r]r^r_r`rarrr comparable s rdcCst|trtSt|trtSt|dd}ddl}t||j|jfrN|rJtStSt st|t rl|dk rht StSddl m }m}ddlm}t||||frtS|S)a[ Determine the type which will be returned from the given file object's read() and accepted by its write() method as an argument. In other words, determine whether the given file is 'opened in text mode'. @param fileIshObject: Any object, but ideally one which resembles a file. @type fileIshObject: L{object} @param default: A default value to return when the type of C{fileIshObject} cannot be determined. @type default: L{type} @return: There are 3 possible return values: 1. L{unicode}, if the file is unambiguously opened in text mode. 2. L{bytes}, if the file is unambiguously opened in binary mode. 3. L{basestring}, if we are on python 2 (the L{basestring} type does not exist on python 3) and the file is opened in binary mode, but has an encoding and can therefore accept both bytes and text reliably for writing, but will return L{bytes} from read methods. 4. The C{default} parameter, if the given type is not understood. @rtype: L{type} encodingNr) InputType OutputTypeStringIO) isinstancerunicoderbytesr3codecs StreamReader StreamWriterrbfile basestring cStringIOrfrgri)Z fileIshObjectdefaultrermrfrgrirrrioTypeWs(     rtcCs`t|ttfstd|tr>t|tr2|dS|dnt|trR|dS|d|S)a  Convert C{bytes} or C{unicode} to the native C{str} type, using ASCII encoding if conversion is necessary. @raise UnicodeError: The input string is not ASCII encodable/decodable. @raise TypeError: The input is neither C{bytes} nor C{unicode}. z%r is neither bytes nor unicodeascii)rjrlrk TypeErrorrbdecodeencodesrrr nativeStrings       r{cCs<t|tr|d}n |d}t|t|kr4|S|SdS)a Some functions, such as C{os.path.join}, operate on string arguments which may be bytes or text, and wish to return a value of the same type. In those cases you may wish to have a string constant (in the case of C{os.path.join}, that constant would be C{os.path.sep}) involved in the parsing or processing, that must be of a matching type in order to use string operations on it. L{_matchingString} will take a constant string (either L{bytes} or L{unicode}) and convert it to the same type as the input string. C{constantString} should contain only characters from ASCII; to ensure this, it will be encoded or decoded regardless. @param constantString: A string literal used in processing. @type constantString: L{unicode} or L{bytes} @param inputString: A byte string or text string provided by the user. @type inputString: L{unicode} or L{bytes} @return: C{constantString} converted into the same type as C{inputString} @rtype: the type of C{inputString} ruN)rjrlrwrxtype)ZconstantStringZ inputStringZ otherTyperrr_matchingStrings    r}cCs||dSN)with_traceback)Z exception tracebackrrrreraisesrzZdef reraise(exception, traceback): raise exception.__class__, exception, tracebackaZ Re-raise an exception, with an optional traceback, in a way that is compatible with both Python 2 and Python 3. Note that on Python 3, re-raised exceptions will be mutated, with their C{__traceback__} attribute being set. @param exception: The exception instance. @param traceback: The traceback to use, or L{None} indicating a new traceback. rh)BytesIOccs(tt|D]}|||dVq dS)Nr)rr7) originalBytesrDrrr iterbytessrcCsd|dS)Nz%dru)rxrDrrr intToBytessrcCs0t|}|dkr||dS||||SdS)a Return a copy of the given bytes-like object. If an offset is given, the copy starts at that offset. If a size is given, the copy will only be of that length. @param object: C{bytes} to be copied. @param offset: C{int}, starting index of copy. @param size: Optional, if an C{int} is given limit the length of copy to this size. N) memoryview)objectoffsetsizeZviewrrr lazyByteSlices rcCst|tstd|dS)Nz*Can only convert text to bytes on Python 3ru)rjrkrvrxryrrr networkString s rcCs|Sr~r)rrrrrscCsd|S)Ns%drrrrrrscCs t|tstd|d|S)Nz'Can only pass-through bytes on Python 2ru)rjstrrvrwryrrrrs  a Return an iterable wrapper for a C{bytes} object that provides the behavior of iterating over C{bytes} on Python 2. In particular, the results of iteration are the individual bytes (rather than integers as on Python 3). @param originalBytes: A C{bytes} object that will be wrapped. z Convert the given integer into C{bytes}, as ASCII-encoded Arab numeral. In other words, this is equivalent to calling C{bytes} in Python 2 on an integer. @param i: The C{int} to convert to C{bytes}. @rtype: C{bytes} a Convert the native string type to C{bytes} if it is not already C{bytes} using ASCII encoding if conversion is necessary. This is useful for sending text-like bytes that are constructed using string interpolation. For example, this is safe on Python 2 and Python 3: networkString("Hello %d" % (n,)) @param s: A native string to convert to bytes if necessary. @type s: C{str} @raise UnicodeError: The input string is not ASCII encodable/decodable. @raise TypeError: The input is neither C{bytes} nor C{unicode}. @rtype: C{bytes} ) InstanceType)FileType)escape)quote)unquote) cookiejarcCs|Sr~itemsdrrr iteritemshsrcCs|Sr~)valuesrrrr itervalueslsrcCs t|Sr~)listrrrrrrpsrcCs|Sr~)rrrrrrwscCs|Sr~)rrrrrr{scCs|Sr~rrrrrrs)izipzM Return an iterable of the items of C{d}. @type d: L{dict} @rtype: iterable zN Return an iterable of the values of C{d}. @type d: L{dict} @rtype: iterable zG Return a list of the items of C{d}. @type d: L{dict} @rtype: L{list} cCstrt|S|SdS)zV Return a list of the keys of C{d}. @type d: L{dict} @rtype: L{list} N)rbrkeysrrrr_keyss rcCsDtsttjSt}tjD] \}}tj||tj|<q|S)z Return a L{dict} of L{os.environ} where all text-strings are encoded into L{bytes}. This function is POSIX only; environment variables are always text strings on Windows. )rbdictr rr encodevalue encodekey)targetryrrr bytesEnvirons  rcCs$|j|}trt||St|||S)aj Construct a bound method. @param cls: The class that the method should be bound to. @type cls: L{types.ClassType} or L{type}. @param name: The name of the method. @type name: native L{str} @param self: The object that the method is bound to. @type self: any object @return: a bound method @rtype: L{types.MethodType} )__dict__rb _MethodType)clsnamerYfuncrrr_constructMethods  r)Version)deprecatedModuleAttribute) OrderedDictZTwistedrz$Use collections.OrderedDict instead.ztwisted.python.compatr) encodebytes) decodebytes) encodestring) decodestringcCstrt|gSt|SdS)z Like L{chr} but always works on ASCII, returning L{bytes}. @param i: The ASCII code point to return. @type i: L{int} @rtype: L{bytes} N)rbrlchrrrrr _bytesChrs  r)interncCs2t|tr*trtd|fq.|dSn|SdS)az Coerce ASCII-only byte strings into unicode for Python 2. In Python 2 C{unicode(b'bytes')} returns a unicode string C{'bytes'}. In Python 3, the equivalent C{str(b'bytes')} will return C{"b'bytes'"} instead. This function mimics the behavior for Python 2. It will decode the byte string as ASCII. In Python 3 it simply raises a L{TypeError} when passing a byte string. Unicode strings are returned as-is. @param s: The string to coerce. @type s: L{bytes} or L{unicode} @raise UnicodeError: The input L{bytes} is not ASCII decodable. @raise TypeError: The input is L{bytes} on Python 3. zExpected str not %r (bytes)ruN)rjrlrbrvrwryrrr_coercedUnicodes   rcCs4t|tstd|ftr$t|Sdt|SdS)a Provide a repr for a byte string that begins with 'b' on both Python 2 and 3. @param bytestring: The string to repr. @type bytestring: L{bytes} @raise TypeError: The input is not L{bytes}. @return: The repr with a leading 'b'. @rtype: L{bytes} zExpected bytes not %rrSN)rjrlrvrbrepr)Z bytestringrrr _bytesRepr$s r)SequencecKsBd|krtjdtdd|dkr2d|kr2|d}|r:tt|S)a Provide a backwards-compatible way to get async param value that does not cause a syntax error under Python 3.7. @param isAsync: isAsync param value (should default to None) @type isAsync: L{bool} @param kwargs: keyword arguments of the caller (only async is allowed) @type kwargs: L{dict} @raise TypeError: Both isAsync and async specified. @return: Final isAsync param value @rtype: L{bool} asyncz:'async' keyword argument is deprecated, please use isAsyncr#) stacklevelN)warningswarnDeprecationWarningpoprvbool)ZisAsynckwargsrrr_get_async_paramFs r frozensetrFsetNativeStringIOrk StringTyperrrxrange urllib_parserurlquote urlunquote cookielib_b64encodebytes_b64decodebytesrunichr raw_input _tokenizer)r)N)rN)N)v__doc__Z __future__rrrr platformr1r:systokenizetypesrrriorr version_inforbZ _PY35PLUSZ _PY37PLUSZpython_implementationZ_PYPYrZ_EXPECT_NEWSTYLErrArErAttributeError NameErrorr<rZadictrr functoolsrF ImportErrorrQrTrdrrkr,Zlongrtr{r}rrHrirrrrrrbufferrqrrrrZ urllib.parseparserZhtmlrrrrrZhttprrZurlparseZcgiZurllibrrrrrzipr itertoolsrrrZ incrementalrZtwisted.python.deprecater collectionsrbase64rrrrrrrrrrrinputrrrgenerate_tokensZcollections.abcrr__all__rrrrs       ?#     A =