
    ϪfF                        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	e      Z G d
 de      Z G d de      Z G d de      Ze G d d             Z  G d de      Z! G d de      Z" G d de      Z# G d de      Z$ G d de      Z% G d de      Z& G d de      Z' G d  d!e      Z( G d" d#e      Z)y)$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                   :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
IOTypeTestsz?
    Test cases for determining a file-like object's type.
    c                 f    | j                  t        t        j                               t               y)z=
        An L{io.StringIO} accepts and returns text.
        N)assertEqualr   ioStringIOstrselfs    :/usr/lib/python3/dist-packages/twisted/test/test_compat.pytest_3StringIOzIOTypeTests.test_3StringIO)   s     	.4    c                 f    | j                  t        t        j                               t               y)z=
        An L{io.BytesIO} accepts and returns bytes.
        N)r   r   r   BytesIObytesr   s    r   test_3BytesIOzIOTypeTests.test_3BytesIO/   s     	

-u5r    c                     t        | j                         d      5 }| j                  t        |      t               ddd       y# 1 sw Y   yxY w)zT
        A file opened via 'io.open' in text mode accepts and returns text.
        wN)openmktempr   r   r   r   fs     r   test_3openTextModezIOTypeTests.test_3openTextMode5   s?     $++-% 	-VAY,	- 	- 	-    AAc                     t        | j                         d      5 }| j                  t        |      t               ddd       y# 1 sw Y   yxY w)zW
        A file opened via 'io.open' in binary mode accepts and returns bytes.
        wbN)r'   r(   r   r   r#   r)   s     r   test_3openBinaryModez IOTypeTests.test_3openBinaryMode<   s?     $++-& 	/!VAY.	/ 	/ 	/r,   c                     t        j                  | j                         d      5 }| j                  t	        |      t
               ddd       y# 1 sw Y   yxY w)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)   s     r   test_codecsOpenBytesz IOTypeTests.test_codecsOpenBytesC   sC    
 [[- 	/VAY.	/ 	/ 	/s    AAc                     t        j                  | j                         dd      5 }| j                  t	        |      t
               ddd       y# 1 sw Y   yxY w)zY
        When passed an encoding, however, the L{codecs} module returns unicode.
        r.   utf-8)encodingN)r1   r'   r(   r   r   r   r)   s     r   test_codecsOpenTextzIOTypeTests.test_codecsOpenTextK   sE     [[w? 	-1VAY,	- 	- 	-s    AAc                 R    | j                  t        t                     t               y)zy
        When passed an object about which no sensible decision can be made, err
        on the side of unicode.
        N)r   r   objectr   r   s    r   test_defaultToTextzIOTypeTests.test_defaultToTextR   s    
 	)3/r    N)__name__
__module____qualname____doc__r   r$   r+   r/   r2   r6   r9    r    r   r   r   $   s*    56-//-0r    r   c                       e Zd ZdZd Zd Zy)CompatTestszy
    Various utility functions in C{twisted.python.compat} provide same
    functionality as modern Python variants.
    c                    t               }|j                  d       |j                  d       |j                  d       t        |      }|j                          | j	                  |g d       |j                  d       t        |      }|j                          | j	                  |ddg       |j                  d       ddh}|j                  |      }t        |      }|j                          | j	                  |g d       y	)
