6Qc@`s-dZddlmZmZddlZddlZddlZddlmZddl m Z m Z m Z ddl m Z mZmZmZddl mZmZddl mZmZmZmZdd lmZd ejfd YZd ejfd YZe rde_ndejfdYZdejfdYZe de fdYZ!dejfdYZ"dejfdYZ#e sde#_ndejfdYZ$dejfdYZ%dejfdYZ&d ejfd!YZ'd"ejfd#YZ(dS($s% Tests for L{twisted.python.compat}. i(tdivisiontabsolute_importN(tunittest(treducetexecfilet_PY3(t comparabletcmpt nativeStringt networkString(tunicodet lazyByteSlice(treraisetNativeStringIOt iterbytest intToBytes(tFilePathtCompatTestCasecB`s)eZdZdZdZdZRS(sy Various utility functions in C{twisted.python.compat} provide same functionality as modern Python variants. cC`st}|jd|jd|jdt|}|j|j|dddg|jdt|}|j|j|ddg|jdtddg}|j|}t|}|j|j|ddddgdS(sG L{set} should behave like the expected set interface. tbtctatdtrtsN(tsettaddtlisttsortt assertEqualtremovetdiscardtunion(tselfRRR((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_sets"            cC`stddg}|jtt|d|jt|ddgtddg}|j|}t|}|j|j|ddddgdS(sS L{frozenset} should behave like the expected frozenset interface. RRRRRN( t frozensett assertRaisestAttributeErrortgetattrRtsortedRRR(R RRR((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_frozenset5s  c C`s]|jdtddddddg|jdtd dddddgdd S( sB L{reduce} should behave like the builtin reduce. icS`s||S(N((txty((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pytHsiiiiiicS`s||S(N((R(R)((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyR*IsN(RR(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyt test_reduceDs+(t__name__t __module__t__doc__R!R'R+(((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyRs  t IPv6TestscB`s eZdZdZdZRS(sE C{inet_pton} and C{inet_ntop} implementations support IPv6. c`sddlmfd}fd}|jd|d|jd|d|jd |d |jd |d|jd|d|jd|d|jd|d d|jd|d dd dS(Ni(t inet_ntopc`stj|S(N(tsockettAF_INET6(R(R0(s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyR*Usc`stj|S(N(R1tAF_INET(R(R0(s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyR*Vss::tis::1iss!aef:b01:506:1001:ffff:9997:55:170s Ups1.0.1.0ss 170.85.170.85sUUs255.255.255.255ss100::s100::1itttR6t(ttwisted.python.compatR0R(R tftg((R0s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttestNToPRs c `sWddlmfd}fd}|jd|d|jd|d|jd |d |jd%|d |jd&|d|jdd'|d|jd|d|jd)|d|jd+|d|jd|dxBddddddd d!d"d#d$g D]}|jt||q6WdS(,Ni(t inet_ptonc`stj|S(N(R1R2(R(R=(s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyR*hsc`stj|S(N(R1R3(R(R=(s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyR*ists0.0.0.0ss 255.0.255.0ss170.170.170.170R4is::s0::0tis1::sEvV $s$45ef:76cb:1a:56ef:afeb:bac:1924:aeaes::1i ss ::1.2.3.4ts1:2:3:4:5:6:1.2.3.255s1:2:3:4:5:6:7:8:s:1:2:3:4:5:6:7:8s1::2::3s1:::3s:::s1:2s::1.2s 1.2.3.4::s%abcd:1.2.3.4:abcd:abcd:abcd:abcd:abcds*1234:1.2.3.4:1234:1234:1234:1234:1234:1234s1.2.3.4R5R5R8R8R7t t(R9R=RR#t ValueError(R R:R;tbadaddr((R=s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttestPToNes.    (R,R-R.R<RE(((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyR/Ms s7These tests are only relevant to old versions of PythontExecfileCompatTestCasecB`s2eZdZdZdZdZdZRS(sE Tests for the Python 3-friendly L{execfile} implementation. cC`sM|j}t|d}|j|jdWdQXt|jdS(so Write L{content} to a new temporary file, returning the L{FilePath} for the new file. twbtasciiNsutf-8(tmktemptopentwritetencodeR(R tcontenttpathR:((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyt writeScripts cC`sD|jd}idd6}t|j||jd|ddS(sX L{execfile} executes the specified file in the given global namespace. u foo += 1 itfooiN(RORRNR(R tscripttglobalNamespace((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_execfileGlobalss cC`sh|jd}idd6}idd6}t|j|||jd|d|jd|ddS(sk L{execfile} executes the specified file in the given global and local namespaces. u foo += 1 i RPiiN(RORRNR(R RQRRtlocalNamespace((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_execfileGlobalsAndLocalss   cC`sYxRdD]J}|jd|}idd6}t|j||jd|dqWdS( s L{execfile} reads in the specified file using universal newlines so that scripts written on one platform will work on another. u u u u foo = 'okay'RPtokayN(u u u (ROtNoneRRNR(R t lineEndingRQRR((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_execfileUniversalNewliness   (R,R-R.RORSRURY(((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyRFs  tPY3TestscB`s eZdZdZdZRS(s2 Identification of Python 2 vs. Python 3. cC`s&tjjdr"|jtndS(s0 On Python 2, C{_PY3} is False. s2.N(tsystversiont startswitht assertFalseR(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyt test_python2scC`s&tjjdr"|jtndS(s/ On Python 3, C{_PY3} is True. s3.N(R[R\R]t assertTrueR(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyt test_python3s(R,R-R.R_Ra(((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyRZs t ComparablecB`s eZdZdZdZRS(sE Objects that can be compared to each other, but not others. cC`s ||_dS(N(tvalue(R Rc((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyt__init__scC`s&t|tstSt|j|jS(N(t isinstanceRbtNotImplementedRRc(R tother((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyt__cmp__s(R,R-R.RdRh(((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyRbs tComparableTestscB`sDeZdZdZdZdZdZdZdZRS(sR L{comparable} decorated classes emulate Python 2's C{__cmp__} semantics. cC`sB|jtdtdk|jtdtdkdS(so Instances of a class that is decorated by C{comparable} support equality comparisons. iiN(R`RbR^(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyt test_equalityscC`sB|jtdtdk|jtdtdkdS(sq Instances of a class that is decorated by C{comparable} support inequality comparisons. iiN(R^RbR`(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_nonEqualityscC`sB|jtdtdk|jtdtdkdS(ss Instances of a class that is decorated by C{comparable} support greater-than comparisons. iiiiN(R`RbR^(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_greaterThanscC`sa|jtdtdk|jtdtdk|jtdtdkdS(s| Instances of a class that is decorated by C{comparable} support greater-than-or-equal comparisons. iiiiN(R`RbR^(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_greaterThanOrEqualscC`sB|jtdtdk|jtdtdkdS(sp Instances of a class that is decorated by C{comparable} support less-than comparisons. iiiN(R`RbR^(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyt test_lessThanscC`sa|jtdtdk|jtdtdk|jtdtdkdS(sy Instances of a class that is decorated by C{comparable} support less-than-or-equal comparisons. iiiN(R`RbR^(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_lessThanOrEquals( R,R-R.RjRkRlRmRnRo(((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyRis tPython3ComparableTestscB`sDeZdZdZdZdZdZdZdZRS(s; Python 3-specific functionality of C{comparable}. cC`s&|jtdjttdS(s 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. iN(RRbt__eq__tobjectRf(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_notImplementedEquals'scC`s&|jtdjttdS(s 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. iN(RRbt__ne__RrRf(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_notImplementedNotEquals0scC`s&|jtdjttdS(s 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. iN(RRbt__gt__RrRf(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_notImplementedGreaterThan9scC`s&|jtdjttdS(s 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. iN(RRbt__lt__RrRf(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_notImplementedLessThanBscC`s&|jtdjttdS(s 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. iN(RRbt__ge__RrRf(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyt$test_notImplementedGreaterThanEqualsKscC`s&|jtdjttdS(s 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. iN(RRbt__le__RrRf(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyt!test_notImplementedLessThanEqualsTs( R,R-R.RsRuRwRyR{R}(((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyRp"s sPython 3 only.tCmpTestscB`s)eZdZdZdZdZRS(sA L{cmp} should behave like the built-in Python 2 C{cmp}. cC`sU|jtddd|jtddd|jtdgdgddS(s5 L{cmp} returns 0 for equal objects. uaiiN(RR(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyt test_equalshscC`s6|jtddd|jtddddS(sS L{cmp} returns 1 if its first argument is bigger than its second. iiitzRN(RR(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyRlqscC`s6|jtddd|jtddddS(sU L{cmp} returns -1 if its first argument is smaller than its second. g?gffffff@iRRN(RR(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyRnys(R,R-R.RRlRn(((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyR~cs t StringTestscB`s_eZdZdZdZdZdZdZdZdZ dZ d Z RS( s8 Compatibility functions and types for strings. cC`s0|jt|||jt|tdS(s 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(RRtassertIsInstancetstr(R toriginaltexpected((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pytassertNativeStringscC`s|jttddS(sj C{nativeString} raises a C{UnicodeError} if input bytes are not ASCII decodable. sN(R#t UnicodeErrorR(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_nonASCIIBytesToStringscC`s|jttddS(sk C{nativeString} raises a C{UnicodeError} if input Unicode is not ASCII encodable. uሴN(R#RR(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_nonASCIIUnicodeToStringscC`s|jdddS(s C{nativeString} converts bytes to the native string format, assuming an ASCII encoding if applicable. thelloN(R(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_bytesToStringscC`s|jdddS(s C{nativeString} converts unicode to the native string format, assuming an ASCII encoding if applicable. uGood daysGood dayN(R(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_unicodeToStringscC`s|jdddS(sJ C{nativeString} leaves native strings as native strings. sHello!N(R(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_stringToStringscC`s|jttddS(su C{nativeString} raises a C{TypeError} if given an object that is not a string of some sort. iN(R#t TypeErrorR(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_unexpectedTypescC`s,trt}nt}|jt|kdS(sR C{compat.unicode} is C{str} on Python 3, C{unicode} on Python 2. N(RRR R`t unicodeCompat(R R((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyt test_unicodes cC`s=t}|jd|jd|j|jddS(sg L{NativeStringIO} is a file-like object that stores native strings in memory. Rs theres hello thereN(R RKRtgetvalue(R R:((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_nativeStringIOs   ( R,R-R.RRRRRRRRR(((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyRs       tNetworkStringTestscB`smeZdZdZdZer7de_e_ndZdZesbde_e_ndZ RS(s% Tests for L{networkString}. cC`s|jdtddS(sU L{networkString} returns a C{bytes} object passed to it unmodified. RPN(RR (R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyt test_bytesscC`s |jttdjddS(s L{networkString} raises C{UnicodeError} if passed a C{bytes} instance containing bytes not used by ASCII. u☃sutf-8N(R#RR RL(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_bytesOutOfRangess:Bytes behavior of networkString only provided on Python 2.cC`s|jdtddS(su L{networkString} returns a C{unicode} object passed to it encoded into a C{bytes} instance. RPufooN(RR (R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyRscC`s|jttddS(s L{networkString} raises L{UnicodeError} if passed a C{unicode} instance containing characters not encodable in ASCII. u☃N(R#RR (R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_unicodeOutOfRangess<Unicode behavior of networkString only provided on Python 3.cC`sI|jttttr2|jttdn|jttddS(s L{networkString} raises L{TypeError} if passed a non-string object or the wrong type of string object. tbytesutextN(R#RR RrR(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_nonStrings( R,R-R.RRRtskipRRR(((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyRs    t ReraiseTestscB`s eZdZdZdZRS(sH L{reraise} re-raises exceptions on both Python 2 and Python 3. cC`sy ddWntj\}}}nXyt|dWnitj\}}}|j|t|j||k|jtj |dtj |dnX|j ddS(s Calling L{reraise} with an exception instance and a traceback of C{None} re-raises it with a new traceback. iiisThe exception was not raised.N( R[texc_infoR RWRtZeroDivisionErrorR`tassertNotEqualt tracebackt format_tbtfail(R ttypRcttbttyp2tvalue2ttb2((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_reraiseWithNones cC`sy ddWntj\}}}nXyt||Wnitj\}}}|j|t|j||k|jtj|dtj|dnX|jddS(s Calling L{reraise} with an exception instance and a traceback re-raises the exception with the given traceback. iiisThe exception was not raised.N( R[RR RRR`RRR(R RRcRRRR((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_reraiseWithTraceback(s (R,R-R.RR(((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyRs tPython3BytesTestscB`s;eZdZdZdZdZdZdZRS(sB Tests for L{iterbytes}, L{intToBytes}, L{lazyByteSlice}. cC`s8d}tt|}|j|ddddgdS(s When L{iterbytes} is called with a bytestring, the returned object can be iterated over, resulting in the individual bytes of the bytestring. tabcdRRRRN(RRR(R tinputtresult((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_iterationCscC`s|jtdddS(s When L{intToBytes} is called with an integer, the result is an ASCII-encoded string representation of the number. it213N(RR(R ((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_intToBytesNscC`s&d}|jtt||dS(sv L{lazyByteSlice} called with some bytes returns a semantically equal version of these bytes. t123XYZN(RRR (R tdata((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_lazyByteSliceNoOffsetVscC`s-d}|jtt|d|ddS(s L{lazyByteSlice} called with some bytes and an offset returns a semantically equal version of these bytes starting at the given offset. RiN(RRR (R R((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyttest_lazyByteSliceOffset_scC`s3d}|jtt|dd|dd!dS(s 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. RiiiN(RRR (R R((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyt!test_lazyByteSliceOffsetAndLengthhs(R,R-R.RRRRR(((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyR>s   ()R.t __future__RRR1R[Rt twisted.trialRR9RRRRRRR R RR R R RRttwisted.python.filepathRtSynchronousTestCaseRR/RRFRZRrRbRiRpR~RRRR(((s</usr/lib/python2.7/dist-packages/twisted/test/test_compat.pyts2$""76 4@: U70