
     h                        S 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
JrJr  SSKJr  SSKJr  SSKJrJrJrJr  SSKJr  SS	KJr  SS
KJr  SSKJrJr  SSKJ r J!r!  SSK"J#r#  SSK$J%r%J&r&  \ " \5      r " S S5      r' " S S5      r(\" SSS9 " S S5      5       r) " S S\5      r*\+\%\,\-\\.   \\.   4      4   r/ " S S5      r0 " S S\05      r1 " S S\05      r2 " S S \05      r3S$S! jr4 " S" S#5      r5g)%zCode parsing for coverage.py.    )annotationsN)IterableSequence)	dataclass)CodeType)CallableOptionalProtocolcast)env)code_objects)short_stack)NoSource	NotPython)isolate_module	nice_pair)generate_tokens)TArcTLineNoc                  
   \ rS rSrSr   S       SS jjrSS jrSS jr\R                  " SS9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\R                  SS j5       rS S jrS!S jrS!S jrSrg)"PythonParser   zParse code to find executable lines, excluded lines, etc.

This information is all based on static analysis: no code execution is
involved.

Nc                   U(       d  U(       d   S5       eU=(       d    SU l         Ub  Xl        OSSKJn   U" U R                   5      U l        X0l        SU l        [        5       U l	        [        5       U l
        [        5       U l        [        5       U l        [        5       U l        SU l        0 U l        SU l        SU l        0 U l        g! [         a!  n[        SU R                    SU 35      UeSnAff = f)	z
Source can be provided as `text`, the text itself, or `filename`, from
which the text will be read.  Excluded lines are those that match
`exclude`, a regex string.

z*PythonParser needs either text or filenamez<code>Nr   )get_python_sourcezNo source for code: 'z': F)filenametextcoverage.pythonr   OSErrorr   exclude	_ast_rootset
statementsexcludedraw_statementsraw_excludedraw_docstringsshow_tokens
_multiline	_all_arcs_missing_arc_fragments_with_jump_fixers)selfr   r   r   r   errs         m/root/racknerd_01/codex-conversation/amazon-q-terraform/.venv/lib/python3.13/site-packages/coverage/parser.py__init__PythonParser.__init__'   s     xM!MM ,H!I9Y-dmm<	  *.
 ), '*e -0E +.% -0E ! 35 ,0<@#@BQ  Y!6t}}oSNOUXXYs   C
 

C5C00C5c           	       ^  [        5       nSnSn[        R                  " UT R                  [        R                  S9 H  nUR                  5       u  pgUT R                  R                  SX65      -   nUT R                  R                  SX75      -   n	UR                  U 4S j[        US-   U	S-   5       5       5        UnUnM     U$ )zFind the lines matching a regex.

Returns a set of line numbers, the lines that contain a match for
`regex`. The entire line needn't match, just a part of it.
Handles multiline regex patterns.

r   )flags
c              3  Z   >#    U  H   nTR                   R                  X5      v   M"     g 7fNr(   get).0ir,   s     r.   	<genexpr>.PythonParser.lines_matching.<locals>.<genexpr>v   s%     b>a4??..q44>as   (+      )	r!   refinditerr   	MULTILINEspancountupdaterange)
r,   regexmatches
last_startlast_start_linematchstartend
start_lineend_lines
   `         r.   lines_matchingPythonParser.lines_matchingf   s     !$
[[		FEJE(499??4+SSJ&z)OOHNNbeJQRNT\_`T`>abbJ(O G     c           
     
   U R                   (       a:  U R                  U R                   5      U l        [        U R                  5      U l        SnSnSnSnSnSnU R
                  c   e[        U R
                  5      nU GH  u  pu  pu  pnU R                  (       aD  [        [        R                  R                  X5      <S S[        X45      <S SU	<S SU< 35        U[        R                  :X  a  US	-  nOU[        R                  :X  a  US	-  nOU[        R                   :X  ay  U	S
:X  a[  US:X  aU  U R                  R#                  [%        XLS	-   5      5      nU(       d&  U(       a  U R                  R'                  U5        UnSnO]U	S;   a  US	-  nOQU	S;   a  US	-  nOEU[        R(                  :X  a1  U(       a(  X:w  a#  [%        XLS	-   5       H  nX@R*                  U'   M     SnU	R-                  5       (       d  GM  U[        R.                  :w  d  GM  SnU(       a  GM  U
nU(       a  X::  a  SnU(       d  GM  U R                  R'                  U5        GM     U(       dG  [1        U R
                  U R2                  S9nU R4                  R7                  UR9                  5       5        U R;                  U R                  5      U l        U R<                  c   e[>        R@                  " U R<                  5       GH  n[C        U[>        RD                  [>        RF                  [>        RH                  [>        RJ                  45      (       a  URL                  (       a  URL                  S   n[C        U[>        RN                  5      (       a  [C        URP                  [>        RR                  5      (       as  [C        URP                  RP                  [T        5      (       aJ  U RV                  R7                  [%        URX                  [[        [\        UR^                  5      S	-   5      5        [C        U[>        RD                  [>        RF                  [>        RH                  45      (       d  GMm  [a        S URb                   5       URX                  S9nU R                  R#                  [%        UURX                  S	-   5      5      (       d  GM  U R                  R7                  [%        U[[        [\        UR^                  5      S	-   5      5        GM     g)zgParse the source to find the interesting facts about its lines.

A handful of attributes are updated.

r   FTNz>10 z>520r<   :z([{z)]})r   c              3  8   #    U  H  oR                   v   M     g 7fr5   lineno)r8   ds     r.   r:   *PythonParser._raw_parse.<locals>.<genexpr>   s     !H4Gq((4Gs   )default)2r   rN   r%   r!   r#   r   r   r'   printtokenizetok_namer7   r   tokenINDENTDEDENTOPintersectionrD   addNEWLINEr(   stripCOMMENT
ByteParserr   r$   rC   _find_statementsfirst_linesr    astwalk
isinstanceClassDefFunctionDefAsyncFunctionDefModulebodyExprvalueConstantstrr&   rW   r   int
end_linenomindecorator_list)r,   indentexclude_indent	excluding
first_lineemptynestingtokgentoktypettextslineno_elinenoltextshould_excludelbyte_parsernodefirsts                      r.   
_raw_parsePythonParser._raw_parse{   s    << $ 3 3DLL AD 1 12DM 	
yy$$$ +AG=GLW,7 !))--g?!7"45 %,,&!ELL(!EHH$C<GqL%)]]%?%?j\]R]@^%_N$ ))'2)/$(	e^qLGe^qLGEMM)'"7 #:{;-7* <
{{}}H,<,<!<!z!(J V%=$)	 y))'2a BHf $TYYGK&&{'C'C'EF((7 ~~)))HHT^^,D$s@T@TVYV`V` abb99 IIaLE"5#((33&u{{CLLAA&u{{'8'8#>>++22!%,,S%:J:J0Ka0OP
 $s@T@T UVV !HD4G4G!HRVR]R]^
==--eJa.PQQMM((z4T__;UXY;Y)Z[% -rP   i  )maxsizec                    US:  a!  U R                   R                  U* U* 5      * nU$ U R                   R                  X5      nU$ )zAReturn the first line number of the statement including `lineno`.r   r6   )r,   rW   s     r.   r}   PythonParser.first_line   sJ     A:oo))6'F7;;F  __((8FrP   c                N    U Vs1 s H  o R                  U5      iM     sn$ s  snf )zrMap the line numbers in `linenos` to the correct first line of the
statement.

Returns a set of the first lines.

)r}   )r,   linenosr   s      r.   ri   PythonParser.first_lines   s#     -44Gq"G444s   "c                $    U R                  U5      $ )z)Implement `FileReporter.translate_lines`.)ri   )r,   liness     r.   translate_linesPythonParser.translate_lines   s    &&rP   c                    U R                  U5       VVs1 s H'  u  p#U R                  U5      U R                  U5      4iM)     snn$ s  snnf )z(Implement `FileReporter.translate_arcs`.)fix_with_jumpsr}   )r,   arcsabs       r.   translate_arcsPythonParser.translate_arcs   sA    GKGZGZ[_G`aG`Va#T__Q%78G`aaas   .Ac                     [         R                  " U R                  5      U l        U R	                  5         U R                  U R                  -  nU R                   U-
  nU R#                  U5      U-
  U l        g! [
        R                  [        [        4 ae  n[        US5      (       a  UR                  nOUR                  S   S   n[        SU R                   S3UR                  S   < SU 3-   5      UeSnAff = f)zParse source text to find executable lines, excluded lines, etc.

Sets the .excluded and .statements attributes, normalized to the first
line of multi-line statements.

rW   r<   r   zCouldn't parse 'z' as Python source: z	 at line N)rj   parser   r    r   r\   
TokenErrorIndentationErrorSyntaxErrorhasattrrW   argsr   r   r#   r&   r$   ri   r"   )r,   r-   rW   ignorestartss        r.   parse_sourcePythonParser.parse_source   s    	 YYtyy1DNOO !4!44$$v-**62V; ##%5{C 	sH%%!Q"4==/1EFXXa[O9VH56 	s   5A9 9C=A C88C=c                r    U R                   c  U R                  5         U R                   c   eU R                   $ )zGet information about the arcs available in the code.

Returns a set of line number pairs.  Line numbers have been normalized
to the first line of multi-line statements.

)r)   _analyze_astr,   s    r.   r   PythonParser.arcs  s4     >>!~~)))~~rP   c                   U R                   c   e[        U R                  U R                   U R                  U R                  5      nUR                  5         UR                  nUR                  5       U l        U R                  (       a  U R                  U5      n[        5       U l        U HJ  u  p4U R                  U5      nU R                  U5      nXV:w  d  M.  U R                  R                  XV45        ML     UR                  U l        g)z[Run the AstArcAnalyzer and save its results.

`_all_arcs` is the set of arcs in the code.

N)r    AstArcAnalyzerr   r$   r(   analyzer   with_jump_fixersr+   r   r!   r)   r}   rc   missing_arc_fragmentsr*   )r,   aaar   l1l2fl1fl2s          r.   r   PythonParser._analyze_ast#  s     ~~)))T]]DNND<O<OQUQ`Q`axx!$!5!5!7!!&&t,DFB//"%C//"%Cz""C:.	  '*&?&?#rP   c                   [        5       n[        5       nU H  nX@R                  ;   d  M  US   nUR                  U5        U R                  U   u  pgX`R                  ;   aD  UR                  U5        U R                  U   u  pgUR                  U5        X`R                  ;   a  MD  UR                  XWS   45        UR                  U5        M     [        U5      U-  U-
  nU$ )a  Adjust arcs to fix jumps leaving `with` statements.

Consider this code:

    with open("/tmp/test", "w") as f1:
        a = 2
        b = 3
    print(4)

In 3.10+, we get traces for lines 1, 2, 3, 1, 4.  But we want to present
it to the user as if it had been 1, 2, 3, 4.  The arc 3->1 should be
replaced with 3->4, and 1->4 should be removed.

For this code, the fixers dict is {(3, 1): ((1, 4), (3, 4))}.  The key
is the actual measured arc from the end of the with block back to the
start of the with-statement.  The values are start_next (the with
statement to the next statement after the with), and end_next (the end
of the with-statement to the next statement after the with).

With nested with-statements, we have to trace through a few levels to
correct a longer chain of arcs.

r   r<   )r!   r+   rc   )r,   r   	to_removeto_addarcend0
start_nextend_nexts           r.   r   PythonParser.fix_with_jumps:  s    0 E	C,,,1vc"'+'='=c'B$
 $:$::MM*-+/+A+A*+M(JMM(+ !$:$:: 

D1+./j)  D	F"i/rP   c                   [         R                  " [        5      nU R                  5        HQ  u  p#US:  d   SU< SU R                   35       eX R
                  ;   a  M4  X0R
                  ;   a  ME  X==   S-  ss'   MS     U$ )zIGet a count of exits from that each line.

Excluded lines are excluded.

r   zl1=z  should be greater than zero in r<   )collectionsdefaultdictrv   r   r   r#   )r,   exit_countsr   r   s       r.   r   PythonParser.exit_countsb  sw     +6*A*A#*FiikFB6RcbU"B4==/RR6]]"]]"Oq O " rP   c                @    Uc  US:  a  SnOSnUR                  US9nU$ )z=Apply some defaulting and formatting to an arc's description.r   zjump to the function exitzjump to line {lineno}rV   )format)r,   
action_msgrK   s      r.   _finish_action_msgPythonParser._finish_action_msgv  s2    Qw8
4
&&c&2
rP   c                V   U R                   c  U R                  5         U R                   c   eU R                   R                  X4S/5      n/ nU HG  u  pVU R                  Xb5      nSU SU 3nUb  USUR	                  US9 3-  nUR                  U5        MI     SR                  U5      $ )z5Provide an English sentence describing a missing arc.NNzline z didn't z	 because rV   z or )r*   r   r7   r   r   appendjoin)r,   rJ   rK   fragment_pairsmsgsmissing_cause_msgr   msgs           r.   missing_arc_description$PythonParser.missing_arc_description  s    &&...:::4488%~V-;)00AJ%5C ,#4#;#;5#;#I"JKKKK .< {{4  rP   c                    U R                   c  U R                  5         U R                   c   eU R                   R                  X4S/5      nU R                  US   S   U5      nU$ )z2Provide an English description of an arc's effect.r   r   r<   )r*   r   r7   r   )r,   rJ   rK   r   r   s        r.   arc_descriptionPythonParser.arc_description  sj    &&...:::4488%~V,,^A->q-A3G
rP   )r)   r    r*   r(   r+   r   r#   r   r&   r%   r$   r'   r"   r   )NNN)r   
str | Noner   r   r   r   returnNone)rE   ru   r   set[TLineNo]r   r   )rW   r   r   r   )r   Iterable[TLineNo]r   r   )r   r   r   r   )r   zIterable[TArc]r   	set[TArc])r   r   )r   zdict[TLineNo, int])r   r   rK   r   r   ru   )rJ   r   rK   r   r   ru   )__name__
__module____qualname____firstlineno____doc__r/   rN   r   	functools	lru_cacher}   ri   r   r   r   r   r   r   r   r   r   r   __static_attributes__ rP   r.   r   r      s      #"	=C=C =C 	=C
 
