a
    !f}                  .   @   s  d 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	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 ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddlm!Z! ddl"m#Z# ddl"m$Z$ ddl%m&Z& ddl'm(Z( ddl)m*Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1 ddl0m2Z2 ddl3m4Z4 e rddl'm5Z5 ej6dkrej7Z8ej9Z:ej9Z;ej9Z<nej=Z8ej>Z:ej?Z;ej@Z<G dd dZAe4d   ZBejCjD d!e& ZEd"d#r(d$p*d% ZFd&eE eF ZGeA ZHG d'd( d(ejIjJejIjKZLeeM ejNejOdd)d*d+ZPd ejOejNeeQd,d-d.ZRee-eejNejOf d/d0d1ZSd2d3 feeeeTgdf eejO d4d5d6ZUd~ejVeMeeT ee- dd7d8d9ZWeXeTd:d;d<ZYee- eeZ d=d>d?Z[eXeTd:d@dAZ\eXeQd:dBdCZ]eeT eQeTdDdEdFZ^eeT eeQ eeT eeX eTdGdHdIZ_eZeTeTddJdKdLZ`eQdMdNdOZaejbdPejcdQejddRejedSiZfejgdTejhdUejidVejjdWejkdXejldYejmdZejnd[ejod\ejpd]ejqd^ejrd_ejsd`ejtdaejudbejvdcejwddejxdeejydfejzdgej{dhej|diej}djiZ~ejeej dkdldmZejdndoeMeeZeTf dpdqdrZe;eXdsdtduZG dvdw dwejZeeQdxdydzZeed{d|d}ZdS )z5Rewrite assertion AST to produce nice error messages.    N)defaultdict)Path)PurePath)Callable)Dict)IO)Iterable)Iterator)List)Optional)Sequence)Set)Tuple)TYPE_CHECKING)Union)DEFAULT_REPR_MAX_SIZE)saferepr)version)util)format_explanation)Config)Session)absolutepath)
fnmatch_ex)StashKey)AssertionState      c                   @   s   e Zd ZdS )SentinelN)__name__
__module____qualname__ r#   r#   Z/var/www/html/python-backend/venv/lib/python3.9/site-packages/_pytest/assertion/rewrite.pyr   <   s   r   r   z-pytest-.pyTco.c                   @   sP  e Zd ZdZeddddZee ddddZe	j
jjZd*eeeeeef   eej ee	j
j d	d
dZe	j
jeej dddZejddddZededddZeededddZededddZeddddZedddd Zeeef ed!d"d#Zejd$krLejd%kr0d&d'l m!Z! nd&d'l"m!Z! ee!dd(d)Z#dS )+AssertionRewritingHookz1PEP302/PEP451 import hook which rewrites asserts.Nconfigreturnc                 C   sf   || _ z|d| _W n ty2   ddg| _Y n0 d | _i | _t | _d| _dh| _	i | _
d| _d S )NZpython_filesz	test_*.pyz	*_test.pyFZconftest)r+   getinifnpats
ValueErrorsession_rewritten_namesset_must_rewrite_writing_pyc_basenames_to_check_rewrite_marked_for_rewrite_cache_session_paths_checked)selfr+   r#   r#   r$   __init__N   s    zAssertionRewritingHook.__init__)r0   r,   c                 C   s   || _ d| _d S )NF)r0   r7   )r8   r0   r#   r#   r$   set_session^   s    z"AssertionRewritingHook.set_session)namepathtargetr,   c                 C   s   | j r
d S | jjt }| ||r&d S |d|  | ||}|d u sp|jd u spt|j	t
jjrptj|jstd S |j}| |||sd S t
jj||| |jdS )Nzfind_module called for: %s)loadersubmodule_search_locations)r4   r+   stashassertstate_key_early_rewrite_bailouttrace
_find_specorigin
isinstancer>   	importlib	machinerySourceFileLoaderosr<   exists_should_rewriter   spec_from_file_locationr?   )r8   r;   r<   r=   statespecfnr#   r#   r$   	find_spece   s2    	z AssertionRewritingHook.find_spec)rO   r,   c                 C   s   d S Nr#   )r8   rO   r#   r#   r$   create_module   s    z$AssertionRewritingHook.create_module)moduler,   c                 C   s  |j d usJ |j jd usJ t|j j}| jjt }|| j|j< tj	 }t
|}|rvt|}|svd}|d|  |jd d t }|| }t|||j}	|	d u r|d| t|| j\}
}	|rd| _zt||	|
| W d| _nd| _0 n|d|  t|	|j d S )NFzread only directory: z
rewriting Tzfound cached rewritten pyc for )__spec__rE   r   r+   r@   rA   r1   r    sysdont_write_bytecodeget_cache_dirtry_makedirsrC   r;   PYC_TAIL	_read_pyc_rewrite_testr4   
_write_pycexec__dict__)r8   rT   rP   rN   write	cache_dirokZ
cache_namepyccosource_statr#   r#   r$   exec_module   s2    
z"AssertionRewritingHook.exec_moduler   )r;   rN   r,   c                 C   s   | j durP| jsPd| _| j jD ]0}t|tj}| jtj	
|d d  q|d}|d | jv rldS t| d}| jD ]&}tj	|r dS t||r dS q| ||rdS |d|  dS )	a'  A fast way to get out of rewriting modules.

        Profiling has shown that the call to PathFinder.find_spec (inside of
        the find_spec from this class) is a major slowdown, so, this method
        tries to filter what we're sure won't be rewritten before getting to
        it.
        NTr   r(   Fr%   z early skip of rewriting module: )r0   r7   Z_initialpathsstrsplitrJ   sepr5   addr<   splitextr   with_suffixr.   dirnamer   _is_marked_for_rewriterC   )r8   r;   rN   Zinitial_pathpartsr<   patr#   r#   r$   rB      s$    


