B
    `                 @   s  d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl	m
Z
 ddl	mZmZ ddlmZ ddlmZ ddlmZmZmZmZmZ dd	lmZmZmZ dd
lmZmZmZ G dd deZG dd deZ G dd deZ!G dd deZ"G dd deZ#G dd de$ddZ%eddd  e&e'ej()ddZ*G dd deZ+G dd  d eZ,e*rd!gZ-d"d# Z.d&d$d%Z/dS )'zCode parsing for coverage.py.    N)env)range)bytes_to_intsstring_class)code_objects)short_stack)contract
join_regexnew_contract	nice_pairone_of)NoSource	NotPythonStopEverything)compile_unicodegenerate_tokensneuter_encoding_declarationc               @   s   e Zd ZdZedddddZedd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zd ddZdS )!PythonParserzParse code to find executable lines, excluded lines, etc.

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

    zunicode|None)textNc          
   C   s   |s|st d|pd| _|| _| jsxddlm} y|| j| _W n4 tk
rv } ztd| j|f W dd}~X Y nX || _| jd| _	t
 | _t
 | _t
 | _t
 | _t
 | _t
 | _d| _i | _d| _d| _d| _dS )	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.

        z*PythonParser needs either text or filenamez<code>r   )get_python_sourcezNo source for code: '%s': %sN
F)AssertionErrorfilenamer   Zcoverage.pythonr   IOErrorr   excludesplitlinesset
statementsexcludedraw_statementsraw_excludedraw_classdefsraw_docstringsshow_tokens
_multiline_byte_parser	_all_arcs_missing_arc_fragments)selfr   r   r   r   err r+   W/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/coverage/parser.py__init__   s.    
"zPythonParser.__init__c             C   s   | j st| j| jd| _ | j S )zCreate a ByteParser on demand.)r   )r&   
ByteParserr   r   )r)   r+   r+   r,   byte_parser\   s    zPythonParser.byte_parserc             G   s\   t |}tjr|d}t|}t }x.t| jddD ]\}}|	|r8|
| q8W |S )zFind the lines matching one of a list of regexes.

        Returns a set of line numbers, the lines that contain a match for one
        of the regexes in `regexes`.  The entire line needn't match, just a
        part of it.

        utf8   )start)r	   r   PY2decoderecompiler   	enumerater   searchadd)r)   ZregexesZcombinedZregex_cmatchesiltextr+   r+   r,   lines_matchingc   s    


zPythonParser.lines_matchingc             C   sj  | j r| | j | _d}d}d}d}tj}d}d}d}t| j}	x|	D ]\}
}\}}\}}}| jrtdt	j
|
|
t||f||f  |
tjkr|d7 }n0|
tjkr|d8 }n|
tjkr|dkr| j| n|
tjkrV|dkr"|| jkp|}|sT|rT| j| |}d}d}n2|d	kr|r|| jkrBd}|r| j| n||
tjkr|tjkr| jt||d  nL|
tjkr|dk	r||krx"t||d D ]}|| j|< qW d}d}| r$|
t	jkr$d}|dkr$|}|r||krd}|r | j| d}|
}qHW |sD| j| j  tjjrf| jrft | j| jd< dS )
zwParse the source to find the interesting facts about its lines.

        A handful of attributes are updated.

        r   FNTz%10s %5s %-20r %rr1   class:@)!r   r=   r!   tokenINDENTr   r   r$   printtokenizetok_namegetr   DEDENTNAMEr"   r9   OPSTRINGr#   updater   NEWLINEr%   stripCOMMENTr    r/   _find_statementsr   
PYBEHAVIORmodule_firstline_1min)r)   indentZexclude_indentZ	excludingZexcluding_decoratorsZprev_toktype
first_lineemptyZfirst_on_lineZtokgenZtoktypeZttextZslineno_Zelinenor<   Zshould_excludelr+   r+   r,   
_raw_parseu   st    
 




zPythonParser._raw_parsec             C   s0   |dk r| j | |  }n| j ||}|S )z?Return the first line number of the statement including `line`.r   )r%   rF   )r)   liner+   r+   r,   rT      s    zPythonParser.first_linec                s    fdd|D S )zMap the line numbers in `lines` to the correct first line of the
        statement.

        Returns a set of the first lines.

        c                s   h | ]}  |qS r+   )rT   ).0rW   )r)   r+   r,   	<setcomp>   s    z+PythonParser.first_lines.<locals>.<setcomp>r+   )r)   r   r+   )r)   r,   first_lines   s    zPythonParser.first_linesc             C   s
   |  |S )z)Implement `FileReporter.translate_lines`.)r\   )r)   r   r+   r+   r,   translate_lines   s    zPythonParser.translate_linesc                s    fdd|D S )z(Implement `FileReporter.translate_arcs`.c                s$   g | ]\}}  |  |fqS r+   )rT   )rZ   ab)r)   r+   r,   
<listcomp>   s    z/PythonParser.translate_arcs.<locals>.<listcomp>r+   )r)   arcsr+   )r)   r,   translate_arcs   s    zPythonParser.translate_arcsc          
   C   s   y|    W nb tjtfk
rn } z>t|dr6|j}n|jd d }td| j|jd |f W dd}~X Y nX | 	| j
| _| j| jB }| j| }| 	|| | _dS )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.

        linenor1   r   z5Couldn't parse '%s' as Python source: '%s' at line %dN)rX   rD   
TokenErrorIndentationErrorhasattrrc   argsr   r   r\   r!   r   r#   r    r   )r)   r*   rc   ignorestartsr+   r+   r,   parse_source   s    
(
zPythonParser.parse_sourcec             C   s   | j dkr|   | j S )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.

        N)r'   _analyze_ast)r)   r+   r+   r,   ra   	  s    
zPythonParser.arcsc             C   sn   t | j| j| j}|  t | _x>|jD ]4\}}| |}| |}||kr*| j	||f q*W |j
| _dS )zkRun the AstArcAnalyzer and save its results.

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

        N)AstArcAnalyzerr   r    r%   analyzer   r'   ra   rT   r9   missing_arc_fragmentsr(   )r)   Zaaal1l2Zfl1Zfl2r+   r+   r,   rk     s    

zPythonParser._analyze_astc             C   s~   t t}xF|  D ]:\}}|dk r&q|| jkr2q|| jkr>q||  d7  < qW x&| jD ]}||krZ||  d8  < qZW |S )zYGet a count of exits from that each line.

        Excluded lines are excluded.

        r   r1   )collectionsdefaultdictintra   r   r"   )r)   exit_countsro   rp   rW   r+   r+   r,   rt   &  s    


zPythonParser.exit_countsc       
      C   s   | j dkr|   |}|rP|dk rP|| krP||f|krP||f| j krP|| }}| j ||fdg}g }x|D ]\}}|dkr|dk r| |f| j kr| | |S d}nd}|j|d}dj||d}	|dk	r|	d	j|j|dd
7 }	||	 qnW d|S )z5Provide an English sentence describing a missing arc.Nr   )NNz didn't jump to the function exitzdidn't jump to line {lineno})rc   zline {start} {emsg})r2   emsgz, because {smsg})smsgz or )r(   rk   rF   missing_arc_descriptionformatappendjoin)
r)   r2   endZexecuted_arcsZactual_startZfragment_pairsZmsgsrv   ru   msgr+   r+   r,   rw   A  s.    

z$PythonParser.missing_arc_description)NNN)N)__name__
__module____qualname____doc__r   r-   propertyr/   r=   rX   rT   r\   r]   rb   rj   ra   rk   rt   rw   r+   r+   r+   r,   r      s   =a	r   c               @   s<   e Zd ZdZedddddZdd Zd	d
 Zdd ZdS )r.   z3Parse bytecode to understand the structure of code.unicode)r   Nc          
   C   s   || _ |r|| _nLyt||d| _W n8 tk
r\ } ztd||j|jf W d d }~X Y nX x dD ]}t| j|sdtdqdW d S )Nexecz5Couldn't parse '%s' as Python source: '%s' at line %d)	co_lnotabco_firstlinenozsThis implementation of Python doesn't support code analysis.
Run coverage.py under another Python for this command.)	r   coder   SyntaxErrorr   r|   rc   rf   r   )r)   r   r   r   Zsynerrattrr+   r+   r,   r-   l  s    $
zByteParser.__init__c                s    fddt  jD S )z~Iterate over all the code objects nested within this one.

        The iteration includes `self` as its first value.

        c             3   s   | ]}t  j|d V  qdS ))r   N)r.   r   )rZ   c)r)   r+   r,   	<genexpr>  s    z+ByteParser.child_parsers.<locals>.<genexpr>)r   r   )r)   r+   )r)   r,   child_parsers  s    zByteParser.child_parsersc       
      c   s   t | jdr6x| j D ]\}}}|dk	r|V  qW nt| jjddd }t| jjddd }d}| jj}d}xTt||D ]F\}}	|r||kr|V  |}||7 }tjj	r|	dkr|	d8 }	||	7 }q~W ||kr|V  dS )zYield the line numbers possible in this code object.

        Uses co_lnotab described in Python/compile.c to find the
        line numbers.  Produces a sequence: l0, l1, ...
        co_linesNr      r1         )
rf   r   r   r   r   r   zipr   rP   Znegative_lnotab)
r)   rV   rY   byte_incrementsline_incrementsZlast_line_numline_numZbyte_num	byte_incr	line_incrr+   r+   r,   _line_numbers  s(    zByteParser._line_numbersc             c   s.   x(|   D ]}x| D ]
}|V  qW q
W dS )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)   ZbprW   r+   r+   r,   rO     s    zByteParser._find_statements)NN)	r}   r~   r   r   r   r-   r   r   rO   r+   r+   r+   r,   r.   i  s   r.   c               @   s"   e Zd ZdZeeddd ZdS )	LoopBlockz@A block on the block stack representing a `for` or `while` loop.)r2   c             C   s   || _ t | _d S )N)r2   r   break_exits)r)   r2   r+   r+   r,   r-     s    zLoopBlock.__init__N)r}   r~   r   r   r   rs   r-   r+   r+   r+   r,   r     s   r   c               @   s$   e Zd ZdZeeeddd ZdS )FunctionBlockz>A block on the block stack representing a function definition.)r2   namec             C   s   || _ || _d S )N)r2   r   )r)   r2   r   r+   r+   r,   r-     s    zFunctionBlock.__init__N)r}   r~   r   r   r   rs   strr-   r+   r+   r+   r,   r     s   r   c               @   s$   e Zd ZdZeddddd ZdS )TryBlockz6A block on the block stack representing a `try` block.zint|None)handler_startfinal_startc             C   s0   || _ || _t | _t | _t | _t | _d S )N)r   r   r   
break_fromcontinue_fromreturn_from
raise_from)r)   r   r   r+   r+   r,   r-     s    zTryBlock.__init__N)r}   r~   r   r   r   r-   r+   r+   r+   r,   r     s   r   c                   s"   e Zd ZdZd fdd	Z  ZS )ArcStarta  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 `startmsg` 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.

    Nc                s   t t| | ||S )N)superr   __new__)clsrc   cause)	__class__r+   r,   r     s    zArcStart.__new__)N)r}   r~   r   r   r   __classcell__r+   r+   )r   r,   r     s   r   ZArczlineno, cause	ArcStartsc             C   s   t dd | D S )Nc             s   s   | ]}t |tV  qd S )N)
isinstancer   )rZ   xr+   r+   r,   r     s    z<lambda>.<locals>.<genexpr>)all)seqr+   r+   r,   <lambda>      r   ZCOVERAGE_AST_DUMPc               @   s   e Zd ZdZdd ZdS )NodeListzA synthetic fictitious node, containing a sequence of nodes.

    This is used when collapsing optimized if-statements, to represent the
    unconditional execution of one of the clauses.

    c             C   s   || _ |d j| _d S )Nr   )bodyrc   )r)   r   r+   r+   r,   r-      s    zNodeList.__init__N)r}   r~   r   r   r-   r+   r+   r+   r,   r     s   r   c               @   s  e Zd ZdZededdd Zdd Zeeeddjd
dZ	dd Z
eeddd Zdd Zdd ZeZdd ZeZeZdd Zdd Zdddddd d!d"d#d$d%d&hZed'dd(d) Zed*ed'ddkd+d,Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zed'd7d8d9 Zed'd7d:d; Zed'd7d<d= Zed'd7d>d? Z ed'dd@dA Z!ed'ddBdC Z"e"Z#ed'ddDdE Z$ed'ddFdG Z%e%Z&e"Z'e"Z(ed'ddHdI Z)ed'ddJdK Z*ed'ddLdM Z+ed'ddNdO Z,ed'ddPdQ Z-ed'd'd'dRdSdT Z.ed'ddUdV Z/ed'ddWdX Z0ed'ddYdZ Z1ed'dd[d\ Z2e2Z3d]d^ Z4d_d` Z5e5Z6dadb Z7dcdd Z8e8deZ9e8dfZ:e8dgZ;e8dhZ<e=j>re8diZ?d	S )lrl   z>Analyze source text with an AST to find executable code paths.r   )r   r   c                s   t t|| _ fdd|D | _ | _trXtd| j td| j t	| j t
 | _tt| _g | _tttjdd| _d S )Nc                s   h | ]}  ||qS r+   )rF   )rZ   rW   )	multiliner+   r,   r[     s    z*AstArcAnalyzer.__init__.<locals>.<setcomp>zStatements: {}zMultiline map: {}ZCOVERAGE_TRACK_ARCSr   )astparser   	root_noder   r   AST_DUMPrC   rx   ast_dumpr   ra   rq   rr   listrn   block_stackboolrs   osenvironrF   debug)r)   r   r   r   r+   )r   r,   r-     s    
