ó
	Q˜[c           @@ s8  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 Z d  d l	 Z	 d  d l
 m Z d  d l m Z m Z m Z m Z m Z d  d l m Z m Z m Z e rÓ d  d l Z n  d „  Z d „  Z d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e j f d „  ƒ  YZ e j e d ƒ Z d „  Z  d „  Z! d „  Z" d „  Z# e$ e j d ƒ rže j j% e j _& n  e' d „ Z( e$ e j d ƒ rÓe e j d e( ƒ n  d e) f d „  ƒ  YZ* d e* f d „  ƒ  YZ+ e' d „ Z, e$ e j d ƒ r4e e j d e, ƒ n  d S(   i    (   t   print_functiont   absolute_importN(   t   dedent(   t   bind_methodt   PY26t   PY3t   PY2t   PY27(   t   check_outputt   STDOUTt   CalledProcessErrorc         C@ s&   |  j  d ƒ r |  d }  n  t |  ƒ S(   s,   
    Removes any leading 
 and dedents.
    s   
i   (   t
   startswithR   (   t   code(    (    s0   lib/python2.7/site-packages/future/tests/base.pyt   reformat_code   s    c         C@ sW  |  j  d ƒ } g  t | ƒ D]! \ } } | j d ƒ r | ^ q } g  t | ƒ D]0 \ } } | j d ƒ sz | j d ƒ rP | ^ qP } g  t | ƒ D]! \ } } | j d ƒ r“ | ^ q“ } |  j ƒ  |  k sØ t d ƒ ‚ d „  } d „  } | | ƒ | | ƒ k st d	 ƒ ‚ t g  | D] } | | ^ qƒ }	 t t | |	 ƒ ƒ }
 t g  | D] } | | ^ qPƒ } t t | | ƒ ƒ } t g  | D] } | | ^ qˆƒ } t t | | ƒ ƒ } g  } x‹ t t	 | ƒ ƒ D]w } | | k rõ| j
 |
 | ƒ qÏ| | k r| j
 | | ƒ qÏ| | k r5| j
 | | ƒ qÏ| j
 | | ƒ qÏWd j | ƒ S(
   s  
    Returns the code block with any ``__future__`` import lines sorted, and
    then any ``future`` import lines sorted, then any ``builtins`` import lines
    sorted.

    This only sorts the lines within the expected blocks.

    See test_order_future_lines() for an example.
    s   
s   from __future__ import s   from futures	   from pasts   from builtinssI   internal usage error: dedent the code before calling order_future_lines()c         S@ s    t  |  ƒ d k r t |  ƒ Sd S(   Ni    (   t   lent   max(   t   numbers(    (    s0   lib/python2.7/site-packages/future/tests/base.pyt   mymax7   s    c         S@ s&   t  |  ƒ d k r t |  ƒ St d ƒ S(   Ni    t   inf(   R   t   mint   float(   R   (    (    s0   lib/python2.7/site-packages/future/tests/base.pyt   mymin:   s    s2   the __future__ and future imports are out of order(   t   splitt	   enumerateR   t   lstript   AssertionErrort   sortedt   dictt   zipt   rangeR   t   appendt   join(   R   t   linest   it   linet   uufuture_line_numberst   future_line_numberst   builtins_line_numbersR   R   t   uult   sorted_uufuture_linest   flt   sorted_future_linest   blt   sorted_builtins_linest	   new_lines(    (    s0   lib/python2.7/site-packages/future/tests/base.pyt   order_future_lines   s:    			###t   VerboseCalledProcessErrorc           B@ s#   e  Z d  Z d d „ Z d „  Z RS(   s…   
    Like CalledProcessError, but it displays more information (message and
    script output) for diagnosing test failures etc.
    c         C@ s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   t   msgt
   returncodet   cmdt   output(   t   selfR/   R0   R1   R2   (    (    s0   lib/python2.7/site-packages/future/tests/base.pyt   __init__`   s    			c         C@ s    d |  j  |  j |  j |  j f S(   Ns>   Command '%s' failed with exit status %d
Message: %s
Output: %s(   R1   R0   R/   R2   (   R3   (    (    s0   lib/python2.7/site-packages/future/tests/base.pyt   __str__f   s    N(   t   __name__t
   __module__t   __doc__t   NoneR4   R5   (    (    (    s0   lib/python2.7/site-packages/future/tests/base.pyR.   [   s   t   FuturizeErrorc           B@ s   e  Z RS(    (   R6   R7   (    (    (    s0   lib/python2.7/site-packages/future/tests/base.pyR:   j   s   t   PasteurizeErrorc           B@ s   e  Z RS(    (   R6   R7   (    (    (    s0   lib/python2.7/site-packages/future/tests/base.pyR;   m   s   t   CodeHandlerc           B@ s­   e  Z d  Z d „  Z d e e e e e d „ Z e d „ Z d „  Z d e e e e e d „ Z	 d „  Z
 d	 d
 „ Z d	 d „ Z d	 d e e e d „ Z d	 e j d „ Z RS(   st   
    Handy mixin for test classes for writing / reading / futurizing /
    running .py files in the test suite.
    c         C@ s™   t  d ƒ |  _ t  d ƒ |  _ t j g |  _ t j ƒ  t j	 j
 |  _ t j d ƒ } | r i t j ƒ  t j | d 6|  _ n i t j ƒ  d 6|  _ d S(   si   
        The outputs from the various futurize stages should have the
        following headers:
        sŽ   
        from __future__ import absolute_import
        from __future__ import division
        from __future__ import print_function
        s4  
        from __future__ import absolute_import
        from __future__ import division
        from __future__ import print_function
        from __future__ import unicode_literals
        from future import standard_library
        standard_library.install_aliases()
        from builtins import *
        t
   PYTHONPATHN(   R   t   headers1t   headers2t   syst
   executablet   interpreterst   tempfilet   mkdtempt   ost   patht   sept   tempdirt   getenvt   getcwdt   pathsept   env(   R3   t   pypath(    (    s0   lib/python2.7/site-packages/future/tests/base.pyt   setUpv   s    $i   i   c      	   C@ sƒ   | r t  | ƒ } n  |  j | ƒ |  j d | d | d | d | ƒ |  j ƒ  } | r x& |  j D] }	 |  j d |	 ƒ }
 q` Wn  | S(   s•  
        Converts the code block using ``futurize`` and returns the
        resulting code.

        Passing stages=[1] or stages=[2] passes the flag ``--stage1`` or
        ``stage2`` to ``futurize``. Passing both stages runs ``futurize``
        with both stages by default.

        If from3 is False, runs ``futurize``, converting from Python 2 to
        both 2 and 3. If from3 is True, runs ``pasteurize`` to convert
        from Python 3 to both 2 and 3.

        Optionally reformats the code block first using the reformat() function.

        If run is True, runs the resulting code under all Python
        interpreters in self.interpreters.
        t   stagest   all_importst   from3t   conservativet   interpreter(   R   t   _write_test_scriptt   _futurize_test_scriptt   _read_test_scriptRB   t   _run_test_script(   R3   R   RO   RP   RQ   t   reformatt   runRR   R2   RS   t   _(    (    s0   lib/python2.7/site-packages/future/tests/base.pyt   convert¡   s    c         C@ s¯   | r' |  j  | ƒ } |  j  | ƒ } n  t | t ƒ rX t | t ƒ rX | j d ƒ } n  t | t ƒ r‰ t | t ƒ r‰ | j d ƒ } n  |  j t | j ƒ  ƒ | j ƒ  ƒ d S(   s›  
        Compares whether the code blocks are equal. If not, raises an
        exception so the test fails. Ignores any trailing whitespace like
        blank lines.

        If ignore_imports is True, passes the code blocks into the
        strip_future_imports method.

        If one code block is a unicode string and the other a
        byte-string, it assumes the byte-string is encoded as utf-8.
        s   utf-8N(   t   strip_future_importst
   isinstancet   bytest   decodet   assertEqualR-   t   rstrip(   R3   R2   t   expectedt   ignore_imports(    (    s0   lib/python2.7/site-packages/future/tests/base.pyt   compare¿   s    c         C@ s‘   g  } x{ | j  d ƒ D]j } | j d ƒ pm | j d ƒ pm | j d ƒ pm d | k pm d | k pm | j d ƒ s | j | ƒ q q Wd j | ƒ S(   s  
        Strips any of these import lines:

            from __future__ import <anything>
            from future <anything>
            from future.<anything>
            from builtins <anything>

        or any line containing:
            install_hooks()
        or:
            install_aliases()

        Limitation: doesn't handle imports split across multiple lines like
        this:

            from __future__ import (absolute_import, division, print_function,
                                    unicode_literals)
        s   
s   from __future__ import s   from future s   from builtins s   install_hooks()s   install_aliases()s   from future.(   R   R   R   R   (   R3   R   R2   R"   (    (    s0   lib/python2.7/site-packages/future/tests/base.pyR\   Õ   s    c	         C@ s~   |  j  | d | d | d | d | d | ƒ}	 | rT d | k rH |  j n |  j }
 n d }
 |  j |	 |
 t | ƒ d | ƒd	 S(
   s<  
        Convenience method that calls convert() and compare().

        Reformats the code blocks automatically using the reformat_code()
        function.

        If all_imports is passed, we add the appropriate import headers
        for the stage(s) selected to the ``expected`` code-block, so they
        needn't appear repeatedly in the test code.

        If ignore_imports is True, ignores the presence of any lines
        beginning:

            from __future__ import ...
            from future import ...

        for the purpose of the comparison.
        RO   RP   RQ   RY   RR   i   t    Rc   N(   R[   R?   R>   Rd   R   (   R3   t   beforeRb   RO   RP   Rc   RQ   RY   RR   R2   t   headers(    (    s0   lib/python2.7/site-packages/future/tests/base.pyt   convert_check÷   s    !c         K@ s   |  j  | | |  d S(   se   
        Convenience method to ensure the code is unchanged by the
        futurize process.
        N(   Rh   (   R3   R   t   kwargs(    (    s0   lib/python2.7/site-packages/future/tests/base.pyt	   unchanged  s    s   mytestscript.pyc         C@ s`   t  | t ƒ r! | j d ƒ } n  t j |  j | d d d ƒ } | j t | ƒ ƒ Wd QXd S(   sœ   
        Dedents the given code (a multiline string) and writes it out to
        a file in a temporary folder like /tmp/tmpUDCn7x/mytestscript.py.
        s   utf-8t   wtt   encodingN(   R]   R^   R_   t   iot   openRH   t   writeR   (   R3   R   t   filenamet   f(    (    s0   lib/python2.7/site-packages/future/tests/base.pyRT     s    "c         C@ s8   t  j |  j | d d d ƒ } | j ƒ  } Wd  QX| S(   Nt   rtRl   s   utf-8(   Rm   Rn   RH   t   read(   R3   Rp   Rq   t	   newsource(    (    s0   lib/python2.7/site-packages/future/tests/base.pyRV   (  s    "c      
   C@ sŠ  g  } t  | ƒ } | r( | j d ƒ n  | r7 d } nr d } | d g k r\ | j d ƒ n7 | d g k r{ | j d ƒ n | d d g k s“ t ‚ | r© | j d ƒ n  |  j | } t j | g | d	 | g }	 y t |	 d
 t d |  j ƒ}
 Wn‘ t	 k
 r…} t
 | ƒ 7 } d d j |	 ƒ d |  j | d | j ƒ  f } Wd  QXd | k r[t n t } | | | j | j d | j ƒ‚ n X|
 S(   Ns   --all-importss   pasteurize.pys   futurize.pyi   s   --stage1i   s   --stage2s   --conservatives   -wt   stderrRL   s8   Error running the command %s
%s
Contents of file %s:

%st    s   env=%ss   ----
%s
----t   futurizeR2   (   t   listR   R   RH   R@   RA   R   R	   RL   R
   Rn   R   Rs   R:   R;   R0   R1   R2   (   R3   Rp   RO   RP   RQ   RR   t   paramst   scriptt   fnt	   call_argsR2   t   eRq   R/   t
   ErrorClass(    (    s0   lib/python2.7/site-packages/future/tests/base.pyRU   -  s8    	
%c      
   C@ sÓ   |  j  | } y% t | | g d |  j d t ƒ} Wnš t k
 rÎ } t | ƒ = } d d j | | g ƒ d |  j | d | j ƒ  f } Wd  QXt | d ƒ sª d  | _
 n  t | | j | j d | j
 ƒ‚ n X| S(   NRL   Ru   s8   Error running the command %s
%s
Contents of file %s:

%sRv   s   env=%ss   ----
%s
----R2   (   RH   R   RL   R	   R
   Rn   R   Rs   t   hasattrR9   R2   R.   R0   R1   (   R3   Rp   RS   R{   R2   R}   Rq   R/   (    (    s0   lib/python2.7/site-packages/future/tests/base.pyRW   X  s    
%(   i   i   (   i   i   (   i   i   (   R6   R7   R8   RN   t   Falset   TrueR[   Rd   R\   Rh   Rj   RT   RV   RU   R@   RA   RW   (    (    (    s0   lib/python2.7/site-packages/future/tests/base.pyR<   q   s"   	+		"		)s#   this test is known to fail on Py2.6c         C@ s   t  s
 |  St j |  ƒ S(   N(   R   t   unittestt   expectedFailure(   t   func(    (    s0   lib/python2.7/site-packages/future/tests/base.pyt   expectedFailurePY3w  s    c         C@ s   t  s
 |  St j |  ƒ S(   N(   R   R‚   Rƒ   (   R„   (    (    s0   lib/python2.7/site-packages/future/tests/base.pyt   expectedFailurePY26|  s    c         C@ s   t  s
 |  St j |  ƒ S(   N(   R   R‚   Rƒ   (   R„   (    (    s0   lib/python2.7/site-packages/future/tests/base.pyt   expectedFailurePY27‚  s    c         C@ s   t  s
 |  St j |  ƒ S(   N(   R   R‚   Rƒ   (   R„   (    (    s0   lib/python2.7/site-packages/future/tests/base.pyt   expectedFailurePY2ˆ  s    t   assertRaisesRegexc         C@ s€   t  | t t f ƒ r9 | s' t d ƒ ‚ t j | ƒ } n  | j | ƒ s| | pQ d } d | | j | f } |  j | ƒ ‚ n  d S(   s=   Fail the test unless the text matches the regular expression.s!   expected_regex must not be empty.s   Regex didn't matchs   %s: %r not found in %rN(	   R]   t   strt   unicodeR   t   ret   compilet   searcht   patternt   failureException(   R3   t   textt   expected_regexR/   (    (    s0   lib/python2.7/site-packages/future/tests/base.pyt   assertRegex“  s    R“   t   _AssertRaisesBaseContextc           B@ s)   e  Z d d d  „ Z d „  Z d „  Z RS(   c         C@ sš   | |  _  | |  _ | d  k	 rT y | j |  _ Wq] t k
 rP t | ƒ |  _ q] Xn	 d  |  _ t | t t f ƒ r„ t	 j
 | ƒ } n  | |  _ d  |  _ d  S(   N(   Rb   t	   test_caseR9   R6   t   obj_namet   AttributeErrorRŠ   R]   R^   RŒ   R   R’   R/   (   R3   Rb   R•   t   callable_objR’   (    (    s0   lib/python2.7/site-packages/future/tests/base.pyR4   ¢  s    				c         C@ s.   |  j  j |  j | ƒ } |  j  j | ƒ ‚ d  S(   N(   R•   t   _formatMessageR/   R   (   R3   t   standardMsgR/   (    (    s0   lib/python2.7/site-packages/future/tests/base.pyt   _raiseFailure²  s    c         C@ sC   | d k r% | j d d ƒ |  _ |  S|   | | | Ž  Wd QXd S(   sÛ   
        If callable_obj is None, assertRaises/Warns is being used as a
        context manager, so check for a 'msg' kwarg and return self.
        If callable_obj is not None, call it passing args and kwargs.
        R/   N(   R9   t   popR/   (   R3   t   nameR˜   t   argsRi   (    (    s0   lib/python2.7/site-packages/future/tests/base.pyt   handle¶  s
    N(   R6   R7   R9   R4   R›   RŸ   (    (    (    s0   lib/python2.7/site-packages/future/tests/base.pyR”      s   	t   _AssertWarnsContextc           B@ s    e  Z d  Z d „  Z d „  Z RS(   sB   A context manager used to implement TestCase.assertWarns* methods.c         C@ sv   x5 t  j j ƒ  D]$ } t | d d  ƒ r i  | _ q q Wt j d t ƒ |  _	 |  j	 j
 ƒ  |  _ t j d |  j ƒ |  S(   Nt   __warningregistry__t   recordt   always(   R@   t   modulest   valuest   getattrR9   R¡   t   warningst   catch_warningsR   t   warnings_managert	   __enter__t   simplefilterRb   (   R3   t   v(    (    s0   lib/python2.7/site-packages/future/tests/base.pyRª   Å  s    c         C@ so  |  j  j | | | ƒ | d  k	 r& d  Sy |  j j } Wn  t k
 rX t |  j ƒ } n Xd  } x— |  j D]Œ } | j } t	 | |  j ƒ s qi n  | d  k r¥ | } n  |  j
 d  k	 rÓ |  j
 j t | ƒ ƒ rÓ qi n  | |  _ | j |  _ | j |  _ d  SW| d  k	 r-|  j d j |  j
 j t | ƒ ƒ ƒ n  |  j rU|  j d j | |  j ƒ ƒ n |  j d j | ƒ ƒ d  S(   Ns   "{}" does not match "{}"s   {} not triggered by {}s   {} not triggered(   R©   t   __exit__R9   Rb   R6   R—   RŠ   R§   t   messageR]   R’   RŽ   t   warningRp   t   linenoR›   t   formatR   R–   (   R3   t   exc_typet	   exc_valuet   tbt   exc_namet   first_matchingt   mt   w(    (    s0   lib/python2.7/site-packages/future/tests/base.pyR­   Ð  s8    				(   R6   R7   R8   Rª   R­   (    (    (    s0   lib/python2.7/site-packages/future/tests/base.pyR    Â  s   	c         O@ s(   t  | |  | ƒ } | j d | | | ƒ S(   s•  Fail unless a warning of class warnClass is triggered
       by callable_obj when invoked with arguments args and keyword
       arguments kwargs.  If a different type of warning is
       triggered, it will not be handled: depending on the other
       warning filtering rules in effect, it might be silenced, printed
       out, or raised as an exception.

       If called with callable_obj omitted or None, will return a
       context object used like this::

            with self.assertWarns(SomeWarning):
                do_something()

       An optional keyword argument 'msg' can be provided when assertWarns
       is used as a context object.

       The context manager keeps a reference to the first matching
       warning as the 'warning' attribute; similarly, the 'filename'
       and 'lineno' attributes give you information about the line
       of Python code from which the warning was triggered.
       This allows you to inspect the warning after the assertion::

           with self.assertWarns(SomeWarning) as cm:
               do_something()
           the_warning = cm.warning
           self.assertEqual(the_warning.some_attribute, 147)
    t   assertWarns(   R    RŸ   (   R3   t   expected_warningR˜   Rž   Ri   t   context(    (    s0   lib/python2.7/site-packages/future/tests/base.pyR¹   ó  s    R¹   (-   t
   __future__R    R   RE   RC   R‚   R@   RŒ   R§   Rm   t   textwrapR   t   future.utilsR   R   R   R   R   t   future.moves.subprocessR   R	   R
   t	   unittest2R   R-   R.   R:   R;   t   TestCaseR<   t   skipIft   skip26R…   R†   R‡   Rˆ   R   t   assertRaisesRegexpR‰   R9   R“   t   objectR”   R    R¹   (    (    (    s0   lib/python2.7/site-packages/future/tests/base.pyt   <module>   sD   (			@ÿ 				
"1