z-AssertionRewritingHook._early_rewrite_bailout)r;   rP   rN   r,   c                 C   s   t j|dkr$|d| dS | jd urR| jt|rR|d| dS t|}| jD ]$}t	||r`|d|  dS q`| 
||S )Nzconftest.pyzrewriting conftest file: Tz.matched test file (was specified on cmdline): zmatched test file )rJ   r<   basenamerC   r0   Z
isinitpathr   r   r.   r   rp   )r8   r;   rP   rN   Zfn_pathrr   r#   r#   r$   rL      s    


z&AssertionRewritingHook._should_rewritec                 C   s|   z| j | W S  tyv   | jD ]D}||ks:||d r |d|d|d d| j |<  Y dS q d| j |< Y dS 0 d S )Nr(   zmatched marked file z (from )TF)r6   KeyErrorr3   
startswithrC   )r8   r;   rN   Zmarkedr#   r#   r$   rp      s    



z-AssertionRewritingHook._is_marked_for_rewrite)namesr,   c                 G   sp   t |tj| j}|D ]8}tj| }t|jp6dst	|j
t| s| | q| j| | j  dS )zMark import names as needing to be rewritten.

        The named module or package as well as any nested modules will
        be rewritten on import.
         N)r2   intersectionrW   modules
differencer1   AssertionRewriteris_rewrite_disabled__doc__rF   
__loader__type_warn_already_importedr3   updater6   clear)r8   rw   Zalready_importedr;   modr#   r#   r$   mark_rewrite  s    
z#AssertionRewritingHook.mark_rewriter;   r,   c                 C   s(   ddl m} | jj|d| dd d S )Nr   PytestAssertRewriteWarningz2Module already imported so cannot be rewritten: %s   )
stacklevel)_pytest.warning_typesr   r+   Zissue_config_time_warning)r8   r;   r   r#   r#   r$   r     s    z-AssertionRewritingHook._warn_already_imported)pathnamer,   c                 C   s6   t |d}| W  d   S 1 s(0    Y  dS )zOptional PEP302 get_data API.rbN)openread)r8   r   fr#   r#   r$   get_data!  s    zAssertionRewritingHook.get_datar   
   )r      r   )TraversableResourcesc                 C   s:   t jdk rddlm} nddlm} |tj| j| dS )N)r      r   )
FileReader)r<   )rW   version_infoZimportlib.readersr   Zimportlib.resources.readerstypesSimpleNamespacer1   )r8   r;   r   r#   r#   r$   get_resource_reader,  s    
z*AssertionRewritingHook.get_resource_reader)NN)$r    r!   r"   r~   r   r9   r   r   r:   rG   rH   
PathFinderrQ   rD   ri   r   r   bytesr   
ModuleType
ModuleSpecrS   rg   boolrB   rL   rp   r   r   r   rW   r   Zimportlib.resources.abcr   importlib.abcr   r#   r#   r#   r$   r)   K   s6   
  
)*'
r)   )fprf   re   r,   c                 C   s\   |  tjj d}|  | t|jd@ }|jd@ }|  td|| |  t	
| d S )N           z<LL)ra   rG   r   MAGIC_NUMBERintst_mtimest_sizestructpackmarshaldumps)r   rf   re   flagsmtimesizer#   r#   r$   _write_pyc_fp7  s    