zG
        L{set} should behave like the expected set interface.
        bca)rD   rB   rC   drs)rD   rC   rF   rG   N)setaddlistsortr   removediscardunionr   rD   rB   rE   s       r   test_setzCompatTests.test_set`   s     E	c
	c
	c
G	O,	G	S#J'			##JGGAJG	01r    c                 *   t        ddg      }| j                  t        t        |d       | j	                  t        |      ddg       t        ddg      }|j                  |      }t        |      }|j                          | j	                  |g d       y)zS
        L{frozenset} should behave like the expected frozenset interface.
        rD   rB   rI   rF   rG   )rD   rB   rF   rG   N)		frozensetassertRaisesAttributeErrorgetattrr   sortedrN   rJ   rK   rO   s       r   test_frozensetzCompatTests.test_frozensetx   s|     sCj!.'1e<S#J/sCj!GGAJG	01r    N)r:   r;   r<   r=   rP   rW   r>   r    r   r@   r@   Z   s    
202r    r@   c                   (    e Zd ZdZd Zd Zd Zd Zy)ExecfileCompatTestszE
    Tests for the Python 3-friendly L{execfile} implementation.
    c                     | j                         }t        |d      5 }|j                  |j                  d             ddd       t	        |j                  d            S # 1 sw Y   #xY w)zo
        Write L{content} to a new temporary file, returning the L{FilePath}
        for the new file.
        r.   asciiNr4   )r(   r'   writeencoder   )r   contentpathr*   s       r   writeScriptzExecfileCompatTests.writeScript   sY    
 {{}$ 	-GGGNN7+,	-G,--	- 	-s   !A  A)c                     | j                  d      }ddi}t        |j                  |       | j                  d|d          y)zX
        L{execfile} executes the specified file in the given global namespace.
        	foo += 1
foo      Nr`   r	   r_   r   )r   scriptglobalNamespaces      r   test_execfileGlobalsz(ExecfileCompatTests.test_execfileGlobals   s@     !!,/ !*o.OE23r    c                     | j                  d      }ddi}ddi}t        |j                  ||       | j                  d|d          | j                  d|d          y)zk
        L{execfile} executes the specified file in the given global and local
        namespaces.
        rb   rc   
         Nrf   )r   rg   rh   localNamespaces       r   test_execfileGlobalsAndLocalsz1ExecfileCompatTests.test_execfileGlobalsAndLocals   s`    
 !!,/ "+o~>_U34^E23r    c                     dD ]E  }| j                  d|z         }ddi}t        |j                  |       | j                  d|d          G y)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'rc   Nokayrf   )r   
lineEndingrg   rh   s       r   test_execfileUniversalNewlinesz2ExecfileCompatTests.test_execfileUniversalNewlines   sT    
 - 	=J%%nz&ABF$dmOV[[/2V_U%;<		=r    N)r:   r;   r<   r=   r`   ri   ro   ru   r>   r    r   rY   rY      s    .4
4	=r    rY   c                       e Zd ZdZd Zy)PYPYTestz!
    Identification of PyPy.
    c                 ~    dt         j                  v r| j                  t               y| j	                  t               y)z,
        On PyPy, L{_PYPY} is True.
        PyPyN)sysversion
assertTruer   assertFalser   s    r   	test_PYPYzPYPYTest.test_PYPY   s*     S[[ OOE"U#r    N)r:   r;   r<   r=   r~   r>   r    r   rw   rw      s    $r    rw   c                       e Zd ZdZd Zd Zy)
ComparablezE
    Objects that can be compared to each other, but not others.
    c                     || _         y N)value)r   r   s     r   __init__zComparable.__init__   s	    
r    c                 n    t        |t              st        S t        | j                  |j                        S r   )
isinstancer   NotImplementedr   r   )r   others     r   __cmp__zComparable.__cmp__   s'    %,!!4::u{{++r    N)r:   r;   r<   r=   r   r   r>   r    r   r   r      s    ,r    r   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	ComparableTestszR
    L{comparable} decorated classes emulate Python 2's C{__cmp__} semantics.
    c                     | j                  t        d      t        d      k(         | j                  t        d      t        d      k(         y)zo
        Instances of a class that is decorated by C{comparable} support
        equality comparisons.
        rd   re   Nr|   r   r}   r   s    r   test_equalityzComparableTests.test_equality   s8     	
1A67A*Q-78r    c                     | j                  t        d      t        d      k7         | j                  t        d      t        d      k7         y)zq
        Instances of a class that is decorated by C{comparable} support
        inequality comparisons.
        rd   re   N)r}   r   r|   r   s    r   test_nonEqualityz ComparableTests.test_nonEquality   s8     	A*Q-78
1A67r    c                     | j                  t        d      t        d      kD         | j                  t        d      t        d      kD         y)zs
        Instances of a class that is decorated by C{comparable} support
        greater-than comparisons.
        re   rd   r      Nr   r   s    r   test_greaterThanz ComparableTests.test_greaterThan   8    
 	
