3
0cM                 @   s  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rd dlZdd Zdd	 ZG d
d deZG dd deZG dd deZG dd dejZejedZdd Z dd Z!dd Z"dd Z#e$ejds(ejj%ej_&d$ddZ'e$ejdsNeejde' G dd de(Z)G d d! d!e)Z*d%d"d#Z+e$ejd#seejd#e+ dS )&    )print_functionabsolute_importN)dedent)bind_methodPY26PY3PY2PY27)check_outputSTDOUTCalledProcessErrorc             C   s   | j dr| dd } t| S )z,
    Removes any leading 
 and dedents.
    
   N)
startswithr   )code r   P/var/www/html/my_flask_app/venv/lib/python3.6/site-packages/future/tests/base.pyreformat_code   s    
r   c                sh  | j d dd t D }dd t D }dd t D }| j | ksTtddd }d	d
 }||||ks|tdt fdd|D }tt||}t fdd|D }tt||}	t fdd|D }
tt||
}g }xptt D ]`}||kr|j	||  q||kr2|j	|	|  q||krL|j	||  q|j	 |  qW dj
|S )a  
    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.
    r   c             S   s   g | ]\}}|j d r|qS )zfrom __future__ import )r   ).0iliner   r   r   
<listcomp>*   s    z&order_future_lines.<locals>.<listcomp>c             S   s(   g | ] \}}|j d s |j dr|qS )zfrom futurez	from past)r   )r   r   r   r   r   r   r   -   s    
c             S   s   g | ]\}}|j d r|qS )zfrom builtins)r   )r   r   r   r   r   r   r   1   s    zIinternal usage error: dedent the code before calling order_future_lines()c             S   s   t | dkrt| S dS )Nr   )lenmax)numbersr   r   r   mymax7   s    z!order_future_lines.<locals>.mymaxc             S   s   t | dkrt| S tdS )Nr   inf)r   minfloat)r   r   r   r   mymin:   s    z!order_future_lines.<locals>.myminz2the __future__ and future imports are out of orderc                s   g | ]} | qS r   r   )r   r   )linesr   r   r   C   s    c                s   g | ]} | qS r   r   )r   r   )r    r   r   r   F   s    c                s   g | ]} | qS r   r   )r   r   )r    r   r   r   I   s    )split	enumeratelstripAssertionErrorsorteddictzipranger   appendjoin)r   Zuufuture_line_numbersZfuture_line_numbersZbuiltins_line_numbersr   r   ZuulZsorted_uufuture_linesZflZsorted_future_linesZblZsorted_builtins_linesZ	new_linesr   r   )r    r   order_future_lines   s2    



r+   c               @   s"   e Zd ZdZdddZdd ZdS )VerboseCalledProcessErrorz
    Like CalledProcessError, but it displays more information (message and
    script output) for diagnosing test failures etc.
    Nc             C   s   || _ || _|| _|| _d S )N)msg
returncodecmdoutput)selfr-   r.   r/   r0   r   r   r   __init__`   s    z"VerboseCalledProcessError.__init__c             C   s   d| j | j| j| jf S )Nz>Command '%s' failed with exit status %d
Message: %s
Output: %s)r/   r.   r-   r0   )r1   r   r   r   __str__f   s    z!VerboseCalledProcessError.__str__)N)__name__
__module____qualname____doc__r2   r3   r   r   r   r   r,   [   s   
r,   c               @   s   e Zd ZdS )FuturizeErrorN)r4   r5   r6   r   r   r   r   r8   j   s   r8   c               @   s   e Zd ZdS )PasteurizeErrorN)r4   r5   r6   r   r   r   r   r9   m   s   r9   c               @   st   e Zd ZdZdd Zddd	Zdd
dZdd Zd ddZdd Z	d!ddZ
d"ddZd$ddZdejfddZdS )%CodeHandlerzt
    Handy mixin for test classes for writing / reading / futurizing /
    running .py files in the test suite.
    c             C   sj   t d| _t d| _tjg| _tj tj	j
 | _tjd}|rXdtj tj | i| _ndtj i| _dS )zi
        The outputs from the various futurize stages should have the
        following headers:
        z
        from __future__ import absolute_import
        from __future__ import division
        from __future__ import print_function
        a4  
        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 *
        