r   )rN   re   rf   rd   r,   c              
   C   s   | dt   }z:t|d}t||| W d    n1 s@0    Y  W n> ty } z&| d| d|j  W Y d }~dS d }~0 0 zt || W n< ty } z$| d| d|  W Y d }~dS d }~0 0 dS )Nr(   wbzerror writing pyc file at z: errno=Fz: T)rJ   getpidr   r   OSErrorrC   errnoreplace)rN   re   rf   rd   Zproc_pycr   er#   r#   r$   r^   I  s    .r^   )rP   r+   r,   c                 C   sN   t | }|  }t| }tj||d}t|||| t||ddd}||fS )z1Read and rewrite *fn* and return the code object.)filenamer_   T)dont_inherit)rJ   stat
read_bytesri   astparserewrite_assertscompile)rP   r+   r   sourceZstrfntreere   r#   r#   r$   r]   b  s    
r]   c                 C   s   d S rR   r#   )xr#   r#   r$   <lambda>n      r   )r   rd   rC   r,   c                 C   sJ  zt |d}W n ty"   Y dS 0 |
 z(t| }t|j}|j}|d}W nF ty } z.|d|  d|  W Y d}~W d   dS d}~0 0 t|dkr|d|   W d   dS |dd t	j
jk r|d|   W d   dS |dd	 d
kr$|d|   W d   dS |d	d }	t|	d|d@ krb|d|   W d   dS |dd }
t|
d|d@ kr|d|   W d   dS zt|}W nH ty } z.|d|  d|  W Y d}~W d   dS d}~0 0 t|tjs"|d|   W d   dS |W  d   S 1 s<0    Y  dS )zsPossibly read a pytest pyc containing rewritten code.

    Return rewritten code if successful or None if not.
    r   N   z
_read_pyc(z): OSError z&_read_pyc(%s): invalid pyc (too short)   z-_read_pyc(%s): invalid pyc (bad magic number)r   r   z._read_pyc(%s): invalid pyc (unsupported flags)r   littler   z_read_pyc(%s): out of datez+_read_pyc(%s): invalid pyc (incorrect size)z): marshal.load error z _read_pyc(%s): not a code object)r   r   rJ   r   r   r   r   r   lenrG   r   r   
from_bytesr   load	ExceptionrF   r   CodeType)r   rd   rC   r   stat_resultr   r   datar   Z
mtime_dataZ	size_datare   r#   r#   r$   r\   m  sN    

$$r\   )r   r   module_pathr+   r,   c                 C   s   t ||||  dS )z%Rewrite the assert statements in mod.N)r|   run)r   r   r   r+   r#   r#   r$   r     s    r   )objr,   c                 C   s   t tj}t| |dddS )a  Get a safe repr of an object for assertion error messages.

    The assertion formatting (util.format_explanation()) requires
    newlines to be escaped since they are a special character for it.
    Normally assertion.util.format_explanation() does this but for a
    custom repr it is possible to contain one of the special escape
    sequences, especially '\n{' and '\n}' are likely to be present in
    JSON reprs.
    maxsize
\n)_get_maxsize_for_safereprr   _configr   r   )r   r   r#   r#   r$   	_saferepr  s    

r   r*   c                 C   s6   | dur|  dnd}|dkr"dS |dkr2td S tS )zJGet `maxsize` configuration for saferepr based on the given config object.Nverboser         r   )Z	getoptionr   )r+   	verbosityr#   r#   r$   r     s    r   c                 C   sB   ddg}t | ts$t| } |d |D ]\}}| ||} q(| S )zFormat the custom assertion message given.

    For strings this simply replaces newlines with '\n~' so that
    util.format_explanation() will preserve them instead of escaping
    newlines.  For other objects saferepr() is used first.
    )r   
~)%%%)r   r   )rF   ri   r   appendr   )r   Zreplacesr1r2r#   r#   r$   _format_assertmsg  s    

