U
    n5{iýA  ã                   @   sf  d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZ ddlmZ ddlmZ ddlmZmZ G dd	„ d	eƒZG d
d„ deƒZG dd„ deƒZG dd„ deƒZeG dd„ dƒƒZG dd„ deƒZ G dd„ deƒZ!G dd„ deƒZ"G dd„ deƒZ#G dd„ deƒZ$G dd„ deƒZ%G dd„ deƒZ&G d d!„ d!eƒZ'dS )"z%
Tests for L{twisted.python.compat}.
é    N)ÚskipIf)Ú_PYPYÚbytesEnvironÚcmpÚ
comparableÚexecfileÚ
intToBytesÚioTypeÚ	iterbytesÚlazyByteSliceÚnativeStringÚnetworkStringÚreraise)ÚFilePath)Úplatform)ÚSynchronousTestCaseÚTestCasec                   @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚIOTypeTestsz?
    Test cases for determining a file-like object's type.
    c                 C   s   |   tt ¡ ƒt¡ dS )z=
        An L{io.StringIO} accepts and returns text.
        N)ÚassertEqualr	   ÚioÚStringIOÚstr©Úself© r   ú</tmp/pip-unpacked-wheel-mnhg7r0q/twisted/test/test_compat.pyÚtest_3StringIO(   s    zIOTypeTests.test_3StringIOc                 C   s   |   tt ¡ ƒt¡ dS )z=
        An L{io.BytesIO} accepts and returns bytes.
        N)r   r	   r   ÚBytesIOÚbytesr   r   r   r   Útest_3BytesIO.   s    zIOTypeTests.test_3BytesIOc              	   C   s.   t |  ¡ dƒ�}|  t|ƒt¡ W 5 Q R X dS )zT
        A file opened via 'io.open' in text mode accepts and returns text.
        ÚwN)ÚopenÚmktempr   r	   r   ©r   Úfr   r   r   Útest_3openTextMode4   s    zIOTypeTests.test_3openTextModec              	   C   s.   t |  ¡ dƒ�}|  t|ƒt¡ W 5 Q R X dS )zW
        A file opened via 'io.open' in binary mode accepts and returns bytes.
        ÚwbN)r!   r"   r   r	   r   r#   r   r   r   Útest_3openBinaryMode;   s    z IOTypeTests.test_3openBinaryModec              	   C   s0   t  |  ¡ d¡�}|  t|ƒt¡ W 5 Q R X dS )z�
        The L{codecs} module, oddly, returns a file-like object which returns
        bytes when not passed an 'encoding' argument.
        r&   N)Úcodecsr!   r"   r   r	   r   r#   r   r   r   Útest_codecsOpenBytesB   s    z IOTypeTests.test_codecsOpenBytesc              	   C   s4   t j|  ¡ ddd��}|  t|ƒt¡ W 5 Q R X dS )zY
        When passed an encoding, however, the L{codecs} module returns unicode.
        r&   úutf-8)ÚencodingN)r(   r!   r"   r   r	   r   r#   r   r   r   Útest_codecsOpenTextJ   s    zIOTypeTests.test_codecsOpenTextc                 C   s   |   ttƒ ƒt¡ dS )zy
        When passed an object about which no sensible decision can be made, err
        on the side of unicode.
        N)r   r	   Úobjectr   r   r   r   r   Útest_defaultToTextQ   s    zIOTypeTests.test_defaultToTextN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r%   r'   r)   r,   r.   r   r   r   r   r   #   s   r   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚCompatTestszy
    Various utility functions in C{twisted.python.compat} provide same
    functionality as modern Python variants.
    c                 C   s´   t ƒ }| d¡ | d¡ | d¡ t|ƒ}| ¡  |  |dddg¡ | d¡ t|ƒ}| ¡  |  |ddg¡ | d¡ ddh}| |¡}t|ƒ}| ¡  |  |ddddg¡ dS )zG
        L{set} should behave like the expected set interface.
        ÚbÚcÚaÚdÚrÚsN)ÚsetÚaddÚlistÚsortr   ÚremoveÚdiscardÚunion©r   r6   r4   r7   r   r   r   Útest_set_   s"    





