o
    Óó·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                   @   ó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   ú^C:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-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_3openTextMode4   ó   "ÿ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_codecsOpenBytesB   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_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.   r1   r3   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:   rA   r>   r?   )r<   r:   r>   r?   N)	Ú	frozensetÚassertRaisesÚAttributeErrorÚgetattrr   ÚsortedrF   rB   rC   rG   r   r   r   Útest_frozensetw   s   
zCompatTests.test_frozensetN)r4   r5   r6   r7   rH   rN   r   r   r   r   r9   Y   s    r9   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+   ÚasciiNr/   )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©rU   r   rT   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.
        rV   rW   é
   é   é   NrZ   )r   r[   r\   Z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'rW   NÚokayrZ   )r   Z
lineEndingr[   r\   r   r   r   Útest_execfileUniversalNewlinesª   s   üz2ExecfileCompatTests.test_execfileUniversalNewlinesN)r4   r5   r6   r7   rU   r]   ra   re   r   r   r   r   rO   †   s    
	rO   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)r4   r5   r6   r7   rl   r   r   r   r   rf   ¶   s    rf   c                   @   r8   )Ú
ComparablezE
    Objects that can be compared to each other, but not others.
    c                 C   s
   || _ d S ©N)Úvalue)r   ro   r   r   r   Ú__init__Ë   s   
zComparable.__init__c                 C   s   t |tƒstS t| j|jƒS rn   )Ú
isinstancerm   ÚNotImplementedr   ro   )r   Úotherr   r   r   Ú__cmp__Î   s   
zComparable.__cmp__N)r4   r5   r6   r7   rp   rt   r   r   r   r   rm   Å   s    rm   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.
        rX   rY   N©rj   rm   rk   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.
        rX   rY   N)rk   rm   rj   r   r   r   r   Útest_nonEqualityâ   ry   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.
        rY   rX   r   é   Nrw   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.
        rX   rY   r   r{   Nrw   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{   rY   Nrw   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   rY   Nrw   r   r   r   r   Útest_lessThanOrEqual  r   z$ComparableTests.test_lessThanOrEqualN)
r4   r5   r6   r7   rx   rz   r|   r~   r€   r�   r   r   r   r   rv   Ô   s    			rv   c                   @   ru   )Ú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.
        rX   N)r   rm   Ú__eq__r2   rr   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.
        rX   N)r   rm   Ú__ne__r2   rr   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.
        rX   N)r   rm   Ú__gt__r2   rr   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.
        rX   N)r   rm   Ú__lt__r2   rr   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.
        rX   N)r   rm   Ú__ge__r2   rr   r   r   r   r   Ú$test_notImplementedGreaterThanEquals3  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.
        rX   N)r   rm   Ú__le__r2   rr   r   r   r   r   Ú!test_notImplementedLessThanEquals;  r†   z8Python3ComparableTests.test_notImplementedLessThanEqualsN)
r4   r5   r6   r7   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   rX   N©r   r   r   r   r   r   Útest_equalsI  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   rX   ó   zó   aNr“   r   r   r   r   r|   Q  ó   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€   X  r™   zCmpTests.test_lessThanN)r4   r5   r6   r7   r”   r|   r€   r   r   r   r   r’   D  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   ZassertIsInstancer   )r   ÚoriginalÚexpectedr   r   r   ÚassertNativeStringe  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©rJ   ÚUnicodeErrorr   r   r   r   r   Útest_nonASCIIBytesToStringn  ó   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_nonASCIIUnicodeToStringu  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   helloZ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.
        rX   N)rJ   Ú	TypeErrorr   r   r   r   r   Útest_unexpectedType�  r¥   zStringTests.test_unexpectedTypeN)r4   r5   r6   r7   rŸ   r¤   r¦   r¨   r«   r¬   r®   r   r   r   r   rœ   `  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   foorW   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)rJ   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)rJ   r­   r   r2   r   r   r   r   Útest_nonString«  s   z!NetworkStringTests.test_nonStringN)r4   r5   r6   r7   r°   r²   r³   r   r   r   r   r¯   ˜  s
    r¯   c                   @   r8   )Ú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.
        rX   r   Nrš   úThe exception was not raised.)ÚBaseExceptionrh   Úexc_infor   r   ÚZeroDivisionErrorÚassertIsZassertNotEqualÚ	tracebackÚ	format_tbÚfail©r   Útypro   ÚtbZtyp2Zvalue2Z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.
        rX   r   rš   rµ   N)
r¶   rh   r·   r   r   r¸   r¹   rº   r»   r¼   r½   r   r   r   Útest_reraiseWithTracebackÎ  s   ÿ&üz&ReraiseTests.test_reraiseWithTracebackN)r4   r5   r6   r7   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)rB   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Ê   rY   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Ê   rY   r{   é   NrË   rÌ   r   r   r   Ú!test_lazyByteSliceOffsetAndLength  s   $z3Python3BytesTests.test_lazyByteSliceOffsetAndLengthN)	r4   r5   r6   r7   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@   ÚitemsrA   Útyper   rB   r!   )r   rÆ   ÚtypesÚkeyÚvalr   r   r   Útest_alwaysBytes  s   z"BytesEnvironTests.test_alwaysBytesN)r4   r5   r6   r7   r   r   Z	isWindowsrØ   r   r   r   r   rÒ     s    rÒ   )(r7   r-   r   rh   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   r9   rO   rf   rm   rv   r‚   r’   rœ   r¯   r´   rÂ   rÒ   r   r   r   r   Ú<module>   s0   86-0:68.0