
    9[g                    f   d Z ddlmZ ddlZddl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ZddlZddlmZ ddlm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/Z0 erddl#m1Z1  G d d      Z2 e-d          Z3ejh                  jj                   de" Z6ddxs dz   Z7de6z   e7z   Z8 e2       Z9 G d  d!e	jt                  jv                  e	jt                  jx                        Z=	 	 	 	 	 	 	 	 dSd"Z>	 	 	 	 	 	 	 	 	 	 dTd#Z?dUd$Z@d% f	 	 	 	 	 	 	 dVd&ZA	 	 dW	 	 	 	 	 	 	 	 	 dXd'ZBdYd(ZCdZd)ZDdYd*ZEd[d+ZFd\d,ZG	 	 	 	 	 	 	 	 	 	 d]d-ZHd^d.ZId_d/ZJej                  d0ej                  d1ej                  d2ej                  d3iZOi ej                  d4ej                  d5ej                  d6ej                  d7ej                  d8ej                  d9ej                  d:ej                  d;ej                  d<ej                  d=ej                  d>ej                  d?ej                  d@ej                  dAej                  dBej                  dCej                  dDej                  dEej                  dFej                  dGej                  dHej                  dIej                  dJiZgd`dKZh ej                  dLM      dadN       Zj G dO dPej                        ZldbdQZmdcdRZny)dz5Rewrite assertion AST to produce nice error messages.    )annotationsN)defaultdict)Path)PurePath)Callable)IO)Iterable)Iterator)Sequence)TYPE_CHECKING)DEFAULT_REPR_MAX_SIZE)saferepr)version)util)Config)Session)absolutepath)
fnmatch_ex)StashKey)format_explanation)AssertionStatec                      e Zd Zy)SentinelN)__name__
__module____qualname__     T/var/www/html/bid-api/venv/lib/python3.12/site-packages/_pytest/assertion/rewrite.pyr   r   0   s    r   r   r   z-pytest-.pyco.c                  $   e Zd ZdZddZddZej                  j                  j                  Z
	 	 d	 	 	 	 	 	 	 ddZ		 	 	 	 ddZddZddZdd	Zdd
ZddZddZddZej(                  dk\  r!ej(                  dk\  rddlmZ nddlmZ ddZyy)AssertionRewritingHookz1PEP302/PEP451 import hook which rewrites asserts.c                    || _         	 |j                  d      | _        d | _        i | _        t               | _        d| _        dh| _	        i | _
        d| _        y # t        $ r ddg| _        Y Ow xY w)Npython_filesz	test_*.pyz	*_test.pyFconftest)configgetinifnpats
ValueErrorsession_rewritten_namesset_must_rewrite_writing_pyc_basenames_to_check_rewrite_marked_for_rewrite_cache_session_paths_checked)selfr)   s     r   __init__zAssertionRewritingHook.__init__B   sz    	5 --7DK (,13'*u ",6<(:<&&+#  	5&4DK	5s   A A/.A/c                     || _         d| _        y )NF)r-   r4   )r5   r-   s     r   set_sessionz"AssertionRewritingHook.set_sessionR   s    &+#r   Nc                   | j                   ry | j                  j                  t           }| j	                  ||      ry |j                  d|        | j                  ||      }|-|+|D ]&  }t        j                  j                  ||      }|& n |c|j                  Wt        |j                  t        j                  j                        r)t        j                   j#                  |j                        sy |j                  }| j%                  |||      sy t        j                  j                  ||| |j&                        S )Nzfind_module called for: )loadersubmodule_search_locations)r1   r)   stashassertstate_key_early_rewrite_bailouttrace
_find_spec	importlibr   spec_from_file_locationorigin
isinstancer:   	machinerySourceFileLoaderospathexists_should_rewriter;   )r5   namerH   targetstatespec	_path_strfns           r   	find_specz AssertionRewritingHook.find_specY   s"    !!/2&&tU3.tf56 tT*<D, "	 ~~==dIN# " L {{"dkk9+>+>+O+OP77>>$++.B##D"e4~~55'+'F'F	 6 
 	
r   c                     y Nr   )r5   rN   s     r   create_modulez$AssertionRewritingHook.create_module   s     r   c                   |j                   J |j                   j                  J t        |j                   j                        }| j                  j                  t
           }|| j                  |j                  <   t        j                   }t        |      }|r#t        |      }|sd}|j                  d|        |j                  d d t        z   }||z  }t        |||j                        }	|	M|j                  d|       t!        || j                        \  }
}	|r2d| _        	 t%        ||	|
|       d| _        n|j                  d|        t'        |	|j(                         y # d| _        w xY w)NFzread only directory: z
rewriting Tzfound cached rewritten pyc for )__spec__rC   r   r)   r<   r=   r.   r   sysdont_write_bytecodeget_cache_dirtry_makedirsr?   rK   PYC_TAIL	_read_pyc_rewrite_testr1   
_write_pycexec__dict__)r5   modulerP   rM   write	cache_dirok
cache_namepyccosource_stats              r   exec_modulez"AssertionRewritingHook.exec_module   sY   ***%%111&//(()!!/213foo. +++!"%	i(B3I;?@WWSb\H,
*$ r3,:KK*RF+,+B<OK$(!.ub+s;(-D%KK9">?R! ).D%s   (E) )	E2c                   | j                   | j                  sd| _        | j                   j                  D ]h  }t        |      j	                  t
        j                        }| j                  j                  t
        j                  j                  |d         d          j |j	                  d      }|d   | j                  v ryt        | j                  d      }| j                  D ]1  }t
        j                  j                  |      r yt        ||      s1 y | j!                  ||      ry|j#                  d|        y)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.
        Tr   r#   Fr    z early skip of rewriting module: )r-   r4   _initialpathsstrsplitrG   sepr2   addrH   splitextr   with_suffixr+   dirnamer   _is_marked_for_rewriter?   )r5   rK   rM   initial_pathpartsrH   pats          r   r>   z-AssertionRewritingHook._early_rewrite_bailout   s    <<#D,G,G*.D' $ : : L)//70044RWW5E5EeBi5PQR5ST !; 

