o
    ¯büF  ã                   @   sz  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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(G d"d#„ d#eƒZ)dS )$z%
Tests for L{twisted.python.compat}.
é    N)ÚskipIf)Ú_PYPYÚ_get_async_paramÚbytesEnvironÚcmpÚ
comparableÚexecfileÚ
intToBytesÚioTypeÚ	iterbytesÚlazyByteSliceÚnativeStringÚnetworkStringÚreraise)ÚFilePath)Úplatform)ÚSynchronousTestCaseÚTestCasec                   @   óH   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   ó   |   tt ¡ ƒt¡ dS )z=
        An L{io.StringIO} accepts and returns text.
        N)ÚassertEqualr
   ÚioÚStringIOÚstr©Úself© r   ú:/usr/lib/python3/dist-packages/twisted/test/test_compat.pyÚtest_3StringIO)   ó   zIOTypeTests.test_3StringIOc                 C   r   )z=
        An L{io.BytesIO} accepts and returns bytes.
        N)r   r
   r   ÚBytesIOÚbytesr   r   r   r   Útest_3BytesIO/   r    zIOTypeTests.test_3BytesIOc                 C   óD   t |  ¡ dƒ�}|  t|ƒt¡ W d  ƒ dS 1 sw   Y  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_3openTextMode5   ó   "ÿzIOTypeTests.test_3openTextModec                 C   r$   )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<   r+   z IOTypeTests.test_3openBinaryModec                 C   sF   t  |  ¡ d¡�}|  t|ƒt¡ W d  ƒ dS 1 sw   Y  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_codecsOpenBytesC   s   "ÿz IOTypeTests.test_codecsOpenBytesc                 C   sJ   t j|  ¡ ddd��}|  t|ƒt¡ W d  ƒ dS 1 sw   Y  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_codecsOpenTextK   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_defaultToTextR   s   zIOTypeTests.test_defaultToTextN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r#   r*   r-   r/   r2   r4   r   r   r   r   r   $   s    r   c                   @   ó    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|ƒ}| ¡  |  |g d¢¡ | d¡ t|ƒ}| ¡  |  |ddg¡ | d¡ ddh}| |¡}t|ƒ}| ¡  |  |g d¢¡ d	S )