zAstArcAnalyzer.__init__c             C   sB   x<t | jD ],}|jj}t| d| d}|dk	r|| qW dS )zExamine the AST tree from `root_node` to determine possible arcs.

        This sets the `arcs` attribute to be a set of (from, to) line number
        pairs.

        Z_code_object__N)r   walkr   r   r}   getattr)r)   node	node_nameZcode_object_handlerr+   r+   r,   rm   &  s
    zAstArcAnalyzer.analyze)r2   r{   Nc             C   sd   | j r(td|||| ttdd | j||f |dk	sH|dk	r`| j||f ||f dS )z@Add an arc, including message fragments to use if it is missing.z!
Adding arc: ({}, {}): {!r}, {!r}   )limitN)r   rC   rx   r   ra   r9   rn   ry   )r)   r2   r{   rv   ru   r+   r+   r,   add_arc3  s    zAstArcAnalyzer.add_arcc             C   s
   t | jS )z.Yield the blocks in nearest-to-farthest order.)reversedr   )r)   r+   r+   r,   nearest_blocks>  s    zAstArcAnalyzer.nearest_blocks)returnsc             C   s2   |j j}t| d| d}|dk	r(||S |jS dS )z}What is the right line number to use for this node?

        This dispatches to _line__Node functions where needed.

        Z_line__N)r   r}   r   rc   )r)   r   r   handlerr+   r+   r,   line_for_nodeB  s
    zAstArcAnalyzer.line_for_nodec             C   s$   |j }tjjr |jr |jd j }|S )zSCompute first line number for things that can be decorated (classes and functions).r   )rc   r   rP   trace_decorated_defdecorator_list)r)   r   rc   r+   r+   r,   _line_decoratedP  s
    zAstArcAnalyzer._line_decoratedc             C   s   |  |jS )N)r   value)r)   r   r+   r+   r,   _line__AssignX  s    zAstArcAnalyzer._line__Assignc             C   sB   t jdkr8|jr8|jd d k	r*|jd jS |jd jS n|jS d S )N)      r   )r   Z	PYVERSIONkeysrc   values)r)   r   r+   r+   r,   _line__Dict]  s
    zAstArcAnalyzer._line__Dictc             C   s    |j r| |j d S |jS d S )Nr   )Zeltsr   rc   )r)   r   r+   r+   r,   _line__Listl  s    zAstArcAnalyzer._line__Listc             C   s*   t jjrdS |jr"| |jd S dS d S )Nr1   r   )r   rP   rQ   r   r   )r)   r   r+   r+   r,   _line__Moduler  s
    zAstArcAnalyzer._line__ModuleZAssignZAssertZ	AugAssignZDeleteZExecZExprZGlobalZImportZ
ImportFromZNonlocalZPassZPrintr   c             C   s<   |j j}t| d| d}|dk	r(||S t| |hS dS )a  Add the arcs for `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 two exits from line 1: they start at line 3 and line 5.

        Z	_handle__N)r   r}   r   r   r   )r)   r   r   r   r+   r+   r,   add_arcs  s
    