39888 ++E2;;C wws##t$  &&tU36tf=>r   c                   t         j                  j                  |      dk(  r|j                  d|       y| j                  9| j                  j                  t        |            r|j                  d|       yt        |      }| j                  D ]$  }t        ||      s|j                  d|        y | j                  ||      S )Nzconftest.pyzrewriting conftest file: Tz.matched test file (was specified on cmdline): zmatched test file )rG   rH   basenamer?   r-   
isinitpathr   r   r+   r   ru   )r5   rK   rP   rM   fn_pathrx   s         r   rJ   z&AssertionRewritingHook._should_rewrite   s    77B=0KK3B6:;<<#||&&|B'78LRFST 2,;;C#w'078 
 **477r   c           	        	 | j                   |   S # t        $ rf | j                  D ]E  }||k(  s|j                  |dz         s|j	                  d|d|d       d| j                   |<    Y y d| j                   |<   Y yw xY w)Nr#   zmatched marked file z (from )TF)r3   KeyErrorr0   
startswithr?   )r5   rK   rM   markeds       r   ru   z-AssertionRewritingHook._is_marked_for_rewrite   s    
	11$77 	,,6>T__Vc\%BKK"6thgfZq QR;?D2248	 - 49D**40	s    1B (B -B ?B c                   t        |      j                  t        j                        j	                  | j
                        }|D ]j  }t        j                  |   }t        j                  |j                  xs d      r:t        |j                  t        |             rZ| j                  |       l | j                  j                  |       | j                  j!                          y)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)r/   intersectionrX   modules
differencer.   AssertionRewriteris_rewrite_disabled__doc__rD   
__loader__type_warn_already_importedr0   updater3   clear)r5   namesalready_importedrK   mods        r   mark_rewritez#AssertionRewritingHook.mark_rewrite  s     J##CKK0;;D<Q<QR 	 %D++d#C$88!r d<++D1 % 	!!%(&&,,.r   c                \    ddl m} | j                  j                   |d|       d       y )Nr   PytestAssertRewriteWarningz0Module already imported so cannot be rewritten:    )
stacklevel)_pytest.warning_typesr   r)   issue_config_time_warning)r5   rK   r   s      r   r   z-AssertionRewritingHook._warn_already_imported  s3    D--&B4&I 	 	. 	
r   c                f    t        |d      5 }|j                         cddd       S # 1 sw Y   yxY w)zOptional PEP302 get_data API.rbN)openread)r5   pathnamefs      r   get_datazAssertionRewritingHook.get_data  s#    (D!Q668 "!!s   '0   
   )r      r   )TraversableResourcesc                    t         j                  dk  rddlm} nddlm}  |t        j                  | j                  |               S )N)r      r   )
FileReader)rH   )rX   version_infoimportlib.readersr   importlib.resources.readerstypesSimpleNamespacer.   )r5   rK   r   s      r   get_resource_readerz*AssertionRewritingHook.get_resource_reader*  s7    ')8Be339N9Nt9TUVVr   )r)   r   returnNone)r-   zSession | Noner   r   NN)rK   rn   rH   zSequence[str | bytes] | NonerL   types.ModuleType | Noner   z%importlib.machinery.ModuleSpec | None)rN   zimportlib.machinery.ModuleSpecr   r   )rb   ztypes.ModuleTyper   r   )rK   rn   rM   r   r   bool)rK   rn   rP   rn   rM   r   r   r   )r   rn   r   r   )rK   rn   r   r   )r   zstr | bytesr   bytes)rK   rn   r   r   )r   r   r   r   r6   r8   rA   rE   
PathFinderrQ   r@   rT   rj   r>   rJ   ru   r   r   r   rX   r   importlib.resources.abcr   importlib.abcr   r   r   r   r%   r%   ?   s    ;, ,
 $$//99J
 .2*.	0