zCompatTests.test_setc                 C   sn   t ddgƒ}|  tt|d¡ |  t|ƒddg¡ t ddgƒ}| |¡}t|ƒ}| ¡  |  |ddddg¡ dS )zS
        L{frozenset} should behave like the expected frozenset interface.
        r6   r4   r;   r8   r9   N)	Ú	frozensetÚassertRaisesÚAttributeErrorÚgetattrr   Úsortedr@   r<   r=   rA   r   r   r   Útest_frozensetw   s    
zCompatTests.test_frozensetN)r/   r0   r1   r2   rB   rH   r   r   r   r   r3   Y   s   r3   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚExecfileCompatTestszE
    Tests for the Python 3-friendly L{execfile} implementation.
    c              	   C   s<   |   ¡ }t|dƒ�}| | d¡¡ W 5 Q R X t| d¡ƒS )zo
        Write L{content} to a new temporary file, returning the L{FilePath}
        for the new file.
        r&   Úasciir*   )r"   r!   ÚwriteÚencoder   )r   ÚcontentÚpathr$   r   r   r   ÚwriteScript‹   s    zExecfileCompatTests.writeScriptc                 C   s2   |   d¡}ddi}t|j|ƒ |  d|d ¡ dS )zX
        L{execfile} executes the specified file in the given global namespace.
        ú	foo += 1
Úfooé   é   N©rO   r   rN   r   )r   ÚscriptÚglobalNamespacer   r   r   Útest_execfileGlobals•   s    
z(ExecfileCompatTests.test_execfileGlobalsc                 C   sL   |   d¡}ddi}ddi}t|j||ƒ |  d|d ¡ |  d|d ¡ dS )zk
        L{execfile} executes the specified file in the given global and local
        namespaces.
        rP   rQ   é
   é   é   NrT   )r   rU   rV   ZlocalNamespacer   r   r   Útest_execfileGlobalsAndLocalsž   s    
z1ExecfileCompatTests.test_execfileGlobalsAndLocalsc                 C   s@   dD ]6}|   d| ¡}ddi}t|j|ƒ |  d|d ¡ qdS )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'rQ   NÚokayrT   )r   Z
lineEndingrU   rV   r   r   r   Útest_execfileUniversalNewlinesª   s
    z2ExecfileCompatTests.test_execfileUniversalNewlinesN)r/   r0   r1   r2   rO   rW   r[   r_   r   r   r   r   rI   †   s
   
	rI   c                   @   s   e Zd ZdZdd„ ZdS )ÚPYPYTestz!
    Identification of PyPy.
    c                 C   s$   dt jkr|  t¡ n
|  t¡ dS )z,
        On PyPy, L{_PYPY} is True.
        ÚPyPyN)ÚsysÚversionÚ
assertTruer   ÚassertFalser   r   r   r   Ú	test_PYPY»   s    
zPYPYTest.test_PYPYN)r/   r0   r1   r2   rf   r   r   r   r   r`   ¶   s   r`   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú
ComparablezE
    Objects that can be compared to each other, but not others.
    c                 C   s
   || _ d S ©N)Úvalue)r   ri   r   r   r   Ú__init__Ë   s    zComparable.__init__c                 C   s   t |tƒstS t| j|jƒS rh   )Ú