zAstArcAnalyzer.add_arcszfrom_start, prev_startsc             C   s   |dkr|h}xv|D ]n}|  |}| j||}|| jkrX| |}|dkrNq|  |}x|D ]}| |j||j q^W | |}qW |S )au  Add arcs for the body of a compound statement.

        `body` is the body node.  `from_start` is a single `ArcStart` that can
        be the previous line in flow before this body.  `prev_starts` is a set
        of ArcStarts that can be the previous line.  Only one of them should be
        given.

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

        N)	r   r   rF   r   find_non_missing_noder   rc   r   r   )r)   r   
from_startprev_startsZ	body_noderc   rT   Z
prev_startr+   r+   r,   add_body_arcs  s    





zAstArcAnalyzer.add_body_arcsc             C   sP   |  |}| j||}|| jkr&|S t| d|jj d}|rH||}nd}|S )a  Search `node` looking for a child that has not been optimized away.

        This might return the node you started with, or it will work recursively
        to find a child node in self.statements.

        Returns a node, or None if none of the node remains.

        Z
_missing__N)r   r   rF   r   r   r   r}   )r)   r   rc   rT   Z
missing_fnr+   r+   r,   r     s    


z$AstArcAnalyzer.find_non_missing_nodec             C   s2   |  t|j}|r|S |jr.|  t|jS d S )N)r   r   r   orelse)r)   r   Znon_missingr+   r+   r,   _missing__If  s    zAstArcAnalyzer._missing__Ifc             C   sT   g }x*|j D ] }| |}|d k	r|| qW |s8d S t|dkrL|d S t|S )Nr1   r   )r   r   ry   lenr   )r)   r   Znon_missing_childrenchildr+   r+   r,   _missing__NodeList  s    
z!AstArcAnalyzer._missing__NodeListc             C   sV   |  t|j}|sd S t }|j|_t |_|j|j_d|j_|j|_d |_	|S )NTrue)
r   r   r   r   ZWhilerc   Nametestidr   )r)   r   Z
body_nodesZ	new_whiler+   r+   r,   _missing__While  s    