0
 +0
 (	0

 
/0
d2	 
("T%N8*/$

 7"w&D:	W #r   r%   c                ^   | j                  t        j                  j                         d}| j                  |       t	        |j
                        dz  }|j                  dz  }| j                  t        j                  d||             | j                  t        j                  |             y )N           z<LL)rc   rA   r   MAGIC_NUMBERintst_mtimest_sizestructpackmarshaldumps)fpri   rh   flagsmtimesizes         r   _write_pyc_fpr   3  s}     HHY^^(()EHHUO$$%
2E+DHHV[[t,-HHW]]2r   c                   | dt        j                          }	 t        |d      5 }t        |||       d d d        	 t        j                  ||       y# 1 sw Y   !xY w# t        $ r+}| j                  d| d|j                          Y d }~yd }~ww xY w# t        $ r!}| j                  d| d|        Y d }~yd }~ww xY w)Nr#   wbzerror writing pyc file at z: errno=Fz: T)rG   getpidr   r   OSErrorr?   errnoreplace)rM   rh   ri   rg   proc_pycr   es          r   r_   r_   E  s     a		}%H(D!R"k2. "


8S!  "! 0
(177)LM  0Rs;< sD   A! AA! B AA! !	B*!BB	C!B==Cc                    t        j                  |       }| j                         }t        |       }t	        j
                  ||      }t        ||||       t        ||dd      }||fS )z1Read and rewrite *fn* and return the code object.)filenamer`   T)dont_inherit)rG   stat
read_bytesrn   astparserewrite_assertscompile)rP   r)   r   sourcestrfntreerh   s          r   r^   r^   ^  sZ    772;D]]_FGE99Ve,DD&%0	uf4	8B8Or   c                     y rS   r   )xs    r   <lambda>r   j  s    dr   c                   	 t        |d      }|5  	 t        j                  |       }t	        |j
                        }|j                  }|j                  d      }t        |      dk7  r |d|  d       	 ddd       y|dd t        j                  j                  k7  r |d|  d       	 ddd       y|dd	 d
k7  r |d|  d       	 ddd       y|d	d }	t        j                  |	d      |dz  k7  r |d|  d       	 ddd       y|dd }
t        j                  |
d      |dz  k7  r |d|  d       	 ddd       y	 t        j                  |      }t!        |t"        j$                        s |d|  d       	 ddd       y|cddd       S # t        $ r Y yw xY w# t        $ r } |d|  d|        Y d}~ddd       yd}~ww xY w# t        $ r } |d|  d|        Y d}~ddd       yd}~ww xY w# 1 sw Y   yxY w)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): invalid pyc (too short)   z!): invalid pyc (bad magic number)   r   z"): invalid pyc (unsupported flags)r   littler   z): out of datez): invalid pyc (incorrect size)z): marshal.load error z): not a code object)r   r   rG   r   r   r   r   r   lenrA   r   r   
from_bytesr   load	ExceptionrD   r   CodeType)r   rg   r?   r   stat_resultr   r   datar   
mtime_data	size_datarh   s               r   r]   r]   i  s*   #t_ 
	''&/K,,-E&&D772;D
 t9Jvh&@AB 
 8y~~222Jvh&GHI 
  !9++Jvh&HIJ% 
& !BZ
>>*h/5:3EEJvhn56- 
. BK	>>)X.$2CCJvh&EFG5 
6	b!B "enn-Jvh&:;<E 
F G 
    	Jvhk!56 
	.  	Jvh&<QC@A? 
:	; 
s   F G7AFG7=-G73G7.G7.G7 G'G7G7	FF	G(G6G7GG7	G4G/"G7/G44G77H c                <    t        |||      j                  |        y)z%Rewrite the assert statements in mod.N)r   run)r   r   module_pathr)   s       r   r   r     s     k66266s;r   c                    t        | t        j                        r| j                  S t	        t
        j                        }t        | |      j                  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)	rD   r   
MethodTyper   _get_maxsize_for_safereprr   _configr   r   )objr   s     r   	_safereprr     sF     #u''(||'5GC)11$>>r   c                ~    | d}n| j                  t        j                        }|dk\  ry|dk\  r	t        dz  S t        S )zJGet `maxsize` configuration for saferepr based on the given config object.Nr         r   )get_verbosityr   VERBOSITY_ASSERTIONSr   )r)   	verbositys     r   r   r     sD    ~	(()D)DE	A~A~$r))  r   c                    ddg}t        | t              s4t        | t        t        j
                              } |j                  d       |D ]  \  }}| j                  ||      }  | 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  )rD   rn   r   r   r   r   appendr   )r   replacesr1r2s       r   _format_assertmsgr    s_     {+Hc3s5dllCD'Bkk"b!  Jr   c                T    t        |       ry	 t        | d       S # t        $ r Y yw xY w)NFr   T)callablehasattrr   )r   s    r   _should_repr_global_namer    s3    }3
+++ s    	''c                d    d|xr dxs dj                  |       z   dz   }|j                  dd      S )N(z or z and r~   r  r  )joinr   )explanationsis_orexplanations      r   _format_boolopr    s9    )64W::<HH3NKsD))r   c                    t        t        t        |             ||      D ]  \  }}}	 | }|s n t        j
                  't	        j
                  |    ||   ||dz            }||S S # t        $ r d}Y Lw xY w)NTr   )zipranger   r   r   _reprcompare)	opsresultsexplseach_objiresexpldonecustoms	            r   _call_reprcomparer!    s     E#c(OWe<3	7D  = $""3q68A;QHMK  	D	s   A**A87A8c                T    t         j                  t        j                  | ||       y y rS   r   _assertion_pass)linenoorigr  s      r   _call_assertion_passr'    s%    'VT40 (r   c                 *    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   _check_if_assertion_pass_implr)    s     ''42U2r   znot %sz~%sz-%sz+%s|^&z<<z>>+-*/z//r  z==z!=<z<=>z>=**iszis notinznot in@c              #  r   K   |  t        j                  |       D ]  }t        |      E d{     y7 w)zARecursively yield node and all its children in depth-first order.N)r   iter_child_nodestraverse_node)nodechilds     r   r9  r9  $  s1     
J%%d+ ''' ,'s   +757r   r   c                  
 i dg d
t               d

fd}t        j                  t        j                  |       j                        }|D ]g  \  }}\  }}}}|t        j
                  k(  r|dk(  r|
(
+|t        j                  k(  r
|dv rdz  n|t        j                  k(  r	|dv rdz  s&j                  ||d        j                  |       dk(  r|t        j                  k(  rm|dk(  rh|v r7t              dk(  r)|t        d	         z   t        |      z
  }	d	   d|	 d	<   n$|v rd	   d| d	<   nj                  |d|         |        |t        j                  t        j                  hv r	 |        <s@|vsFj                  |       j                  |       j S )z<Return a mapping from {lineno: "assertion test expression"}.r   Nc                      J dj                        j                         j                  d       <   dg d  t               y )Nr   \r   )r  rstripr/   )assert_linenodepthlinesret
seen_liness   r   _write_and_resetz._get_assertion_exprs.<locals>._write_and_reset5  sK    (((WWU^224;;DAMU
r   assertz([{r   z)]},rl   r   r   )r/   tokenizeioBytesIOreadlineNAMEOPr  rq   r   NEWLINE	ENDMARKER)srcrE  tokenstpr   r%  offset_lineoffset_in_trimmedr@  rA  rB  rC  rD  s             @@@@@r   _get_assertion_exprsrX  +  s    CEE $M5J  rzz#778F17-F$VVa6X#5"M&X[[ Vu_
x{{"v
T&']+v&!hkk 1fmZ'CJ!O(.U2Y(?#d)(K% %b	*<+< =E"Iz) %b	'6 2E"I LLgv/ "(((*<*<== "63T"v&= 28@ Jr   c                       e Zd ZdZ	 	 	 	 	 	 	 	 d fdZddZedd       ZddZddZ	ddZ
ddZdd	Zd d
Zd!dZd"dZd#dZd$dZd%dZd&dZd'dZd(dZd)dZd*dZd+dZd,dZd-dZ xZS ).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.
    c                    t         |           || _        || _        ||j	                  d      | _        nd| _        || _        d| _        t        t              | _
        y )Nenable_assertion_pass_hookFr   )superr6   r   r)   r*   r[  r   scoper   dictvariables_overwrite)r5   r   r)   r   	__class__s       r   r6   zAssertionRewriter.__init__  sa     	&.4mm,/D+ /4D+*,
 	 r   c                   |j                   syt        |dd      }|du }|| j                  |      ryd}d}|j                   D ]  }|rt        |t        j
                        rtt        |j                  t        j                        rPt        |j                  j                  t              r,|j                  j                  }| j                  |      r yd}n;t        |t        j                        r|j                  dk(  r|j                  dk(  rn n|dz  } t        |t        j                        r&|j                  r|j                  d   j                  }n|j                  }t        j                   dk\  r3t	        j"                  dd	|d
      t	        j"                  dd|d
      g}n,t	        j"                  dd	      t	        j"                  dd      g}|D cg c]  }t	        j$                  |g|d
       }	}|	|j                   || |f| _        |g}
|
r|
j)                         }t        |t        j                  t        j*                  t        j,                  f      r4t/        g | j&                  |      | _        |
j1                  t2               |t2        k(  r| j&                  dd | _        t        |t        j4                        sJ t	        j6                  |      D ]  \  }}t        |t8              rg }t;        |      D ]}  \  }}t        |t        j<                        r!|j?                  | jA                  |             A|j1                  |       t        |t        j4                        sm|
j1                  |        tC        |||       t        |t        j4                        st        |t        jD                        r|
j1                  |        |
ryyc c}w )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_arrl   )#bodygetattrr   rD   r   ExprvalueConstantrn   
ImportFromlevelrb   FunctionDefdecorator_listr%  rX   r   aliasImportr]  popAsyncFunctionDefClassDeftupler  _SCOPE_END_MARKERASTiter_fieldslist	enumerateAssertextendvisitsetattrexpr)r5   r   docexpect_docstringpositemr%  aliasesrq  importsnodesr:  rK   fieldnewr  r;  s                    r   r   zAssertionRewriter.run  s   xx c;-$;?t77<HHD tSXX.tzz3<<8tzz//5jj&&++C0#( 40JJ!OKK</1HC' , dCOO,1D1D((+22F[[Fw&		*nVPQR		/ ! 	G 		*n5		5|DG
 KR
JQCJJwv!<' 	 
 $S V
+.%99;D$#2F2F UV"#6TZZ#6#67
./((!ZZ_
dCGG,,,"t4eeT*)+C$-e$45%eSZZ8JJtzz%'89JJu-)%9 %U 3 %5 D$,ucgg. 'uchh7LL'%  5 
s    O%c                
    d| v S )NPYTEST_DONT_REWRITEr   )rb  s    r   r   z%AssertionRewriter.is_rewrite_disabled  s    $	11r   c                ~    dt        t        | j                              z   }| j                  j	                  |       |S )zGet a new variable.z
@py_assert)rn   nextvariable_counter	variablesr  )r5   rK   s     r   variablezAssertionRewriter.variable  s5     c$t'<'<"=>>d#r   c           	     F   | j                         }| j                  j                  t        j                  t        j
                  |t        j                               g|             t        j                  t        j
                  |t        j                               |      S )zGive *expr* a name.)	r  
statementsr  r   AssignNameStorecopy_locationLoad)r5   r  rK   s      r   assignzAssertionRewriter.assign  sb    }}szz388D#))++F*GNO  $
!;TBBr   c                &    | j                  d|      S )z Call saferepr on the expression.r   )helper)r5   r  s     r   displayzAssertionRewriter.display  s    {{;--r   c                    t        j                  dt        j                               }t        j                  ||t        j                               }t        j                  |t        |      g       S )zCall a helper in this module.rg  )r   r  r  	AttributeCallrz  )r5   rK   argspy_nameattrs        r   r  zAssertionRewriter.helper!  sG    ((<4}}WdCHHJ7xxd4j"--r   c                    t        j                  dt        j                               }t        j                  ||t        j                               S )z!Return the builtin called *name*.re  )r   r  r  r  )r5   rK   builtin_names      r   builtinzAssertionRewriter.builtin'  s1    xx
;}}\4<<r   c                r    dt        t        | j                              z   }|| j                  |<   d|z   dz   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)rn   r  r  explanation_specifiers)r5   r  	specifiers      r   explanation_paramz#AssertionRewriter.explanation_param,  s>     3tD$9$9:;;	15##I.i$&&r   c                \    i | _         | j                  j                  | j                          y)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)r  stackr  )r5   s    r   push_format_contextz%AssertionRewriter.push_format_context8  s$     <>#

$556r   c           	        | j                   j                         }| j                   r| j                   d   | _        |j                         D cg c]  }t	        j
                  |       }}t	        j                  |t        |j                                     }t	        j                  |t	        j                         |      }dt        t        | j                              z   }| j                  r| j                  j!                  |       | j"                  j!                  t	        j$                  t	        j&                  |t	        j(                               g|             t	        j&                  |t	        j*                               S c c}w )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.
        rl   z
@py_format)r  rs  r  keysr   rl  Dictrz  valuesBinOpModrn   r  r  r[  format_variablesr  
expl_stmtsr  r  r  r  )r5   	expl_exprcurrentkeyr  format_dictformrK   s           r   pop_format_contextz$AssertionRewriter.pop_format_contextE  s    **.."::*.**R.D'DKLLN&SNSs||C'8N&ShhtT'..*:%;<yyCGGI{;c$t'<'<"=>>**!!((.szz388D#))++F*GNOxxchhj)) 'Ts   Fc                    t        |t        j                        sJ | j                  |      }|| j	                  | j                  |            fS )z1Handle expressions we don't have custom code for.)rD   r   r  r  r  r  )r5   r:  r  s      r   generic_visitzAssertionRewriter.generic_visitY  sC    $)))kk$D**4<<+<===r   c           	        t        |j                  t        j                        rit	        |j                  j
                        dk\  rGddl}ddlm} | j                  J |j                   |d      d| j                  |j                         g | _        g | _        t        j                         | _        | j"                  rg | _        g | _        g | _        | j+                          | j-                  |j                        \  }}t        j.                  t        j0                         |      }| j"                  r| j3                  t        j4                  |            }|j6                  r| j9                  d|j6                        }d}	nt        j4                  d	      }d
}	t        j:                  t        j4                  |	      t        j<                         |      }
t        j:                  |t        j<                         |
      }t        j>                  dt        j@                               }| j9                  d|      }t        jB                  ||gg       }t        jD                  |d      }g }|jG                  | j(                         |jI                  |       | j9                  d|      }tK        | jL                        |j                     }t        jN                  | j9                  dt        j4                  |j                        t        j4                  |      |            }t        jP                  | j9                  d      g | j(                  |g       }|g}t        jP                  |||      }| j                  jI                  |       | j$                  r| j$                  D cg c]*  }t        j>                  |t        jR                               , }}t        jT                  |t        j4                  d            }| j                  jI                  |       nJ| j(                  }| j                  jI                  t        jP                  ||g              |j6                  r"| j9                  d|j6                        }d|z   }nt        j4                  d	      }d
|z   }t        j:                  |t        j<                         t        j4                  |            }| j3                  |      }| j9                  d|      }t        j>                  dt        j@                               }t        jB                  ||gg       }t        jD                  |d      }|jI                  |       | j                  r| j                  D cg c]*  }t        j>                  |t        jR                               , }}t        jT                  |t        j4                  d            }| j                  jI                  |       | j                  D ]6  }tW        |      D ]&  }tY        |dd      t        jZ                  ||       ( 8 | j                  S c c}w c c}w )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   Nr   z5assertion is always true, perhaps remove parentheses?)categoryr   r%  r  z	
>assert r   zassert AssertionError_format_explanationr'  r)  r%  ).rD   testr   Tupler   eltswarningsr   r   r   warn_explicitr%  r  r  	itertoolscountr  r[  r  r  r  r  r~  UnaryOpNotr  rl  msgr  r  Addr  r  r  Raiser}  r  rX  r   rj  Ifr  r  r9  ri  r  )r5   assert_r  r   top_conditionr  negationr  	assertmsggluestrerr_explanationerr_msgerr_namefmtexcraise_statements_failfmt_passr&  hook_call_passhook_impl_teststatements_pass	main_testrK   r  clear_formatrh  templater   stmtr:  s                                  r   visit_AssertzAssertionRewriter.visit_Assert_  s    gllCII.3w||7H7H3IQ3NH ##///""*K ))~~ #  +-$& ) 1**/1D!02
*,  "%)ZZ%="{;;swwy-8**))#,,{*CDC {{ KK(;W[[I	&LL,	#!iiW(=swwy#NOii	3779oFGxx 0#((*=H++3W=C((8cUB/CYYsD)F O""4??3""6* {{#8#>H'4W^^DD XX*LL0LL&	N !VV;<2$//2>2N
 0>.>O x/JIOO""9-$$<@<Q<Q-<QDCHHT399;/<Q  -  #zz)S\\$5GH&&|4 ??DOO""366(D"#=>{{ KK(;W[[I	*[8LL,	'+5yyCGGIs||K7PQH))(3C++3S9Cxx 0#((*=H((8cUB/CYYsD)FKK >>AEP$		4IPJJy#,,t*<=EOO""5)OOD%d+4408 %%dG4	 , $ K-6 Qs   )/Y
/Yc                   t        j                  | j                  d      g g       }|j                  j                  }t        j
                  t        j                  |      t        j                         g|g      }| j                  d|      }t        j                  t        j                         ||g      }t        j                  || j                  |      t        j                  |            }|| j                  |      fS Nlocalsr  )r   r  r  rL   idComparerl  Inr  BoolOpOrIfExpr  r  )r5   rK   locs	target_idinlocsdoreprr  r  s           r   visit_NamedExprz!AssertionRewriter.visit_NamedExpr  s     xxX.B7KKNN	S\\)4svvxj4&I7>zz#&&(VV$45yyt||D13<<	3JKT++D111r   c                   t        j                  | j                  d      g g       }t        j                  t        j                  |j
                        t        j                         g|g      }| j                  d|      }t        j                  t        j                         ||g      }t        j                  || j                  |      t        j                  |j
                              }|| j                  |      fS r  )r   r  r  r  rl  r  r  r  r  r  r  r  r  )r5   rK   r  r  r  r  r  s          r   
visit_NamezAssertionRewriter.visit_Name  s     xxX.B7S\\$''2SVVXJG7>zz#&&(VV$45yyt||D13<<3HIT++D111r   c           
        | j                         }| j                  t        j                  g t        j                                     }t        j
                  |dt        j                               }t        t        |j                  t        j                              }| j                  x}}| j                  }t        |j                        dz
  }	| j                          t        |j                        D ]  \  }
}|
r9g }| j                  j!                  t        j"                  |g              || _        t        |t        j$                        rt        |j&                  t        j(                        r|j&                  j*                  j,                  |j                  d |
 D cg c]  }t/        |d      r|j,                   c}v ro| j                         }|j&                  | j0                  | j2                     |j&                  j*                  j,                  <   ||j&                  j*                  _        | j                          | j5                  |      \  }}|j!                  t        j6                  t        j8                  |t        j:                               g|             | j=                  t        j>                  |            }t        j@                  ||gg       }| j                  j!                  t        jB                  |             |
|	k  s$|}|r(t        jD                  t        jF                         |      }g }| j                  j!                  t        j"                  ||g              |x| _
        } || _
        || _        | jI                  d|t        j>                  |            }| j=                  |      }t        j8                  |t        j                               | jK                  |      fS c c}w )Nr  r   r  r  )&r  r  r   Listr  r  r   rD   opr  r  r  r   r  r  r{  r  r  r  left	NamedExprrL   r  r  r_  r]  r~  r  r  r  r  rl  r  rj  r  r  r  r  )r5   boolopres_var	expl_listappr  rh  save	fail_savelevelsr  v
fail_innercondast_exprpytest_tempr  r  expl_formatcallinnerexpl_templates                         r   visit_BoolOpzAssertionRewriter.visit_BoolOp  s   --/KKSXXZ 89	mmIx<Jvyy#&&12oo%tOO	V]]#a'  "fmm,DAq-/
&&svvdJ'CD", q#++."1663==9(( )/bq(9(9H"8T2 !(9 #'--/KMNVVD,,TZZ89I9IJ'2AFFMM$$$&

1ICKK

CHHWciik$B#CSIJ11#,,t2DEK88C+3DOO""388D>26z!$;;swwy$7D(*&&svvdE2'>?)..$A -B #$4ieATU&&}5xx,d.D.DT.JJJ5s   &Pc                   t         |j                  j                     }| j                  |j                        \  }}| j                  t        j                  t        j                  |j                  |      |            }|||fz  fS rS   )		UNARY_MAPr  r`  r~  operandr  r   r  r  )r5   unarypatternoperand_resoperand_explr  s         r   visit_UnaryOpzAssertionRewriter.visit_UnaryOp  sj    EHH../$(JJu}}$=!\kk#++CKK+,NPUVWG|o---r   c           	     \   t         |j                  j                     }| j                  |j                        \  }}| j                  |j
                        \  }}d| d| d| d}| j                  t        j                  t        j                  ||j                  |      |            }||fS )Nr   r~   )
	BINOP_MAPr  r`  r~  r  rightr  r   r  r  )	r5   binopsymbol	left_expr	left_expl
right_expr
right_explr  r  s	            r   visit_BinOpzAssertionRewriter.visit_BinOp%  s    588--.#zz%**5	9!%EKK!8
J)AfXQzl!<kkcii	588ZH%P
 Kr   c                D   | j                  |j                        \  }}g }g }g }|j                  D ]  }t        |t        j
                        rX|j                  | j                  j                  | j                  i       v r&| j                  | j                     |j                     }| j                  |      \  }}	|j                  |	       |j                  |        |j                  D ](  }
t        |
j                  t        j
                        rq|
j                  j                  | j                  j                  | j                  i       v r5| j                  | j                     |
j                  j                     |
_        | j                  |
j                        \  }}	|j                  t	        j                  |
j                  |             |
j                  r#|j                  |
j                  dz   |	z          |j                  d|	z          + dj                  |dj!                  |            }	t	        j"                  t	        j$                  |||      |      }| j'                  |      }| j)                  | j+                  |            }| d| d|	 d}||fS )N=r3  z{}({})z, z
{z = z
})r~  funcr  rD   r   r  r  r_  getr]  r  keywordsrk  keywordargformatr  r  r  r  r  r  )r5   r  new_func	func_expl	arg_explsnew_args
new_kwargsr"  r  r  r!  new_callres_expl
outer_expls                 r   
visit_CallzAssertionRewriter.visit_Call/  s   "jj3)	
99C#sxx(SVVt7O7O7S7S

B8 . ..tzz:366B

3ICT"OOC   }}Gsxx--""d&>&>&B&B4::r&RR $ 8 8 DW]]EUEU V

7==1ICckk'++s;<{{  s!2T!9:  - % y$))I*>?$$SXXh*%MtTkk(#))$,,s*;< zhZs4&=
Jr   c                    | j                  |j                        \  }}t        j                  ||j                        }|d|z   fS )Nr/  )r~  rk  r   Starredctx)r5   starredr  r  new_starreds        r   visit_StarredzAssertionRewriter.visit_StarredO  s;    JJw}}-	Tkk#w{{3C$J&&r   c           
        t        |j                  t        j                        s| j	                  |      S | j                  |j                        \  }}| j                  t        j                  t        j                  ||j                  t        j                               |            }| j                  | j                  |            }d}|||||j                  fz  }||fS )Nz%s
{%s = %s.%s
})rD   r/  r   r  r  r~  rk  r  r  r  r  r  r  )r5   r  rk  
value_explr  r*  rx   r  s           r   visit_Attributez!AssertionRewriter.visit_AttributeU  s    $((CHH-%%d++ JJtzz2zkkcmmE499chhjI4P
 ))$,,s*;<"h*dii@@Dyr   c                p
   | j                          t        |j                  t        j                        rq|j                  j
                  | j                  j                  | j                  i       v r5| j                  | j                     |j                  j
                     |_        t        |j                  t        j                        rD|j                  | j                  | j                     |j                  j                  j
                  <   | j                  |j                        \  }}t        |j                  t        j                  t        j                  f      rd| d}t        t        |j                               D cg c]  }| j#                          }}|D cg c]*  }t        j                  |t        j$                               , }}|D cg c]*  }t        j                  |t        j&                               , }}t)        t        t        |j                               |j                   |j*                        }	g }
g }|g}|	D ]  \  }}}t        |t        j                        rt        |t        j                        rh|j                  j
                  |j
                  k(  rE| j#                         |j                  _        || j                  | j                     |j
                  <   | j                  |      \  }}t        |t        j                  t        j                  f      rd| d}|j-                  |       t.        |j0                     }|j-                  t        j2                  |             | d| d| }|
j-                  t        j2                  |             t        j4                  t        j                  ||g|g      |      }| j6                  j-                  t        j8                  ||   g|             ||}} | j;                  dt        j<                  |t        j$                               t        j<                  |t        j$                               t        j<                  |
t        j$                               t        j<                  |t        j$                                     }t        |j                         dkD  r)t        j                  t        j>                         |      }n|d   }|| jA                  | jC                  |            fS c c}w c c}w c c}w )Nr  r~   r  r!  r   r   )"r  rD   r  r   r  r  r_  r  r]  r  rL   r~  r  r  r  r   r  r  r  r  r  comparatorsr  r  r`  rl  r  r  r  r  r  Andr  r  )r5   compleft_resr  r  res_variablesr  
load_namesstore_namesitr  symsr  r  next_operandnext_res	next_explsymr  res_expr	expl_callr  s                         r   visit_ComparezAssertionRewriter.visit_Comparea  s     "IIsxx
iilld66::4::rJJ00<TYY\\JDIdii/HL		D$$TZZ01A1A1D1DE"jj3)dii#++szz!:;I;a(I27DHH2FG2FQ2FGGT%U}!chhq#((*&=}
%U9FGAsxx399;/Gs488}%txx1A1AB "!*#%Ar<<7x2 ''**hkk9)-##&DP((4X[[A"&**\":Hi,cjj(AB	{!,	NN8$BLL)CKKS)*[#a	{3DLLd+,((XthZ)PRVWHOO""3::{1~.>#IJ"*IiH% $&( KKIIdCHHJ'IIj#((*-IIeSXXZ(IIgsxxz*
	 txx=1JJswwy*=CQ-CD**4+B+B9+MNNNO H%UGs   T)#/T./T3)r   