r   c                 C   s4   t | rdS zt| d W S  ty.   Y dS 0 d S )NFr    T)callablehasattrr   )r   r#   r#   r$   _should_repr_global_name  s    r   )explanationsis_orr,   c                 C   s&   d|r
dpd |  d }|ddS )N(z or z and rt   r   r   )joinr   )r   r   explanationr#   r#   r$   _format_boolop  s    r   )opsresultsexplseach_objr,   c           	   	   C   s   t tt| ||D ]4\}}}z
| }W n ty>   d}Y n0 |r qJqtjd ur~t| | || ||d  }|d ur~|S |S )NTr   )zipranger   r   r   Z_reprcompare)	r   r   r   r   iresexpldoneZcustomr#   r#   r$   _call_reprcompare  s    


r   )linenoorigr   r,   c                 C   s   t jd urt | || d S rR   r   Z_assertion_pass)r   r   r   r#   r#   r$   _call_assertion_pass  s    
r   r,   c                   C   s   t jr
dS dS )zCheck if any plugins implement the pytest_assertion_pass hook
    in order not to generate explanation unnecessarily (might be expensive).TFr   r#   r#   r#   r$   _check_if_assertion_pass_impl  s    r   znot %sz~%sz-%sz+%s|^&z<<z>>+-*/z//r   z==z!=<z<=>z>=**iszis notinznot in@noder,   c                 c   s(   | V  t | D ]}t|E dH  qdS )zARecursively yield node and all its children in depth-first order.N)r   iter_child_nodestraverse_node)r  childr#   r#   r$   r  !  s    r  r   r   )srcr,   c           
         s  i dg d t  dd fdd}tt| j}|D ]d\}}\}}}}|tjkrr|dkrr| qF durF|tjkr|dv rd7 n|tjkr|d	v rd8 s҈||d  | qFdkrr|tjkrr|d
krr|v r8t	dkr8|t	d  t	| }	d d|	 d< n2|v rXd d| d< n|d|  |  qF|tj
tjhv r|  qFrF|vrF| | qFS )z<Return a mapping from {lineno: "assertion test expression"}.r   Nr   c                      s:    d usJ d  d < dg d  t d S )Nrx   \r   )r   rstripr2   r#   Zassert_linenodepthlinesretZ
seen_linesr#   r$   _write_and_reset2  s    z._get_assertion_exprs.<locals>._write_and_resetassertz([{r   z)]},rh   )r2   tokenizeioBytesIOreadlineNAMEOPr   rl   r   NEWLINE	ENDMARKER)
r  r  tokenstpr   r   offset_lineZoffset_in_trimmedr#   r  r$   _get_assertion_exprs(  s@    	
 

r&  )valuer,   c                 C   s   t jdkr| jS | jS d S )Nr   )rW   r   r'  s)r'  r#   r#   r$   _get_ast_constant_value_  s    
r)  c                       s  e Zd ZdZee ee edd fddZe	j
ddddZeeed	d
dZedddZe	je	jdddZe	je	jdddZee	je	jdddZee	jdddZe	jedddZddddZe	je	jddd Ze	jee	jef d!d"d#Ze	jee	j d$d%d&Z e!ee!ef dd'd(Z"e	jee	jef dd)d*Z#e	j$ee	jef d+d,d-Z%e	j&ee	jef d.d/d0Z'e	j(ee	jef d1d2d3Z)e	j*ee	jef d4d5d6Z+e	j,ee	j,ef d7d8d9Z-e	jee	jef d:d;d<Z.e	j/ee	jef d=d>d?Z0  Z1S )@r|   a
  Assertion rewriting implementation.

    The main entrypoint is to call .run() with an ast.Module instance,
    this will then find all the assert statements and rewrite them to
    provide intermediate values and a detailed assertion error.  See
    http://pybites.blogspot.be/2011/07/behind-scenes-of-pytests-new-assertion.html
    for an overview of how this works.

    The entry point here is .run() which will iterate over all the
    statements in an ast.Module and for each ast.Assert statement it
    finds call .visit() with it.  Then .visit_Assert() takes over and
    is responsible for creating new ast statements to replace the
    original assert statement: it rewrites the test of an assertion
    to provide intermediate values and replace it with an if statement
    which raises an assertion error with a detailed explanation in
    case the expression is false and calls pytest_assertion_pass hook
    if expression is true.

    For this .visit_Assert() uses the visitor pattern to visit all the
    AST nodes of the ast.Assert.test field, each visit call returning
    an AST node and the corresponding explanation string.  During this
    state is kept in several instance attributes:

    :statements: All the AST statements which will replace the assert
       statement.

    :variables: This is populated by .variable() with each variable
       used by the statements so that they can all be set to None at
       the end of the statements.

    :variable_counter: Counter to create new unique variables needed
       by statements.  Variables are created using .variable() and
       have the form of "@py_assert0".

    :expl_stmts: The AST statements which will be executed to get
       data from the assertion.  This is the code which will construct
       the detailed assertion message that is used in the AssertionError
       or for the pytest_assertion_pass hook.

    :explanation_specifiers: A dict filled by .explanation_param()
       with %-formatting placeholders and their corresponding
       expressions to use in the building of an assertion message.
       This is used by .pop_format_context() to build a message.

    :stack: A stack of the explanation_specifiers dicts maintained by
       .push_format_context() and .pop_format_context() which allows
       to build another %-formatted string while already building one.

    :scope: A tuple containing the current scope used for variables_overwrite.

    :variables_overwrite: A dict filled with references to variables
       that change value within an assert. This happens when a variable is
       reassigned with the walrus operator

    This state, except the variables_overwrite,  is reset on every new assert
    statement visited and used by the other visitors.
    N)r   r+   r   r,   c                    sL   t    || _|| _|d ur,|d| _nd| _|| _d| _tt	| _
