ó
,º]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(    (    s5   /usr/lib/python2.7/site-packages/future/tests/base.pyt   reformat_code   s    c         C@ s  |  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“ } d „  } d „  } t g  | D] } | | ^ qÖ ƒ }	 t t | |	 ƒ ƒ }
 t g  | D] } | | ^ qƒ } t t | | ƒ ƒ } t g  | D] } | | ^ qFƒ } t t | | ƒ ƒ } g  } x‹ t t | ƒ ƒ D]w } | | k r³| j |
 | ƒ q| | k rÓ| j | | ƒ q| | k ró| 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 builtinsc         S@ s    t  |  ƒ d k r t |  ƒ Sd S(   Ni    (   t   lent   max(   t   numbers(    (    s5   /usr/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   (    (    s5   /usr/lib/python2.7/site-packages/future/tests/base.pyt   mymin:   s    (
   t   splitt	   enumerateR   t   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(    (    s5   /usr/lib/python2.7/site-packages/future/tests/base.pyt   order_future_lines   s4    			###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-   R.   R/   R0   (    (    s5   /usr/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(   R/   R.   R-   R0   (   R1   (    (    s5   /usr/lib/python2.7/site-packages/future/tests/base.pyt   __str__f   s    N(   t   __name__t
   __module__t   __doc__t   NoneR2   R3   (    (    (    s5   /usr/lib/python2.7/site-packages/future/tests/base.pyR,   [   s   t   FuturizeErrorc           B@ s   e  Z RS(    (   R4   R5   (    (    (    s5   /usr/lib/python2.7/site-packages/future/tests/base.pyR8   j   s   t   PasteurizeErrorc           B@ s   e  Z RS(    (   R4   R5   (    (    (    s5   /usr/lib/python2.7/site-packages/future/tests/base.pyR9   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(   R1   t   pypath(    (    s5   /usr/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_scriptR@   t   _run_test_script(   R1   R   RM   RN   RO   t   reformatt   runRP   R0   RQ   t   _(    (    s5   /usr/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(   R1   R0   t   expectedt   ignore_imports(    (    s5   /usr/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   (   R1   R   R0   R    (    (    s5   /usr/lib/python2.7/site-packages/future/tests/base.pyRZ   Õ   s    c	         C@ s™   |  j  | d | d | d | d | d | ƒ}	 | rT d | k rH |  j n |  j }
 n d }
 t | ƒ } |
 | k r{ d }
 n  |  j |	 |
 | 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.
        RM   RN   RO   RW   RP   i   t    Ra   N(   RY   R=   R<   R   Rb   (   R1   t   beforeR`   RM   RN   Ra   RO   RW   RP   R0   t   headerst   reformatted(    (    s5   /usr/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(   Rg   (   R1   R   t   kwargs(    (    s5   /usr/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   openRF   t   writeR   (   R1   R   t   filenamet   f(    (    s5   /usr/lib/python2.7/site-packages/future/tests/base.pyRR   !  s    "c         C@ s8   t  j |  j | d d d ƒ } | j ƒ  } Wd  QX| S(   Nt   rtRk   s   utf-8(   Rl   Rm   RF   t   read(   R1   Ro   Rp   t	   newsource(    (    s5   /usr/lib/python2.7/site-packages/future/tests/base.pyRT   ,  s    "c      
   C@ s  g  } t  | ƒ } | r( | j d ƒ n  | r7 d } nZ d } | d g k r\ | j d ƒ n | d g k r{ | j d ƒ n  | 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 rCt n t } t | d ƒ sdd  | _ n  | | | j | j d | j ƒ‚ n X|
 S(   Ns   --all-importss   pasteurize.pys   futurize.pyi   s   --stage1i   s   --stage2s   --conservatives   -wt   stderrRJ   s8   Error running the command %s
%s
Contents of file %s:

%st    s   env=%ss   ----
%s
----t   futurizeR0   (   t   listR   RF   R>   R?   R   R	   RJ   R
   Rm   R   Rr   R8   R9   t   hasattrR7   R0   R.   R/   (   R1   Ro   RM   RN   RO   RP   t   paramst   scriptt   fnt	   call_argsR0   t   eRp   R-   t
   ErrorClass(    (    s5   /usr/lib/python2.7/site-packages/future/tests/base.pyRS   1  s:    	
%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(   NRJ   Rt   s8   Error running the command %s
%s
Contents of file %s:

%sRu   s   env=%ss   ----
%s
----R0   (   RF   R   RJ   R	   R
   Rm   R   Rr   Rx   R7   R0   R,   R.   R/   (   R1   Ro   RQ   R{   R0   R}   Rp   R-   (    (    s5   /usr/lib/python2.7/site-packages/future/tests/base.pyRU   `  s    
%(   i   i   (   i   i   (   i   i   (   R4   R5   R6   RL   t   Falset   TrueRY   Rb   RZ   Rg   Ri   RR   RT   RS   R>   R?   RU   (    (    (    s5   /usr/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(    (    s5   /usr/lib/python2.7/site-packages/future/tests/base.pyt   expectedFailurePY3  s    c         C@ s   t  s
 |  St j |  ƒ S(   N(   R   R   R‚   (   Rƒ   (    (    s5   /usr/lib/python2.7/site-packages/future/tests/base.pyt   expectedFailurePY26„  s    c         C@ s   t  s
 |  St j |  ƒ S(   N(   R   R   R‚   (   Rƒ   (    (    s5   /usr/lib/python2.7/site-packages/future/tests/base.pyt   expectedFailurePY27Š  s    c         C@ s   t  s
 |  St j |  ƒ S(   N(   R   R   R‚   (   Rƒ   (    (    s5   /usr/lib/python2.7/site-packages/future/tests/base.pyt   expectedFailurePY2  s    t   assertRaisesRegexc         C@ sn   t  | t t f ƒ r' t j | ƒ } n  | j | ƒ sj | p? d } d | | j | f } |  j | ƒ ‚ n  d S(   s=   Fail the test unless the text matches the regular expression.s   Regex didn't matchs   %s: %r not found in %rN(   R[   t   strt   unicodet   ret   compilet   searcht   patternt   failureException(   R1   t   textt   expected_regexR-   (    (    s5   /usr/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(   R`   t	   test_caseR7   R4   t   obj_namet   AttributeErrorR‰   R[   R\   R‹   RŒ   R‘   R-   (   R1   R`   R”   t   callable_objR‘   (    (    s5   /usr/lib/python2.7/site-packages/future/tests/base.pyR2   ª  s    				c         C@ s.   |  j  j |  j | ƒ } |  j  j | ƒ ‚ d  S(   N(   R”   t   _formatMessageR-   R   (   R1   t   standardMsgR-   (    (    s5   /usr/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(   R7   t   popR-   (   R1   t   nameR—   t   argsRh   (    (    s5   /usr/lib/python2.7/site-packages/future/tests/base.pyt   handle¾  s
    N(   R4   R5   R7   R2   Rš   Rž   (    (    (    s5   /usr/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   getattrR7   R    t   warningst   catch_warningsR€   t   warnings_managert	   __enter__t   simplefilterR`   (   R1   t   v(    (    s5   /usr/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__R7   R`   R4   R–   R‰   R¦   t   messageR[   R‘   R   t   warningRo   t   linenoRš   t   formatRŽ   R•   (   R1   t   exc_typet	   exc_valuet   tbt   exc_namet   first_matchingt   mt   w(    (    s5   /usr/lib/python2.7/site-packages/future/tests/base.pyR¬   Ø  s8    				(   R4   R5   R6   R©   R¬   (    (    (    s5   /usr/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ž   (   R1   t   expected_warningR—   R   Rh   t   context(    (    s5   /usr/lib/python2.7/site-packages/future/tests/base.pyR¸   û  s    R¸   (-   t
   __future__R    R   RC   RA   R   R>   R‹   R¦   Rl   t   textwrapR   t   future.utilsR   R   R   R   R   t   future.moves.subprocessR   R	   R
   t	   unittest2R   R+   R,   R8   R9   t   TestCaseR:   t   skipIft   skip26R„   R…   R†   R‡   Rx   t   assertRaisesRegexpRˆ   R7   R’   t   objectR“   RŸ   R¸   (    (    (    s5   /usr/lib/python2.7/site-packages/future/tests/base.pyt   <module>   sD   (			@ÿ 				
"1