
    ?g                       S r SSKJr  SSKrSSKJr  SSKrSSKrSSKr	SSK
r	SSKr	SSKrSSKrSSKrSSKrSSKJr  SSKJr  SSKrSSKrSSKrSSKrSSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJ r   SSKJ!r!  SSK"J#r#  SSK$J%r%  SSK&J'r'  SSK(J)r)  SSK*J+r+  SSK*J,r,  SSK-J.r.  SSK/J0r1  \(       a  SSK$J2r2   " S S5      r3\.S   " 5       r4\Rj                  Rl                   S\# 3r7SS=(       d    S-   r8S \7-   \8-   r9\3" 5       r: " S! S"\	Rv                  Rx                  \	Rv                  Rz                  5      r>        STS# jr?          SUS$ jr@SVS% jrAS& 4       SWS' jjrB  SX         SYS( jjrCSZS) jrDS[S* jrESZS+ jrFS\S, jrGS]S- jrH          S^S. jrIS_S/ jrJS`S0 jrK\R                  S1\R                  S2\R                  S3\R                  S40rP0 \R                  S5_\R                  S6_\R                  S7_\R                  S8_\R                  S9_\R                  S:_\R                  S;_\R                  S<_\R                  S=_\R                  S>_\R                  S?_\R                  S@_\R                  SA_\R                  SB_\R                  SC_\R                  SD_\R                  SE_\R                  SF\R                  SG\R                  SH\R                  SI\R                  SJ\R                  SK0ErhSaSL jri\R                  " SMSN9SbSO j5       rk " SP SQ\R                  5      rmScSR jrnSdSS jrog)ez5Rewrite assertion AST to produce nice error messages.    )annotationsN)defaultdict)Path)PurePath)Callable)IO)Iterable)Iterator)Sequence)TYPE_CHECKING)DEFAULT_REPR_MAX_SIZE)saferepr)saferepr_unlimited)version)util)Config)Session)absolutepath)
fnmatch_ex)StashKey)format_explanation)AssertionStatec                      \ rS rSrSrg)Sentinel1    N)__name__
__module____qualname____firstlineno____static_attributes__r       ;/usr/lib/python3/dist-packages/_pytest/assertion/rewrite.pyr   r   1   s    r"   r   r   z-pytest-.pyco.c                  N   \ rS rSrSrSS jrSS jr\R                  R                  R                  r  S       SS jjr
    SS jrSS jrSS	 jrSS
 jrSS jrSS jrSS jrSS jr\R*                  S:  a%  \R*                  S:  a  SSKJr  OSSKJr  S S jrSrgSrg)!AssertionRewritingHook@   z1PEP302/PEP451 import hook which rewrites asserts.c                    Xl          UR                  S5      U l        S U l        0 U l        [        5       U l        SU l        S1U l	        0 U l
        SU l        g ! [         a    SS/U l         NPf = f)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__AssertionRewritingHook.__init__C   sx    	5 --7DK (,13'*u ",6<(:<&&+#  	5&4DK	5s   A A/.A/c                    Xl         SU l        g )NF)r2   r9   )r:   r2   s     r#   set_session"AssertionRewritingHook.set_sessionS   s    &+#r"   Nc                   U R                   (       a  g U R                  R                  [           nU R	                  X5      (       a  g UR                  SU 35        U R                  X5      nUc0  Ub-  U H'  n[        R                  R                  X5      nUc  M'    O   Ubn  UR                  ba  [        UR                  [        R                  R                  5      (       a.  [        R                   R#                  UR                  5      (       d  g UR                  nU R%                  XU5      (       d  g [        R                  R                  UUU UR&                  S9$ )Nzfind_module called for: )loadersubmodule_search_locations)r6   r.   stashassertstate_key_early_rewrite_bailouttrace
_find_spec	importlibr   spec_from_file_locationorigin
isinstancerA   	machinerySourceFileLoaderospathexists_should_rewriterB   )r:   namerO   targetstatespec	_path_strfns           r#   	find_spec AssertionRewritingHook.find_specZ   s&    !!/2&&t33.tf56 t*<D, "	 ~~==dN# " L {{"dkk9+>+>+O+OPP77>>$++..B##De44~~55'+'F'F	 6 
 	
r"   c                    g Nr   )r:   rU   s     r#   create_module$AssertionRewritingHook.create_module   s     r"   c                
   UR                   c   eUR                   R                  c   e[        UR                   R                  5      nU R                  R                  [
           nX R                  UR                  '   [        R                  (       + n[        U5      nU(       a(  [        U5      nU(       d  SnUR                  SU 35        UR                  S S [        -   nXW-  n[        X(UR                  5      n	U	cP  UR                  SU< 35        [!        X R                  5      u  pU(       a  SU l         [%        X9X5        SU l        OOUR                  SU 35        ['        XR(                  5        g ! SU l        f = f)NFzread only directory: z
rewriting Tzfound cached rewritten pyc for )__spec__rJ   r   r.   rC   rD   r3   r   sysdont_write_bytecodeget_cache_dirtry_makedirsrF   rR   PYC_TAIL	_read_pyc_rewrite_testr6   
_write_pycexec__dict__)r:   modulerW   rT   write	cache_dirok
cache_namepyccosource_stats              r#   exec_module"AssertionRewritingHook.exec_module   sP   ***%%111&//(()!!/213foo. +++!"%	i(B3I;?@WWSb\H,
$ r,:KK*RF+,+B<OK$(!.u+;(-D%  KK9">?R! ).D%s   :E9 9	Fc                   U R                   b  U R                  (       d  SU l        U R                   R                   Hi  n[        U5      R	                  [
        R                  5      nU R                  R                  [
        R                  R                  US   5      S   5        Mk     UR	                  S5      nUS   U R                  ;   a  g[        U6 R                  S5      nU R                   H;  n[
        R                  R                  U5      (       a    g[        Xe5      (       d  M;    g   U R!                  X5      (       a  gUR#                  SU 35        g)zA 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: )r2   r9   _initialpathsstrsplitrN   sepr7   addrO   splitextr   with_suffixr0   dirnamer   _is_marked_for_rewriterF   )r:   rR   rT   initial_pathpartsrO   pats          r#   rE   -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336tf=>r"   c                   [         R                  R                  U5      S:X  a  UR                  SU< 35        gU R                  b?  U R                  R                  [        U5      5      (       a  UR                  SU< 35        g[        U5      nU R                   H*  n[        XT5      (       d  M  UR                  SU< 35          g   U R                  X5      $ )Nzconftest.pyzrewriting conftest file: Tz.matched test file (was specified on cmdline): zmatched test file )rN   rO   basenamerF   r2   
isinitpathr   r   r0   r   r   )r:   rR   rW   rT   fn_pathr   s         r#   rQ   &AssertionRewritingHook._should_rewrite   s    77B=0KK3B6:;<<#||&&|B'788LRFST 2,;;C#''078 
 **477r"   c           	         U R                   U   $ ! [         ao    U R                   HM  nX:X  d  UR                  US-   5      (       d  M#  UR	                  SU< SU< S35        SU R                   U'      g   SU R                   U'    gf = f)Nr'   zmatched marked file z (from )TF)r8   KeyErrorr5   
startswithrF   )r:   rR   rT   markeds       r#   r   -AssertionRewritingHook._is_marked_for_rewrite   s    
	11$77 	,,>T__Vc\%B%BKK"6thgfZq QR;?D2248	 - 49D**40	s    5B

*B
6B
	B
c                   [        U5      R                  [        R                  5      R	                  U R
                  5      nU H|  n[        R                  U   n[        R                  UR                  =(       d    S5      (       a  ME  [        UR                  [        U 5      5      (       a  Mk  U R                  U5        M~     U R                  R                  U5        U R                  R!                  5         g)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)r4   intersectionra   modules
differencer3   AssertionRewriteris_rewrite_disabled__doc__rK   
__loader__type_warn_already_importedr5   updater8   clear)r:   namesalready_importedrR   mods        r#   mark_rewrite#AssertionRewritingHook.mark_rewrite  s     J##CKK0;;D<Q<QR 	 %D++d#C$88!r  d<<++D1 % 	!!%(&&,,.r"   c                V    SSK Jn  U R                  R                  U" SU 35      SS9  g )Nr   PytestAssertRewriteWarningz0Module already imported so cannot be rewritten:    )
stacklevel)_pytest.warning_typesr   r.   issue_config_time_warning)r:   rR   r   s      r#   r   -AssertionRewritingHook._warn_already_imported  s3    D--&B4&I 	 	. 	
r"   c                p    [        US5       nUR                  5       sSSS5        $ ! , (       d  f       g= f)zOptional PEP302 get_data API.rbN)openread)r:   pathnamefs      r#   get_dataAssertionRewritingHook.get_data   s#    (D!Q668 "!!s   '
5   
   )r      r   )TraversableResourcesc                    [         R                  S:  a  SSKJn  OSSKJn  U" [
        R                  " U R                  U   S95      $ )N)r      r   )
FileReader)rO   )ra   version_infoimportlib.readersr   importlib.resources.readerstypesSimpleNamespacer3   )r:   rR   r   s      r#   get_resource_reader*AssertionRewritingHook.get_resource_reader+  s7    ')8Be339N9Nt9TUVVr"   )	r7   r8   r5   r3   r9   r6   r.   r0   r2   )r.   r   returnNone)r2   zSession | Noner   r   NN)rR   rx   rO   zSequence[str | bytes] | NonerS   types.ModuleType | Noner   z%importlib.machinery.ModuleSpec | None)rU   zimportlib.machinery.ModuleSpecr   r   )rk   ztypes.ModuleTyper   r   )rR   rx   rT   r   r   bool)rR   rx   rW   rx   rT   r   r   r   )r   rx   r   r   )rR   rx   r   r   )r   zstr | bytesr   bytes)rR   rx   r   r   )r   r   r   r    r   r;   r>   rH   rL   
PathFinderrX   rG   r\   rs   rE   rQ   r   r   r   r   ra   r   importlib.resources.abcr   importlib.abcr   r!   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                `   U R                  [        R                  R                  5        SnU R                  U5        [	        UR
                  5      S-  nUR                  S-  nU R                  [        R                  " SXE5      5        U R                  [        R                  " U5      5        g )N           z<LL)rl   rH   r   MAGIC_NUMBERintst_mtimest_sizestructpackmarshaldumps)fprr   rq   flagsmtimesizes         r#   _write_pyc_fpr   4  s{     HHY^^(()EHHUO$$%
2E+DHHV[[,-HHW]]2r"   c                   U S[         R                  " 5        3n [        US5       n[        XRU5        S S S 5         [         R                  " XC5        g! , (       d  f       N&= f! [         a+  nU R                  SU SUR                   35         S nAgS nAff = f! [         a!  nU R                  SU SU 35         S nAgS nAff = f)Nr'   wbzerror writing pyc file at z: errno=Fz: T)rN   getpidr   r   OSErrorrF   errnoreplace)rT   rq   rr   rp   proc_pycr   es          r#   rh   rh   F  s     a		}%H(D!R"2. "


8!  "! 0
(177)LM  0Rs;< sJ   A& AA& B 
A#A& #A& &
B0!BB
C	(CC	c                    [         R                  " U 5      nU R                  5       n[        U 5      n[        R
                  " X4S9n[        XSXA5        [        XTSSS9nX&4$ )z1Read and rewrite *fn* and return the code object.)filenameri   T)dont_inherit)rN   stat
read_bytesrx   astparserewrite_assertscompile)rW   r.   r   sourcestrfntreerq   s          r#   rg   rg   _  sP    772;D]]_FGE99V,DD%0	f4	8B8Or"   c                    g r[   r   )xs    r#   <lambda>r   k  s    dr"   c                .    [        US5      nU    [        R                  " U 5      n[	        UR
                  5      nUR                  nUR                  S5      n[        U5      S:w  a  U" SU  S35         SSS5        gUSS [        R                  R                  :w  a  U" SU  S35         SSS5        gUSS	 S
:w  a  U" SU  S35         SSS5        gUS	S n	[        R                  U	S5      US-  :w  a  U" SU  S35         SSS5        gUSS n
[        R                  U
S5      US-  :w  a  U" SU  S35         SSS5        g [        R                  " U5      n[!        U["        R$                  5      (       d  U" SU  S35         SSS5        gUsSSS5        $ ! [         a     gf = f! [         a   nU" SU  SU 35         SnASSS5        gSnAff = f! [         a   nU" SU  SU 35         SnASSS5        gSnAff = f! , (       d  f       g= f)zkPossibly 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   rN   r   r   r   r   r   lenrH   r   r   
from_bytesr   load	ExceptionrK   r   CodeType)r   rp   rF   r   stat_resultr   r   datar   
mtime_data	size_datarq   s               r#   rf   rf   j  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 HAF,H?.H6H/H/HG,HH
F)(F),
G6GHGH
H#G>1H>HH
Hc                :    [        X#U5      R                  U 5        g)z%Rewrite the assert statements in mod.N)r   run)r   r   module_pathr.   s       r#   r   r     s     k6266s;r"   c                    [        U [        R                  5      (       a  U R                  $ [	        [
        R                  5      nU(       d  [        U 5      R                  SS5      $ [        XS9R                  SS5      $ )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.

\nmaxsize)
rK   r   
MethodTyper   _get_maxsize_for_safereprr   _configr   r   r   )objr  s     r#   	_safereprr
    sb     #u''((||'5G!#&..tU;;C)11$>>r"   c                    U c  SnOU R                  [        R                  5      nUS:  a  gUS:  a	  [        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                    SS/n[        U [        5      (       d4  [        U [        [        R
                  5      5      n UR                  S5        U H  u  p#U R                  X#5      n M     U $ )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  )rK   rx   r   r  r   r  appendr   )r	  replacesr1r2s       r#   _format_assertmsgr    s^     {+Hc3s5dllCD'kk"!  Jr"   c                h    [        U 5      (       a  g [        U S5      (       + $ ! [         a     gf = f)NFr   T)callablehasattrr   )r	  s    r#   _should_repr_global_namer    s5    }}3
+++ s   $ 
11c                x    SU=(       a    S=(       d    SR                  U 5      -   S-   nUR                  SS5      $ )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                   [        [        [        U 5      5      X5       H  u  pEn U(       + nU(       d  M    O   [        R
                  b'  [        R
                  " U W   X4   X4S-      5      nUb  U$ W$ ! [         a    Sn NUf = f)NTr  )zipranger   r   r   _reprcompare)	opsresultsexplseach_objiresexpldonecustoms	            r#   _call_reprcomparer2    s     E#c(OW<	7D 4 = $""3q68;QHMK  	D	s   A44BBc                V    [         R                  b  [         R                  " XU5        g g r[   r   _assertion_pass)linenoorigr/  s      r#   _call_assertion_passr8    s#    'V40 (r"   c                 4    [         R                  (       a  S$ S$ )zCheck if any plugins implement the pytest_assertion_pass hook
in order not to generate explanation unnecessarily (might be expensive).TFr4  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              #  x   #    U v   [         R                  " U 5       H  n[        U5       Sh  vN   M     g N	7f)zARecursively yield node and all its children in depth-first order.N)r   iter_child_nodestraverse_node)nodechilds     r#   rJ  rJ  '  s0     
J%%d+ ''' ,'s   ,:8
:r  r  c                  ^
^^^^ 0 mSm/ mSm
[        5       mS
U
UUUU4S jjn[        R                  " [        R                  " U 5      R                  5      nU GH  u  p4u  pVpxU[        R
                  :X  a
  US:X  a  Um
M(  T
c  M-  U[        R                  :X  a  US;   a  TS-  mOU[        R                  :X  a  US;   a  TS-  mT(       d&  TR                  XS 5        TR                  U5        M  TS:X  a  U[        R                  :X  at  US:X  an  UT;   a8  [        T5      S:X  a)  U[        TS	   5      -   [        U5      -
  n	TS	   SU	 TS	'   O&UT;   a  TS	   SU TS	'   OTR                  USU 5        U" 5         GM'  U[        R                  [        R                  1;   a
  U" 5         GMU  T(       d  GM_  UT;  d  GMh  TR                  U5        TR                  U5        GM     T$ )z<Return a mapping from {lineno: "assertion test expression"}.r   Nc                    > T c   eSR                  T5      R                  5       R                  S5      TT '   Sm/ mS m [        5       mg )Nr   \r   )r   rstripr4   )assert_linenodepthlinesret
seen_liness   r#   _write_and_reset._get_assertion_exprs.<locals>._write_and_reset8  sK    (((WWU^224;;DAMU
r"   assertz([{r  z)]},rv   r   r   )r4   tokenizeioBytesIOreadlineNAMEOPr  r{   r   NEWLINE	ENDMARKER)srcrV  tokenstpr   r6  offset_lineoffset_in_trimmedrQ  rR  rS  rT  rU  s             @@@@@r#   _get_assertion_exprsrj  .  s    CEE $M5J  rzz#778F17-$Va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                    ^  \ rS rSrSr        SU 4S jjrSS jr\SS j5       rSS jr	SS jr
SS jrS S	 jrS!S
 jrS"S jrS#S jrS$S jrS%S jrS&S jrS'S jrS(S jrS)S jrS*S jrS+S jrS,S jrS-S jrS.S jrS/S jrSrU =r$ )0r   ie  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U ]  5         Xl        X l        Ub  UR	                  S5      U l        OSU l        X0l        SU l        [        [        5      U l
        g )Nenable_assertion_pass_hookFr   )superr;   r   r.   r/   rm  r   scoper   dictvariables_overwrite)r:   r   r.   r   	__class__s       r#   r;   AssertionRewriter.__init__  s[     	&.4mm,/D+ /4D+*,
 	 r"   c           	        UR                   (       d  g[        USS5      nUSL nUb  U R                  U5      (       a  gSnSnUR                    H  nU(       a  [        U[        R
                  5      (       a  [        UR                  [        R                  5      (       aZ  [        UR                  R                  [        5      (       a1  UR                  R                  nU R                  U5      (       a    gSnOB[        U[        R                  5      (       a!  UR                  S:X  a  UR                  S:X  a  O  O	US-  nM     [        U[        R                  5      (       a+  UR                  (       a  UR                  S   R                  nOUR                  n[        R                   S:  a/  [        R"                  " SS	USS
9[        R"                  " SSUSS
9/nO.[        R"                  " SS	5      [        R"                  " SS5      /nU Vs/ s H  n[        R$                  " U/USS
9PM     n	nXR                   XD& U4U l        U/n
U
(       Ga  U
R)                  5       n[        U[        R                  [        R*                  [        R,                  45      (       a4  [/        / U R&                  QUP75      U l        U
R1                  [2        5        U[2        :X  a  U R&                  SS U l        M  [        U[        R4                  5      (       d   e[        R6                  " U5       GH  u  p[        U[8        5      (       a  / n[;        U5       H  u  nn[        U[        R<                  5      (       a"  UR?                  U RA                  U5      5        MG  UR1                  U5        [        U[        R4                  5      (       d  My  U
R1                  U5        M     [C        XU5        M  [        U[        R4                  5      (       d  M  [        U[        RD                  5      (       a  GM  U
R1                  U5        GM     U
(       a  GM   ggs  snf )z5Find all assert statements in *mod* and rewrite them.N	docstringr   F
__future__r  r   builtins@py_builtins)r6  
col_offsetz_pytest.assertion.rewrite
@pytest_arrv   )#bodygetattrr   rK   r   ExprvalueConstantrx   
ImportFromlevelrk   FunctionDefdecorator_listr6  ra   r   aliasImportro  popAsyncFunctionDefClassDeftupler  _SCOPE_END_MARKERASTiter_fieldslist	enumerateAssertextendvisitsetattrexpr)r:   r   docexpect_docstringpositemr6  aliasesr  importsnodesrK  rR   fieldnewr-  rL  s                    r#   r   AssertionRewriter.run  sB   xx c;-$;?t77<<HHD tSXX..tzz3<<88tzz//55jj&&++C00#( 400JJ!OKK</1HC' , dCOO,,1D1D((+22F[[Fw&		*nVPQR		/ ! 	G 		*n5		5|DG
 KR
JQCJJwv!<' 	 
 $ V
+.%99;D$#2F2F UVV"#6TZZ#6#67
./((!ZZ_
dCGG,,,,"t4eT**)+C$-e$45%eSZZ88JJtzz%'89JJu-)%99 %U 3 %5 D,ucgg.. 'uchh77LL'%  5 e
s   !Qc                    SU ;   $ )NPYTEST_DONT_REWRITEr   )ru  s    r#   r   %AssertionRewriter.is_rewrite_disabled  s    $	11r"   c                ~    S[        [        U R                  5      5      -   nU R                  R	                  U5        U$ )zGet a new variable.z
@py_assert)rx   nextvariable_counter	variablesr  )r:   rR   s     r#   variableAssertionRewriter.variable  s5     c$t'<'<"=>>d#r"   c           	     R   U R                  5       nU R                  R                  [        R                  " [        R
                  " U[        R                  " 5       5      /U5      5        [        R                  " [        R
                  " U[        R                  " 5       5      U5      $ )zGive *expr* a name.)	r  
statementsr  r   AssignNameStorecopy_locationLoad)r:   r  rR   s      r#   assignAssertionRewriter.assign  sb    }}szz388D#))++F*GNO  $
!;TBBr"   c                &    U R                  SU5      $ )z Call saferepr on the expression.r
  )helper)r:   r  s     r#   displayAssertionRewriter.display   s    {{;--r"   c                    [         R                  " S[         R                  " 5       5      n[         R                  " X1[         R                  " 5       5      n[         R                  " U[        U5      / 5      $ )zCall a helper in this module.rz  )r   r  r  	AttributeCallr  )r:   rR   argspy_nameattrs        r#   r  AssertionRewriter.helper$  sE    ((<4}}WCHHJ7xxd4j"--r"   c                    [         R                  " S[         R                  " 5       5      n[         R                  " X![         R                  " 5       5      $ )z!Return the builtin called *name*.rx  )r   r  r  r  )r:   rR   builtin_names      r#   builtinAssertionRewriter.builtin*  s/    xx
;}}\<<r"   c                p    S[        [        U R                  5      5      -   nXR                  U'   SU-   S-   $ )a"  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)rx   r  r  explanation_specifiers)r:   r  	specifiers      r#   explanation_param#AssertionRewriter.explanation_param/  s<     3tD$9$9:;;	15##I.i$&&r"   c                \    0 U l         U R                  R                  U R                   5        g)aT  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  )r:   s    r#   push_format_context%AssertionRewriter.push_format_context;  s$     <>#

$556r"   c           	     D   U R                   R                  5       nU R                   (       a  U R                   S   U l        UR                  5        Vs/ s H  n[        R
                  " U5      PM     nn[        R                  " U[        UR                  5       5      5      n[        R                  " U[        R                  " 5       U5      nS[        [        U R                  5      5      -   nU R                  (       a  U R                  R!                  U5        U R"                  R!                  [        R$                  " [        R&                  " U[        R(                  " 5       5      /U5      5        [        R&                  " U[        R*                  " 5       5      $ s  snf )a2  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.
rv   z
@py_format)r  r  r  keysr   r  Dictr  valuesBinOpModrx   r  r  rm  format_variablesr  
expl_stmtsr  r  r  r  )r:   	expl_exprcurrentkeyr  format_dictformrR   s           r#   pop_format_context$AssertionRewriter.pop_format_contextH  s    **.."::*.**R.D'DKLLN&SNSs||C'8N&ShhtT'..*:%;<yyCGGI{;c$t'<'<"=>>**!!((.szz388D#))++F*GNOxxchhj)) 'Ts    Fc                    [        U[        R                  5      (       d   eU R                  U5      nX R	                  U R                  U5      5      4$ )z1Handle expressions we don't have custom code for.)rK   r   r  r  r  r  )r:   rK  r.  s      r#   generic_visitAssertionRewriter.generic_visit\  sD    $))))kk$**4<<+<===r"   c           	     F   [        UR                  [        R                  5      (       ah  [	        UR                  R
                  5      S:  aE  SSKnSSKJn  U R                  c   eUR                  U" S5      SU R                  UR                  S9  / U l        / U l        [        R                  " 5       U l        U R"                  (       a  / U l        / U l        / U l        U R+                  5         U R-                  UR                  5      u  pE[        R.                  " [        R0                  " 5       U5      nU R"                  (       Ga  U R3                  [        R4                  " U5      5      nUR6                  (       a  U R9                  SUR6                  5      nSn	O[        R4                  " S	5      nS
n	[        R:                  " [        R4                  " U	5      [        R<                  " 5       U5      n
[        R:                  " U[        R<                  " 5       U
5      n[        R>                  " S[        R@                  " 5       5      nU R9                  SU5      n[        RB                  " X// 5      n[        RD                  " US5      n/ nURG                  U R(                  5        URI                  U5        U R9                  SU5      n[K        U RL                  5      UR                     n[        RN                  " U R9                  S[        R4                  " UR                  5      [        R4                  " U5      U5      5      n[        RP                  " U R9                  S5      / U R(                  QUP/ 5      nU/n[        RP                  " UUU5      nU R                  RI                  U5        U R$                  (       a  U R$                   Vs/ s H-  n[        R>                  " U[        RR                  " 5       5      PM/     nn[        RT                  " U[        R4                  " S5      5      nU R                  RI                  U5        GOWU R(                  nU R                  RI                  [        RP                  " UU/ 5      5        UR6                  (       a"  U R9                  SUR6                  5      nSU-   nO[        R4                  " S	5      nS
U-   n[        R:                  " U[        R<                  " 5       [        R4                  " U5      5      nU R3                  U5      nU R9                  SU5      n[        R>                  " S[        R@                  " 5       5      n[        RB                  " X// 5      n[        RD                  " US5      nURI                  U5        U R                  (       a  U R                   Vs/ s H-  n[        R>                  " U[        RR                  " 5       5      PM/     nn[        RT                  " U[        R4                  " S5      5      nU R                  RI                  U5        U R                   H<  n[W        U5       H*  n[Y        USS5      b  M  [        RZ                  " UU5        M,     M>     U R                  $ s  snf s  snf )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   r6  r  z	
>assert r   zassert AssertionError_format_explanationr8  r:  r6  ).rK   testr   Tupler   eltswarningsr   r   r   warn_explicitr6  r  r  	itertoolscountr  rm  r  r  r  r  r  UnaryOpNotr  r  msgr  r  Addr  r  r  Raiser  r  rj  r   r}  Ifr  r  rJ  r|  r  )r:   assert_r  r   top_conditionr#  negationr  	assertmsggluestrerr_explanationerr_msgerr_namefmtexcraise_statements_failfmt_passr7  hook_call_passhook_impl_teststatements_pass	main_testrR   r  clear_formatr{  templater   stmtrK  s                                  r#   visit_AssertAssertionRewriter.visit_Assertb  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((8UB/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((8UB/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   4Z4Zc                   [         R                  " U R                  S5      / / 5      nUR                  R                  n[         R
                  " [         R                  " U5      [         R                  " 5       /U/5      nU R                  SU5      n[         R                  " [         R                  " 5       XE/5      n[         R                  " X`R                  U5      [         R                  " U5      5      nXR                  U5      4$ Nlocalsr  )r   r  r  rS   idComparer  Inr  BoolOpOrIfExpr  r  )r:   rR   locs	target_idinlocsdoreprr  r  s           r#   visit_NamedExpr!AssertionRewriter.visit_NamedExpr  s     xxX.B7KKNN	S\\)4svvxj4&I7>zz#&&(V$45yy||D13<<	3JK++D111r"   c                   [         R                  " U R                  S5      / / 5      n[         R                  " [         R                  " UR
                  5      [         R                  " 5       /U/5      nU R                  SU5      n[         R                  " [         R                  " 5       X4/5      n[         R                  " XPR                  U5      [         R                  " UR
                  5      5      nXR                  U5      4$ r  )r   r  r  r  r  r  r  r  r  r	  r
  r  r  )r:   rR   r  r  r  r  r  s          r#   
visit_NameAssertionRewriter.visit_Name  s     xxX.B7S\\$''2SVVXJG7>zz#&&(V$45yy||D13<<3HI++D111r"   c           
     h   U R                  5       nU R                  [        R                  " / [        R                  " 5       5      5      n[        R
                  " US[        R                  " 5       5      n[        [        UR                  [        R                  5      5      nU R                  =pgU R                  n[        UR                  5      S-
  n	U R                  5         [        UR                  5       GH  u  pU
(       GaN  / nU R                  R!                  [        R"                  " WU/ 5      5        Xl        [        U[        R$                  5      (       a  [        UR&                  [        R(                  5      (       a  UR&                  R*                  R,                  UR                  S U
  Vs/ s H"  n[/        US5      (       d  M  UR,                  PM$     sn;   an  U R                  5       nUR&                  U R0                  U R2                     UR&                  R*                  R,                  '   XR&                  R*                  l        U R                  5         U R5                  U5      u  nnUR!                  [        R6                  " [        R8                  " U[        R:                  " 5       5      /U5      5        U R=                  [        R>                  " U5      5      n[        R@                  " UU// 5      nU R                  R!                  [        RB                  " U5      5        X:  d  GMD  UnU(       a*  [        RD                  " [        RF                  " 5       U5      n/ nU R                  R!                  [        R"                  " UU/ 5      5        U=U l
        nGM     Xpl
        Xl        U RI                  SU[        R>                  " U5      5      nU R=                  U5      n[        R8                  " U[        R                  " 5       5      U RK                  U5      4$ s  snf )Nr  r  r  r$  )&r  r  r   Listr  r  r   rK   opr	  r  r  r   r  r  r  r  r  r  left	NamedExprrS   r  r  rq  ro  r  r  r  r  r  r  r  r}  r  r  r  r  )r:   boolopres_var	expl_listappr"  r{  save	fail_savelevelsr-  v
fail_innercondast_exprpytest_tempr.  r/  expl_formatcallinnerexpl_templates                         r#   visit_BoolOpAssertionRewriter.visit_BoolOp  s   --/KKSXXZ 89	mmIx<Jvyy#&&12oo%OO	V]]#a'  "fmm,DA-/
&&svvdJ'CD", q#++.."1663==99(( )/bq(9(9H"8T2 $(9 #'--/KMNVVD,,TZZ89I9IJ'2FFMM$$$&

1ICKK

CHHWciik$B#CSIJ11#,,t2DEK88C+3DOO""388D>2z!$;;swwy$7D(*&&svvdE2'>?)..$A -B #$4ieATU&&}5xx,d.D.DT.JJJ5s   7P/P/c                   [         UR                  R                     nU R                  UR                  5      u  p4U R                  [        R                  " [        R                  " UR                  U5      U5      5      nXRU4-  4$ r[   )		UNARY_MAPr  rr  r  operandr  r   r  r  )r:   unarypatternoperand_resoperand_explr.  s         r#   visit_UnaryOpAssertionRewriter.visit_UnaryOp"  sf    EHH../$(JJu}}$=!kk#++CKK+,NPUVW|o---r"   c           	     X   [         UR                  R                     nU R                  UR                  5      u  p4U R                  UR
                  5      u  pVSU SU SU S3nU R                  [        R                  " [        R                  " X1R                  U5      U5      5      nX4$ )Nr   r   )
	BINOP_MAPr  rr  r  r  rightr  r   r  r  )	r:   binopsymbol	left_expr	left_expl
right_expr
right_explr#  r.  s	            r#   visit_BinOpAssertionRewriter.visit_BinOp(  s    588--.#zz%**5	!%EKK!8
)AfXQzl!<kkcii	88ZH%P
 r"   c                p   U R                  UR                  5      u  p#/ n/ n/ nUR                   H  n[        U[        R
                  5      (       aZ  UR                  U R                  R                  U R                  0 5      ;   a&  U R                  U R                     UR                     nU R                  U5      u  pUR                  U	5        UR                  U5        M     UR                   GH6  n
[        U
R                  [        R
                  5      (       as  U
R                  R                  U R                  R                  U R                  0 5      ;   a5  U R                  U R                     U
R                  R                     U
l        U R                  U
R                  5      u  pUR                  [        R                  " U
R                  U5      5        U
R                  (       a$  UR                  U
R                  S-   U	-   5        GM"  UR                  SU	-   5        GM9     SR                  USR!                  U5      5      n	[        R"                  " [        R$                  " X%U5      U5      nU R'                  U5      nU R)                  U R+                  U5      5      nU SU SU	 S3nX4$ )N=rD  z{}({})z, z
{z = z
})r  funcr  rK   r   r  r  rq  getro  r  keywordsr~  keywordargformatr   r  r  r  r  r  )r:   r&  new_func	func_expl	arg_explsnew_args
new_kwargsrF  r.  r/  rE  new_callres_expl
outer_expls                 r#   
visit_CallAssertionRewriter.visit_Call2  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&=
r"   c                    U R                  UR                  5      u  p#[        R                  " X!R                  5      nUSU-   4$ )Nr@  )r  r~  r   Starredctx)r:   starredr.  r/  new_starreds        r#   visit_StarredAssertionRewriter.visit_StarredR  s7    JJw}}-	kk#{{3C$J&&r"   c           
        [        UR                  [        R                  5      (       d  U R	                  U5      $ U R                  UR                  5      u  p#U R                  [        R                  " [        R                  " X!R                  [        R                  " 5       5      U5      5      nU R                  U R                  U5      5      nSnXeXSUR                  4-  nXG4$ )Nz%s
{%s = %s.%s
})rK   rT  r   r  r  r  r~  r  r  r  r  r  r  )r:   r  r~  
value_explr.  rN  r   r/  s           r#   visit_Attribute!AssertionRewriter.visit_AttributeX  s    $((CHH--%%d++ JJtzz2kkcmmE99chhjI4P
 ))$,,s*;<"dii@@yr"   c                
   U R                  5         [        UR                  [        R                  5      (       as  UR                  R
                  U R                  R                  U R                  0 5      ;   a5  U R                  U R                     UR                  R
                     Ul        [        UR                  [        R                  5      (       aD  UR                  U R                  U R                     UR                  R                  R
                  '   U R                  UR                  5      u  p#[        UR                  [        R                  [        R                  45      (       a  SU S3n[        [        UR                   5      5       Vs/ s H  o@R#                  5       PM     nnU Vs/ s H-  n[        R                  " U[        R$                  " 5       5      PM/     nnU Vs/ s H-  n[        R                  " U[        R&                  " 5       5      PM/     nn[)        [        [        UR                   5      5      UR                   UR*                  5      n	/ n
/ nU/nU	 GH  u  pMn[        U[        R                  5      (       a  [        U[        R                  5      (       ah  UR                  R
                  UR
                  :X  aD  U R#                  5       UR                  l        XR                  U R                     UR
                  '   U R                  U5      u  nn[        U[        R                  [        R                  45      (       a  SU S3nUR-                  U5        [.        UR0                     nUR-                  [        R2                  " U5      5        U SU SU 3nU
R-                  [        R2                  " U5      5        [        R4                  " [        R                  " X-/U/5      U5      nU R6                  R-                  [        R8                  " X   /U5      5        UUp2GM     U R;                  S[        R<                  " U[        R$                  " 5       5      [        R<                  " U[        R$                  " 5       5      [        R<                  " U
[        R$                  " 5       5      [        R<                  " U[        R$                  " 5       5      5      n[        UR                   5      S:  a+  [        R                  " [        R>                  " 5       U5      nOUS   nUU RA                  U RC                  U5      5      4$ s  snf s  snf s  snf )Nr  r   r5  r2  r  r   )"r  rK   r  r   r  r  rq  rC  ro  r  rS   r  r  r  r'  r   r)  r  r  r  r&  comparatorsr  r6  rr  r  r  r  r  r  r  Andr  r  )r:   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_CompareAssertionRewriter.visit_Compared  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 "!*#%A<<77x22 ''**hkk9)-##&DP((4X[[A"&**\":Hi,cjj(ABB	{!,	NN8$BLL)CKKS)*[#a	{3DLLd+,((XthZ)PRVWHOO""3::{~.>#IJ"*Ii% $&( KKIIdCHHJ'IIj#((*-IIeSXXZ(IIgsxxz*
	 txx=1JJswwy*=CQ-CD**4+B+B9+MNNNO H%UGs   U"54U'/4U,)r.   rm  r  r  r  r   ro  r   r  r  r  r  rq  )r   
str | Noner.   Config | Noner   r   r   r   )r   
ast.Moduler   r   )ru  rx   r   r   )r   rx   )r  ast.exprr   ast.Name)r  rr  r   rr  )rR   rx   r  rr  r   rr  )rR   rx   r   ast.Attribute)r  rr  r   rx   rZ  )r  rr  r   rs  )rK  ast.ASTr   tuple[ast.Name, str])r  z
ast.Assertr   zlist[ast.stmt])rR   zast.NamedExprr   ztuple[ast.NamedExpr, str])rR   rs  r   rv  )r  z
ast.BoolOpr   rv  )r.  zast.UnaryOpr   rv  )r8  z	ast.BinOpr   rv  )r&  zast.Callr   rv  )rU  zast.Starredr   ztuple[ast.Starred, str])r  rt  r   rv  )r`  zast.Comparer   ztuple[ast.expr, str])r   r   r   r    r   r;   r   staticmethodr   r  r  r  r  r  r  r  r  r  r   r  r  r)  r2  r>  rP  rW  r[  rm  r!   __classcell__)rr  s   @r#   r   r   e  s    8t
%
/<
FK
	
$[(z 2 2C..=

'7*(>wr
22/Kb. @'
3O 3Or"   r   c                     [         R                  " U SS9  g! [        [        [        4 a     g[
         a     g[         a9  nUR                  [        R                  [        R                  1;   a   SnAge SnAff = f)zvAttempt to create the given directory and sub-directories exist.

Returns True if successful or if it already exists.
T)exist_okFN)
rN   makedirsFileNotFoundErrorNotADirectoryErrorFileExistsErrorPermissionErrorr   r   EROFSENOSYS)rm   r   s     r#   rd   rd     sm    

I-   1?C    
 77u{{ELL11s%    A<	A<	A<.A76A77A<c                    [         R                  (       a/  [        [         R                  5      [        U R                  SS 6 -  $ U R                  S-  $ )zKReturn the cache directory to write .pyc files for the given .py file path.r  rv   __pycache__)ra   pycache_prefixr   r   parent)	file_paths    r#   rc   rc     sF    
 C&&'$	"0E*FFF -//r"   )r   z	IO[bytes]rr   os.stat_resultrq   types.CodeTyper   r   )
rT   r   rq   r  rr   r  rp   r   r   r   )rW   r   r.   r   r   z%tuple[os.stat_result, types.CodeType])r   r   rp   r   rF   zCallable[[str], None]r   ztypes.CodeType | Noner   )
r   rq  r   r   r   ro  r.   rp  r   r   )r	  objectr   rx   )r.   rp  r   z
int | None)r	  r  r   r   )r!  zIterable[str]r"  r   r   rx   )
r)  Sequence[str]r*  zSequence[bool]r+  r  r,  zSequence[object]r   rx   )r6  r   r7  rx   r/  rx   r   r   )r   r   )rK  ru  r   zIterator[ast.AST])rc  r   r   zdict[int, str])rm   r   r   r   )r  r   r   r   )pr   rv  r   r   collectionsr   r   	functoolsr   rH   importlib.machineryimportlib.utilr\  r  r   rN   pathlibr   r   r   ra   r[  r   typingr   r   r	   r
   r   r   _pytest._io.safereprr   r   r   _pytest._versionr   _pytest.assertionr   _pytest.configr   _pytest.mainr   _pytest.pathlibr   r   _pytest.stashr   _pytest.assertion.utilr   r  r   r   rD   implementation	cache_tag
PYTEST_TAGPYC_EXTre   r  abcMetaPathFinderLoaderr)   r   rh   rg   rf   r   r
  r  r  r  r$  r2  r8  r:  r  InvertUSubUAddr,  BitOrBitXorBitAndLShiftRShiftr  SubMultDivFloorDivr  EqNotEqLtLtEGtGtEPowIsIsNotr  NotInMatMultr6  rJ  	lru_cacherj  NodeVisitorr   rd   rc   r   r"   r#   <module>r     s   ; " 
 #      	   	    
          6 ) 3 $ " !   ( & " 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"   