o b#B@sdZddlZddlZddlZddlZddlZddlmZddl Z ddl m Z ddl m Z ddlmZddlmZddlmZmZmZddlmZdd lmZmZdd lmZmZm Z dd lm!Z"m#Z$dd l%m&Z&dd l'm(Z(m)Z)ej*dkrxdZ+ndZ+e,dkrdZ-ndZ-eZ.e)e&ddddde/de0Z0e)e&ddddde/de1Z2e)e&ddddde/de3Z4e)e&ddddde/de5Z6e)e&ddddde/de7Z7e)e&ddddde/d e8Z9e)e&ddddd!e/d"e:Z:e)e&ddddd#e/d$e;Ze)e&ddddd'e/d(e;Z?e)e&ddddd%e/d)e7Z@e)e&ddddde/d*e(e&ddddd+d,d-d.ZAe(e&ddddd/d,d0d1ZBe(e&ddddd2d,d3d4ZCdqd5e5d6efd7d8ZDdrd9d:ZEd;e1dZFd?d@ZGe;fdAdBZHdCed6e;fdDdEZIdFdGZJe(e&dddddHd,dIdJZKdKdLZLe(e&dddddMd,dNe5d6eMfdOdPZNdsdQdRZOdCe;d6eMfdSdTZPe(e&dddddUd,dVdWZQdXdYZRdrdZd[ZSd\d]ZTeTe)e&ddddd^e/d_e)e&ddddd`e/dae)e&dddddbe/dce)e&dddddde/dee)e&dddddfe/dge)e&dddddhe/die)e&dddddje/dke)e&dddddle/dme)e&dddddne/dogdpZUdS)ta) 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) N)Sequence)reduce)escape) cookiejar)IOBaseStringIO TextIOBase)intern) FrameType MethodType)AnyAnyStrcast)quoteunquote)Version) deprecateddeprecatedModuleAttribute)rTFPyPyTwistedzObsolete alias for io.IOBaseFileTypez)Obsolete alias for frozenset builtin type frozensetz)Old-style classes don't exist in Python 3 InstanceTypez Obsolete alias for zip() builtinizipz#Obsolete alias for int builtin typelongz"Obsolete alias for range() builtinrangez"Obsolete alias for input() builtin raw_inputz#Obsolete alias for set builtin typesetz#Obsolete alias for str builtin type StringTypez Obsolete alias for chr() builtinunichrunicodexrangez d.items()) replacementcC|S)z] Return an iterable of the items of C{d}. @type d: L{dict} @rtype: iterable )itemsdr+7/usr/lib/python3/dist-packages/twisted/python/compat.py iteritemsr-z d.values()cCr')z^ Return an iterable of the values of C{d}. @type d: L{dict} @rtype: iterable )valuesr)r+r+r, itervaluesr.r0zlist(d.items())cCs t|S)zW Return a list of the items of C{d}. @type d: L{dict} @rtype: L{list} )listr(r)r+r+r,r(s r(nreturncCs<t}t|dD] }|dusJ|j}q |dusJ|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. @return: a frame, n levels up the stack from the caller. N)inspect currentframerf_back)r2fxr+r+r,r6s    r6cCsZ|dur|}t|d }|}Wdn1swYt||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)openreadcompiler;)filenameglobalslocalsfinsourcecoder+r+r,execfiles    rEabcCs||krdS||kr dSdS)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}. rr4r+)rFrGr+r+r,cmps rIcCsdtdtdtfdd}dtdtdtfdd}dtdtdtfdd }dtdtdtfd d }dtdtdtfd d }dtdtdtfdd}||_||_||_||_||_||_|S)z Class decorator that ensures support for the special C{__cmp__} method. C{__eq__}, C{__lt__}, etc. methods are added to the class, relying on C{__cmp__} to implement their comparisons. selfotherr3cSs$tt||}|tur|S|dkSNrrbool__cmp__NotImplementedrJrKcr+r+r,__eq__zcomparable..__eq__cSs$tt||}|tur|S|dkSrLrMrQr+r+r,__ne__rTzcomparable..__ne__cSs$tt||}|tur|S|dkSrLrMrQr+r+r,__lt__rTzcomparable..__lt__cSs$tt||}|tur|S|dkSrLrMrQr+r+r,__le__ rTzcomparable..__le__cSs$tt||}|tur|S|dkSrLrMrQr+r+r,__gt__rTzcomparable..__gt__cSs$tt||}|tur|S|dkSrLrMrQr+r+r,__ge__rTzcomparable..__ge__) r objectrNrVrXrWrYrSrU)klassrSrUrVrWrXrYr+r+r, comparablesr\cCsRt|trtSt|trtSt|dd}ddl}t||j|jfr'|r%tStS|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{str}, if the file is unambiguously opened in text mode. 2. L{bytes}, if the file is unambiguously opened in binary mode. 3. The C{default} parameter, if the given type is not understood. @rtype: L{type} encodingNr) isinstancerstrrbytesgetattrcodecs StreamReader StreamWriter) fileIshObjectdefaultr]rbr+r+r,ioType%s   rgscCs<t|ttfs td|t|tr|dS|d|S)z Convert C{bytes} or C{str} to 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{str}. z%r is neither bytes nor strascii)r^r`r_ TypeErrordecodeencoderhr+r+r, nativeStringPs     rncCs8t|tr |d}n|d}t|t|kr|S|S)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{str}) 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{str} or L{bytes} @param inputString: A byte string or text string provided by the user. @type inputString: L{str} or L{bytes} @return: C{constantString} converted into the same type as C{inputString} @rtype: the type of C{inputString} ri)r^r`rkrltype)constantString inputString otherTyper+r+r,_matchingStringbs   rsz)raise exception.with_traceback(traceback)cCs ||)a" Re-raise an exception, with an optional traceback. 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. )with_traceback) exception tracebackr+r+r,reraises rwccs*tt|D] }|||dVqdS)a5 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. r4N)rlen) originalBytesir+r+r, iterbytess r{zb'%d'rzcCs d|fS)z Convert the given integer into C{bytes}, as ASCII-encoded Arab numeral. @param i: The C{int} to convert to C{bytes}. @rtype: C{bytes} s%dr+)rzr+r+r, intToBytess r|cCs,t|}|dur||dS||||S)aq 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)rZoffsetsizeviewr+r+r, lazyByteSlices rcCst|ts td|dS)a Convert a string to L{bytes} using ASCII encoding. This is useful for sending text-like bytes that are constructed using string interpolation. For example:: networkString("Hello %d" % (n,)) @param s: A string to convert to bytes. @type s: L{str} @raise UnicodeError: The input string is not ASCII encodable. @raise TypeError: The input is not L{str}. @rtype: L{bytes} z!Can only convert strings to bytesri)r^r_rjrlrmr+r+r, networkStrings  rz os.environbcs*tjjtjjfddtjDS)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. csi|] \}}||qSr+r+).0r9y encodekey encodevaluer+r, sz bytesEnviron..)osenvironrrr(r+r+rr, bytesEnvirons rcCs|j|}t||S)aN Construct a bound method. @param cls: The class that the method should be bound to. @type cls: 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{_MethodType} )__dict__ _MethodType)clsnamerJfuncr+r+r,_constructMethods  rcKsBd|vr tjdtdd|durd|vr|d}|rtt|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)warningswarnDeprecationWarningpoprjrN)isAsynckwargsr+r+r,_get_async_params rcsjz ddlmmmWn tyYdSwtsdSdfdd }tjj|jkr0dS|t_dS)z Work around U{https://foss.heptapod.net/pypy/pypy/-/issues/3051} by replacing C{socket.fromfd} with a more conservative version. r)F_GETFLF_SETFLfcntlNc sJ|g|du}|}z|||g|RW||S||wNr+)fdfamilyroproto passprotoflagsrrr realFromFDr+r,fromFDWithoutModifyingFlags#s  z7_pypy3BlockingHack..fromFDWithoutModifyingFlagsr)rrr ImportError_PYPYsocketfromfd__name__)rr+rr,_pypy3BlockingHacks   rzUse functools.reduce() directlyrzUse io.StringIO directlyNativeStringIOzImport urllib.parse directly urllib_parsezUse html.escape directlyrz!Use urllib.parse.quote() directlyurlquotez#Use urllib.parse.unquote() directly urlunquotezUse http.cookiejar directly cookielibzUse sys.intern() directlyr z%Use collections.abc.Sequence directlyr)!rwrErrr!rIr\rnrrr$r{r|rr"rrr(r-r0rr%rrrrrrr r#r rr)rr)rN)V__doc__r5rplatformrsys urllib.parseparserrcollections.abcr functoolsrhtmlrhttprriorrrrr typesr r rtypingr r rrrrr incrementalrtwisted.python.deprecaterr version_info _PY37PLUSpython_implementationrrrrrZrziprintrrinputr r!r_r"chrr#r$r%r-r0r(r6rErIr\rgrnrsrwr{r`r|rrrrrr__all__r+r+r+r,sv                          5+