isinstancerg   ÚNotImplementedr   ri   )r   Úotherr   r   r   Ú__cmp__Î   s    
zComparable.__cmp__N)r/   r0   r1   r2   rj   rn   r   r   r   r   rg   Å   s   rg   c                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )ÚComparableTestszR
    L{comparable} decorated classes emulate Python 2's C{__cmp__} semantics.
    c                 C   s0   |   tdƒtdƒk¡ |  tdƒtdƒk¡ dS )zo
        Instances of a class that is decorated by C{comparable} support
        equality comparisons.
        rR   rS   N©rd   rg   re   r   r   r   r   Útest_equalityÙ   s    zComparableTests.test_equalityc                 C   s0   |   tdƒtdƒk¡ |  tdƒtdƒk¡ dS )zq
        Instances of a class that is decorated by C{comparable} support
        inequality comparisons.
        rR   rS   N)re   rg   rd   r   r   r   r   Útest_nonEqualityâ   s    z ComparableTests.test_nonEqualityc                 C   s0   |   tdƒtdƒk¡ |  tdƒtdƒk¡ dS )zs
        Instances of a class that is decorated by C{comparable} support
        greater-than comparisons.
        rS   rR   r   é   Nrp   r   r   r   r   Útest_greaterThanë   s    z ComparableTests.test_greaterThanc                 C   sF   |   tdƒtdƒk¡ |   tdƒtdƒk¡ |  tdƒtdƒk¡ dS )z|
        Instances of a class that is decorated by C{comparable} support
        greater-than-or-equal comparisons.
        rR   rS   r   rs   Nrp   r   r   r   r   Útest_greaterThanOrEqualó   s    z'ComparableTests.test_greaterThanOrEqualc                 C   s0   |   tdƒtdƒk ¡ |  tdƒtdƒk ¡ dS )zp
        Instances of a class that is decorated by C{comparable} support
        less-than comparisons.
        r   rs   rS   Nrp   r   r   r   r   Útest_lessThanü   s    zComparableTests.test_lessThanc                 C   sF   |   tdƒtdƒk¡ |   tdƒtdƒk¡ |  tdƒtdƒk¡ dS )zy
        Instances of a class that is decorated by C{comparable} support
        less-than-or-equal comparisons.
        rs   r   rS   Nrp   r   r   r   r   Útest_lessThanOrEqual  s    z$ComparableTests.test_lessThanOrEqualN)
r/   r0   r1   r2   rq   rr   rt   ru   rv   rw   r   r   r   r   ro   Ô   s   			ro   c                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )ÚPython3ComparableTestsz;
    Python 3-specific functionality of C{comparable}.
    c                 C   s   |   tdƒ tƒ ¡t¡ dS )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.
        rR   N)r   rg   Ú__eq__r-   rl   r   r   r   r   Útest_notImplementedEquals  s    z0Python3ComparableTests.test_notImplementedEqualsc                 C   s   |   tdƒ tƒ ¡t¡ dS )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.
        rR   N)r   rg   Ú__ne__r-   rl   r   r   r   r   Útest_notImplementedNotEquals  s    z3Python3ComparableTests.test_notImplementedNotEqualsc                 C   s   |   tdƒ tƒ ¡t¡ dS )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.
        rR   N)r   rg   Ú__gt__r-   rl   r   r   r   r   Útest_notImplementedGreaterThan#  s    z5Python3ComparableTests.test_notImplementedGreaterThanc                 C   s   |   tdƒ tƒ ¡t¡ dS )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.
        rR   N)r   rg   Ú__lt__r-   rl   r   r   r   r   Útest_notImplementedLessThan+  s    z2Python3ComparableTests.test_notImplementedLessThanc                 C   s   |   tdƒ tƒ ¡t¡ dS )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.
        rR   N)r   rg   Ú__ge__r-   rl   r   r   r   r   Ú$test_notImplementedGreaterThanEquals3  s    z;Python3ComparableTests.test_notImplementedGreaterThanEqualsc                 C   s   |   tdƒ tƒ ¡t¡ dS )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.
        rR   N)r   rg   Ú__le__r-   rl   r   r   r   r   Ú!test_notImplementedLessThanEquals;  s    z8Python3ComparableTests.test_notImplementedLessThanEqualsN)
