U
    ËZjôM  ã                   @   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r�d 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 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#ƒ�s”eejd#e+ƒ dS )&é    )Úprint_functionÚabsolute_importN)Údedent)Úbind_methodÚPY26ÚPY3ÚPY2ÚPY27)Úcheck_outputÚSTDOUTÚCalledProcessErrorc                 C   s   |   d¡r| dd… } t| ƒS )z,
    Removes any leading 
 and dedents.
    Ú
é   N)Ú
startswithr   )Úcode© r   úT/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/future/tests/base.pyÚreformat_code   s    
r   c                    sd  |   d¡‰ dd„ tˆ ƒD ƒ}dd„ tˆ ƒD ƒ}dd„ tˆ ƒD ƒ}|  ¡ | 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 }ttˆ ƒƒ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   c                 S   s   g | ]\}}|  d ¡r|‘qS )úfrom __future__ import ©r   ©Ú.0ÚiÚliner   r   r   Ú
<listcomp>*   s    
ÿz&order_future_lines.<locals>.<listcomp>c                 S   s(   g | ] \}}|  d ¡s |  d¡r|‘qS )zfrom futurez	from pastr   r   r   r   r   r   -   s    

þc                 S   s   g | ]\}}|  d ¡r|‘qS )zfrom builtinsr   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Úfloatr   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   F   s     c                    s   g | ]}ˆ | ‘qS 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_linesÚflZsorted_future_linesÚblZsorted_builtins_linesÚ	new_linesr   r   r%   r   Úorder_future_lines   s4    
ÿ


r4   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)Úselfr7   r8   r9   r:   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)r9   r8   r7   r:   )r;   r   r   r   Ú__str__f   s    ÿz!VerboseCalledProcessError.__str__)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r<   r=   r   r   r   r   r5   [   s   
r5   c                   @   s   e Zd ZdS )ÚFuturizeErrorN©r>   r?   r@   r   r   r   r   rB   j   s   rB   c                   @   s   e Zd ZdS )ÚPasteurizeErrorNrC   r   r   r   r   rD   m   s   rD   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 ¡ tj	j
 | _t d¡}|rXdt ¡ tj | i| _ndt ¡ 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)r;   Zpypathr   r   r   ÚsetUpv   s    

	

zCodeHandler.setUp©r   é   FTc                 C   sP   |rt |ƒ}|  |¡ | j||||d� |  ¡ }|rL| jD ]}	| 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_scriptrK   Ú_run_test_script)r;   r   rY   rZ   r[   ZreformatÚrunr\   r:   r]   Ú_r   r   r   Úconvert¡   s    
 ÿ
zCodeHandler.convertc                 C   sp   |r|   |¡}|   |¡}t|tƒr6t|tƒs6| d¡}t|tƒrTt|tƒsT| d¡}|  t| ¡ ƒ| ¡ ¡ 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.
        úutf-8N)Ústrip_future_importsÚ
isinstanceÚbytesÚdecodeZassertEqualr4   Úrstrip)r;   r:   ÚexpectedÚignore_importsr   r   r   Úcompare¿   s    



ÿzCodeHandler.comparec                 C   s`   g }|  d¡D ]F}| d¡s| d¡s| d¡sd|ksd|ks| d¡s| |¡ qd |¡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   r   zfrom future zfrom builtins zinstall_hooks()zinstall_aliases()zfrom future.)r'   r   r/   r0   )r;   r   r:   r   r   r   r   rf   Õ   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.
        )rY   rZ   r[   rb   r\   rX   Ú )rl   N)rd   rH   rG   r   rm   )r;   Úbeforerk   rY   rZ   rl   r[   rb   r\   r:   Ú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)rq   )r;   r   Úkwargsr   r   r   Ú	unchanged  s    zCodeHandler.unchangedúmytestscript.pyc              	   C   sH   t |tƒr| d¡}tj| j| ddd��}| t|ƒ¡ W 5 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.
        re   Zwt©ÚencodingN)rg   rh   ri   ÚioÚopenrQ   Úwriter   )r;   r   ÚfilenameÚfr   r   r   r^   !  s    

zCodeHandler._write_test_scriptc              	   C   s.   t j| j| ddd��}| ¡ }W 5 Q R X |S )NÚrtre   ru   )rw   rx   rQ   Úread)r;   rz   r{   Z	newsourcer   r   r   r`   ,  s    zCodeHandler._read_test_scriptc                 C   s@  g }t |ƒ}|r| d¡ |r$d}nNd}|dgkr>| d¡ n&|dgkrT| d¡ n|ddgksdt‚|rr| d¡ | j| }tj|g| d	|g }	zt|	t| jd
�}
W n” t	k
�r: } ztt
|ƒ�*}dd |	¡d| j |d| ¡  f }W 5 Q R X d|krþtnt}t|dƒ�sd |_|||j|j|jd�‚W 5 d }~X Y nX |
S )Nz--all-importszpasteurize.pyzfuturize.pyr   z--stage1rX   z--stage2z--conservativez-w)ÚstderrrU   ú8Error running the command %s
%s
Contents of file %s:

%sú úenv=%sú----
%s
----Zfuturizer:   ©r:   )Úlistr/   r*   rQ   rI   rJ   r
   r   rU   r   rx   r0   r}   rB   rD   Úhasattrr:   r8   r9   )r;   rz   rY   rZ   r[   r\   ÚparamsÚscriptÚfnZ	call_argsr:   Úer{   r7   Z
ErrorClassr   r   r   r_   1  s@    






üû(z!CodeHandler._futurize_test_scriptc                 C   s¬   | j | }zt||g| jtd�}W n„ tk
r¦ } zft|ƒ�.}dd ||g¡d| j |d| ¡  f }W 5 Q R X t|dƒs€d |_	t
||j|j|j	d�‚W 5 d }~X Y nX |S )N)rU   r~   r   r€   r�   r‚   r:   rƒ   )rQ   r
   rU   r   r   rx   r0   r}   r…   r:   r5   r8   r9   )r;   rz   r]   rˆ   r:   r‰   r{   r7   r   r   r   ra   `  s&    
 ÿ


üû
(zCodeHandler._run_test_scriptN)rW   FFTTF)T)rW   FTFTF)rt   )rt   )rt   rW   FFF)r>   r?   r@   rA   rV   rd   rm   rf   rq   rs   r^   r`   r_   rI   rJ   ra   r   r   r   r   rE   q   s4   +      ÿ

"       þ
#

     þ
/ÿrE   z#this test is known to fail on Py2.6c                 C   s   t s| S t | ¡S r6   )r   ÚunittestÚexpectedFailure©Úfuncr   r   r   ÚexpectedFailurePY3  s    rŽ   c                 C   s   t s| S t | ¡S r6   )r   rŠ   r‹   rŒ   r   r   r   ÚexpectedFailurePY26„  s    r�   c                 C   s   t s| S t | ¡S r6   )r	   rŠ   r‹   rŒ   r   r   r   ÚexpectedFailurePY27Š  s    r�   c                 C   s   t s| S t | ¡S r6   )r   rŠ   r‹   rŒ   r   r   r   ÚexpectedFailurePY2�  s    r‘   ÚassertRaisesRegexc                 C   sT   t |ttfƒr$|stdƒ‚t |¡}| |¡sP|p4d}d||j|f }|  |¡‚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)	rg   ÚstrÚunicoder*   ÚreÚcompileÚsearchÚpatternÚfailureException)r;   ÚtextÚexpected_regexr7   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	rBz|j| _W qH tk
r>   t|ƒ| _Y qHX nd | _t|ttfƒr`t 	|¡}|| _
d | _d S r6   )rk   Ú	test_caser>   Úobj_nameÚAttributeErrorr“   rg   rh   r•   r–   r›   r7   )r;   rk   rž   Úcallable_objr›   r   r   r   r<   ª  s    
z!_AssertRaisesBaseContext.__init__c                 C   s    | j  | j|¡}| j  |¡‚d S r6   )rž   Z_formatMessager7   r™   )r;   ZstandardMsgr7   r   r   r   Ú_raiseFailureº  s    z&_AssertRaisesBaseContext._raiseFailurec              	   C   s8   |dkr|  dd¡| _| S | � |||Ž W 5 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.
        Nr7   )Úpopr7   )r;   Únamer¡   Úargsrr   r   r   r   Úhandle¾  s
    z_AssertRaisesBaseContext.handle)NN)r>   r?   r@   r<   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   sN   t j ¡ D ]}t|dd ƒr
i |_q
tjdd�| _| j ¡ | _t 	d| j
¡ | S )NÚ__warningregistry__T)ÚrecordÚalways)rI   ÚmodulesÚvaluesÚgetattrr¨   ÚwarningsÚcatch_warningsÚwarnings_managerÚ	__enter__Úsimplefilterrk   )r;   Úvr   r   r   r±   Í  s    z_AssertWarnsContext.__enter__c                 C   s   | j  |||¡ |d k	rd S z| jj}W n tk
rF   t| jƒ}Y nX d }| jD ]Z}|j}t|| jƒsjqR|d krv|}| j	d k	r’| j	 
t|ƒ¡s’qR|| _|j| _|j| _ d S |d k	rÐ|  d | j	jt|ƒ¡¡ | jrì|  d || j¡¡ n|  d |¡¡ d S )Nz"{}" does not match "{}"z{} not triggered by {}z{} not triggered)r°   Ú__exit__rk   r>   r    r“   r®   Úmessagerg   r›   r—   Úwarningrz   Úlinenor¢   Úformatr˜   rŸ   )r;   Úexc_typeÚ	exc_valueÚtbZexc_nameZfirst_matchingÚmÚwr   r   r   r´   Ø  s@    

ÿ ÿ
ÿz_AssertWarnsContext.__exit__N)r>   r?   r@   rA   r±   r´   r   r   r   r   r§   Ê  s   r§   c                 O   s   t || |ƒ}| 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¦   )r;   Zexpected_warningr¡   r¥   rr   Úcontextr   r   r   r¾   û  s    r¾   )N)N),Ú
__future__r   r   rN   rL   rŠ   rI   r•   r®   rw   Útextwrapr   Zfuture.utilsr   r   r   r   r	   Zfuture.moves.subprocessr
   r   r   Z	unittest2r   r4   r5   rB   rD   ZTestCaserE   ZskipIfZskip26rŽ   r�   r�   r‘   r…   ZassertRaisesRegexpr’   rœ   Úobjectr�   r§   r¾   r   r   r   r   Ú<module>   sF   	@  

"1
