o
    g                     @  s&  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	m
Z
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rlddlm&Z& eeZd!ddZ'd"ddZ(d#ddZ)d"ddZ*d$ddZ+G dd  d e Z,dS )%z'Python source expertise for coverage.py    )annotationsN)DictIterableOptionalSet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)Coveragefilenamestrreturnbytesc                 C  sF   t | d}| }W d   n1 sw   Y  |ddddS )zFRead the Python source text from `filename`.

    Returns bytes.

    rbNs   
   
   )openreadreplace)r   fsource r&   O/var/www/html/bloggers_ems/venv/lib/python3.10/site-packages/coverage/python.pyread_python_source   s   
r(   c                 C  s   t j| \}}|dkrtjrddg}n|g}|D ]}|| }t j|r+t|} nt|}|dur5 n	qtd|  d|	dd}|
t|d}|rX|d	 d
krX|d
7 }|S )z#Return the source code, as unicode..pyz.pywNzNo source for code: 'z'.       r#   
)ospathsplitextr   WINDOWSexistsr(   get_zip_bytesr
   r#   decoder   )r   baseextextstry_filenamesource_bytesr%   r&   r&   r'   get_python_source)   s&   
r:   Optional[bytes]c                 C  sh   t | }|dur2|\}}zt|}W n tjy   Y dS w z||}W |S  ty1   Y dS w dS )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datar&   r&   r'   r3   L   s   r3   c                 C  sZ   |  dr| S |  dr+| dd }tj|r|S tjr)|d }t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/   r2   r   r1   )r   py_filenamepyw_filenamer&   r&   r'   source_for_filec   s   

rJ   morfr   c                 C  sD   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)hasattrrL   
isinstancetypes
ModuleTyper	   rJ   )rK   r   r&   r&   r'   source_for_morf   s   rQ   c                      s   e Zd ZdZd7d8 fd	d
Zd9ddZd9ddZed:ddZd;ddZ	d;ddZ
d<ddZd=dd Zed;d!d"Zed>d#d$Zed?d&d'Z	d7d@d-d.Zd9d/d0ZdAd2d3ZdBd5d6Z  ZS )CPythonFileReporterz!Report support for a Python file.NrK   r   coverageOptional[Coverage]r   Nonec                   s   || _ t|}|}d}| j d ur| j jjrd}|rt|}t | t|drE|j	dt
j}t
j|dr@|t
jd 7 }|d7 }nt|}|| _d | _d | _d | _d S )NTF__name__.z	__init__.__init__r)   )rS   rQ   configrelative_filesr   superrX   rM   rV   r#   r.   sepr/   basename
startswithr   relname_source_parser	_excluded)selfrK   rS   r   fnamecanonicalizename	__class__r&   r'   rX      s(   




zPythonFileReporter.__init__r   c                 C  s   d| j dS )Nz<PythonFileReporter >)r   rc   r&   r&   r'   __repr__   s   zPythonFileReporter.__repr__c                 C  s   | j S N)r_   rj   r&   r&   r'   r      s   z$PythonFileReporter.relative_filenamer   c                 C  s@   | j dusJ | jdu rt| j| j dd| _| j  | jS )z&Lazily create a :class:`PythonParser`.Nexclude)r   rm   )rS   ra   r   r   _exclude_regexparse_sourcerj   r&   r&   r'   parser   s   


zPythonFileReporter.parserSet[TLineNo]c                 C     | j jS z2Return the line numbers of statements in the file.)rp   
statementsrj   r&   r&   r'   lines      zPythonFileReporter.linesc                 C  rr   rs   )rp   excludedrj   r&   r&   r'   excluded_lines   rv   z!PythonFileReporter.excluded_linesru   Iterable[TLineNo]c                 C     | j |S rl   )rp   translate_lines)rc   ru   r&   r&   r'   r{         z"PythonFileReporter.translate_linesarcsIterable[TArc]	Set[TArc]c                 C  rz   rl   )rp   translate_arcs)rc   r}   r&   r&   r'   r      r|   z!PythonFileReporter.translate_arcsc                 C  s4   | j d usJ | jt| j jjt| j jj}|S rl   )rS   rp   lines_matchingr   rY   partial_listpartial_always_list)rc   	no_branchr&   r&   r'   no_branch_lines   s   z"PythonFileReporter.no_branch_linesc                 C  
   | j  S rl   )rp   r}   rj   r&   r&   r'   r}         
zPythonFileReporter.arcsDict[TLineNo, int]c                 C  r   rl   )rp   exit_countsrj   r&   r&   r'   r      r   zPythonFileReporter.exit_countsstartr   endexecuted_arcsOptional[Iterable[TArc]]c                 C  s   | j |||S rl   )rp   missing_arc_description)rc   r   r   r   r&   r&   r'   r      s   z*PythonFileReporter.missing_arc_descriptionc                 C  s   | j d u rt| j| _ | j S rl   )r`   r:   r   rj   r&   r&   r'   r%      s   
zPythonFileReporter.sourceboolc                 C  s,   t j| j\}}|drdS |sdS dS )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/   r0   r   r^   )rc   _r6   r&   r&   r'   should_be_python   s   	
z#PythonFileReporter.should_be_pythonr   c                 C  s   t |  S rl   )r   r%   rj   r&   r&   r'   r      r|   z%PythonFileReporter.source_token_linesrl   )rK   r   rS   rT   r   rU   )r   r   )r   r   )r   rq   )ru   ry   r   rq   )r}   r~   r   r   )r   r   )r   r   )r   r   r   r   r   r   r   r   )r   r   )r   r   )rV   
__module____qualname____doc__rX   rk   r   propertyrp   ru   rx   r{   r   r   r   r}   r   r   r%   r   r   __classcell__r&   r&   rg   r'   rR      s,    







rR   )r   r   r   r   )r   r   r   r   )r   r   r   r;   )rK   r   r   r   )-r   
__future__r   os.pathr.   rO   r<   typingr   r   r   r   r   rS   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(   r:   r3   rJ   rQ   rR   r&   r&   r&   r'   <module>   s.   


#

