o
    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rHd 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sejj%ej_&d$ddZ'e$ejdse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 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   ^/var/www/html/portale_awareness/portale-venv/lib/python3.10/site-packages/future/tests/base.pyreformat_code   s   
r   c                    s^  |  d dd t D }dd t D }dd t D }|  | ks*J ddd }d	d
 }||||ks>J 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 ]-}||v r|||  q|||v r||	|  q|||v r|||  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      g | ]\}}| d r|qS )from __future__ import r   .0iliner   r   r   
<listcomp>*       
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   r   )zfrom builtinsr   r   r   r   r   r   1   r   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                       g | ]} | qS r   r   r   r   linesr   r   r   C       c                    r&   r   r   r'   r(   r   r   r   F   r*   c                    r&   r   r   r'   r(   r   r   r   I   r*   )
split	enumeratelstripsorteddictzipranger   appendjoin)r   uufuture_line_numbersfuture_line_numbersbuiltins_line_numbersr!   r%   uulsorted_uufuture_linesflsorted_future_linesblsorted_builtins_lines	new_linesr   r   r(   r   order_future_lines   s4   

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)selfrA   rB   rC   rD   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)rC   rB   rA   rD   )rE   r   r   r   __str__f   s   z!VerboseCalledProcessError.__str__r@   )__name__
__module____qualname____doc__rF   rG   r   r   r   r   r?   [   s    
r?   c                   @      e Zd ZdS )FuturizeErrorNrH   rI   rJ   r   r   r   r   rM   j       rM   c                   @   rL   )PasteurizeErrorNrN   r   r   r   r   rP   m   rO   rP   c                   @   s   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   sl   t d| _t d| _tjg| _t tj	j
 | _td}|r-dt tj | i| _dS 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)rE   pypathr   r   r   setUpv   s   


	
zCodeHandler.setUpr      FTc                 C   sP   |rt |}| | | j||||d |  }|r&| 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_scriptrW   _run_test_script)rE   r   rf   rg   rh   reformatrunri   rD   rj   _r   r   r   convert   s   

zCodeHandler.convertc                 C   sp   |r|  |}|  |}t|trt|ts|d}t|tr*t|ts*|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decodeassertEqualr>   rstrip)rE   rD   expectedignore_importsr   r   r   compare   s   



zCodeHandler.comparec                 C   s`   g }| dD ]#}|ds*|ds*|ds*d|v s*d|v 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   r2   r3   )rE   r   rD   r   r   r   r   rt      s   


z CodeHandler.strip_future_importsc	                 C   s`   | j ||||||d}	|rd|v r| jn| j}
nd}
t|}|
|v r$d}
| 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.
        )rf   rg   rh   rp   ri   re    )r{   N)rr   rT   rS   r   r|   )rE   beforerz   rf   rg   r{   rh   rp   ri   rD   headersreformattedr   r   r   convert_check   s   

zCodeHandler.convert_checkc                 K   s   | j ||fi | dS )ze
        Convenience method to ensure the code is unchanged by the
        futurize process.
        N)r   )rE   r   kwargsr   r   r   	unchanged  s   zCodeHandler.unchangedmytestscript.pyc                 C   s^   t |tr
|d}tj| j| ddd}|t| W d   dS 1 s(w   Y  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.
        rs   wtencodingN)ru   rv   rw   ioopenr]   writer   )rE   r   filenamefr   r   r   rk   !  s
   

"zCodeHandler._write_test_scriptc                 C   sD   t j| j| ddd}| }W d    |S 1 sw   Y  |S )Nrtrs   r   )r   r   r]   read)rE   r   r   	newsourcer   r   r   rm   ,  s   

zCodeHandler._read_test_scriptc                 C   sD  g }t |}|r|d |rd}n'd}|dgkr|d n|dgkr*|d n|ddgks2J |r9|d | j| }tj|g| d	|g }	zt|	t| jd
}
W |
S  ty } zAt	|}dd
|	d| j |d|  f }W d    n1 s}w   Y  d|v rtnt}t|dsd |_|||j|j|jdd }~ww )Nz--all-importszpasteurize.pyzfuturize.pyr   z--stage1re   z--stage2z--conservativez-w)stderrra   8Error running the command %s
%s
Contents of file %s:

%s env=%s----
%s
----futurizerD   rD   )listr2   r]   rU   rV   r
   r   ra   r   r   r3   r   rM   rP   hasattrrD   rB   rC   )rE   r   rf   rg   rh   ri   paramsscriptfn	call_argsrD   er   rA   
ErrorClassr   r   r   rl   1  sF   







z!CodeHandler._futurize_test_scriptc                 C   s   | j | }zt||g| jtd}W |S  tyY } z;t|}dd||gd| j |d|  f }W d    n1 s=w   Y  t|dsJd |_	t
||j|j|j	dd }~ww )N)ra   r   r   r   r   r   rD   r   )r]   r
   ra   r   r   r   r3   r   r   rD   r?   rB   rC   )rE   r   rj   r   rD   r   r   rA   r   r   r   rn   `  s*   



zCodeHandler._run_test_scriptN)rd   FFTTF)T)rd   FTFTF)r   )r   rd   FFF)rH   rI   rJ   rK   rc   rr   r|   rt   r   r   rk   rm   rl   rU   rV   rn   r   r   r   r   rQ   q   s,    +

"
#


/rQ   z#this test is known to fail on Py2.6c                 C      t s| S t| S r@   )r   unittestexpectedFailurefuncr   r   r   expectedFailurePY3     
r   c                 C   r   r@   )r   r   r   r   r   r   r   expectedFailurePY26  r   r   c                 C   r   r@   )r	   r   r   r   r   r   r   expectedFailurePY27  r   r   c                 C   r   r@   )r   r   r   r   r   r   r   expectedFailurePY2  r   r   assertRaisesRegexc                 C   sT   t |ttfr|sJ dt|}||s(|pd}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)ru   strunicoderecompilesearchpatternfailureException)rE   textexpected_regexrA   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   sl   || _ || _|d urz|j| _W n ty   t|| _Y nw d | _t|ttfr.t	|}|| _
d | _d S r@   )rz   	test_caserH   obj_nameAttributeErrorr   ru   rv   r   r   r   rA   )rE   rz   r   callable_objr   r   r   r   rF     s   

z!_AssertRaisesBaseContext.__init__c                 C   s   | j | j|}| j |r@   )r   _formatMessagerA   r   )rE   standardMsgrA   r   r   r   _raiseFailure  s   z&_AssertRaisesBaseContext._raiseFailurec                 C   sR   |du r| dd| _| S |  ||i | W d   dS 1 s"w   Y  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.
        NrA   )poprA   )rE   namer   argsr   r   r   r   handle  s   "z_AssertRaisesBaseContext.handle)NN)rH   rI   rJ   rF   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)rU   modulesvaluesgetattrr   warningscatch_warningswarnings_manager	__enter__simplefilterrz   )rE   vr   r   r   r     s   z_AssertWarnsContext.__enter__c                 C   s   | j ||| |d urd S z| jj}W n ty"   t| j}Y nw d }| jD ]-}|j}t|| js4q(|d u r:|}| j	d urH| j	
t|sHq(|| _|j| _|j| _ d S |d urg| d| j	jt| | jrv| d|| j d S | d| d S )Nz"{}" does not match "{}"z{} not triggered by {}z{} not triggered)r   __exit__rz   rH   r   r   r   messageru   r   r   warningr   linenor   formatr   r   )rE   exc_type	exc_valuetbexc_namefirst_matchingmwr   r   r   r     s@   



z_AssertWarnsContext.__exit__N)rH   rI   rJ   rK   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   )rE   expected_warningr   r   r   contextr   r   r   r     s   r   r@   ),
__future__r   r   rZ   rX   r   rU   r   r   r   textwrapr   future.utilsr   r   r   r   r	   future.moves.subprocessr
   r   r   	unittest2r   r>   r?   rM   rP   TestCaserQ   skipIfskip26r   r   r   r   r   assertRaisesRegexpr   r   objectr   r   r   r   r   r   r   <module>   sJ    	@  

"
1