d S )Nenable_assertion_pass_hookFr#   )superr9   r   r+   r-   r*  r   scoper   dictvariables_overwrite)r8   r   r+   r   	__class__r#   r$   r9     s    
zAssertionRewriter.__init__)r   r,   c                    s  |j s
dS t|dd}|du }|dur4| |r4dS d}d}|j D ]}|rt|tjrt|jtrtt|jt	rt|j}t|t	sJ | |r dS d}n&t|tj
r|jdkr|jdkrn q|d7 }qBt|tjr|jr|jd j n|j tjdkr(tjdd	 dd
tjdd dd
g}ntdd	tddg} fdd|D }||j ||< |f| _|g}|r| }	t|	tjtjtjfrtg | j|	R | _|t |	tkr| jdd | _qnt|	tjsJ t|	D ]\}
}t|trjg }t|D ]L\}}t|tjr6||  | n"|| t|tjr|| qt!|	|
| n&t|tjrt|tj"s|| qqndS )z5Find all assert statements in *mod* and rewrite them.N	docstringr   F
__future__r   r   builtins@py_builtinsr   
col_offsetz_pytest.assertion.rewrite
@pytest_arc                    s   g | ]}t j|g d dqS )r   r5  )r   Import).0aliasr   r#   r$   
<listcomp>  s   z)AssertionRewriter.run.<locals>.<listcomp>rh   )#bodygetattrr}   rF   r   Exprr'  astStrr)  ri   
ImportFromlevelrT   FunctionDefdecorator_listr   rW   r   r:  r,  popAsyncFunctionDefClassDeftupler   _SCOPE_END_MARKERASTiter_fieldslist	enumerateAssertextendvisitsetattrexpr)r8   r   docZexpect_docstringpositemaliasesZimportsnodesr  r;   fieldnewr   r  r#   r;  r$   r     s    














zAssertionRewriter.run)r1  r,   c                 C   s   d| v S )NZPYTEST_DONT_REWRITEr#   )r1  r#   r#   r$   r}     s    z%AssertionRewriter.is_rewrite_disabledr   c                 C   s"   dt t| j }| j| |S )zGet a new variable.z
@py_assert)ri   nextvariable_counter	variablesr   )r8   r;   r#   r#   r$   variable  s    zAssertionRewriter.variable)rR  r,   c              	   C   s:   |   }| jtt|t g| t|t S )zGive *expr* a name.)r]  
statementsr   r   AssignNameStoreLoad)r8   rR  r;   r#   r#   r$   assign  s    "zAssertionRewriter.assignc                 C   s   |  d|S )z Call saferepr on the expression.r   )helper)r8   rR  r#   r#   r$   display"  s    zAssertionRewriter.display)r;   argsr,   c                 G   s4   t dt  }t ||t  }t |t|g S )zCall a helper in this module.r7  )r   r`  rb  	AttributeCallrL  )r8   r;   rf  Zpy_nameattrr#   r#   r$   rd  &  s    zAssertionRewriter.helperr   c                 C   s"   t dt  }t ||t  S )z!Return the builtin called *name*.r4  )r   r`  rb  rg  )r8   r;   builtin_namer#   r#   r$   builtin,  s    zAssertionRewriter.builtinc                 C   s(   dt t| j }|| j|< d| d S )aJ  Return a new named %-formatting placeholder for expr.

        This creates a %-formatting placeholder for expr in the
        current formatting context, e.g. ``%(py0)s``.  The placeholder
        and expr are placed in the current format context so that it
        can be used on the next call to .pop_format_context().
        pyz%(z)s)ri   rZ  r[  explanation_specifiers)r8   rR  	specifierr#   r#   r$   explanation_param1  s    
