o
    bF                     @   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