zAstArcAnalyzer._missing__Whilec             C   s.   |j j}|dkrdS |dkr*|jdkr*dS dS )z Is this a compile-time constant?)ZConstantNameConstantNumr   r   )r   FalseNone	__debug__N)r   r}   r   )r)   r   r   r+   r+   r,   is_constant_expr  s    
zAstArcAnalyzer.is_constant_expr)exitsc             C   sR   xL|   D ]@}t|tr(|j| P q
t|tr
|jdk	r
|j| P q
W dS )z0Add arcs due to jumps from `exits` being breaks.N)r   r   r   r   rK   r   r   r   )r)   r   blockr+   r+   r,   process_break_exits  s    
z"AstArcAnalyzer.process_break_exitsc             C   sh   xb|   D ]V}t|tr>x |D ]}| |j|j|j qW P q
t|tr
|jdk	r
|j	
| P q
W dS )z3Add arcs due to jumps from `exits` being continues.N)r   r   r   r   rc   r2   r   r   r   r   rK   )r)   r   r   xitr+   r+   r,   process_continue_exits*  s    

z%AstArcAnalyzer.process_continue_exitsc          
   C   s   x|   D ]}t|trb|jdk	rHx |D ]}| |j|j|j q(W P q|jdk	r|j	| P q
t|t
r
x,|D ]$}| |j|j |jd|j qrW P q
W dS )z0Add arcs due to jumps from `exits` being raises.Nz didn't except from function {!r})r   r   r   r   r   rc   r   r   r   rK   r   r2   rx   r   )r)   r   r   r   r+   r+   r,   process_raise_exits6  s    





