U
    W[^r                     @   sX  d Z ddlmZmZ ddl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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& G dd	 d	e
j'Z(G d
d de
j'Z)G dd de
j'Z*erde*_+G dd de
j'Z,G dd de
j'Z-G dd de
j'Z.eG dd de/Z0G dd de
j'Z1G dd de
j'Z2es|de2_+G dd de
j'Z3G dd de
j'Z4G d d! d!e
j'Z5G d"d# d#e
j'Z6G d$d% d%e
j'Z7G d&d' d'e
j8Z9G d(d) d)e
j8Z:G d*d+ d+e
j8Z;G d,d- d-e
j8Z<G d.d/ d/e
j8Z=G d0d1 d1e
j8Z>G d2d3 d3e
j'Z?dS )4z%
Tests for L{twisted.python.compat}.
    )divisionabsolute_importN)unittest)reduceexecfile_PY3_PYPY
comparablecmpnativeStringnetworkStringunicodelazyByteSlicereraiseNativeStringIO	iterbytes
intToBytesioTypebytesEnviron	iteritems_coercedUnicodeunichr	raw_input
_bytesRepr_get_async_param)FilePath)platformc                   @   sv   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
erZde_de
_de	_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unicodeCompatself r$   :/usr/lib/python3/dist-packages/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   s0   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)r   openmktempr   r   r!   r#   fr$   r$   r%   test_3openTextMode-   s    zIOTypeTests.test_3openTextModec              	   C   s0   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$   r%   test_3openBinaryMode5   s    z IOTypeTests.test_3openBinaryModec                 C   s.   G dd dt }| t||  dt dS )z
        The special built-in console file in Python 2 which has an 'encoding'
        attribute should qualify as a special type, since it accepts both bytes
        and text faithfully.
        c                   @   s   e Zd ZdZdZdS )z7IOTypeTests.test_2openTextMode.<locals>.VerySpecificLiea3  
            In their infinite wisdom, the CPython developers saw fit not to
            allow us a writable 'encoding' attribute on the built-in 'file'
            type in Python 2, despite making it writable in C with
            PyFile_SetEncoding.

            Pretend they did not do that.
            utf-8N)__name__
__module____qualname____doc__encodingr$   r$   r$   r%   VerySpecificLieC   s   r8   r0   N)filer   r   r,   Z
basestring)r#   r8   r$   r$   r%   test_2openTextMode=   s    zIOTypeTests.test_2openTextModec                 C   s@   ddl m} ddlm} | t| t | t| t dS )zV
        Python 2's L{StringIO} and L{cStringIO} modules are both binary I/O.
        r   )r    N)	cStringIOr    r   r   r(   )r#   r;   r    r$   r$   r%   test_2StringIOR   s    zIOTypeTests.test_2StringIOc              	   C   s.   t |  d}| t|t W 5 Q R X dS )zX
        The normal 'open' builtin in Python 2 will always result in bytes I/O.
        r*   N)r+   r,   r   r   r(   r-   r$   r$   r%   test_2openBinaryMode\   s    z IOTypeTests.test_2openBinaryModez'The 'file' type is no longer available.z$'io.open' is now the same as 'open'.zHThe 'StringIO' and 'cStringIO' modules were subsumed by the 'io' module.c              	   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.
        r0   N)codecsr+   r,   r   r   r(   r-   r$   r$   r%   test_codecsOpenBytesj   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.
        r0   r2   )r7   N)r>   r+   r,   r   r   r!   r-   r$   r$   r%   test_codecsOpenTexts   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_defaultToText{   s    zIOTypeTests.test_defaultToTextN)r3   r4   r5   r6   r&   r)   r/   r1   r:   r<   r=   r   skipr?   r@   rB   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 )	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 t ddg}||}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#   rG   rE   rH   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.
        rG   rE   rL   rI   rJ   N)		frozensetassertRaisesAttributeErrorgetattrr   sortedrQ   rM   rN   rR   r$   r$   r%   test_frozenset   s    
zCompatTests.test_frozensetc              
   C   sF   |  dtdd dddddg |  d	td