zG
        L{set} should behave like the expected set interface.
        ÚbÚcÚa)r=   r;   r<   ÚdÚrÚs)r=   r<   r?   r@   N)ÚsetÚaddÚlistÚsortr   ÚremoveÚdiscardÚunion©r   r=   r;   r>   r   r   r   Útest_set`   s"   





zCompatTests.test_setc                 C   sj   t ddgƒ}|  tt|d¡ |  t|ƒddg¡ t ddgƒ}| |¡}t|ƒ}| ¡  |  |g d¢¡ dS )zS
        L{frozenset} should behave like the expected frozenset interface.
        r=   r;   rB   r?   r@   )r=   r;   r?   r@   N)	Ú	frozensetÚassertRaisesÚAttributeErrorÚgetattrr   ÚsortedrG   rC   rD   rH   r   r   r   Útest_frozensetx   s   
zCompatTests.test_frozensetN)r5   r6   r7   r8   rI   rO   r   r   r   r   r:   Z   s    r:   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   sP   |   ¡ }t|dƒ�}| | d¡¡ W d  ƒ n1 sw   Y  t| d¡ƒS )zo
        Write L{content} to a new temporary file, returning the L{FilePath}
        for the new file.
        r,   ÚasciiNr0   )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©rV   r   rU   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.
        rW   rX   é
   é   é   Nr[   )r   r\   r]   ÚlocalNamespacer   r   r   Útest_execfileGlobalsAndLocalsŸ   s   
z1ExecfileCompatTests.test_execfileGlobalsAndLocalsc                 C   s@   dD ]}|   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'rX   NÚokayr[   )r   Ú
lineEndingr\   r]   r   r   r   Útest_execfileUniversalNewlines«   s   üz2ExecfileCompatTests.test_execfileUniversalNewlinesN)r5   r6   r7   r8   rV   r^   rc   rh   r   r   r   r   rP   ‡   s    
	rP   c                   @   s   e Zd ZdZdd„ ZdS )ÚPYPYTestz!
    Identification of PyPy.
    c                 C   s&   dt jv r|  t¡ dS |  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)r5   r6   r7   r8   ro   r   r   r   r   ri   ·   s    ri   c                   @   r9   )Ú
ComparablezE
    Objects that can be compared to each other, but not others.
    c                 C   s
   || _ d S ©N)Úvalue)r   rr   r   r   r   Ú__init__Ì   s   
zComparable.__init__c                 C   s   t |tƒstS t| j|jƒS rq   )Ú
isinstancerp   ÚNotImplementedr   rr   )r   Úotherr   r   r   Ú__cmp__Ï   s   
zComparable.__cmp__N)r5   r6   r7   r8   rs   rw   r   r   r   r   rp   Æ   s    rp   c                   @   ó@   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.
        rY   rZ   N©rm   rp   rn   r   r   r   r   Útest_equalityÚ   ó   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.
        rY   rZ   N)rn   rp   rm   r   r   r   r   Útest_nonEqualityã   r|   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.
        rZ   rY   r   é   Nrz   r   r   r   r   Útest_greaterThanì   ó   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.
        rY   rZ   r   r~   Nrz   r   r   r   r   Útest_greaterThanOrEqualô   ó   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   r~   rZ   Nrz   r   r   r   r   Útest_lessThaný   r€   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.
        r~   r   rZ   Nrz   r   r   r   r   Útest_lessThanOrEqual  r‚   z$ComparableTests.test_lessThanOrEqualN)
r5   r6   r7   r8   r{   r}   r   r�   rƒ   r„   r   r   r   r   ry   Õ   s    			ry   c                   @   rx   )ÚPython3ComparableTestsz;
    Python 3-specific functionality of C{comparable}.
    c                 C   ó   |   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.
        rY   N)r   rp   Ú__eq__r3   ru   r   r   r   r   Útest_notImplementedEquals  ó   z0Python3ComparableTests.test_notImplementedEqualsc                 C   r†   )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.
        rY   N)r   rp   Ú__ne__r3   ru   r   r   r   r   Útest_notImplementedNotEquals  r‰   z3Python3ComparableTests.test_notImplementedNotEqualsc                 C   r†   )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.
        rY   N)r   rp   Ú__gt__r3   ru   r   r   r   r   Útest_notImplementedGreaterThan$  r‰   z5Python3ComparableTests.test_notImplementedGreaterThanc                 C   r†   )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.
        rY   N)r   rp   Ú__lt__r3   ru   r   r   r   r   Útest_notImplementedLessThan,  r‰   z2Python3ComparableTests.test_notImplementedLessThanc                 C   r†   )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.
        rY   N)r   rp   Ú__ge__r3   ru   r   r   r   r   Ú$test_notImplementedGreaterThanEquals4  r‰   z;Python3ComparableTests.test_notImplementedGreaterThanEqualsc                 C   r†   )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.
        rY   N)r   rp   Ú__le__r3   ru   r   r   r   r   Ú!test_notImplementedLessThanEquals<  r‰   z8Python3ComparableTests.test_notImplementedLessThanEqualsN)
r5   r6   r7   r8   rˆ   r‹   r�   r�   r‘   r“   r   r   r   r   r…     s    r…   c                   @   ó(   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.
        r=   r   rY   N©r   r   r   r   r   r   Útest_equalsJ  s   zCmpTests.test_equalsc                 C   ó(   |   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   rY   ó   zó   aNr–   r   r   r   r   r   R  ó   zCmpTests.test_greaterThanc                 C   r˜   )zU
        L{cmp} returns -1 if its first argument is smaller than its second.
        gš™™™™™¹?gffffff@éÿÿÿÿr›   ó   dNr–   r   r   r   r   rƒ   Y  rœ   zCmpTests.test_lessThanN)r5   r6   r7   r8   r—   r   rƒ   r   r   r   r   r•   E  s
    r•   c                   @   r   )Ú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   ÚassertIsInstancer   )r   ÚoriginalÚexpectedr   r   r   ÚassertNativeStringf  s   zStringTests.assertNativeStringc                 C   ó   |   ttd¡ dS )zj
        C{nativeString} raises a C{UnicodeError} if input bytes are not ASCII
        decodable.
        ó   ÿN©rK   ÚUnicodeErrorr   r   r   r   r   Útest_nonASCIIBytesToStringo  ó   z&StringTests.test_nonASCIIBytesToStringc                 C   r¤   )zk
        C{nativeString} raises a C{UnicodeError} if input Unicode is not ASCII
        encodable.
        u   áˆ´Nr¦   r   r   r   r   Útest_nonASCIIUnicodeToStringv  r©   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   helloÚhelloN©r£   r   r   r   r   Útest_bytesToString}  ó   zStringTests.test_bytesToStringc                 C   ó   |   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„  r®   z StringTests.test_unicodeToStringc                 C   r¯   )zJ
        C{nativeString} leaves native strings as native strings.
        zHello!Nr¬   r   r   r   r   Útest_stringToString‹  s   zStringTests.test_stringToStringc                 C   r¤   )zu
        C{nativeString} raises a C{TypeError} if given an object that is not a
        string of some sort.
        rY   N)rK   Ú	TypeErrorr   r   r   r   r   Útest_unexpectedType‘  r©   zStringTests.test_unexpectedTypeN)r5   r6   r7   r8   r£   r¨   rª   r­   r°   r±   r³   r   r   r   r   rŸ   a  s    	rŸ   c                   @   r”   )	Ú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   foorX   N)r   r   r   r   r   r   Útest_strž  ó   zNetworkStringTests.test_strc                 C   r¤   )z�
        L{networkString} raises L{UnicodeError} if passed a C{unicode} instance
        containing characters not encodable in ASCII.
        u   â˜ƒN)rK   r§   r   r   r   r   r   Útest_unicodeOutOfRange¥  r©   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)rK   r²   r   r3   r   r   r   r   Útest_nonString¬  s   z!NetworkStringTests.test_nonStringN)r5   r6   r7   r8   rµ   r·   r¸   r   r   r   r   r´   ™  s
    r´   c                   @   r9   )ÚReraiseTestszH
    L{reraise} re-raises exceptions on both Python 2 and Python 3.
    c              	   C   s¦   zdd  W n t y   t ¡ \}}}Y nw zt|dƒ W n- t yK   t ¡ \}}}|  |t¡ |  ||¡ |  t 	|¡d t 	|¡d ¡ Y dS w |  
d¡ dS )z…
        Calling L{reraise} with an exception instance and a traceback of
        L{None} re-raises it with a new traceback.
        rY   r   Nr�   úThe exception was not raised.)ÚBaseExceptionrk   Úexc_infor   r   ÚZeroDivisionErrorÚassertIsÚassertNotEqualÚ	tracebackÚ	format_tbÚfail©r   Útyprr   ÚtbÚtyp2Úvalue2Útb2r   r   r   Útest_reraiseWithNoneº  s    ÿ
ÿüz!ReraiseTests.test_reraiseWithNonec              	   C   s¦   zdd  W n t y   t ¡ \}}}Y nw zt||ƒ W n- t yK   t ¡ \}}}|  |t¡ |  ||¡ |  t |¡d t |¡d ¡ Y dS w |  	d¡ dS )z‰
        Calling L{reraise} with an exception instance and a traceback
        re-raises the exception with the given traceback.
        rY   r   r�   rº   N)
r»   rk   r¼   r   r   r½   r¾   rÀ   rÁ   rÂ   rÃ   r   r   r   Útest_reraiseWithTracebackÏ  s   ÿ&üz&ReraiseTests.test_reraiseWithTracebackN)r5   r6   r7   r8   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|ƒƒ}|  |g d¢¡ 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   abcd)r›   ó   bó   crž   N)rC   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ò  r¶   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Ó   rZ   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Ó   rZ   r~   é   NrÔ   rÕ   r   r   r   Ú!test_lazyByteSliceOffsetAndLength	  s   $z3Python3BytesTests.test_lazyByteSliceOffsetAndLengthN)	r5   r6   r7   r8   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   rA   ÚitemsrB   Útyper   rC   r"   )r   rÏ   ÚtypesÚkeyÚvalr   r   r   Útest_alwaysBytes  s   z"BytesEnvironTests.test_alwaysBytesN)r5   r6   r7   r8   r   r   Ú	isWindowsrá   r   r   r   r   rÛ     s    rÛ   c                   @   r9   )ÚGetAsyncParamTestsz=
    Tests for L{twisted.python.compat._get_async_param}
    c                 C   sx   |   tdd�d¡ |   tdd�d¡ |   tdddiddi¤Žd¡ |   tdddiddi¤Žd¡ |  ttdddi¡ dS )z•
        L{twisted.python.compat._get_async_param} uses isAsync by default,
        or deprecated async keyword argument if isAsync is None.
        F)ÚisAsyncTrä   NÚasyncr   )r   r   rK   r²   r   r   r   r   Útest_get_async_param-  s
   z'GetAsyncParamTests.test_get_async_paramc                 C   sF   |   td	ddiddi¤Žd¡ | j| jgd�}|   |d d d¡ dS )
z…
        L{twisted.python.compat._get_async_param} raises a deprecation
        warning if async keyword argument is passed.
        rä   Nrå   F)ÚoffendingFunctionsr   Úmessagez:'async' keyword argument is deprecated, please use isAsyncr   )r   r   ÚflushWarningsÚ test_get_async_param_deprecation)r   ÚcurrentWarningsr   r   r   rê   8  s   ÿ
þz3GetAsyncParamTests.test_get_async_param_deprecationN)r5   r6   r7   r8   ræ   rê   r   r   r   r   rã   (  s    rã   )*r8   r.   r   rk   rÀ   Úunittestr   Útwisted.python.compatr   r   r   r   r   r   r	   r
   r   r   r   r   r   Útwisted.python.filepathr   Útwisted.python.runtimer   Útwisted.trial.unittestr   r   r   r:   rP   ri   rp   ry   r…   r•   rŸ   r´   r¹   rË   rÛ   rã   r   r   r   r   Ú<module>   s2   <6-0:68.0