r/   r0   r1   r2   rz   r|   r~   r€   r‚   r„   r   r   r   r   rx     s   rx   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚCmpTestszA
    L{cmp} should behave like the built-in Python 2 C{cmp}.
    c                 C   s>   |   tddƒd¡ |   tddƒd¡ |   tdgdgƒd¡ dS )z5
        L{cmp} returns 0 for equal objects.
        r6   r   rR   N©r   r   r   r   r   r   Útest_equalsI  s    zCmpTests.test_equalsc                 C   s(   |   tddƒd¡ |   tddƒd¡ dS )zS
        L{cmp} returns 1 if its first argument is bigger than its second.
        é   r   rR   ó   zó   aNr†   r   r   r   r   rt   Q  s    zCmpTests.test_greaterThanc                 C   s(   |   tddƒd¡ |   tddƒd¡ dS )zU
        L{cmp} returns -1 if its first argument is smaller than its second.
        gš™™™™™¹?gffffff@éÿÿÿÿrŠ   ó   dNr†   r   r   r   r   rv   X  s    zCmpTests.test_lessThanN)r/   r0   r1   r2   r‡   rt   rv   r   r   r   r   r…   D  s   r…   c                   @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚStringTestsz8
    Compatibility functions and types for strings.
    c                 C   s$   |   t|ƒ|¡ |  t|ƒt¡ dS )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)r   r   ZassertIsInstancer   )r   ÚoriginalÚexpectedr   r   r   ÚassertNativeStringe  s    zStringTests.assertNativeStringc                 C   s   |   ttd¡ dS )zj
        C{nativeString} raises a C{UnicodeError} if input bytes are not ASCII
        decodable.
        ó   ÿN©rD   ÚUnicodeErrorr   r   r   r   r   Útest_nonASCIIBytesToStringn  s    z&StringTests.test_nonASCIIBytesToStringc                 C   s   |   ttd¡ dS )zk
        C{nativeString} raises a C{UnicodeError} if input Unicode is not ASCII
        encodable.
        u   áˆ´Nr’   r   r   r   r   Útest_nonASCIIUnicodeToStringu  s    z(StringTests.test_nonASCIIUnicodeToStringc                 C   s   |   dd¡ dS )z
        C{nativeString} converts bytes to the native string format, assuming
        an ASCII encoding if applicable.
        s   helloZhelloN©r�   r   r   r   r   Útest_bytesToString|  s    zStringTests.test_bytesToStringc                 C   s   |   dd¡ dS )z�
        C{nativeString} converts unicode to the native string format, assuming
        an ASCII encoding if applicable.
        zGood dayNr–   r   r   r   r   Útest_unicodeToStringƒ  s    z StringTests.test_unicodeToStringc                 C   s   |   dd¡ dS )zJ
        C{nativeString} leaves native strings as native strings.
        zHello!Nr–   r   r   r   r   Útest_stringToStringŠ  s    zStringTests.test_stringToStringc                 C   s   |   ttd¡ dS )zu
        C{nativeString} raises a C{TypeError} if given an object that is not a
        string of some sort.
        rR   N)rD   Ú	TypeErrorr   r   r   r   r   Útest_unexpectedType�  s    zStringTests.test_unexpectedTypeN)r/   r0   r1   r2   r�   r”   r•   r—   r˜   r™   r›   r   r   r   r   r�   `  s   	r�   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚNetworkStringTestsz%
    Tests for L{networkString}.
    c                 C   s   |   dtdƒ¡ dS )zu
        L{networkString} returns a C{unicode} object passed to it encoded into
        a C{bytes} instance.
        s   foorQ   N)r   r   r   r   r   r   Útest_str�  s    zNetworkStringTests.test_strc                 C   s   |   ttd¡ dS )z�
        L{networkString} raises L{UnicodeError} if passed a C{unicode} instance
        containing characters not encodable in ASCII.
        u   â˜ƒN)rD   r“   r   r   r   r   r   Útest_unicodeOutOfRange¤  s    z)NetworkStringTests.test_unicodeOutOfRangec                 C   s"   |   tttƒ ¡ |   ttd¡ dS )z€
        L{networkString} raises L{TypeError} if passed a non-string object or
        the wrong type of string object.
        s   bytesN)rD   rš   r   r-   r   r   r   r   Útest_nonString«  s    z!NetworkStringTests.test_nonStringN)r/   r0   r1   r2   r�   rž   rŸ   r   r   r   r   rœ   ˜  s   rœ   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚReraiseTestszH
    L{reraise} re-raises exceptions on both Python 2 and Python 3.
    c              	   C   s¨   zdd  W n" t k
r.   t ¡ \}}}Y nX zt|dƒ W nZ t k
r˜   t ¡ \}}}|  |t¡ |  ||¡ |  t 	|¡d t 	|¡d ¡ Y nX |  
d¡ dS )z…
        Calling L{reraise} with an exception instance and a traceback of
        L{None} re-raises it with a new traceback.
        rR   r   Nr‹   úThe exception was not raised.)ÚBaseExceptionrb   Úexc_infor   r   ÚZeroDivisionErrorÚassertIsZassertNotEqualÚ	tracebackÚ	format_tbÚfail©r   Útypri   ÚtbZtyp2Zvalue2Ztb2r   r   r   Útest_reraiseWithNone¹  s     ÿ
z!ReraiseTests.test_reraiseWithNonec              	   C   s¨   zdd  W n" t k
r.   t ¡ \}}}Y nX zt||ƒ W nZ t k
r˜   t ¡ \}}}|  |t¡ |  ||¡ |  t |¡d t |¡d ¡ Y nX |  	d¡ dS )z‰
        Calling L{reraise} with an exception instance and a traceback
        re-raises the exception with the given traceback.
        rR   r   r‹   r¡   N)
r¢   rb   r£   r   r   r¤   r¥   r¦   r§   r¨   r©   r   r   r   Útest_reraiseWithTracebackÎ  s    &z&ReraiseTests.test_reraiseWithTracebackN)r/   r0   r1   r2   r¬   r­   r   r   r   r   r    ´  s   r    c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚPython3BytesTestszB
    Tests for L{iterbytes}, L{intToBytes}, L{lazyByteSlice}.
    c                 C   s(   d}t t|ƒƒ}|  |ddddg¡ dS )z¯
        When L{iterbytes} is called with a bytestring, the returned object
        can be iterated over, resulting in the individual bytes of the
        bytestring.
        s   abcdrŠ   ó   bó   crŒ   N)r<   r
   r   )r   ÚinputÚresultr   r   r   Útest_iterationç  s    z Python3BytesTests.test_iterationc                 C   s   |   tdƒd¡ dS )z‹
        When L{intToBytes} is called with an integer, the result is an
        ASCII-encoded string representation of the number.
        éÕ   s   213N)r   r   r   r   r   r   Útest_intToBytesñ  s    z!Python3BytesTests.test_intToBytesc                 C   s   d}|   tt|ƒƒ|¡ dS )zv
        L{lazyByteSlice} called with some bytes returns a semantically equal
        version of these bytes.
        ó   123XYZN©r   r   r   ©r   Údatar   r   r   Útest_lazyByteSliceNoOffsetø  s    z,Python3BytesTests.test_lazyByteSliceNoOffsetc                 C   s&   d}|   tt|dƒƒ|dd… ¡ dS )z¡
        L{lazyByteSlice} called with some bytes and an offset returns a
        semantically equal version of these bytes starting at the given offset.
        r¶   rS   Nr·   r¸   r   r   r   Útest_lazyByteSliceOffset   s    z*Python3BytesTests.test_lazyByteSliceOffsetc                 C   s(   d}|   tt|ddƒƒ|dd… ¡ dS )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.
        r¶   rS   rs   é   Nr·   r¸   r   r   r   Ú!test_lazyByteSliceOffsetAndLength  s    z3Python3BytesTests.test_lazyByteSliceOffsetAndLengthN)	r/   r0   r1   r2   r³   rµ   rº   r»   r½   r   r   r   r   r®   â  s   
r®   c                   @   s&   e Zd ZdZee ¡ dƒdd„ ƒZdS )ÚBytesEnvironTestsz$
    Tests for L{BytesEnviron}.
    z+Environment vars are always str on Windows.c                 C   sP   t ƒ }tƒ }| ¡ D ]$\}}| t|ƒ¡ | t|ƒ¡ q|  t|ƒtg¡ dS )zz
        The output of L{BytesEnviron} should always be a L{dict} with L{bytes}
        values and L{bytes} keys.
        N)r   r:   Úitemsr;   Útyper   r<   r   )r   r²   ÚtypesÚkeyÚvalr   r   r   Útest_alwaysBytes  s    z"BytesEnvironTests.test_alwaysBytesN)r/   r0   r1   r2   r   r   Z	isWindowsrÄ   r   r   r   r   r¾     s   r¾   )(r2   r(   r   rb   r¦   Zunittestr   Ztwisted.python.compatr   r   r   r   r   r   r	   r
   r   r   r   r   Ztwisted.python.filepathr   Ztwisted.python.runtimer   Ztwisted.trial.unittestr   r   r   r3   rI   r`   rg   ro   rx   r…   r�   rœ   r    r®   r¾   r   r   r   r   Ú<module>   s.   86-0:68.0