
    He[                       d Z ddlmZ ddlZddlZddlZddlmZm	Z	 ddl
mZ ddlmZmZ ddlmZmZmZ ddlmZmZmZ dd	lmZ dd
lmZmZ ddlmZ ddlmZm Z m!Z!m"Z" e	rddl
m#Z#  ee      ZddZ$ddZ%ddZ&ddZ'ddZ( G d de      Z)y)z'Python source expertise for coverage.py    )annotationsN)IterableTYPE_CHECKING)env)CoverageExceptionNoSource)canonical_filenamerelative_filenamezip_location)	expensiveisolate_module
join_regex)PythonParser)source_token_linessource_encoding)FileReporter)TArcTLineNoTMorfTSourceTokenLines)Coveragec                    t        | d      5 }|j                         }ddd       j                  dd      j                  dd      S # 1 sw Y   +xY w)zFRead the Python source text from `filename`.

    Returns bytes.

    rbNs   
   
   )openreadreplace)filenamefsources      1/usr/lib/python3/dist-packages/coverage/python.pyread_python_sourcer#      sP     
h	  >>'5)11%?? s   AAc                   t         j                  j                  |       \  }}|dk(  rt        j                  rddg}n|g}|D ]B  }||z   }t         j                  j                  |      rt        |      } n t        |      }|B n t        d|  d      |j                  dd      }|j                  t        |      d      }|r|d   d	k7  r|d	z  }|S )
z#Return the source code, as unicode..pyz.pywzNo source for code: 'z'.       r   
)ospathsplitextr   WINDOWSexistsr#   get_zip_bytesr   r   decoder   )r   baseextextstry_filenamesource_bytesr!   s          r"   get_python_sourcer6   )   s      *ID#
e|vu  =cz77>>,'-l;L %\2#= .xj;<<  ''t4L  !>	JF &*$$M    c                    t        |       }|/|\  }}	 t        j                  |      }	 |j	                  |      }|S y# t        j                  $ r Y yw xY w# t
        $ r Y yw xY w)zGet data from `filename` if it is a zip file path.

    Returns the bytestring data read from the zip file, or None if no zip file
    could be found or `filename` isn't in it.  The data returned will be
    an empty string if the file is empty.

    N)r   	zipimportzipimporterZipImportErrorget_dataOSError)r   zipfile_innerzipfileinnerzidatas         r"   r/   r/   L   s|     !*M &	&&w/B	;;u%D  '' 		  		s    > A AA	A#"A#c                
   | j                  d      r| S | j                  d      r^| dd }t        j                  j                  |      r|S t        j
                  r&|dz   }t        j                  j                  |      r|S |S | S )zReturn the source filename for `filename`.

    Given a file name being traced, return the best guess as to the source
    file to attribute it to.

    r%   )z.pycz.pyoNr(   w)endswithr*   r+   r.   r   r-   )r   py_filenamepyw_filenames      r"   source_for_filerH   c   s{     			+	,sm77>>+&;;&,Lww~~l+## Or7   c                    t        | d      r| j                  r| j                  }n+t        | t        j                        rt        d|  d      | }t        |      }|S )z6Get the source filename for the module-or-file `morf`.__file__zModule z has no file)hasattrrJ   
isinstancetypes
ModuleTyper   rH   )morfr   s     r"   source_for_morfrP      sU    tZ T]]==	D%**	+  '$| <==x(HOr7   c                       e Zd ZdZdd fdZddZddZedd       ZddZ	ddZ
ddZdd	Zedd
       Zedd       Zedd       Z	 d	 	 	 	 	 	 	 ddZddZddZddZ xZS )PythonFileReporterz!Report support for a Python file.c                   || _         t        |      }|}d}| j                   "| j                   j                  j                  rd}|rt	        |      }t
        |   |       t        |d      rt|j                  j                  dt        j                        }t        j                  j                  |      j                  d      r|t        j                  dz   z  }|dz  }nt        |      }|| _        d | _        d | _        d | _        y )NTF__name__.z	__init__.__init__r%   )coveragerP   configrelative_filesr	   superrV   rK   rT   r   r*   sepr+   basename