str | Noner)   Config | Noner   r   r   r   )r   
ast.Moduler   r   )rb  rn   r   r   )r   rn   )r  ast.exprr   ast.Name)r  rJ  r   rJ  )rK   rn   r  rJ  r   rJ  )rK   rn   r   ast.Attribute)r  rJ  r   rn   rH  )r  rJ  r   rK  )r:  ast.ASTr   tuple[ast.Name, str])r  z
ast.Assertr   zlist[ast.stmt])rK   zast.NamedExprr   ztuple[ast.NamedExpr, str])rK   rK  r   rN  )r  z
ast.BoolOpr   rN  )r  zast.UnaryOpr   rN  )r  z	ast.BinOpr   rN  )r  zast.Callr   rN  )r0  zast.Starredr   ztuple[ast.Starred, str])r  rL  r   rN  )r9  zast.Comparer   ztuple[ast.expr, str])r   r   r   r   r6   r   staticmethodr   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r,  r2  r5  rF  __classcell__)r`  s   @r   r   r   b  s    8t
%
/<
FK
	
$[(z 2 2C..=

'7*(>wr
22/Kb. @'
3Or   r   c                    	 t        j                  | d       y# t        t        t        f$ r Y yt
        $ r Y yt        $ r7}|j                  t        j                  t        j                  hv rY d}~y d}~ww xY w)z~Attempt to create the given directory and sub-directories exist.

    Returns True if successful or if it already exists.
    T)exist_okFN)
rG   makedirsFileNotFoundErrorNotADirectoryErrorFileExistsErrorPermissionErrorr   r   EROFSENOSYS)rd   r   s     r   r[   r[     sm    

I-   1?C    
 77u{{ELL11s%    A9A9A9,A43A44A9c                    t         j                  r/t        t         j                        t        | j                  dd  z  S | j                  dz  S )zKReturn the cache directory to write .pyc files for the given .py file path.r   rl   __pycache__)rX   pycache_prefixr   rw   parent)	file_paths    r   rZ   rZ     sF    
 C&&'$	"0E*FFF -//r   )r   z	IO[bytes]ri   os.stat_resultrh   types.CodeTyper   r   )
rM   r   rh   r`  ri   r_  rg   r   r   r   )rP   r   r)   r   r   z%tuple[os.stat_result, types.CodeType])r   r   rg   r   r?   zCallable[[str], None]r   ztypes.CodeType | Noner   )
r   rI  r   r   r   rG  r)   rH  r   r   )r   objectr   rn   )r)   rH  r   z
int | None)r   ra  r   r   )r  zIterable[str]r  r   r   rn   )
r  Sequence[str]r  zSequence[bool]r  rb  r  zSequence[object]r   rn   )r%  r   r&  rn   r  rn   r   r   )r   r   )r:  rM  r   zIterator[ast.AST])rQ  r   r   zdict[int, str])rd   r   r   r   )r^  r   r   r   )or   rc  r   r   collectionsr   r   	functoolsr   rA   importlib.machineryimportlib.utilrJ  r  r   rG   pathlibr   r   r   rX   rI  r   typingr   r   r	   r
   r   r   _pytest._io.safereprr   r   _pytest._versionr   _pytest.assertionr   _pytest.configr   _pytest.mainr   _pytest.pathlibr   r   _pytest.stashr   _pytest.assertion.utilr   r  r   r   r=   implementation	cache_tag
PYTEST_TAGPYC_EXTr\   rw  abcMetaPathFinderLoaderr%   r   r_   r^   r]   r   r   r   r  r  r  r!  r'  r)  r  InvertUSubUAddr
  BitOrBitXorBitAndLShiftRShiftr  SubMultDivFloorDivr  EqNotEqLtLtEGtGtEPowIsIsNotr  NotInMatMultr  r9  	lru_cacherX  NodeVisitorr   r[   rZ   r   r   r   <module>r     s   ; " 
 #      	   	    
          6 ) $ " !   ( & " M 0	 	 -(+,. "",,-XgY?

+
,g% J qWY]]999==;O;O qWh   . 4B 	 $   
	
 
2 =K...$9..h # 	<	<< < 	<
 
<?$
!,*
	  	
 	(1
3 WWh

E388UCHHeT	IIsJJ JJ JJ	
 JJ GGS GGS HHc GGS LL$ GGT FFD IIt FFC GGT  FFC!" GGT#$ GGTFFDIIxFFDIIxKK/	6( Q3  3lrO rOj20r   