o
    ,º]ô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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ƒs“ejj%ej_&d$dd„Z'e$ejdƒs¥e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 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   úN/var/www/html/myproject/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 rˆtnt}t|dƒs’d |_|||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ÿ