=C~*h\T & '5'b<0
@.&P  &!&rP   r   c                  V    \ rS rSrSr  S	       S
S jjrSS jrSS jrSS jrSr	g)rg   i  z3Parse bytecode to understand the structure of code.Nc                N    Xl         Ub  X l        g Uc   e[        XSSS9U l        g )NexecT)dont_inherit)r   codecompile)r,   r   r   r   s       r.   r/   ByteParser.__init__  s2     	I'''  TJDIrP   c                B   ^  U 4S j[        T R                  5       5       $ )aL  Iterate over all the code objects nested within this one.

The iteration includes `self` as its first value.

We skip code objects named `__annotate__` since they are deferred
annotations that usually are never run.  If there are errors in the
annotations, they will be caught by type checkers or other tools that
use annotations.

c              3  p   >#    U  H+  nUR                   S :w  d  M  [        TR                  US9v   M-     g7f)__annotate__)r   N)co_namerg   r   )r8   cr,   s     r.   r:   +ByteParser.child_parsers.<locals>.<genexpr>  s1      
,yyN* *Jtyyq),s   66)r   r   r   s   `r.   child_parsersByteParser.child_parsers  s    
!$)),
 	
rP   c              #  n   #    U R                   R                  5        H  u    pU(       d  M  Uv   M     g7f)ziYield the line numbers possible in this code object.

Uses co_lines() to produce a sequence: l0, l1, ...
N)r   co_lines)r,   r   lines      r.   _line_numbersByteParser._line_numbers  s,     
 )),,.JAqt
 /s   &5	5c              #  n   #    U R                  5        H  nUR                  5        Sh  vN   M     g N	7f)zFind the statements in `self.code`.

Produce a sequence of line numbers that start statements.  Recurses
into all code objects reachable from `self.code`.

N)r   r   )r,   bps     r.   rh   ByteParser._find_statements  s/      $$&B''))) ')s   '53
5)r   r   r   )r   ru   r   zCodeType | Noner   r   r   r   )r   zIterable[ByteParser])r   r   )
r   r   r   r   r   r/   r   r   rh   r   r   rP   r.   rg   rg     sL    =
 !%#	KK K 	K
 
K
"	*rP   rg   T)frozenorderc                  2    \ rS rSr% SrS\S'   SrS\S'   Srg	)
ArcStarti  a  The information needed to start an arc.

`lineno` is the line number the arc starts from.

`cause` is an English text fragment used as the `missing_cause_msg` for
AstArcAnalyzer.missing_arc_fragments.  It will be used to describe why an
arc wasn't executed, so should fit well into a sentence of the form,
"Line 17 didn't run because {cause}."  The fragment can include "{lineno}"
to have `lineno` interpolated into it.

As an example, this code::

    if something(x):        # line 1
        func(x)             # line 2
    more_stuff()            # line 3

would have two ArcStarts:

- ArcStart(1, "the condition on line 1 was always true")
- ArcStart(1, "the condition on line 1 was never true")

The first would be used to create an arc from 1 to 3, creating a message like
"line 1 didn't jump to line 3 because the condition on line 1 was always true."

The second would be used for the arc from 1 to 2, creating a message like
"line 1 didn't jump to line 2 because the condition on line 1 was never true."

r   rW    ru   causer   N)r   r   r   r   r   __annotations__r  r   r   rP   r.   r  r    s    : OE3OrP   r  c                  <    \ rS rSrSr  S         SS jjrSrg)	TAddArcFni  z&The type for AstArcAnalyzer.add_arc().Nc                    g)a*  
Record an arc from `start` to `end`.

`missing_cause_msg` is a description of the reason the arc wasn't
taken if it wasn't taken.  For example, "the condition on line 10 was
never true."

`action_msg` is a description of what the arc does, like "jump to line
10" or "exit from function 'fooey'."

Nr   r,   rJ   rK   r   r   s        r.   __call__TAddArcFn.__call__  s    rP   r   r   
rJ   r   rK   r   r   r   r   r   r   r   )r   r   r   r   r   r
  r   r   rP   r.   r  r    sG    0 )-!%  &	
  
 rP   r  c                  @    \ rS rSrSrS	S jrS	S jrS	S jrS	S jrSr	g)
Blocki  a#  
Blocks need to handle various exiting statements in their own ways.

All of these methods take a list of exits, and a callable `add_arc`
function that they can use to add arcs if needed.  They return True if the
exits are handled, or False if the search should continue up the block
stack.
c                    g)zProcess break exits.Fr   r,   exitsadd_arcs      r.   process_break_exitsBlock.process_break_exits#      rP   c                    g)zProcess continue exits.Fr   r  s      r.   process_continue_exitsBlock.process_continue_exits'  r  rP   c                    g)zProcess raise exits.Fr   r  s      r.   process_raise_exitsBlock.process_raise_exits+  r  rP   c                    g)zProcess return exits.Fr   r  s      r.   process_return_exitsBlock.process_return_exits/  r  rP   r   Nr  set[ArcStart]r  r  r   bool)
r   r   r   r   r   r  r  r  r  r   r   rP   r.   r  r    s    rP   r  c                  6    \ rS rSrSrSS jrS	S jrS	S jrSrg)
	LoopBlocki4  z@A block on the block stack representing a `for` or `while` loop.c                .    Xl         [        5       U l        g r5   )rJ   r!   break_exits)r,   rJ   s     r.   r/   LoopBlock.__init__7  s    
*-%rP   c                :    U R                   R                  U5        gNT)r%  rC   r  s      r.   r  LoopBlock.process_break_exits=  s    &rP   c                f    U H+  nU" UR                   U R                  UR                  5        M-     gr(  )rW   rJ   r  r,   r  r  xits       r.   r   LoopBlock.process_continue_exitsA  s'    CCJJ

CII6 rP   )r%  rJ   N)rJ   r   r   r   r  )	r   r   r   r   r   r/   r  r  r   r   rP   r.   r#  r#  4  s    J0rP   r#  c                  6    \ rS rSrSrSS jrS	S jrS	S jrSrg)
FunctionBlockiG  z>A block on the block stack representing a function definition.c                    Xl         X l        g r5   rJ   name)r,   rJ   r2  s      r.   r/   FunctionBlock.__init__J  s    
	rP   c                    U H;  nU" UR                   U R                  * UR                  SU R                  < 35        M=     g)Nzexcept from function TrW   rJ   r  r2  r+  s       r.   r  !FunctionBlock.process_raise_exitsP  >    C

		'		}5	  rP   c                    U H;  nU" UR                   U R                  * UR                  SU R                  < 35        M=     g)Nzreturn from function Tr5  r+  s       r.   r  "FunctionBlock.process_return_exitsZ  r7  rP   )r2  rJ   N)rJ   r   r2  ru   r   r   r  )	r   r   r   r   r   r/   r  r  r   r   rP   r.   r/  r/  G  s    HrP   r/  c                  ,    \ rS rSrSrSS jrSS jrSrg)	TryBlockie  z6A block on the block stack representing a `try` block.c                    Xl         X l        g r5   )handler_startfinal_start)r,   r=  r>  s      r.   r/   TryBlock.__init__h  s    *&rP   c                    U R                   b1  U H+  nU" UR                  U R                   UR                  5        M-     gr(  )r=  rW   r  r+  s       r.   r  TryBlock.process_raise_exitsn  s5    )

D$6$6		B rP   )r>  r=  N)r=  TLineNo | Noner>  rB  r   r   r  )r   r   r   r   r   r/   r  r   r   rP   r.   r;  r;  e  s    @'rP   r;  c                   U =[         R                  S` =b  u     S[        U R                  5      4$   =[         R                  S` =b,  u     U R
                  S;   a  S[        U R
                  5      4$  g  =[         R                  S` =bM  u     [        U R                  [         R                  5      (       a  [        U R                  5      u  pX(       + 4$  g  [         R                  S` =b  u   U R                   Vs/ s H  n[        U5      PM     nn[        S U 5       5      nU(       aF  [        U R                  [         R                   5      (       a  ["        O[        nSU" S U 5       5      4$  g  gs  snf )zIs this a compile-time constant test expression?

We don't try to mimic all of CPython's optimizations.  We just have to
handle the kinds of constant expressions people might actually use.

r   T)TrueFalser   	__debug__c              3  *   #    U  H	  u  pUv   M     g 7fr5   r   )r8   is_constr   s      r.   r:   (is_constant_test_expr.<locals>.<genexpr>  s     ?$;8h$   c              3  *   #    U  H	  u  pUv   M     g 7fr5   r   )r8   r   vs      r.   r:   rI    s     3ddadrJ  )FF)rj   rt   r!  rs   NameidevalUnaryOprl   opNotis_constant_test_exproperandBoolOpvaluesallOrany)r   is_constantvalrL  retsrQ  s         r.   rS  rS  x  s:    S\\^djj))) SXXZww@@T$'']** A   S[[]$''377++#8#F "G++ ,   ZZ\6:kkBk)!,kDB?$??K&tww77SSR3d3333     Cs   ;E5c                     \ rS rSrSr          S%S jrS&S jrS'S jrS(S jrS)S jr	\	r
S*S jr  S+         S,S
 jjrS-S jrS.S jrS/S jrS0S jr\rS1S jr\r\rS2S jrS3S jr1 SkrS4S jr  S+       S5S jjrS6S jrS6S jrS6S jrS6S jrS7S jrS8S jr\r S9S jr!S:S jr"\"r#\r$\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+SAS# jr,\,r-S$r.g	)Br   i  a  Analyze source text with an AST to find executable code paths.

The .analyze() method does the work, and populates these attributes:

`arcs`: a set of (from, to) pairs of the the arcs possible in the code.

`missing_arc_fragments`: a dict mapping (from, to) arcs to lists of
message fragments explaining why the arc is missing from execution::

    { (start, end): [(missing_cause_msg, action_msg), ...], }

For an arc starting from line 17, they should be usable to form complete
sentences like: "Line 17 didn't {action_msg} because {missing_cause_msg}".

NOTE: Starting in July 2024, I've been whittling this down to only report
arc that are part of true branches.  It's not clear how far this work will
go.

c                   Xl         X l        U Vs1 s H  oTR                  XU5      iM     snU l        X@l        [        [        [        R                  " SS5      5      5      nU(       aY  [        SU R                   35        [        SU R                   35        [        [        R                  " U R                  SSS95        [        5       U l        [        R                  " [         5      U l        / U l        [        5       U l        [        5       U l        [        5       U l        [        5       U l        [        [        [        R                  " SS5      5      5      U l        g s  snf )	NCOVERAGE_AST_DUMP0zStatements: zMultiline map: T   )include_attributesrz   COVERAGE_TRACK_ARCS)r   	root_noder7   r"   	multiliner!  rv   osgetenvr[   rj   dumpr!   r   r   r   listr   block_stackcurrent_with_startsall_with_startswith_entries
with_exitsdebug)r,   r   rd  r"   re  r   dump_asts          r.   r/   AstArcAnalyzer.__init__  s    !"8BC
1==.
C" BII&93?@AL 123ODNN#345#((4>>d1MN"u	4?4K4KD4Q"(* 25 -0U'*u%(U #bii(=sCDE
7 Ds   E"c                    [         R                  " U R                  5       H6  nUR                  R                  n[        U SU 3S5      nUc  M.  U" U5        M8     g)zFExamine the AST tree from `self.root_node` to determine possible arcs._code_object__N)rj   rk   rd  	__class__r   getattr)r,   r   	node_namecode_object_handlers       r.   r   AstArcAnalyzer.analyze  sL    HHT^^,D//I")$.0Ld"S".#D)	 -rP   c                   0 nU R                    Vs1 s H+  nUS   U R                  ;   d  M  X R                  ;  d  M)  UiM-     nnU R                   H  nU Vs1 s H  o"S   U:X  d  M  US   iM     nnU(       d  M+  [        U5      S:X  d   SU SU< 35       eUR	                  5       nU R
                   Vs1 s H  o"S   U:X  d  M  US   iM     nnU H  nXF4X44XU4'   M     M     U$ s  snf s  snf s  snf )a#  Get a dict with data for fixing jumps out of with statements.

Returns a dict.  The keys are arcs leaving a with-statement by jumping
back to its start.  The values are pairs: first, the arc from the start
to the next statement, then the arc that exits the with without going
to the start.

r   r<   zExpected one arc, got z with start = )r   rl  rm  lenpoprn  )	r,   fixersr   
with_nextsrJ   nextsnxtendsrK   s	            r.   r   AstArcAnalyzer.with_jump_fixers  s     yy
 1v--- 25=N=N2N   	 

 ))E'1EzVu_VSVzEEu:?T&<UG?%$TT?))+C&*ooIosQ5FCFoDI).sj'AU|$  * 
 F
 Js'   C+C+C+C0(	C07C5	C5c                   U R                  U5      nUR                  (       aN  U R                  UR                  5      nU H,  nU R                  UR                  U* UR
                  S5        M.     g U R                  X"* 5        g )Nzexit the module)line_for_noderq   process_bodyr  rW   r  r,   r   rJ   r  r,  s        r.   _code_object__Module#AstArcAnalyzer._code_object__Module  sd    ""4(99%%dii0ESZZ%<MN  LL'rP   c                
   U R                  U5      nU R                  R                  [        X!R                  S95        U R                  UR                  5      nU R                  U5        U R                  R                  5         g )Nr1  )	r  rj  r   r/  r2  r  rq   r  r{  )r,   r   rJ   r  s       r.   _code_object__FunctionDef(AstArcAnalyzer._code_object__FunctionDef  sc    ""4(E		 JK!!$)),!!%(rP   c                    U R                  U5      nU R                  UR                  5      nU H:  nU R                  UR                  U* UR
                  SUR                  < 35        M<     g )Nzexit class )r  r  rq   r  rW   r  r2  r  s        r.   _code_object__ClassDef%AstArcAnalyzer._code_object__ClassDef  sW    ""4(!!$)),CLLeVSYY+dii]8ST rP   Nc           
     X   U R                   (       a+  [        SU SU SU< SU< 35        [        [        5       SS9  U R                  R	                  X45        XR
                  ;   a  U R                  R	                  X45        Uc  Ub!  U R                  X4   R                  X445        gg)z@Add an arc, including message fragments to use if it is missing.zAdding possible arc: (z, z): z

)rK   N)	ro  r[   r   r   rc   rk  rm  r   r   r	  s        r.   r  AstArcAnalyzer.add_arc  s     ::*5'C5<M;PPRS]R`ab+-V,		ul#,,,!!5,/(J,B&&|4;;=N<[\ -CrP   c                ,    [        U R                  5      $ )z.Yield the blocks in nearest-to-farthest order.)reversedrj  r   s    r.   nearest_blocksAstArcAnalyzer.nearest_blocks  s    (())rP   c                   UR                   R                  n[        [        [        [
        R                  /[        4      [        U SU 3S5      5      nUb	  U" U5      nOUR                  nU R                  R                  XD5      $ )zmWhat is the right line number to use for this node?

This dispatches to _line__Node functions where needed.

_line__N)rt  r   r   r	   r   rj   ASTr   ru  rW   re  r7   )r,   r   rv  handlerr   s        r.   r  AstArcAnalyzer.line_for_node#  sw     NN++	Xswwi012DGI;/6
 4=D;;D~~!!$--rP   c                v    UR                   (       a  UR                   S   R                  nU$ UR                  nU$ )zSCompute first line number for things that can be decorated (classes and functions).r   )ry   rW   )r,   r   rW   s      r.   _line_decoratedAstArcAnalyzer._line_decorated9  s8    ((+22F  [[FrP   c                8    U R                  UR                  5      $ r5   )r  rs   r,   r   s     r.   _line__AssignAstArcAnalyzer._line__AssignA  s    !!$**--rP   c                    UR                   (       aB  UR                   S   b  UR                   S   R                  $ UR                  S   R                  $ UR                  $ Nr   )keysrW   rV  r  s     r.   _line__DictAstArcAnalyzer._line__DictF  sK    99yy|'yy|*** {{1~,,,;;rP   c                x    UR                   (       a  U R                  UR                   S   5      $ UR                  $ r  )eltsr  rW   r  s     r.   _line__ListAstArcAnalyzer._line__ListT  s,    99%%diil33;;rP   c                    g)Nr<   r   r  s     r.   _line__ModuleAstArcAnalyzer._line__ModuleZ  s    rP   >   rr   PassAssertAssignDeleteGlobalImportNonlocal	AnnAssign	AugAssign
ImportFromc                n   UR                   R                  n[        [        [        [
        R                  /[        [           4      [        U SU 3S5      5      nUb
  U" U5      nU$ [        R                  (       a  X R                  ;  a  [        SU 35      e[        U R                  U5      5      1nU$ )a  Find the set of arc starts that exit this node.

Return a set of ArcStarts, exits from this node to the next. Because a
node represents an entire sub-tree (including its children), the exits
from a node can be arbitrarily complex::

    if something(1):
        if other(2):
            doit(3)
        else:
            doit(5)

There are three exits from line 1: they start at lines 1, 3 and 5.
There are two exits from line 2: lines 3 and 5.

	_handle__Nz*** Unhandled: )rt  r   r   r	   r   rj   r  r!   r  ru  r   TESTINGOK_TO_DEFAULTRuntimeErrorr  )r,   r   rv  r  
arc_startss        r.   
node_exitsAstArcAnalyzer.node_exitsl  s    " NN++	XswwiX678DIi[148
  J  {{$6$66&'?@@ #4#5#5d#;<=JrP   c                   Uc  Uc  [        5       nO	U1nOUb   eU He  nU R                  U5      nXPR                  ;  a  M%  U H)  nU R                  UR                  XVR
                  5        M+     U R                  U5      nMg     U$ )a  Process the body of a compound statement.

`body` is the body node to process.

`from_start` is a single `ArcStart` that starts an arc into this body.
`prev_starts` is a set of ArcStarts that can all be the start of arcs
into this body.  Only one of `from_start` and `prev_starts` should be
given.

Records arcs within the body by calling `self.add_arc`.

Returns a set of ArcStarts, the exits from this body.

)r!   r  r"   r  rW   r  r  )r,   rq   
from_startprev_starts	body_noderW   
prev_starts          r.   r  AstArcAnalyzer.process_body  s    ( !!e)l%%% I''	2F__,)
Z..8H8HI *//)4K  rP   c                v    U R                  5        H%  nUR                  XR                  5      (       d  M%    g   g)z0Add arcs due to jumps from `exits` being breaks.N)r  r  r  r,   r  blocks      r.   r  "AstArcAnalyzer.process_break_exits  -    ((*E((== +rP   c                v    U R                  5        H%  nUR                  XR                  5      (       d  M%    g   g)z3Add arcs due to jumps from `exits` being continues.N)r  r  r  r  s      r.   r  %AstArcAnalyzer.process_continue_exits  s-    ((*E++E<<@@ +rP   c                v    U R                  5        H%  nUR                  XR                  5      (       d  M%    g   g)z0Add arcs due to jumps from `exits` being raises.N)r  r  r  r  s      r.   r  "AstArcAnalyzer.process_raise_exits  r  rP   c                v    U R                  5        H%  nUR                  XR                  5      (       d  M%    g   g)z1Add arcs due to jumps from `exits` being returns.N)r  r  r  r  s      r.   r  #AstArcAnalyzer.process_return_exits  s-    ((*E))%>> +rP   c                p    U R                  U5      n[        USS9nU R                  U15        [        5       $ )Nz*the break on line {lineno} wasn't executedr  )r  r  r  r!   )r,   r   herebreak_starts       r.   _handle__BreakAstArcAnalyzer._handle__Break  s6    !!$'t+WX  +/urP   c                   UR                   nUR                   nUR                  nU(       a  SnU H/  nU R                  U5      nUb  Xc:w  a  U R                  X65        UnM1     Uc   eU R                  X25        UnUR                  (       d/   SUR                  < SU R
                   SUR                    35       eUc   e[        U5      1$ )zBAdd arcs for things that can be decorated (classes and functions).NzOops: node.body = z in @)rW   ry   r  r  rq   r   r  )r,   r   	main_linelastdecsdec_node	dec_starts          r.   _handle_decorated AstArcAnalyzer._handle_decorated  s    ![[	#{{""D  ..x8	#	(9LL1 	 !
 ###LL)D
 99V 3tyynDqVV9rP   c                p    U R                  U5      n[        USS9nU R                  U15        [        5       $ )Nz-the continue on line {lineno} wasn't executedr  )r  r  r  r!   )r,   r   r  continue_starts       r.   _handle__Continue AstArcAnalyzer._handle__Continue  s7    !!$'!$.]^##^$45urP   c                8   U R                  UR                  5      nU R                  R                  [	        US95        [        USS9nU R                  UR                  US9nU H)  nU R                  UR                  X%R                  5        M+     U R                  R                  5       n[        U[        5      (       d   eUR                  n[        USS9nUR                  (       a   U R                  UR                  US9nXG-  nU$ UR                  U5        U$ )NrJ   z'the loop on line {lineno} never startedr  r  z)the loop on line {lineno} didn't complete)r  iterrj  r   r#  r  r  rq   r  rW   r  r{  rl   r%  orelserc   )r,   r   rJ   r  r  r,  my_block
else_exitss           r.   _handle__ForAstArcAnalyzer._handle__For  s    ""499-	 67e+TU
!!$))
!CCLLUII6 ##'')(I....$$e+VW
;;**4;;:*NJE  IIj!rP   c                N   U R                  UR                  5      n[        UR                  5      u  p4[        5       nU(       a  U(       a&  [	        USS9nXPR                  UR                  US9-  nU(       a  U(       d&  [	        USS9nXPR                  UR                  US9-  nU$ )N-the condition on line {lineno} was never truer  r  .the condition on line {lineno} was always true)r  testrS  r!   r  r  rq   r  )r,   r   rJ   constant_testr[  r  r  s          r.   _handle__IfAstArcAnalyzer._handle__If   s    ""499-2499=!%/^_J&&tyyZ&HHEC!%/_`J&&t{{z&JJErP   c                Z   U R                  U5      nUn[        5       nUR                   HX  nU R                  UR                  5      nU R	                  X6S5        [        USS9nX@R                  UR                  US9-  nUnMZ     WR                  n[        U[        R                  5      (       a0  UR                  S   n[        U[        R                  5      (       a  M0  [        U[        R                  5      (       aG  UR                  b:  UR                  n[        U[        R                  5      (       a  UR                  b  M:  [        U[        R                  5      =(       a#    UR                  S L =(       a    UR                  S L n	U	(       d  UR                  [        WSS95        U$ )Nz+the pattern on line {lineno} always matchedz*the pattern on line {lineno} never matchedr  r  )r  r!   casespatternr  r  r  rq   rl   rj   MatchOrpatternsMatchAsguardrc   )
r,   r   rJ   rG   r  case
case_startr  r  had_wildcards
             r.   _handle__MatchAstArcAnalyzer._handle__Match,  sU   ""4(
JJD++DLL9JLL1^_!BJ &&tyyZ&HHE#J  ,,#++..&&r*G #++..#++..7??3NooG #++..7??3N w,_D1H_TZZ[_M_ 	 II+XY rP   c                p    U R                  U5      n[        USS9nU R                  U15        [        5       $ )Nz*the raise on line {lineno} wasn't executedr  )r  r  r  r!   )r,   r   r  raise_starts       r.   _handle__RaiseAstArcAnalyzer._handle__RaiseJ  s6    !!$'t+WX  +/urP   c                p    U R                  U5      n[        USS9nU R                  U15        [        5       $ )Nz+the return on line {lineno} wasn't executedr  )r  r  r  r!   )r,   r   r  return_starts       r.   _handle__ReturnAstArcAnalyzer._handle__ReturnQ  s6    !!$',YZ!!<.1urP   c                   UR                   (       a  U R                  UR                   S   5      nOS nUR                  (       a  U R                  UR                  S   5      nOS nUc  Uc   e[        X#5      nU R                  R                  U5        U R                  U5      nU R                  UR                  [        U5      S9nUR                  (       a  S Ul	        OU R                  R                  5         [        5       nUR                   (       aK  UR                    H;  nU R                  U5      nSn	[        X)S9n
XpR                  UR                  U
S9-  nM=     UR                  (       a  U R                  UR                  US9nXg-  nUR                  (       a?  U R                  R                  5         UnU R                  UR                  US9nU(       a  UnU$ )Nr   r  z3the exception caught by line {lineno} didn't happenr  )r  )handlersr  	finalbodyr;  rj  r   r  rq   r  r=  r{  r!   r  )r,   r   r=  r>  	try_blockrJ   r  handler_exitshandler_node
from_causer  
final_fromfinal_exitss                r.   _handle__TryAstArcAnalyzer._handle__TryX  s   == ..t}}Q/?@M M>>,,T^^A->?KK (K,CCC]8		*""4(!!$))!H
 >>&*I#  "'*u== $ $ 2 2< @R
%mF
!2!2<3D3DQ[!2!\\	 !. ;;%%dkku%EE>>  "J++DNN
+SK $rP   c                   U R                  UR                  5      =p#[        UR                  5      u  pEU R                  R	                  [        US95        [        USS9nU R                  UR                  US9nU H)  nU R                  UR                  X8R                  5        M+     [        5       nU R                  R                  5       n	[        U	[
        5      (       d   eUR                  U	R                   5        [        USS9nUR"                  (       a   U R                  UR"                  US9n
Xz-  nU$ U(       d  UR%                  U5        U$ )Nr  r  r  r  r  )r  r  rS  rj  r   r#  r  r  rq   r  rW   r  r!   r{  rl   rC   r%  r  rc   )r,   r   rJ   to_topr  r   r  r  r,  r  r  s              r.   _handle__WhileAstArcAnalyzer._handle__While  s   ++DII660;	 78e+Z[
!!$))
!CCLLVYY7 ##'')(I....X))*e+[\
;;**4;;:*NJE
  !		*%rP   c                   [         R                  R                  (       a5  UR                   Vs/ s H  o R	                  UR
                  5      PM     nnOU R	                  U5      /nU H9  nU R                  R                  U5        U R                  R                  U5        M;     U R                  UR                  [        US   5      S9nUS   nU R                  R                  U5        [        U5      1nU(       aN  U HF  nU R                  UR                  U5        U R                  R                  UR                  U45        MH     UnU$ s  snf )Nr  r  )r   
PYBEHAVIORexit_with_through_ctxmgritemsr  context_exprrk  rc   rl  r  rq   r  remover  rW   rn  )r,   r   itemr   rJ   r  	with_exitr,  s           r.   _handle__WithAstArcAnalyzer._handle__With  s   >>22HL

S
(():):;
FSF((./FE$$((/  $$U+  !!$))8L!Mr
  ''.e_%	SZZ/##SZZ$78  E% Ts   $E)rl  r   rj  rk  ro  r   r   re  rd  r"   rm  rn  )
r   ru   rd  ast.ASTr"   r   re  zdict[TLineNo, TLineNo]r   r   r   )r   zdict[TArc, tuple[TArc, TArc]])r   
ast.Moduler   r   )r   ast.FunctionDefr   r   )r   zast.ClassDefr   r   r   r  )r   zIterable[Block])r   r   r   r   )r   r"  r   r   )r   z
ast.Assignr   r   )r   zast.Dictr   r   )r   zast.Listr   r   )r   r!  r   r   )r   r   r   r   )rq   zSequence[ast.AST]r  zArcStart | Noner  zset[ArcStart] | Noner   r   )r  r   r   r   )r   z	ast.Breakr   r   )r   r"  r   r   )r   zast.Continuer   r   )r   zast.Forr   r   )r   zast.Ifr   r   )r   z	ast.Matchr   r   )r   z	ast.Raiser   r   )r   z
ast.Returnr   r   )r   zast.Tryr   r   )r   z	ast.Whiler   r   )r   zast.Withr   r   )/r   r   r   r   r   r/   r   r   r  r  _code_object__AsyncFunctionDefr  r  r  r  r  r  _line__ClassDefr  _line__FunctionDef_line__AsyncFunctionDefr  r  r  r  r  r  r  r  r  r  r  _handle__ClassDefr  r  _handle__AsyncFor_handle__FunctionDef_handle__AsyncFunctionDefr  r  r  r  r  r  r  _handle__AsyncWithr   r   rP   r.   r   r     s   ($F$F $F !	$F
 *$F 
$FL*>( &?"U )-!%]] ] &	]
 ] 
]$*.,. &O	 )-M!L '+,0	%% $% *	%
 
%^" 0 *( %, 1
<5n,, 'rP   r   )r   r   r   ztuple[bool, bool])6r   
__future__r   rj   r   r   rf  r>   r^   r\   collections.abcr   r   dataclassesr   typesr   typingr   r	   r
   r   coverager   coverage.bytecoder   coverage.debugr   coverage.exceptionsr   r   coverage.miscr   r   coverage.phystokensr   coverage.typesr   r   r   rg   r  r  dictri  tupleru   TArcFragmentsr  r#  r/  r;  rS  r   r   rP   r.   <module>r;     s    $ " 
   	 	   . !  5 5  * & 3 3 / (B| |~5* 5*z $d#  $D . T4hsmXc]&B CDDE 8 &E <u &4i' i'rP   