startswithr
   relname_source_parser	_excluded)selfrO   rW   r   fnamecanonicalizename	__class__s          r"   rV   zPythonFileReporter.__init__   s     "4(==$}}##22$&x0E4$==((bff5Dww)44[A++EMD$X.D#',0r7   c                "    d| j                   dS )Nz<PythonFileReporter >)r   rb   s    r"   __repr__zPythonFileReporter.__repr__   s    %dmm%6a88r7   c                    | j                   S N)r^   ri   s    r"   r
   z$PythonFileReporter.relative_filename   s    ||r7   c                    | j                   J | j                  Ot        | j                  | j                   j	                  d            | _        | j                  j                          | j                  S )z&Lazily create a :class:`PythonParser`.exclude)r   rn   )rW   r`   r   r   _exclude_regexparse_sourceri   s    r"   parserzPythonFileReporter.parser   s_     }}(((<<'44Y?DL LL%%'||r7   c                .    | j                   j                  S z2Return the line numbers of statements in the file.)rq   
statementsri   s    r"   lineszPythonFileReporter.lines   s    {{%%%r7   c                .    | j                   j                  S rs   )rq   excludedri   s    r"   excluded_linesz!PythonFileReporter.excluded_lines   s    {{###r7   c                8    | j                   j                  |      S rl   )rq   translate_lines)rb   ru   s     r"   rz   z"PythonFileReporter.translate_lines   s    {{**511r7   c                8    | j                   j                  |      S rl   )rq   translate_arcs)rb   arcss     r"   r|   z!PythonFileReporter.translate_arcs   s    {{))$//r7   c                    | j                   J | j                  j                  t        | j                   j                  j
                        t        | j                   j                  j                              }|S rl   )rW   rq   lines_matchingr   rX   partial_listpartial_always_list)rb   	no_branchs     r"   no_branch_linesz"PythonFileReporter.no_branch_lines   s^    }}(((KK..t}}++889t}}++??@
	 r7   c                6    | j                   j                         S rl   )rq   r}   ri   s    r"   r}   zPythonFileReporter.arcs   s    {{!!r7   c                6    | j                   j                         S rl   )rq   exit_countsri   s    r"   r   zPythonFileReporter.exit_counts   s    {{&&((r7   c                <    | j                   j                  |||      S rl   )rq   missing_arc_description)rb   startendexecuted_arcss       r"   r   z*PythonFileReporter.missing_arc_description   s     {{225#}MMr7   c                f    | j                   t        | j                        | _         | j                   S rl   )r_   r6   r   ri   s    r"   r!   zPythonFileReporter.source   s&    <<,T]];DL||r7   c                    t         j                  j                  | j                        \  }}|j	                  d      ry|syy)zDoes it seem like this file should contain Python?

        This is used to decide if a file reported as part of the execution of
        a program was really likely to have contained Python in the first
        place.

        r%   TF)r*   r+   r,   r   r]   )rb   _r2   s      r"   should_be_pythonz#PythonFileReporter.should_be_python   s:     !!$--03 >>% r7   c                4    t        | j                               S rl   )r   r!   ri   s    r"   r   z%PythonFileReporter.source_token_lines   s    !$++-00r7   rl   )rO   r   rW   zCoverage | NonereturnNone)r   str)r   r   )r   set[TLineNo])ru   zIterable[TLineNo]r   r   )r}   zIterable[TArc]r   	set[TArc])r   r   )r   zdict[TLineNo, int])r   r   r   r   r   zIterable[TArc] | Noner   r   )r   bool)r   r   )rT   
__module____qualname____doc__rV   rj   r
   propertyrq   ru   rx   rz   r|   r   r   r}   r   r   r!   r   r   __classcell__)rf   s   @r"   rR   rR      s    +69 	 	&$20   " " ) ) 04	NN N -	N
 
N
(1r7   rR   )r   r   r   bytes)r   r   r   r   )r   r   r   zbytes | None)rO   r   r   r   )*r   
__future__r   os.pathr*   rM   r9   typingr   r   rW   r   coverage.exceptionsr   r   coverage.filesr	   r
   r   coverage.miscr   r   r   coverage.parserr   coverage.phystokensr   r   coverage.pluginr   coverage.typesr   r   r   r   r   r#   r6   r/   rH   rP   rR    r7   r"   <module>r      ss    . "    *  ; N N ? ? ( C ( B B!B	@ F.:q1 q1r7   