z"AstArcAnalyzer.process_raise_exitsc          
   C   st   xn|   D ]b}t|tr2|jdk	r2|j| P q
t|tr
x,|D ]$}| |j|j	 |j
d|j qBW P q
W dS )z1Add arcs due to jumps from `exits` being returns.Nz didn't return from function {!r})r   r   r   r   r   rK   r   r   rc   r2   r   rx   r   )r)   r   r   r   r+   r+   r,   process_return_exitsJ  s    

z#AstArcAnalyzer.process_return_exitsc             C   s(   |  |}t|dd}| |g t S )Nz*the break on line {lineno} wasn't executed)r   )r   r   r   r   )r)   r   hereZbreak_startr+   r+   r,   _handle__Breakb  s    
zAstArcAnalyzer._handle__Breakc             C   s   |j  }}|jrtjjrd}x8|jD ].}| |}|dk	rN||krN| || |}q$W tjjrn| || |}|jr| |jd }| j	||}x0t
|d |D ]}|| jkr| || |}qW t|hS )zBAdd arcs for things that can be decorated (classes and functions).Nr   r1   )rc   r   r   rP   r   r   r   r   r   rF   r   r   r   )r)   r   Z	main_linelastZdec_nodeZ	dec_startZ
body_startrc   r+   r+   r,   _handle_decoratedi  s(    


z AstArcAnalyzer._handle_decoratedc             C   s(   |  |}t|dd}| |g t S )Nz-the continue on line {lineno} wasn't executed)r   )r   r   r   r   )r)   r   r   Zcontinue_startr+   r+   r,   _handle__Continue  s    
z AstArcAnalyzer._handle__Continuec             C   s   |  |j}| jt|d t|dd}| j|j|d}x|D ]}| |j	||j
 q@W | j }|j}t|dd}|jr| j|j|d}||O }n
