Your IP : 216.73.216.182


Current Path : /lib/python3/dist-packages/future/tests/__pycache__/
Upload File :
Current File : //lib/python3/dist-packages/future/tests/__pycache__/base.cpython-38.pyc

U

,�]�M�@s�ddlmZmZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZddlm
Z
mZmZmZmZddlmZmZmZer�ddlZdd�Zdd	�ZGd
d�de�ZGdd
�d
e�ZGdd�de�ZGdd�dej�Ze�ed�Zdd�Z dd�Z!dd�Z"dd�Z#e$ejd��s(ejj%ej_&d$dd�Z'e$ejd��sNe
ejde'�Gdd�de(�Z)Gd d!�d!e)�Z*d%d"d#�Z+e$ejd#��s�e
ejd#e+�dS)&�)�print_function�absolute_importN)�dedent)�bind_method�PY26�PY3�PY2�PY27)�check_output�STDOUT�CalledProcessErrorcCs|�d�r|dd�}t|�S)z,
    Removes any leading 
 and dedents.
    �
�N)�
startswithr)�code�r�3/usr/lib/python3/dist-packages/future/tests/base.py�
reformat_codes
rcsd|�d��dd�t��D�}dd�t��D�}dd�t��D�}|��|ksTtd��dd�}d	d
�}||�||�ks|td��t�fdd�|D��}tt||��}t�fd
d�|D��}tt||��}	t�fdd�|D��}
tt||
��}g}tt���D]`}
|
|k�r|�	||
�q�|
|k�r0|�	|	|
�q�|
|k�rJ|�	||
�q�|�	�|
�q�d�
|�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
cSsg|]\}}|�d�r|�qS)�from __future__ import �r��.0�i�linerrr�
<listcomp>*s
�z&order_future_lines.<locals>.<listcomp>cSs(g|] \}}|�d�s |�d�r|�qS)zfrom futurez	from pastrrrrrr-s

�cSsg|]\}}|�d�r|�qS)z
from builtinsrrrrrr1s
�zIinternal usage error: dedent the code before calling order_future_lines()cSst|�dkrt|�SdS)Nr)�len�max�Znumbersrrr�mymax7sz!order_future_lines.<locals>.mymaxcSst|�dkrt|�Std�S)Nr�inf)r�min�floatrrrr�mymin:sz!order_future_lines.<locals>.myminz2the __future__ and future imports are out of ordercsg|]}�|�qSrr�rr��linesrrrCscsg|]}�|�qSrrr#r$rrrFscsg|]}�|�qSrrr#r$rrrIs)�split�	enumerate�lstrip�AssertionError�sorted�dict�zip�ranger�append�join)rZuufuture_line_numbersZfuture_line_numbersZbuiltins_line_numbersrr"ZuulZsorted_uufuture_linesZflZsorted_future_linesZblZsorted_builtins_linesZ	new_linesrrr$r�order_future_liness4
�


r0c@s"eZdZdZddd�Zdd�ZdS)�VerboseCalledProcessErrorz�
    Like CalledProcessError, but it displays more information (message and
    script output) for diagnosing test failures etc.
    NcCs||_||_||_||_dS�N)�msg�
returncode�cmd�output)�selfr3r4r5r6rrr�__init__`sz"VerboseCalledProcessError.__init__cCsd|j|j|j|jfS)Nz>Command '%s' failed with exit status %d
Message: %s
Output: %s)r5r4r3r6)r7rrr�__str__fs�z!VerboseCalledProcessError.__str__)N)�__name__�
__module__�__qualname__�__doc__r8r9rrrrr1[s
r1c@seZdZdS)�
FuturizeErrorN�r:r;r<rrrrr>jsr>c@seZdZdS)�PasteurizeErrorNr?rrrrr@msr@c@steZdZdZdd�Zddd�Zdd	d
�Zdd�Zdd
d�Zdd�Z	ddd�Z
ddd�Zd dd�Zde
jfdd�ZdS)!�CodeHandlerzt
    Handy mixin for test classes for writing / reading / futurizing /
    running .py files in the test suite.
    cCsjtd�|_td�|_tjg|_t��tj	j