1
156AA67r    c                     | j                  t        d      t        d      k\         | j                  t        d      t        d      k\         | j                  t        d      t        d      k\         y)z|
        Instances of a class that is decorated by C{comparable} support
        greater-than-or-equal comparisons.
        rd   re   r   r   Nr   r   s    r   test_greaterThanOrEqualz'ComparableTests.test_greaterThanOrEqual   Q    
 	
1A67
1A67A*Q-78r    c                     | j                  t        d      t        d      k         | j                  t        d      t        d      k         y)zp
        Instances of a class that is decorated by C{comparable} support
        less-than comparisons.
        r   r   re   Nr   r   s    r   test_lessThanzComparableTests.test_lessThan   r   r    c                     | j                  t        d      t        d      k         | j                  t        d      t        d      k         | j                  t        d      t        d      k         y)zy
        Instances of a class that is decorated by C{comparable} support
        less-than-or-equal comparisons.
        r   r   re   Nr   r   s    r   test_lessThanOrEqualz$ComparableTests.test_lessThanOrEqual  r   r    N)
r:   r;   r<   r=   r   r   r   r   r   r   r>   r    r   r   r      s%    988989r    r   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	Python3ComparableTestsz;
    Python 3-specific functionality of C{comparable}.
    c                 p    | j                  t        d      j                  t                     t               y)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.
        rd   N)r   r   __eq__r8   r   r   s    r   test_notImplementedEqualsz0Python3ComparableTests.test_notImplementedEquals  %     	A--fh7Hr    c                 p    | j                  t        d      j                  t                     t               y)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.
        rd   N)r   r   __ne__r8   r   r   s    r   test_notImplementedNotEqualsz3Python3ComparableTests.test_notImplementedNotEquals  r   r    c                 p    | j                  t        d      j                  t                     t               y)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.
        rd   N)r   r   __gt__r8   r   r   s    r   test_notImplementedGreaterThanz5Python3ComparableTests.test_notImplementedGreaterThan$  r   r    c                 p    | j                  t        d      j                  t                     t               y)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.
        rd   N)r   r   __lt__r8   r   r   s    r   test_notImplementedLessThanz2Python3ComparableTests.test_notImplementedLessThan,  r   r    c                 p    | j                  t        d      j                  t                     t               y)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.
        rd   N)r   r   __ge__r8   r   r   s    r   $test_notImplementedGreaterThanEqualsz;Python3ComparableTests.test_notImplementedGreaterThanEquals4  r   r    c                 p    | j                  t        d      j                  t                     t               y)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.
        rd   N)r   r   __le__r8   r   r   s    r   !test_notImplementedLessThanEqualsz8Python3ComparableTests.test_notImplementedLessThanEquals<  r   r    N)
r:   r;   r<   r=   r   r   r   r   r   r   r>   r    r   r   r     s+    IIIIIIr    r   c                   "    e Zd ZdZd Zd Zd Zy)CmpTestszA
    L{cmp} should behave like the built-in Python 2 C{cmp}.
    c                     | j                  t        dd      d       | j                  t        dd      d       | j                  t        dgdg      d       y)z5
        L{cmp} returns 0 for equal objects.
        rD   r   rd   Nr   r   r   s    r   test_equalszCmpTests.test_equalsJ  sJ     	S#*QA&aS1#*r    c                 t    | j                  t        dd      d       | j                  t        dd      d       y)zS
        L{cmp} returns 1 if its first argument is bigger than its second.
           r   rd      z   aNr   r   s    r   r   zCmpTests.test_greaterThanR  s0     	QA&T4!,r    c                 t    | j                  t        dd      d       | j                  t        dd      d       y)zU
        L{cmp} returns -1 if its first argument is smaller than its second.
        g?gffffff@r      dNr   r   s    r   r   zCmpTests.test_lessThanY  s0     	S#+T4"-r    N)r:   r;   r<   r=   r   r   r   r>   r    r   r   r   E  s    +-.r    r   c                   :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