|| |S )N)r2   z'the loop on line {lineno} never started)r   )r   z)the loop on line {lineno} didn't complete)r   iterr   ry   r   r   r   r   r   rc   r   popr   r   r9   )r)   r   r2   r   r   r   my_block
else_exitsr+   r+   r,   _handle__For  s    



zAstArcAnalyzer._handle__Forc             C   sL   |  |j}t|dd}| j|j|d}t|dd}|| j|j|dO }|S )Nz-the condition on line {lineno} was never true)r   )r   z.the condition on line {lineno} was never false)r   r   r   r   r   r   )r)   r   r2   r   r   r+   r+   r,   _handle__If  s    zAstArcAnalyzer._handle__Ifc             C   s"   |  |}| j|jt|d}|S )N)r   )r   r   r   r   )r)   r   r2   r   r+   r+   r,   _handle__NodeList  s    
z AstArcAnalyzer._handle__NodeListc             C   s(   |  |}t|dd}| |g t S )Nz*the raise on line {lineno} wasn't executed)r   )r   r   r   r   )r)   r   r   Zraise_startr+   r+   r,   _handle__Raise  s    
zAstArcAnalyzer._handle__Raisec             C   s(   |  |}t|dd}| |g t S )Nz+the return on line {lineno} wasn't executed)r   )r   r   r   r   )r)   r   r   Zreturn_startr+   r+   r,   _handle__Return  s    
zAstArcAnalyzer._handle__Returnc             C   s  |j r| |j d }nd }|jr4| |jd }nd }t||}| j| | |}| j|jt|d}|jrd |_	|j rt
 |_n