|_t�d�}|rXdt�
�tj|i|_ndt�
�i|_dS)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�tempfileZmkdtemp�os�path�sep�tempdir�getenv�getcwd�pathsep�env)r7Zpypathrrr�setUpvs

	

zCodeHandler.setUp�r�FTcCsP|rt|�}|�|�|j||||d�|��}|rL|jD]}	|j|	d�}
q:|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_scriptrG�_run_test_script)r7rrTrUrVZreformat�runrWr6rX�_rrr�convert�s
�
zCodeHandler.convertcCsp|r|�|�}|�|�}t|t�r6t|t�s6|�d�}t|t�rTt|t�sT|�d�}|�t|���|���dS)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.
        �utf-8N)�strip_future_imports�
isinstance�bytes�decodeZassertEqualr0�rstrip)r7r6�expected�ignore_importsrrr�compare�s



�zCodeHandler.comparecCs`g}|�d�D]F}|�d�s|�d�s|�d�sd|ksd|ks|�d�s|�|�qd�|�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
rzfrom future zfrom builtins zinstall_hooks()zinstall_aliases()zfrom future.)r&rr.r/)r7rr6rrrrra�s
�����z CodeHandler.strip_future_importsc	Cs`|j||||||d�}	|r0d|kr(|jn|j}
nd}
t|�}|
|krHd}
|j|	|
||d�dS)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.
        )rTrUrVr]rWrS�)rgN)r_rDrCrrh)r7ZbeforerfrTrUrgrVr]rWr6ZheadersZreformattedrrr�
convert_check�s
��zCodeHandler.convert_checkcKs|j||f|�dS)ze
        Convenience method to ensure the code is unchanged by the
        futurize process.
        N)rj)r7r�kwargsrrr�	unchangedszCodeHandler.unchanged�mytestscript.pyc	CsHt|t�r|�d�}tj|j|ddd��}|�t|��W5QRXdS)z�
        Dedents the given code (a multiline string) and writes it out to
        a file in a temporary folder like /tmp/tmpUDCn7x/mytestscript.py.
        r`Zwt��encodingN)rbrcrd�io�openrL�writer)r7r�filename�frrrrY!s

zCodeHandler._write_test_scriptc	Cs.tj|j|ddd��}|��}W5QRX|S)NZrtr`rn)rprqrL�read)r7rsrtZ	newsourcerrrr[,szCodeHandler._read_test_scriptcCs@g}t|�}|r|�d�|r$d}nNd}|dgkr>|�d�n&|dgkrT|�d�n|ddgksdt�|rr|�d�|j|}tj|g|d	|g}	zt|	t|jd
�}
Wn�t	k
�r:}ztt
|��*}dd�|	�d
|j|d|��f}
W5QRXd|kr�t
nt}t|d��sd|_||
|j|j|jd��W5d}~XYnX|
S)Nz
--all-importsz
pasteurize.pyzfuturize.pyrz--stage1rSz--stage2z--conservativez-w)�stderrrP�8Error running the command %s
%s
Contents of file %s:

%s� �env=%s�----
%s
----Zfuturizer6�r6)�listr.r)rLrErFr
rrPrrqr/rur>r@�hasattrr6r4r5)r7rsrTrUrVrWZparamsZscript�fnZ	call_argsr6�ertr3Z
ErrorClassrrrrZ1s@