StringTestsz8
    Compatibility functions and types for strings.
    c                 x    | j                  t        |      |       | j                  t        |      t               y)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expecteds      r   assertNativeStringzStringTests.assertNativeStringf  s.     	h/:l84c:r    c                 :    | j                  t        t        d       y)zj
        C{nativeString} raises a C{UnicodeError} if input bytes are not ASCII
        decodable.
           NrS   UnicodeErrorr   r   s    r   test_nonASCIIBytesToStringz&StringTests.test_nonASCIIBytesToStringo  s    
 	,g>r    c                 :    | j                  t        t        d       y)zk
        C{nativeString} raises a C{UnicodeError} if input Unicode is not ASCII
        encodable.
        u   ሴNr   r   s    r   test_nonASCIIUnicodeToStringz(StringTests.test_nonASCIIUnicodeToStringv  s    
 	,h?r    c                 (    | j                  dd       y)z
        C{nativeString} converts bytes to the native string format, assuming
        an ASCII encoding if applicable.
        s   hellohelloNr   r   s    r   test_bytesToStringzStringTests.test_bytesToString}  s    
 	'2r    c                 (    | j                  dd       y)z
        C{nativeString} converts unicode to the native string format, assuming
        an ASCII encoding if applicable.
        zGood dayNr   r   s    r   test_unicodeToStringz StringTests.test_unicodeToString  s    
 	
J7r    c                 (    | j                  dd       y)zJ
        C{nativeString} leaves native strings as native strings.
        zHello!Nr   r   s    r   test_stringToStringzStringTests.test_stringToString  s     	(3r    c                 :    | j                  t        t        d       y)zu
        C{nativeString} raises a C{TypeError} if given an object that is not a
        string of some sort.
        rd   N)rS   	TypeErrorr   r   s    r   test_unexpectedTypezStringTests.test_unexpectedType  s    
 	)\15r    N)r:   r;   r<   r=   r   r   r   r   r   r   r   r>   r    r   r   r   a  s+    ;?@3846r    r   c                   "    e Zd ZdZd Zd Zd Zy)NetworkStringTestsz%
    Tests for L{networkString}.
    c                 :    | j                  dt        d             y)zu
        L{networkString} returns a C{unicode} object passed to it encoded into
        a C{bytes} instance.
        s   foorc   N)r   r   r   s    r   test_strzNetworkStringTests.test_str  s    
 	u!56r    c                 :    | j                  t        t        d       y)z
        L{networkString} raises L{UnicodeError} if passed a C{unicode} instance
        containing characters not encodable in ASCII.
        u   ☃N)rS   r   r   r   s    r   test_unicodeOutOfRangez)NetworkStringTests.test_unicodeOutOfRange  s    
 	,}Er    c                     | j                  t        t        t                      | j                  t        t        d       y)z
        L{networkString} raises L{TypeError} if passed a non-string object or
        the wrong type of string object.
        s   bytesN)rS   r   r   r8   r   s    r   test_nonStringz!NetworkStringTests.test_nonString  s*    
 	)]FH=)]H=r    N)r:   r;   r<   r=   r   r   r   r>   r    r   r   r     s    7F>r    r   c                       e Zd ZdZd Zd Zy)ReraiseTestszH
    L{reraise} re-raises exceptions on both Python 2 and Python 3.
    c                    	 ddz   	 t        d       | j	                  d       y# t         $ r t        j                         \  }}}Y Bw xY w# t         $ r t        j                         \  }}}| j                  |t               | j                  |       | j                  t        j                        d   t        j                  |      d          Y yw xY w)z
        Calling L{reraise} with an exception instance and a traceback of
        L{None} re-raises it with a new traceback.
        rd   r   NThe exception was not raised.r   )BaseExceptionrz   exc_infor   failr   ZeroDivisionErrorassertIsassertNotEqual	traceback	format_tbr   typr   tbtyp2value2tb2s          r   test_reraiseWithNonez!ReraiseTests.test_reraiseWithNone  s    
	,E
	7E4  II56  	, \\^NC	,  	 #D&#T#45MM%(##B'+Y-@-@-Eb-I		!   & A !A
	A
BCCc                    	 ddz   	 t               | j	                  d       y# t         $ r t        j                         \  }}}Y Bw xY w# t         $ r t        j                         \  }}}| j                  |t               | j                  |       | j                  t        j                        d   t        j                  |      d          Y yw xY w)z
        Calling L{reraise} with an exception instance and a traceback
        re-raises the exception with the given traceback.
        rd   r   r   r   N)