z#AssertionRewriter.explanation_paramc                 C   s   i | _ | j| j  dS )a  Create a new formatting context.

        The format context is used for when an explanation wants to
        have a variable value formatted in the assertion message.  In
        this case the value required can be added using
        .explanation_param().  Finally .pop_format_context() is used
        to format a string of %-formatted values as added by
        .explanation_param().
        N)rm  stackr   r8   r#   r#   r$   push_format_context=  s    
z%AssertionRewriter.push_format_context)	expl_exprr,   c              	   C   s   | j  }| j r| j d | _dd | D }t|t| }t|t	 |}dt
t| j }| jrx| j| | jtt|t g| t|t S )aZ  Format the %-formatted string with current format context.

        The expl_expr should be an str ast.expr instance constructed from
        the %-placeholders created by .explanation_param().  This will
        add the required code to format said string to .expl_stmts and
        return the ast.Name instance of the formatted string.
        rh   c                 S   s   g | ]}t |qS r#   )r@  )r9  keyr#   r#   r$   r<  U  r   z8AssertionRewriter.pop_format_context.<locals>.<listcomp>z
@py_format)rp  rE  rm  keysr   r   rL  valuesBinOpModri   rZ  r[  r*  format_variablesr   
expl_stmtsr_  r`  ra  rb  )r8   rs  currentru  Zformat_dictformr;   r#   r#   r$   pop_format_contextJ  s    
"z$AssertionRewriter.pop_format_contextr
  c                 C   s.   t |tjsJ | |}|| | |fS )z1Handle expressions we don't have custom code for.)rF   r   rR  rc  ro  re  )r8   r  r   r#   r#   r$   generic_visit^  s    
zAssertionRewriter.generic_visit)assert_r,   c                 C   s&  t |jtjrZt|jjdkrZddlm} ddl}| j	dus@J |j
|dd| j	|jd g | _g | _t | _| jr|g | _g | _g | _|   | |j\}}tt |}| jr| t|}|jr| d|j}d}	ntd	}d
}	tt|	t |}
t|t |
}tdt  }| d|}t!||gg }t"|d}g }|#| j |$| | d|}t%| j&|j }t'| dt(|jt||}t)| d| j|g g }|g}t)|||}| j$| | jrdd | jD }t*|t+d}| j$| n| j}| j$t)||g  |jrP| d|j}d| }ntd	}d
| }t|t t|}| |}| d|}tdt  }t!||gg }t"|d}|$| | jrdd | jD }t*|t+d}| j$| | jD ]"}t,|D ]}t-|| qq| jS )a.  Return the AST statements to replace the ast.Assert instance.

        This rewrites the test of an assertion to provide
        intermediate values and replace it with an if statement which
        raises an assertion error with a detailed explanation in case
        the expression is false.
        r   r   r   Nz5assertion is always true, perhaps remove parentheses?)categoryr   r   r   z	
>assert rx   zassert AssertionError_format_explanationr   r   c                 S   s   g | ]}t |t  qS r#   r   r`  ra  r9  r;   r#   r#   r$   r<    s   z2AssertionRewriter.visit_Assert.<locals>.<listcomp>c                 S   s   g | ]}t |t  qS r#   r  r  r#   r#   r$   r<    r   ).rF   testr   r   r   eltsr   r   warningsr   warn_explicitr   r^  r\  	itertoolscountr[  r*  ry  rp  rz  rr  rP  UnaryOpNotr}  r@  msgrd  rw  Addr`  rb  rh  RaiserO  r   r&  r   r?  astNumIfr_  astNameConstantr  copy_location)r8   r  r   r  Ztop_conditionr   Znegationr  Z	assertmsgZgluestrZerr_explanationerr_msgZerr_namefmtexcZraise_Zstatements_failZfmt_passr   Zhook_call_passZhook_impl_testZstatements_passZ	main_testr\  Zclear_formatr=  templater   stmtr  r#   r#   r$   visit_Assertd  s    	

	




zAssertionRewriter.visit_Assertc                 C   s|   t | dg g }|jj}t t|t  g|g}| d|}t 	t 
 ||g}t || |t|}|| |fS Nlocalsr   )r   rh  rk  r=   idComparer@  Inrd  BoolOpOrIfExpre  ro  )r8   r;   locsZ	target_idinlocsdoreprr  rR  r#   r#   r$   visit_NamedExpr  s    z!AssertionRewriter.visit_NamedExprc                 C   sx   t | dg g }t t|jt  g|g}| d|}t t 	 ||g}t 