��(z!CodeHandler._futurize_test_scriptcCs�|j|}zt||g|jtd�}Wn�tk
r�}zft|��.}dd�||g�d|j|d|��f}W5QRXt|d�s�d|_	t
||j|j|j	d��W5d}~XYnX|S)N)rPrvrwrxryrzr6r{)
rLr
rPrrrqr/rur}r6r1r4r5)r7rsrXr~r6rrtr3rrrr\`s&
�


��
(zCodeHandler._run_test_scriptN)rRFFTTF)T)rRFTFTF)rm)rm)rmrRFFF)r:r;r<r=rQr_rhrarjrlrYr[rZrErFr\rrrrrAqs4+�

"�
#

�
/�rAz#this test is known to fail on Py2.6cCsts|St�|�Sr2)r�unittest�expectedFailure��funcrrr�expectedFailurePY3sr�cCsts|St�|�Sr2)rr�r�r�rrr�expectedFailurePY26�sr�cCsts|St�|�Sr2)r	r�r�r�rrr�expectedFailurePY27�sr�cCsts|St�|�Sr2)rr�r�r�rrr�expectedFailurePY2�sr��assertRaisesRegexcCsTt|ttf�r$|std��t�|�}|�|�sP|p4d}d||j|f}|�|��dS)z=Fail the test unless the text matches the regular expression.z!expected_regex must not be empty.zRegex didn't matchz%s: %r not found in %rN)	rb�strZunicoder)�re�compile�search�pattern�failureException)r7�text�expected_regexr3rrr�assertRegex�s

r�c@s&eZdZddd�Zdd�Zdd�ZdS)	�_AssertRaisesBaseContextNcCsp||_||_|dk	rBz|j|_WqHtk
r>t|�|_YqHXnd|_t|ttf�r`t�	|�}||_
d|_dSr2)rf�	test_caser:�obj_name�AttributeErrorr�rbrcr�r�r�r3)r7rfr��callable_objr�rrrr8�s
z!_AssertRaisesBaseContext.__init__cCs |j�|j|�}|j�|��dSr2)r�Z_formatMessager3r�)r7ZstandardMsgr3rrr�
_raiseFailure�sz&_AssertRaisesBaseContext._raiseFailurec	Cs8|dkr|�dd�|_|S|�|||�W5QRXdS)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.
        Nr3)�popr3)r7�namer��argsrkrrr�handle�s
z_AssertRaisesBaseContext.handle)NN)r:r;r<r8r�r�rrrrr��s
�
r�c@s eZdZdZdd�Zdd�ZdS)�_AssertWarnsContextzBA context manager used to implement TestCase.assertWarns* methods.cCsNtj��D]}t|dd�r
i|_q
tjdd�|_|j��|_t�	d|j
�|S)N�__warningregistry__T)�record�always)rE�modules�values�getattrr��warnings�catch_warnings�warnings_manager�	__enter__�simplefilterrf)r7�vrrrr��sz_AssertWarnsContext.__enter__cCs|j�|||�|dk	rdSz|jj}Wntk
rFt|j�}YnXd}|jD]Z}|j}t||j�sjqR|dkrv|}|j	dk	r�|j	�
t|��s�qR||_|j|_|j
|_
dS|dk	r�|�d�|j	jt|���|jr�|�d�||j��n|�d�|��dS)Nz"{}" does not match "{}"z{} not triggered by {}z{} not triggered)r��__exit__rfr:r�r�r��messagerbr�r�Zwarningrs�linenor��formatr�r�)r7�exc_type�	exc_value�tbZexc_nameZfirst_matching�m�wrrrr��s@

��
�z_AssertWarnsContext.__exit__N)r:r;r<r=r�r�rrrrr��sr�cOst|||�}|�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�)r7Zexpected_warningr�r�rk�contextrrrr��sr�)N)N),Z
__future__rrrIrHr�rEr�r�rp�textwraprZfuture.utilsrrrrr	Zfuture.moves.subprocessr
rrZ	unittest2rr0r1r>r@ZTestCaserAZskipIfZskip26r�r�r�r�r}ZassertRaisesRegexpr�r��objectr�r�r�rrrr�<module>sF	@


"1