r   rz   r   r   r   r   r   r   r   r   r   s          r   test_reraiseWithTracebackz&ReraiseTests.test_reraiseWithTraceback  s    
	,E	7E2 II56  	, \\^NC	,  	X #D&#T#45MM%(Y004R8):M:Mc:RSU:VW		Xr   N)r:   r;   r<   r=   r   r   r>   r    r   r   r     s    7*7r    r   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)Python3BytesTestszB
    Tests for L{iterbytes}, L{intToBytes}, L{lazyByteSlice}.
    c                 X    d}t        t        |            }| j                  |g d       y)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)rJ   r   r   )r   inputresults      r   test_iterationz Python3BytesTests.test_iteration  s)     i&'!9:r    c                 :    | j                  t        d      d       y)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   s    r   test_intToBytesz!Python3BytesTests.test_intToBytes  s    
 	C&1r    c                 P    d}| j                  t        t        |            |       y)zv
        L{lazyByteSlice} called with some bytes returns a semantically equal
        version of these bytes.
           123XYZNr   r#   r   r   datas     r   test_lazyByteSliceNoOffsetz,Python3BytesTests.test_lazyByteSliceNoOffset  s#    
 }T23T:r    c                 X    d}| j                  t        t        |d            |dd        y)z
        L{lazyByteSlice} called with some bytes and an offset returns a
        semantically equal version of these bytes starting at the given offset.
        r   re   Nr  r  s     r   test_lazyByteSliceOffsetz*Python3BytesTests.test_lazyByteSliceOffset  s+    
 }T156QRAr    c           	      Z    d}| j                  t        t        |dd            |dd        y)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   re   r      Nr  r  s     r   !test_lazyByteSliceOffsetAndLengthz3Python3BytesTests.test_lazyByteSliceOffsetAndLength	  s-     }T1a894!9Er    N)	r:   r;   r<   r=   r   r   r  r  r	  r>   r    r   r   r     s"    ;2;BFr    r   c                   L    e Zd ZdZ e ej                         d      d        Zy)BytesEnvironTestsz$
    Tests for L{BytesEnviron}.
    z+Environment vars are always str on Windows.c                    t               }t               }|j                         D ]9  \  }}|j                  t	        |             |j                  t	        |             ; | j                  t        |      t        g       y)zz
        The output of L{BytesEnviron} should always be a L{dict} with L{bytes}
        values and L{bytes} keys.
        N)r   rH   itemsrI   typer   rJ   r#   )r   r   typeskeyvals        r   test_alwaysBytesz"BytesEnvironTests.test_alwaysBytes  sc      	!HCIId3i IId3i 	! 	eug.r    N)r:   r;   r<   r=   r   r   	isWindowsr  r>   r    r   r  r    s0     H "OP/ Q/r    r  c                       e Zd ZdZd Zd Zy)GetAsyncParamTestsz=
    Tests for L{twisted.python.compat._get_async_param}
    c                 ,   | j                  t        d      d       | j                  t        d      d       | j                  t        dddiddid       | j                  t        dddiddid       | j                  t        t        dddi       y)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   rS   r   r   s    r   test_get_async_paramz'GetAsyncParamTests.test_get_async_param-  s    
 	)%8%@)$7>)K$K7E:JKUS)J$J7D/JDQ)%5uw>NOr    c                     | j                  t        d	ddiddid       | j                  | j                  g      }| j                  |d   d   d       y)
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   currentWarningss     r   r  z3GetAsyncParamTests.test_get_async_param_deprecation8  sh    
 	)K$K7E:JKUS,, $ E EF - 
 	Ay)H	
r    N)r:   r;   r<   r=   r  r  r>   r    r   r  r  (  s    	P
r    r  )*r=   r1   r   rz   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@   rY   rw   r   r   r   r   r   r   r   r   r  r  r>   r    r   <module>r%     s  

  	 
      - + @30% 30l*2% *2Z-=- -=`$" $ , , ,79) 79t3I0 3Il." .856% 56p>, >8+7& +7\-F+ -F`/ /*
, 
r    