d dddddgd dS )zB
        L{reduce} should behave like the builtin reduce.
           c                 S   s   | | S Nr$   xyr$   r$   r%   <lambda>       z)CompatTests.test_reduce.<locals>.<lambda>                  c                 S   s   | | S r[   r$   r\   r$   r$   r%   r_      r`   N)r   r   r"   r$   r$   r%   test_reduce   s     zCompatTests.test_reduceN)r3   r4   r5   r6   rS   rY   rg   r$   r$   r$   r%   rD      s   rD   c                   @   s    e Zd ZdZdd Zdd ZdS )	IPv6TestszE
    C{inet_pton} and C{inet_ntop} implementations support IPv6.
    c                    s   ddl m   fdd} fdd}| d|d | d|d	 | d
|d | d|d | d|d | d|d | d|d | d|d d S )Nr   	inet_ntopc                    s    t j| S r[   socketZAF_INET6rG   ri   r$   r%   r_      r`   z$IPv6Tests.testNToP.<locals>.<lambda>c                    s    t j| S r[   rl   ZAF_INETrm   ri   r$   r%   r_      r`   ::                ::1               z!aef:b01:506:1001:ffff:9997:55:170u   
ïÿÿ Upz1.0.1.0z  z170.85.170.85u   ªUªUz255.255.255.255u   ÿÿÿÿz100::z               z100::1z              )twisted.python.compatrj   r   )r#   r.   gr$   ri   r%   testNToP   s    zIPv6Tests.testNToPc                    s   ddl m   fdd} fdd}| d|d | d|d	 | d
|d | d|d | d|d | d|d | d|d | d|d | d|d | d|d | d|d dD ]}| t|| qdS )z
        L{twisted.python.compat.inet_pton} parses IPv4 and IPv6 addresses in a
        manner similar to that of L{socket.inet_pton}.
        r   	inet_ptonc                    s    t j| S r[   rk   rm   rv   r$   r%   r_      r`   z$IPv6Tests.testPToN.<locals>.<lambda>c                    s    t j| S r[   rn   rm   rv   r$   r%   r_      r`   z    z0.0.0.0u   ÿ ÿ z255.0.255.0u   ªªªªz170.170.170.170rp   ro   z0::0z               z1::u   EïvË Vï¯ë¬$®®z$45ef:76cb:1a:56ef:afeb:bac:1924:aeaez(45ef:76cb:1a:56ef:afeb:bac:1924:aeae%en0rr   rq   z            z	::1.2.3.4u         ÿz1:2:3:4:5:6:1.2.3.255)z1:2:3:4:5:6:7:8:z:1:2:3:4:5:6:7:8z1::2::3z1:::3z:::z1:2z::1.2z	1.2.3.4::z%abcd:1.2.3.4:abcd:abcd:abcd:abcd:abcdz*1234:1.2.3.4:1234:1234:1234:1234:1234:1234z1.2.3.4 z%eth0N)rs   rw   r   rU   
ValueError)r#   r.   rt   Zbadaddrr$   rv   r%   testPToN   s2    zIPv6Tests.testPToNN)r3   r4   r5   r6   ru   rz   r$   r$   r$   r%   rh      s   rh   z7These tests are only relevant to old versions of Pythonc                   @   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.
        r0   asciir2   )r,   r+   writeencoder   )r#   Z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
foora   rb   Nr   r   r   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.
        r   r   
         Nr   )r#   r   r   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'r   NZokayr   )r#   Z
lineEndingr   r   r$   r$   r%   test_execfileUniversalNewlines'  s
    z2ExecfileCompatTests.test_execfileUniversalNewlinesN)r3   r4   r5   r6   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 )PY3Testsz2
    Identification of Python 2 vs. Python 3.
    c                 C   s   t jdr| t dS )z0
        On Python 2, C{_PY3} is False.
        z2.N)sysversion
startswithassertFalser   r"   r$   r$   r%   test_python29  s    zPY3Tests.test_python2c                 C   s   t jdr| t dS )z/
        On Python 3, C{_PY3} is True.
        z3.N)r   r   r   
assertTruer   r"   r$   r$   r%   test_python3A  s    zPY3Tests.test_python3N)r3   r4   r5   r6   r   r   r$   r$   r$   r%   r   4  s   r   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.
        ZPyPyN)r   r   r   r   r   r"   r$   r$   r%   	test_PYPYO  s    
zPYPYTest.test_PYPYN)r3   r4   r5   r6   r   r$   r$   r$   r%   r   J  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 r[   )value)r#   r   r$   r$   r%   __init___  s    zComparable.__init__c                 C   s   t |tstS t| j|jS r[   )
isinstancer   NotImplementedr
   r   )r#   otherr$   r$   r%   __cmp__c  s    
zComparable.__cmp__N)r3   r4   r5   r6   r   r   r$   r$   r$   r%   r   Z  s   r   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.
        ra   rb   Nr   r   r   r"   r$   r$   r%   test_equalityo  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.
        ra   rb   N)r   r   r   r"   r$   r$   r%   test_nonEqualityy  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.
        rb   ra   r   rc   Nr   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.
        ra   rb   r   rc   Nr   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   rc   rb   Nr   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.
        rc   r   rb   Nr   r"   r$   r$   r%   test_lessThanOrEqual  s    z$ComparableTests.test_lessThanOrEqualN)
r3   r4   r5   r6   r   r   r   r   r   r   r$   r$   r$   r%   r   j  s   

	
	r   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.
        ra   N)r   r   __eq__rA   r   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.
        ra   N)r   r   __ne__rA   r   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.
        ra   N)r   r   __gt__rA   r   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.
        ra   N)r   r   __lt__rA   r   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.
        ra   N)r   r   __ge__rA   r   r"   r$   r$   r%   $test_notImplementedGreaterThanEquals  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.
        ra   N)r   r   __le__rA   r   r"   r$   r$   r%   !test_notImplementedLessThanEquals  s    z8Python3ComparableTests.test_notImplementedLessThanEqualsN)
r3   r4   r5   r6   r   r   r   r   r   r   r$   r$   r$   r%   r     s   					r   zPython 3 only.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.
        rG   r   ra   Nr   r
   r"   r$   r$   r%   test_equals  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.
        rd   r   ra      z   aNr   r"   r$   r$   r%   r     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%   r     s    zCmpTests.test_lessThanN)r3   r4   r5   r6   r   r   r   r$   r$   r$   r%   r     s   	r   c                   @   sX   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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   assertIsInstancestr)r#   Zoriginalexpectedr$   r$   r%   assertNativeString  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rU   UnicodeErrorr   r"   r$   r$   r%   test_nonASCIIBytesToString  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_nonASCIIUnicodeToString!  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   hello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_unicodeToString1  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_stringToString9  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.
        ra   N)rU   	TypeErrorr   r"   r$   r$   r%   test_unexpectedType@  s    zStringTests.test_unexpectedTypec                 C   s   t r
t}nt}| t| dS )zR
        C{compat.unicode} is C{str} on Python 3, C{unicode} on Python 2.
        N)r   r   r   assertIsr!   )r#   r   r$   r$   r%   test_unicodeH  s    zStringTests.test_unicodec                 C   s.   t  }|d |d | | d dS )zg
        L{NativeStringIO} is a file-like object that stores native strings in
        memory.
        r   z therezhello thereN)r   r}   r   getvaluer-   r$   r$   r%   test_nativeStringIOS  s    

zStringTests.test_nativeStringION)r3   r4   r5   r6   r   r   r   r   r   r   r   r   r   r$   r$   r$   r%   r   
  s   
r   c                   @   sX   e Zd ZdZdd Zdd Zer,d e_e_dd Zd	d
 Z	esLd e_e	_dd Z
dS )NetworkStringTestsz%
    Tests for L{networkString}.
    c                 C   s   |  dtd dS )zU
        L{networkString} returns a C{bytes} object passed to it unmodified.
           fooNr   r   r"   r$   r$   r%   
test_bytesc  s    zNetworkStringTests.test_bytesc                 C   s   |  ttdd dS )z
        L{networkString} raises C{UnicodeError} if passed a C{bytes} instance
        containing bytes not used by ASCII.
           ☃r2   N)rU   r   r   r~   r"   r$   r$   r%   test_bytesOutOfRangej  s
      z'NetworkStringTests.test_bytesOutOfRangez:Bytes behavior of networkString only provided on Python 2.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.
        r   r   Nr   r"   r$   r$   r%   r   v  s    zNetworkStringTests.test_unicodec                 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.
        r   N)rU   r   r   r"   r$   r$   r%   test_unicodeOutOfRange~  s
      z)NetworkStringTests.test_unicodeOutOfRangez<Unicode behavior of networkString only provided on Python 3.c                 C   s6   |  ttt  tr$|  ttd n|  ttd dS )z
        L{networkString} raises L{TypeError} if passed a non-string object or
        the wrong type of string object.
           bytestextN)rU   r   r   rA   r   r"   r$   r$   r%   test_nonString  s    z!NetworkStringTests.test_nonStringN)r3   r4   r5   r6   r   r   r   rC   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  \}}}Y nX zt|d W nR   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.
        ra   r   Nr   The exception was not raised.)
r   exc_infor   r   ZeroDivisionErrorr   ZassertNotEqual	traceback	format_tbfailr#   typr   tbZtyp2Zvalue2Ztb2r$   r$   r%   test_reraiseWithNone  s    
z!ReraiseTests.test_reraiseWithNonec              	   C   s   zdd  W n   t  \}}}Y nX zt|| W nR   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.
        ra   r   r   r   N)	r   r   r   r   r   r   r   r   r   r   r$   r$   r%   test_reraiseWithTraceback  s    
z&ReraiseTests.test_reraiseWithTracebackN)r3   r4   r5   r6   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)rM   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   rb   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   rb   rc   re   Nr   r   r$   r$   r%   !test_lazyByteSliceOffsetAndLength  s    z3Python3BytesTests.test_lazyByteSliceOffsetAndLengthN)	r3   r4   r5   r6   r   r   r   r   r   r$   r$   r$   r%   r     s   		r   c                   @   s&   e Zd ZdZdd Ze r"de_dS )BytesEnvironTestsz$
    Tests for L{BytesEnviron}.
    c                 C   sP   t  }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   rK   r   rL   typer   rM   r(   )r#   r   typeskeyvalr$   r$   r%   test_alwaysBytes   s    z"BytesEnvironTests.test_alwaysBytesz+Environment vars are always str on Windows.N)r3   r4   r5   r6   r   r   Z	isWindowsrC   r$   r$   r$   r%   r     s   r   c                   @   s   e Zd ZdZdd ZdS )OrderedDictTestsz9
    Tests for L{twisted.python.compat.OrderedDict}.
    c                 C   s\   ddl m} | | j| jgd}| |d d d | |d d t | t|d dS )	zE
        L{twisted.python.compat.OrderedDict} is deprecated.
        r   )OrderedDictZoffendingFunctionsmessagezhtwisted.python.compat.OrderedDict was deprecated in Twisted 15.5.0: Use collections.OrderedDict instead.categoryra   N)rs   r   flushWarningstest_deprecatedr   DeprecationWarninglen)r#   r   currentWarningsr$   r$   r%   r    s    
z OrderedDictTests.test_deprecatedN)r3   r4   r5   r6   r  r$   r$   r$   r%   r     s   r   c                   @   sL   e Zd ZdZdd Zdd Zdd Zdd	 Zes6d
e_	dd Z
erHd
e
_	dS )CoercedUnicodeTestsz=
    Tests for L{twisted.python.compat._coercedUnicode}.
    c                 C   s$   t d}| |d | |t dS )zG
        Unicode strings with ASCII code points are unchanged.
        r   Nr   r   r   r!   r#   r   r$   r$   r%   test_unicodeASCII.  s    z%CoercedUnicodeTests.test_unicodeASCIIc                 C   s$   t d}| |d | |t dS )zK
        Unicode strings with non-ASCII code points are unchanged.
        r   Nr  r  r$   r$   r%   test_unicodeNonASCII7  s    z(CoercedUnicodeTests.test_unicodeNonASCIIc                 C   s$   t d}| |d | |t dS )z
        Native strings with ASCII code points are unchanged.

        On Python 2, this verifies that ASCII-only byte strings are accepted,
        whereas for Python 3 it is identical to L{test_unicodeASCII}.
        r   Nr  r  r$   r$   r%   test_nativeASCII@  s    z$CoercedUnicodeTests.test_nativeASCIIc                 C   s"   |  ttd}| t|d dS )z?
        Byte strings are not accceptable in Python 3.
        r   z!Expected str not b'bytes' (bytes)N)rU   r   r   r   r   r#   excr$   r$   r%   test_bytesPy3L  s    z!CoercedUnicodeTests.test_bytesPy3z<Bytes behavior of _coercedUnicode only provided on Python 2.c                 C   s   |  ttd dS )zM
        Byte strings with non-ASCII code points raise an exception.
        s   ☃N)rU   r   r   r"   r$   r$   r%   test_bytesNonASCIIW  s    z&CoercedUnicodeTests.test_bytesNonASCIIN)r3   r4   r5   r6   r  r	  r
  r  r   rC   r  r$   r$   r$   r%   r  )  s   		r  c                   @   s   e Zd ZdZdd ZdS )UnichrTestsz
    Tests for L{unichr}.
    c                 C   s   |  tdd dS )zX
        unichar exists and returns a unicode string with the given code point.
        i&  r   N)r   r   r"   r$   r$   r%   test_unichrg  s    zUnichrTests.test_unichrN)r3   r4   r5   r6   r  r$   r$   r$   r%   r  b  s   r  c                   @   s   e Zd ZdZdd ZdS )RawInputTestsz 
    Tests for L{raw_input}
    c                 C   sb   G dd d}G dd d}|  td|  | }|  td| | tdd | |jd d	S )
z4
        L{twisted.python.compat.raw_input}
        c                   @   s   e Zd Zdd ZdS )z/RawInputTests.test_raw_input.<locals>.FakeStdinc                 S   s   dS )NzUser input
r$   r"   r$   r$   r%   readlinew  s    z8RawInputTests.test_raw_input.<locals>.FakeStdin.readlineN)r3   r4   r5   r  r$   r$   r$   r%   	FakeStdinv  s   r  c                   @   s   e Zd ZdZdd ZdS )z0RawInputTests.test_raw_input.<locals>.FakeStdoutrx   c                 S   s   |  j |7  _ d S r[   )r   r   r$   r$   r%   r}   |  s    z6RawInputTests.test_raw_input.<locals>.FakeStdout.writeN)r3   r4   r5   r   r}   r$   r$   r$   r%   
FakeStdoutz  s   r  stdinstdoutZPromptz
User inputN)Zpatchr   r   r   r   )r#   r  r  r  r$   r$   r%   test_raw_inputr  s    zRawInputTests.test_raw_inputN)r3   r4   r5   r6   r  r$   r$   r$   r%   r  n  s   r  c                   @   s    e Zd ZdZdd Zdd ZdS )FutureBytesReprTestsz8
    Tests for L{twisted.python.compat._bytesRepr}.
    c                 C   s$   |  ttdg}| t|d dS )z
        L{twisted.python.compat._bytesRepr} raises a
        L{TypeError} when called any object that is not an instance of
        L{bytes}.
        z	not bytesz Expected bytes not ['not bytes']N)rU   r   r   ZassertEqualsr   r  r$   r$   r%   test_bytesReprNotBytes  s    z+FutureBytesReprTests.test_bytesReprNotBytesc                 C   s   |  tdd dS )z
        L{twisted.python.compat._bytesRepr} always prepends
        ``b`` to the returned repr on both Python 2 and 3.
            zb'\x00'N)r   r   r"   r$   r$   r%   test_bytesReprPrefix  s    z)FutureBytesReprTests.test_bytesReprPrefixN)r3   r4   r5   r6   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 )GetAsyncParamTestsz=
    Tests for L{twisted.python.compat._get_async_param}
    c                 C   sx   |  tddd |  tddd |  tf ddiddid |  tf 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async)r   r   rU   r   r"   r$   r$   r%   test_get_async_param  s      z'GetAsyncParamTests.test_get_async_paramc                 C   sF   |  tf 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  Fr   r   r   z:'async' keyword argument is deprecated, please use isAsync)r   r   r    test_get_async_param_deprecation)r#   r  r$   r$   r%   r     s     
z3GetAsyncParamTests.test_get_async_param_deprecationN)r3   r4   r5   r6   r  r   r$   r$   r$   r%   r    s   r  )@r6   Z
__future__r   r   rl   r   r   r   r>   Ztwisted.trialr   rs   r   r   r   r   r	   r
   r   r   r   r!   r   r   r   r   r   r   r   r   r   r   r   r   r   Ztwisted.python.filepathr   Ztwisted.python.runtimer   ZSynchronousTestCaser   rD   rh   rC   r{   r   r   rA   r   r   r   r   r   r   r   r   ZTestCaser   r   r  r  r  r  r  r$   r$   r$   r%   <module>   s@   (`j7@4@:U8059