PYTHONPATHN)r   headers1headers2sys
executableinterpreterstempfilemkdtempospathseptempdirgetenvgetcwdpathsepenv)r1   Zpypathr   r   r   setUpv   s    

zCodeHandler.setUpr      FTc             C   sT   |rt |}| j| | j||||d | j }|rPx| jD ]}	| j|	d}
q<W |S )a  
        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.
        )stagesall_importsfrom3conservative)interpreter)r   _write_test_script_futurize_test_script_read_test_scriptr@   _run_test_script)r1   r   rM   rN   rO   ZreformatrunrP   r0   rQ   _r   r   r   convert   s    

zCodeHandler.convertc             C   st   |r| j |}| j |}t|tr8t|t r8|jd}t|trXt|t rX|jd}| jt|j |j  dS )a  
        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.
        zutf-8N)strip_future_imports
isinstancebytesdecodeZassertEqualr+   rstrip)r1   r0   expectedignore_importsr   r   r   compare   s    



zCodeHandler.comparec             C   sd   g }xT|j dD ]F}|jdpJ|jdpJ|jdpJd|kpJd|kpJ|jds|j| qW dj|S )a  
        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)
        r   zfrom __future__ import zfrom future zfrom builtins zinstall_hooks()zinstall_aliases()zfrom future.)r!   r   r)   r*   )r1   r   r0   r   r   r   r   rY      s    



z CodeHandler.strip_future_importsc	             C   s`   | j ||||||d}	|r0d|kr(| jn| j}
nd}
t|}|
|krHd}
| j|	|
| |d dS )a<  
        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   rV   rP   rL    )r_   N)rX   r=   r<   r   r`   )r1   beforer^   rM   rN   r_   rO   rV   rP   r0   headersZreformattedr   r   r   convert_check   s    
zCodeHandler.convert_checkc             K   s   | j ||f| dS )ze
        Convenience method to ensure the code is unchanged by the
        futurize process.
        N)rd   )r1   r   kwargsr   r   r   	unchanged  s    zCodeHandler.unchangedmytestscript.pyc          
   C   sH   t |tr|jd}tj| j| ddd}|jt| W dQ R X dS )z
        Dedents the given code (a multiline string) and writes it out to
        a file in a temporary folder like /tmp/tmpUDCn7x/mytestscript.py.
        zutf-8wt)encodingN)rZ   r[   r\   ioopenrF   writer   )r1   r   filenamefr   r   r   rR   !  s    

zCodeHandler._write_test_scriptc          	   C   s.   t j| j| ddd}|j }W d Q R X |S )NZrtzutf-8)ri   )rj   rk   rF   read)r1   rm   rn   Z	newsourcer   r   r   rT   ,  s    zCodeHandler._read_test_scriptc             C   s@  g }t |}|r|jd |r$d}nNd}|dgkr>|jd n&|dgkrT|jd n|ddgksdt|rr|jd | j| }tj|g| d	|g }	yt|	t| jd
}
W n t	k
r: } zvt
|*}ddj|	d| j |d|j  f }W d Q R X d|krtnt}t|dsd |_|||j|j|jdW Y d d }~X nX |
S )Nz--all-importszpasteurize.pyzfuturize.pyr   z--stage1rL   z--stage2z--conservativez-w)stderrrJ   z8Error running the command %s
%s
Contents of file %s:

%s zenv=%sz----
%s
----Zfuturizer0   )r0   )listr)   r$   rF   r>   r?   r
   r   rJ   r   rk   r*   ro   r8   r9   hasattrr0   r.   r/   )r1   rm   rM   rN   rO   rP   paramsscriptfn	call_argsr0   ern   r-   Z
ErrorClassr   r   r   rS   1  s<    





(z!CodeHandler._futurize_test_scriptc             C   s   | j | }yt||g| jtd}W n tk
r } zht|.}ddj||gd| j |d|j  f }W d Q R X t|dsd |_	t
||j|j|j	dW Y d d }~X nX |S )N)rJ   rp   z8Error running the command %s
%s
Contents of file %s:

%srq   zenv=%sz----
%s
----r0   )r0   )rF   r
   rJ   r   r   rk   r*   ro   rs   r0   r,   r.   r/   )r1   rm   rQ   rv   r0   rx   rn   r-   r   r   r   rU   `  s    


(zCodeHandler._run_test_scriptNr   rL   )ry   FFTTF)Tr   rL   )rz   FTFTF)rg   )rg   r   rL   )rg   r{   FFF)r4   r5   r6   r7   rK   rX   r`   rY   rd   rf   rR   rT   rS   r>   r?   rU   r   r   r   r   r:   q   s"   + 

"  
!

  
-r:   z#this test is known to fail on Py2.6c             C   s   t s| S tj| S )N)r   unittestexpectedFailure)funcr   r   r   expectedFailurePY3  s    r   c             C   s   t s| S tj| S )N)r   r|   r}   )r~   r   r   r   expectedFailurePY26  s    r   c             C   s   t s| S tj| S )N)r	   r|   r}   )r~   r   r   r   expectedFailurePY27  s    r   c             C   s   t s| S tj| S )N)r   r|   r}   )r~   r   r   r   expectedFailurePY2  s    r   assertRaisesRegexc             C   sT   t |ttfr$|stdtj|}|j|sP|p4d}d||j|f }| j|dS )z=Fail the test unless the text matches the regular expression.z!expected_regex must not be empty.zRegex didn't matchz%s: %r not found in %rN)	rZ   strunicoder$   recompilesearchpatternfailureException)r1   textexpected_regexr-   r   r   r   assertRegex  s    

r   c               @   s&   e Zd ZdddZdd Zdd ZdS )	_AssertRaisesBaseContextNc             C   sp   || _ || _|d k	rBy|j| _W qH tk
r>   t|| _Y qHX nd | _t|ttfr`tj	|}|| _
d | _d S )N)r^   	test_caser4   obj_nameAttributeErrorr   rZ   r[   r   r   r   r-   )r1   r^   r   callable_objr   r   r   r   r2     s    
z!_AssertRaisesBaseContext.__init__c             C   s    | j j| j|}| j j|d S )N)r   Z_formatMessager-   r   )r1   ZstandardMsgr-   r   r   r   _raiseFailure  s    z&_AssertRaisesBaseContext._raiseFailurec          
   C   s8   |dkr|j dd| _| S |  ||| W dQ R X dS )z
        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.
        Nr-   )popr-   )r1   namer   argsre   r   r   r   handle  s
    z_AssertRaisesBaseContext.handle)NN)r4   r5   r6   r2   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 )_AssertWarnsContextzBA context manager used to implement TestCase.assertWarns* methods.c             C   sR   x$t jj D ]}t|dd ri |_qW tjdd| _| jj | _tj	d| j
 | S )N__warningregistry__T)recordalways)r>   modulesvaluesgetattrr   warningscatch_warningswarnings_manager	__enter__simplefilterr^   )r1   vr   r   r   r     s    
z_AssertWarnsContext.__enter__c             C   s  | j j||| |d k	rd S y| jj}W n tk
rF   t| j}Y nX d }xd| jD ]Z}|j}t|| jslqT|d krx|}| j	d k	r| j	j
t| rqT|| _|j| _|j| _d S W |d k	r| jdj| j	jt| | jr| jdj|| j n| jdj| d S )Nz"{}" does not match "{}"z{} not triggered by {}z{} not triggered)r   __exit__r^   r4   r   r   r   messagerZ   r   r   warningrm   linenor   formatr   r   )r1   exc_type	exc_valuetbZexc_nameZfirst_matchingmwr   r   r   r     s8    

z_AssertWarnsContext.__exit__N)r4   r5   r6   r7   r   r   r   r   r   r   r     s   r   c             O   s   t || |}|jd|||S )a  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)
    assertWarns)r   r   )r1   Zexpected_warningr   r   re   contextr   r   r   r     s    r   )N)N),
__future__r   r   rC   rA   r|   r>   r   r   rj   textwrapr   Zfuture.utilsr   r   r   r   r	   Zfuture.moves.subprocessr
   r   r   Z	unittest2r   r+   r,   r8   r9   ZTestCaser:   ZskipIfZskip26r   r   r   r   rs   ZassertRaisesRegexpr   r   objectr   r   r   r   r   r   r   <module>   sF   	@  

"1