| j  t
 }|j rd }xT|j D ]J}	| |	}|d k	r| || |}d}
t||
d}|| j|	j|dO }qW |jr| j|j|d}||O }|jr| j  ||jB |jB |jB |jB }| j|j|d}|jrtjjrxD|jD ]:}|j}|jj|d}x|D ]}| |j|| qW qpW |j}n| |j|}| | |jrFtjjr.xD|jD ]:}|j}|jj|d}x|D ]}| |j|| qW qW |j}n| |j|}| | |jrb| | |j| |jrtjjrxD|jD ]:}|j}|jj|d}x|D ]}| |j|| qW q|W |j}n| |j|}| | |r|}|S )Nr   )r   z3the exception caught by line {lineno} didn't happen)r   )r   )rc   )handlersr   	finalbodyr   r   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   rP   Zfinally_jumps_backrc   r   rx   _combine_finally_startsr   r   r   r   )r)   r   r   r   Z	try_blockr2   r   Zhandler_exitsZlast_handler_startZhandler_nodeZ
from_causer   Z
final_fromZfinal_exitsZ
break_linerc   r   Z
final_exitZbreaksZcontinue_lineZ	continuesZreturn_liner   r+   r+   r,   _handle__Try  s    














zAstArcAnalyzer._handle__Try)ri   r   r   c                sV   g }x0t |D ]$}|jdk	r||jj|jd qW d|  fdd|D }|S )a  Helper for building the cause of `finally` branches.

        "finally" clauses might not execute their exits, and the causes could
        be due to a failure to execute any of the exits in the try block. So
        we use the causes from `starts` as the causes for `exits`.
        N)rc   z or c                s   h | ]}t |j qS r+   )r   rc   )rZ   r   )r   r+   r,   r[   A  s    z9AstArcAnalyzer._combine_finally_starts.<locals>.<setcomp>)sortedr   ry   rx   rc   rz   )r)   ri   r   causesr2   r+   )r   r,   r  4  s    

z&AstArcAnalyzer._combine_finally_startsc             C   s   g |_ | |S )N)r  r  )r)   r   r+   r+   r,   _handle__TryExceptD  s    z!AstArcAnalyzer._handle__TryExceptc             C   sb   g |_ g |_|jd }|jjdkrX|j|jkrXt|jdks@t|j|_|j |_ |j|_| |S )Nr   Z	TryExceptr1   )	r  r   r   r   r}   rc   r   r   r  )r)   r   firstr+   r+   r,   _handle__TryFinallyL  s    
z"AstArcAnalyzer._handle__TryFinallyc             C   s   |  |j }}| |j}d}|r6tjs2|dkr6d}tjjrBd}|rV|  |jd }| j	t
|d t|dd}| j|j|d}x|D ]}| |j||j qW t }| j }	||	j t|d	d}|jr| j|j|d}
||
O }n|s|| |S )
NFr   Tr   )r2   z-the condition on line {lineno} was never true)r   )r   z.the condition on line {lineno} was never false)r   r   r   r   PY3rP   Zkeep_constant_testr   r   ry   r   r   r   r   rc   r   r   r   rK   r   r   r9   )r)   r   r2   Zto_topZconstant_testZtop_is_body0r   r   r   r   r   r+   r+   r,   _handle__While]  s0    



zAstArcAnalyzer._handle__Whilec             C   s"   |  |}| j|jt|d}|S )N)r   )r   r   r   r   )r)   r   r2   r   r+   r+   r,   _handle__Withz  s    
zAstArcAnalyzer._handle__Withc             C   sl   |  |}|jrL| j|jt| d}x@|D ]}| |j| |jd q,W n| | | | ||  d S )N)r   zdidn't exit the module)r   r   r   r   r   rc   r   )r)   r   r2   r   r   r+   r+   r,   _code_object__Module  s    