|| |t|j}|| |fS r  )r   rh  rk  r  r@  r  r  rd  r  r  r  re  ro  )r8   r;   r  r  r  r  rR  r#   r#   r$   
visit_Name  s    zAssertionRewriter.visit_Name)boolopr,   c              
   C   s  |   }| tg t }t|dt }tt|jtj	}| j
 }}| j}t|jd }	|   t|jD ]D\}
}|
rg }| jt||g  || _t|tjrt|jtr|jjjdd |jd |
 D v r|   }|j| j| j |jjj< ||jj_|   | |\}}|tt|t g| | t|}t||gg }| jt | |
|	k rr|}|rt!t" |}g }| j
t||g  | | _
}qr|| _
|| _| #d|t$|}| |}t|t | %|fS )Nr   r   c                 S   s   g | ]}t |d r|jqS )r  )r   r  )r9  Zast_exprr#   r#   r$   r<    s   
z2AssertionRewriter.visit_BoolOp.<locals>.<listcomp>r   )&r]  rc  r   r
   rb  rg  r   rF   opr  r^  rz  r   rv  rr  rM  r   r  r  left	namedExprr=   r  r.  r,  rP  r_  r`  ra  r}  r@  rh  r?  r  r  rd  r  ro  )r8   r  Zres_varZ	expl_listZappr   r=  saveZ	fail_saveZlevelsr   vZ
fail_innerZcondZpytest_tempr   r   Zexpl_formatcallinnerZexpl_templater#   r#   r$   visit_BoolOp  s^    





 
zAssertionRewriter.visit_BoolOp)unaryr,   c                 C   s>   t |jj }| |j\}}| t|j|}|||f fS rR   )	UNARY_MAPr  r0  rP  operandrc  r   r  )r8   r  patternZoperand_resZoperand_explr   r#   r#   r$   visit_UnaryOp"  s    zAssertionRewriter.visit_UnaryOp)binopr,   c           	      C   sb   t |jj }| |j\}}| |j\}}d| d| d| d}| t||j|}||fS )Nr    rt   )		BINOP_MAPr  r0  rP  r  rightrc  r   rw  )	r8   r  symbolZ	left_expr	left_explZ
right_exprZ
right_explr   r   r#   r#   r$   visit_BinOp(  s    zAssertionRewriter.visit_BinOp)r  r,   c                 C   sn  |  |j\}}g }g }g }|jD ]Z}t|tjrZ|j| j| j	i v rZ| j| j	 |j }|  |\}}	|
|	 |
| q"|jD ]}
t|
jtjr|
jj| j| j	i v r| j| j	 |
jj |
_|  |
j\}}	|
t|
j| |
jr|
|
jd |	  q|
d|	  qd|d|}	t|||}| |}| | |}| d| d|	 d}||fS )N=r  z{}({})z, z
{z = z
})rP  funcrf  rF   r   r`  r  r.  getr,  r   keywordsr'  keywordargformatr   rh  rc  ro  re  )r8   r  new_funcZ	func_explZ	arg_explsnew_argsZ
new_kwargsr  r   r   r  Znew_callres_explZ
outer_explr#   r#   r$   
visit_Call0  sD    





