ϪfFdZddlZddlZddlZddlZddlmZddlmZm Z m Z m Z m Z m Z mZmZmZmZmZmZmZddlmZddlmZddlmZmZGdd eZGd d eZGd d eZGddeZe GddZ GddeZ!GddeZ"GddeZ#GddeZ$GddeZ%GddeZ&GddeZ'Gd d!eZ(Gd"d#eZ)y)$z% Tests for L{twisted.python.compat}. N)skipIf) _PYPY_get_async_param bytesEnvironcmp comparableexecfile intToBytesioType iterbytes lazyByteSlice nativeString networkStringreraise)FilePath)platform)SynchronousTestCaseTestCasec:eZdZdZdZdZdZdZdZdZ dZ y ) IOTypeTestsz? Test cases for determining a file-like object's type. cf|jttjty)z= An L{io.StringIO} accepts and returns text. N) assertEqualr ioStringIOstrselfs :/usr/lib/python3/dist-packages/twisted/test/test_compat.pytest_3StringIOzIOTypeTests.test_3StringIO)s  .4cf|jttjty)z= An L{io.BytesIO} accepts and returns bytes. N)rr rBytesIObytesrs r test_3BytesIOzIOTypeTests.test_3BytesIO/s  -u5r ct|jd5}|jt|tdddy#1swYyxYw)zT A file opened via 'io.open' in text mode accepts and returns text. wN)openmktemprr rrfs rtest_3openTextModezIOTypeTests.test_3openTextMode5s?$++- % -   VAY , - - - AA ct|jd5}|jt|tdddy#1swYyxYw)zW A file opened via 'io.open' in binary mode accepts and returns bytes. wbN)r'r(rr r#r)s rtest_3openBinaryModez IOTypeTests.test_3openBinaryMode<s?$++- & /!   VAY . / / /r,ctj|jd5}|jt |t dddy#1swYyxYw)z The L{codecs} module, oddly, returns a file-like object which returns bytes when not passed an 'encoding' argument. r.N)codecsr'r(rr r#r)s rtest_codecsOpenBytesz IOTypeTests.test_codecsOpenBytesCsC [[ - /   VAY . / / /s AActj|jdd5}|jt |t dddy#1swYyxYw)zY When passed an encoding, however, the L{codecs} module returns unicode. r.utf-8)encodingN)r1r'r(rr rr)s rtest_codecsOpenTextzIOTypeTests.test_codecsOpenTextKsE[[w ? -1   VAY , - - -s AAcR|jttty)zy When passed an object about which no sensible decision can be made, err on the side of unicode. N)rr objectrrs rtest_defaultToTextzIOTypeTests.test_defaultToTextRs )3/r N) __name__ __module__ __qualname____doc__rr$r+r/r2r6r9r rrr$s*5 6 -//-0r rceZdZdZdZdZy) CompatTestszy Various utility functions in C{twisted.python.compat} provide same functionality as modern Python variants. ct}|jd|jd|jdt|}|j|j |gd|j dt|}|j|j |ddg|j dddh}|j|}t|}|j|j |gdy ) zG L{set} should behave like the expected set interface. bca)rDrBrCdrs)rDrCrFrGN)setaddlistsortrremovediscardunionrrDrBrEs rtest_setzCompatTests.test_set`s E c  c  c G  O,  G  S#J' # #J GGAJ G  01r c*tddg}|jtt|d|j t |ddgtddg}|j |}t|}|j|j |gdy)zS L{frozenset} should behave like the expected frozenset interface. rDrBrIrFrG)rDrBrFrGN) frozenset assertRaisesAttributeErrorgetattrrsortedrNrJrKrOs rtest_frozensetzCompatTests.test_frozensetxs| sCj ! .'1e< S#J/ sCj ! GGAJ G  01r N)r:r;r<r=rPrWr>r rr@r@Zs 20 2r r@c(eZdZdZdZdZdZdZy)ExecfileCompatTestszE Tests for the Python 3-friendly L{execfile} implementation. c|j}t|d5}|j|jddddt |jdS#1swY#xYw)zo Write L{content} to a new temporary file, returning the L{FilePath} for the new file. r.asciiNr4)r(r'writeencoder)rcontentpathr*s r writeScriptzExecfileCompatTests.writeScriptsY {{} $  - GGGNN7+ , - G,-- - -s !A  A)c|jd}ddi}t|j||jd|dy)zX L{execfile} executes the specified file in the given global namespace. foo += 1 fooNr`r r_r)rscriptglobalNamespaces rtest_execfileGlobalsz(ExecfileCompatTests.test_execfileGlobalss@!!,/ !*o. OE23r c|jd}ddi}ddi}t|j|||jd|d|jd|dy)zk L{execfile} executes the specified file in the given global and local namespaces. rbrc Nrf)rrgrhlocalNamespaces rtest_execfileGlobalsAndLocalsz1ExecfileCompatTests.test_execfileGlobalsAndLocalss` !!,/ "+o~> _U34 ^E23r cdD]E}|jd|z}ddi}t|j||jd|dGy)z L{execfile} reads in the specified file using universal newlines so that scripts written on one platform will work on another. )  z z foo = 'okay'rcNokayrf)r lineEndingrgrhs rtest_execfileUniversalNewlinesz2ExecfileCompatTests.test_execfileUniversalNewlinessT - =J%%nz&ABF$dmO V[[/ 2   V_U%; <  =r N)r:r;r<r=r`rirorur>r rrYrYs.4 4 =r rYceZdZdZdZy)PYPYTestz! Identification of PyPy. c~dtjvr|jty|j ty)z, On PyPy, L{_PYPY} is True. PyPyN)sysversion assertTruer assertFalsers r test_PYPYzPYPYTest.test_PYPYs* S[[ OOE "   U #r N)r:r;r<r=r~r>r rrwrws $r rwceZdZdZdZdZy) ComparablezE Objects that can be compared to each other, but not others. c||_yN)value)rrs r__init__zComparable.__init__s  r cnt|tstSt|j|jSr) isinstancerNotImplementedrr)rothers r__cmp__zComparable.__cmp__s'%,! !4::u{{++r N)r:r;r<r=rrr>r rrrs,r rc4eZdZdZdZdZdZdZdZdZ y) ComparableTestszR L{comparable} decorated classes emulate Python 2's C{__cmp__} semantics. c|jtdtdk(|jtdtdk(y)zo Instances of a class that is decorated by C{comparable} support equality comparisons. rdreNr|rr}rs r test_equalityzComparableTests.test_equalitys8  1 A67 A*Q-78r c|jtdtdk7|jtdtdk7y)zq Instances of a class that is decorated by C{comparable} support inequality comparisons. rdreN)r}rr|rs rtest_nonEqualityz ComparableTests.test_nonEqualitys8 A*Q-78  1 A67r c|jtdtdkD|jtdtdkDy)zs Instances of a class that is decorated by C{comparable} support greater-than comparisons. rerdrNrrs rtest_greaterThanz ComparableTests.test_greaterThan8  1 1 56 AA67r c|jtdtdk\|jtdtdk\|jtdtdk\y)z| Instances of a class that is decorated by C{comparable} support greater-than-or-equal comparisons. rdrerrNrrs rtest_greaterThanOrEqualz'ComparableTests.test_greaterThanOrEqualQ  1 A67  1 A67 A*Q-78r c|jtdtdk|jtdtdky)zp Instances of a class that is decorated by C{comparable} support less-than comparisons. rrreNrrs r test_lessThanzComparableTests.test_lessThanrr c|jtdtdk|jtdtdk|jtdtdky)zy Instances of a class that is decorated by C{comparable} support less-than-or-equal comparisons. rrreNrrs rtest_lessThanOrEqualz$ComparableTests.test_lessThanOrEqualrr N) r:r;r<r=rrrrrrr>r rrrs%988989r rc4eZdZdZdZdZdZdZdZdZ y) Python3ComparableTestsz; Python 3-specific functionality of C{comparable}. cp|jtdjtty)z Instances of a class that is decorated by C{comparable} support returning C{NotImplemented} from C{__eq__} if it is returned by the underlying C{__cmp__} call. rdN)rr__eq__r8rrs rtest_notImplementedEqualsz0Python3ComparableTests.test_notImplementedEquals% A--fh7Hr cp|jtdjtty)z Instances of a class that is decorated by C{comparable} support returning C{NotImplemented} from C{__ne__} if it is returned by the underlying C{__cmp__} call. rdN)rr__ne__r8rrs rtest_notImplementedNotEqualsz3Python3ComparableTests.test_notImplementedNotEqualsrr cp|jtdjtty)z Instances of a class that is decorated by C{comparable} support returning C{NotImplemented} from C{__gt__} if it is returned by the underlying C{__cmp__} call. rdN)rr__gt__r8rrs rtest_notImplementedGreaterThanz5Python3ComparableTests.test_notImplementedGreaterThan$rr cp|jtdjtty)z Instances of a class that is decorated by C{comparable} support returning C{NotImplemented} from C{__lt__} if it is returned by the underlying C{__cmp__} call. rdN)rr__lt__r8rrs rtest_notImplementedLessThanz2Python3ComparableTests.test_notImplementedLessThan,rr cp|jtdjtty)z Instances of a class that is decorated by C{comparable} support returning C{NotImplemented} from C{__ge__} if it is returned by the underlying C{__cmp__} call. rdN)rr__ge__r8rrs r$test_notImplementedGreaterThanEqualsz;Python3ComparableTests.test_notImplementedGreaterThanEquals4rr cp|jtdjtty)z Instances of a class that is decorated by C{comparable} support returning C{NotImplemented} from C{__le__} if it is returned by the underlying C{__cmp__} call. rdN)rr__le__r8rrs r!test_notImplementedLessThanEqualsz8Python3ComparableTests.test_notImplementedLessThanEquals<rr N) r:r;r<r=rrrrrrr>r rrrs+IIIIIIr rc"eZdZdZdZdZdZy)CmpTestszA L{cmp} should behave like the built-in Python 2 C{cmp}. c|jtddd|jtddd|jtdgdgdy)z5 L{cmp} returns 0 for equal objects. rDrrdNrrrs r test_equalszCmpTests.test_equalsJsJ S#* QA& aS1#*r ct|jtddd|jtdddy)zS L{cmp} returns 1 if its first argument is bigger than its second. rrdzaNrrs rrzCmpTests.test_greaterThanRs0 QA& T4!,r ct|jtddd|jtdddy)zU L{cmp} returns -1 if its first argument is smaller than its second. g?gffffff@rdNrrs rrzCmpTests.test_lessThanYs0 S#+ T4"-r N)r:r;r<r=rrrr>r rrrEs+-.r rc:eZdZdZdZdZdZdZdZdZ dZ y ) StringTestsz8 Compatibility functions and types for strings. cx|jt|||jt|ty)z Raise an exception indicating a failed test if the output of C{nativeString(original)} is unequal to the expected string, or is not a native string. N)rrassertIsInstancer)roriginalexpecteds rassertNativeStringzStringTests.assertNativeStringfs. h/: l84c:r c:|jttdy)zj C{nativeString} raises a C{UnicodeError} if input bytes are not ASCII decodable. NrS UnicodeErrorrrs rtest_nonASCIIBytesToStringz&StringTests.test_nonASCIIBytesToStringos , g>r c:|jttdy)zk C{nativeString} raises a C{UnicodeError} if input Unicode is not ASCII encodable. uሴNrrs rtest_nonASCIIUnicodeToStringz(StringTests.test_nonASCIIUnicodeToStringvs , h?r c(|jddy)z C{nativeString} converts bytes to the native string format, assuming an ASCII encoding if applicable. shellohelloNrrs rtest_bytesToStringzStringTests.test_bytesToString}s '2r c(|jddy)z C{nativeString} converts unicode to the native string format, assuming an ASCII encoding if applicable. zGood dayNrrs rtest_unicodeToStringz StringTests.test_unicodeToStrings  J7r c(|jddy)zJ C{nativeString} leaves native strings as native strings. zHello!Nrrs rtest_stringToStringzStringTests.test_stringToStrings (3r c:|jttdy)zu C{nativeString} raises a C{TypeError} if given an object that is not a string of some sort. rdN)rS TypeErrorrrs rtest_unexpectedTypezStringTests.test_unexpectedTypes )\15r N) r:r;r<r=rrrrrrrr>r rrras+;?@384 6r rc"eZdZdZdZdZdZy)NetworkStringTestsz% Tests for L{networkString}. c:|jdtdy)zu L{networkString} returns a C{unicode} object passed to it encoded into a C{bytes} instance. sfoorcN)rrrs rtest_strzNetworkStringTests.test_strs u!56r c:|jttdy)z L{networkString} raises L{UnicodeError} if passed a C{unicode} instance containing characters not encodable in ASCII. u☃N)rSrrrs rtest_unicodeOutOfRangez)NetworkStringTests.test_unicodeOutOfRanges , }Er c|jttt|jttdy)z L{networkString} raises L{TypeError} if passed a non-string object or the wrong type of string object. sbytesN)rSrrr8rs rtest_nonStringz!NetworkStringTests.test_nonStrings* )]FH= )]H=r N)r:r;r<r=rrrr>r rrrs7F>r rceZdZdZdZdZy) ReraiseTestszH L{reraise} re-raises exceptions on both Python 2 and Python 3. c ddz  td|j dy#t$rtj\}}}YBwxYw#t$rtj\}}}|j |t |j||jtjdtj|dYywxYw)z Calling L{reraise} with an exception instance and a traceback of L{None} re-raises it with a new traceback. rdrNThe exception was not raised.r) BaseExceptionrzexc_inforfailrZeroDivisionErrorassertIsassertNotEqual traceback format_tbrtyprtbtyp2value2tb2s rtest_reraiseWithNonez!ReraiseTests.test_reraiseWithNones  , E 7 E4  II5 6 , \\^NC ,  #  D&#   T#4 5 MM% (   ##B'+Y-@-@-Eb-I   !& A !A  A  BCCc ddz  t|j dy#t$rtj\}}}YBwxYw#t$rtj\}}}|j |t |j||j tjdtj|dYywxYw)z Calling L{reraise} with an exception instance and a traceback re-raises the exception with the given traceback. rdrrrN) rrzrrrrrrrrrs rtest_reraiseWithTracebackz&ReraiseTests.test_reraiseWithTracebacks  , E 7 E2  II5 6 , \\^NC , X #  D&#   T#4 5 MM% (   Y004R8):M:Mc:RSU:V W  XrN)r:r;r<r=rrr>r rrrs7*7r rc.eZdZdZdZdZdZdZdZy)Python3BytesTestszB Tests for L{iterbytes}, L{intToBytes}, L{lazyByteSlice}. cXd}tt|}|j|gdy)z When L{iterbytes} is called with a bytestring, the returned object can be iterated over, resulting in the individual bytes of the bytestring. sabcd)rbcrN)rJr r)rinputresults rtest_iterationz Python3BytesTests.test_iterations) i&' !9:r c:|jtddy)z When L{intToBytes} is called with an integer, the result is an ASCII-encoded string representation of the number. s213N)rr rs rtest_intToBytesz!Python3BytesTests.test_intToBytess C&1r cPd}|jtt||y)zv L{lazyByteSlice} called with some bytes returns a semantically equal version of these bytes. 123XYZNrr#r rdatas rtest_lazyByteSliceNoOffsetz,Python3BytesTests.test_lazyByteSliceNoOffsets#  }T23T:r cXd}|jtt|d|ddy)z L{lazyByteSlice} called with some bytes and an offset returns a semantically equal version of these bytes starting at the given offset. rreNrrs rtest_lazyByteSliceOffsetz*Python3BytesTests.test_lazyByteSliceOffsets+  }T156QRAr c Zd}|jtt|dd|ddy)z L{lazyByteSlice} called with some bytes, an offset and a length returns a semantically equal version of these bytes starting at the given offset, up to the given length. rrerNrrs r!test_lazyByteSliceOffsetAndLengthz3Python3BytesTests.test_lazyByteSliceOffsetAndLength s-  }T1a894!9Er N) r:r;r<r=rrrrr r>r rrrs";2;BFr rcLeZdZdZeej ddZy)BytesEnvironTestsz$ Tests for L{BytesEnviron}. z+Environment vars are always str on Windows.ct}t}|jD]9\}}|jt ||jt |;|j t |tgy)zz The output of L{BytesEnviron} should always be a L{dict} with L{bytes} values and L{bytes} keys. N)rrHitemsrItyperrJr#)rrtypeskeyvals rtest_alwaysBytesz"BytesEnvironTests.test_alwaysBytessc   !HC IId3i IId3i  ! eug.r N)r:r;r<r=rr isWindowsrr>r rr r s0 H   "OP /Q /r r ceZdZdZdZdZy)GetAsyncParamTestsz= Tests for L{twisted.python.compat._get_async_param} c,|jtdd|jtdd|jtdddiddid|jtdddiddid|jttdddiy)z L{twisted.python.compat._get_async_param} uses isAsync by default, or deprecated async keyword argument if isAsync is None. F)isAsyncTrNasyncr>)rrrSrrs rtest_get_async_paramz'GetAsyncParamTests.test_get_async_param-s )%8%@ )$7> )K$K7E:JKUS )J$J7D/JDQ )%5uw>NOr c|jtd ddiddid|j|jg}|j|dddy) z L{twisted.python.compat._get_async_param} raises a deprecation warning if async keyword argument is passed. rNrF)offendingFunctionsrmessagez:'async' keyword argument is deprecated, please use isAsyncr>)rr flushWarnings test_get_async_param_deprecation)rcurrentWarningss rrz3GetAsyncParamTests.test_get_async_param_deprecation8sh )K$K7E:JKUS,, $ E EF-   A y ) H r N)r:r;r<r=rrr>r rrr(s P  r r)*r=r1rrzrunittestrtwisted.python.compatrrrrrr r r r r rrrtwisted.python.filepathrtwisted.python.runtimertwisted.trial.unittestrrrr@rYrwrrrrrrrrr rr>r rr%s   -+@30%30l*2%*2Z-=--=` $" $  , ,  ,79)79t3I03Il.".856%56p>,>8+7&+7\-F+-F`//* , r