z#AstArcAnalyzer._code_object__Modulec             C   sN   |  |}| jt||jd | j|jt| d}| | | j	  d S )N)r2   r   )r   )
r   r   ry   r   r   r   r   r   r   r   )r)   r   r2   r   r+   r+   r,   _code_object__FunctionDef  s
    

z(AstArcAnalyzer._code_object__FunctionDefc          	   C   s\   |  |}| | | | j|jt|d}x*|D ]"}| |j| |jd|j q2W d S )N)r   z"didn't exit the body of class {!r})	r   r   r   r   r   rc   r   rx   r   )r)   r   r2   r   r   r+   r+   r,   _code_object__ClassDef  s    

z%AstArcAnalyzer._code_object__ClassDefc                s    fdd}|S )zFA function to make methods for online callable _code_object__ methods.c          	      sB   |  |}| | |d d | | || d d | d S )Nzdidn't run the {} on line {}zdidn't finish the {} on line {})r   r   rx   )r)   r   r2   )nounr+   r,   _code_object__oneline_callable  s
    
zPAstArcAnalyzer._make_oneline_code_method.<locals>._code_object__oneline_callabler+   )r  r  r+   )r  r,   _make_oneline_code_method  s    z(AstArcAnalyzer._make_oneline_code_methodlambdazgenerator expressionzdictionary comprehensionzset comprehensionzlist comprehension)NN)NN)@r}   r~   r   r   r   r   r-   rm   rs   r   r   r   r   r   Z_line__ClassDefr   Z_line__FunctionDefZ_line__AsyncFunctionDefr   r   ZOK_TO_DEFAULTr   r   r   r   r   r   r   r   r   r   r   r   r   r   Z_handle__ClassDefr   r   Z_handle__AsyncForZ_handle__FunctionDefZ_handle__AsyncFunctionDefr   r   r   r   r  r  r  r	  r  r  Z_handle__AsyncWithr  r  Z_code_object__AsyncFunctionDefr  r  Z_code_object__LambdaZ_code_object__GeneratorExpZ_code_object__DictCompZ_code_object__SetCompr   r
  Z_code_object__ListCompr+   r+   r+   r,   rl   	  sx   

 	m
rl   ctxc             C   s$   | dg di t  gkp"t| tttfS )z:Is `value` simple enough to be displayed on a single line?Nr+   )r   r   r   rs   float)r   r+   r+   r,   _is_simple_value  s    r  c             C   s  d| }t | tjs.td|| jj|  dS t| dd}|dk	rPd| j}nd}d|| jj|}dd	 t	| D }|std
| nt
|dkrt|d d r|d \}}td||| nt| |d }	x|D ]\}}d|	|}
t|rtd|
| qt |trXtd|
 x|D ]}t||d  q0W td|	 qt|
 t||d  qW td
| dS )zhDump the AST for `node`.

        This recursively walks the AST, printing a readable version.

         z{}<{} {!r}>Nrc   z @ {} z{}<{}{}c             S   s    g | ]\}}|t kr||fqS r+   )SKIP_DUMP_FIELDS)rZ   r   r   r+   r+   r,   r`     s   zast_dump.<locals>.<listcomp>z{}>r1   r   z{} {}: {!r}>z    z{}{}:z{} {!r}z{} [   z{}])r   r   ASTrC   rx   r   r}   r   rc   iter_fieldsr   r  r   r   )r   depthrS   rc   ZlinemarkheadZnamed_fields
field_namer   Znext_indentprefixnr+   r+   r,   r     s<    

r   )r   )0r   r   rq   r   r5   rA   rD   Zcoverager   Zcoverage.backwardr   r   r   Zcoverage.bytecoder   Zcoverage.debugr   Zcoverage.miscr   r	   r
   r   r   r   r   r   Zcoverage.phystokensr   r   r   objectr   r.   r   r   r   
namedtupler   r   rs   r   rF   r   r   rl   r  r  r   r+   r+   r+   r,   <module>   sF     TR

     /