zAssertionRewriter.visit_Call)starredr,   c                 C   s*   |  |j\}}t||j}|d| fS )Nr  )rP  r'  r   Starredctx)r8   r  r   r   Znew_starredr#   r#   r$   visit_StarredT  s    zAssertionRewriter.visit_Starred)ri  r,   c                 C   sp   t |jtjs| |S | |j\}}| t||j	t }| 
| |}d}|||||j	f }||fS )Nz%s
{%s = %s.%s
})rF   r  r   rb  r~  rP  r'  rc  rg  ri  ro  re  )r8   ri  r'  Z
value_explr   r  rr   r   r#   r#   r$   visit_AttributeZ  s    
z!AssertionRewriter.visit_Attribute)compr,   c                    sx      t|jtjrD|jj j ji v rD j j |jj |_t|jt	rh|j j j |jj
j<  |j\}}t|jtjtjfrd| d} fddtt|jD }dd |D }dd |D }ttt|j|j|j}g }g }	|g}
|D ]\}}}t|t	rNt|tjrN|j
j|jkrN  |j
_| j j |j<  |\}}t|tjtjfr|d| d}|
| t|j }|	t| | d| d| }|t| t||g|g} jt|| g| || }}q dt|	t t|t t|t t|
t }t|jd	kr\tt |}n|d
 }|  |fS )Nr   rt   c                    s   g | ]}   qS r#   )r]  )r9  r   rq  r#   r$   r<  t  r   z3AssertionRewriter.visit_Compare.<locals>.<listcomp>c                 S   s   g | ]}t |t  qS r#   )r   r`  rb  r9  r  r#   r#   r$   r<  u  r   c                 S   s   g | ]}t |t  qS r#   r  r  r#   r#   r$   r<  v  r   r  r   r   r   ) rr  rF   r  r   r`  r  r.  r  r,  r  r=   rP  r  r  r   r   r   r   comparatorsr]  r   r  r0  r@  r^  r_  rd  r   rb  Andro  r}  )r8   r  Zleft_resr  Zres_variablesZ
load_namesZstore_namesitr   Zsymsr   r   r  Znext_operandZnext_resZ	next_explsymr   Zres_exprZ	expl_callr   r#   rq  r$   visit_Compared  st    





zAssertionRewriter.visit_Compare)2r    r!   r"   r~   r   ri   r   r   r9   r   Moduler   staticmethodr   r}   r]  rR  r`  rc  re  rd  rg  rk  ro  rr  r}  rJ  r   r~  rN  r
   r  r  r  r  r  r  r  r  r  rw  r  rh  r  r  r  r  r  r  __classcell__r#   r#   r/  r$   r|   f  s4   ;^u
3$
r|   )rb   r,   c              
   C   s   zt j| dd W nj tttfy,   Y dS  ty>   Y dS  ty| } z(|jtjkrfW Y d}~dS  W Y d}~n
d}~0 0 dS )z~Attempt to create the given directory and sub-directories exist.

    Returns True if successful or if it already exists.
    T)exist_okFN)	rJ   makedirsFileNotFoundErrorNotADirectoryErrorFileExistsErrorPermissionErrorr   r   ZEROFS)rb   r   r#   r#   r$   rZ     s    rZ   )	file_pathr,   c                 C   s:   t jdkr,t jr,tt jt| jdd   S | jd S dS )zKReturn the cache directory to write .pyc files for the given .py file path.r   r   rh   __pycache__N)rW   r   pycache_prefixr   rq   parent)r  r#   r#   r$   rY     s    rY   )NN)r~   r   r   	functoolsr   rG   Zimportlib.machineryimportlib.utilr  r  r   rJ   r   rW   r  r   collectionsr   pathlibr   r   typingr   r   r   r   r	   r
   r   r   r   r   r   r   Z_pytest._io.safereprr   r   Z_pytest._versionr   Z_pytest.assertionr   Z_pytest.assertion.utilr   r  Z_pytest.configr   Z_pytest.mainr   Z_pytest.pathlibr   r   Z_pytest.stashr   r   r   	NamedExprr  Constantr  r@  r  r?  NameConstantStrNumr   rA   implementation	cache_tagZ
PYTEST_TAGZPYC_EXTr[   rI  abcMetaPathFinderLoaderr)   r   r   r   r   r   r^   r]   ri   r\   r  r   objectr   r   r   r   r   r   r   r   r   r  InvertUSubUAddr  BitOrBitXorBitAndLShiftRShiftr  SubMultDivFloorDivrx  EqNotEqLtLtEGtGtEPowIsIsNotr  NotInMatMultr  rJ  r  	lru_cacher&  r)  NodeVisitorr|   rZ   rY   r#   r#   r#   r$   <module>   s  
 n4  



6    >