U
    g                     @   sn   d Z ddlZddlZddlZddlZdddgZdddZi add Zddd	Z	dd
dZ
dddZdd ZdS )zCache lines from Python source files.

This is intended to read lines from modules imported -- hence if a filename
is not found, it will look down the module search path for a file by
that name.
    Ngetline
clearcache
checkcachec                 C   s:   t | |}d|  kr"t|kr2n n||d  S dS d S )N    )getlineslen)filenamelinenomodule_globalslines r   /usr/lib/python3.8/linecache.pyr      s    
c                   C   s   i a dS )zClear the cache entirely.N)cacher   r   r   r   r      s    c                 C   sX   | t kr(t |  }t|dkr(t |  d S zt| |W S  tk
rR   t  g  Y S X dS )zGet the lines for a Python source file from the cache.
    Update the cache if it doesn't contain an entry for this file already.r      N)r   r   updatecacheMemoryErrorr   )r	   r   entryr   r   r   r   %   s    r   c              	   C   s   | dkrt t }n| tkr&| g}ndS |D ]} t|  }t|dkrHq.|\}}}}|dkr^q.zt|}W n$ tk
r   t| d Y q.Y nX ||jks||j	kr.t| d q.dS )zUDiscard cache entries that are out of date.
    (This is not checked upon each call!)Nr   )
listr   keysr   osstatOSErrorpopst_sizest_mtime)r	   	filenamesr   sizemtimer   fullnamer   r   r   r   r   5   s&    
c                 C   s  | t kr$tt |  dkr$t | d | r<| dr@| dr@g S | }zt|}W n tk
rn   | }t| |rzt |  d  }W n t	tfk
r   Y nDX |dkrg  Y S t|ddd |
 D |ft | < t |  d  Y S tj| rg  Y S tjD ]d}ztj||}W n ttfk
r0   Y qY nX zt|}W  qjW q tk
r^   Y qX qg  Y S Y nX z"t|}| }W 5 Q R X W n tk
r   g  Y S X |r|d	 d
s|d	  d
7  < |j|j }	}
|	|
||ft | < |S )zUpdate a cache entry and return its list of lines.
    If something's wrong, print a message, discard the cache entry,
    and return an empty list.r   N<>r   c                 S   s   g | ]}|d  qS )
r   ).0liner   r   r   
<listcomp>q   s     zupdatecache.<locals>.<listcomp>r   r"   )r   r   r   
startswithendswithr   r   r   	lazycacheImportError
splitlinespathisabssysjoin	TypeErrorAttributeErrortokenizeopen	readlinesr   r   )r	   r   r   r   basenamedatadirnamefpr   r   r   r   r   r   r   R   s\    
  



r   c                 C   s   | t kr tt |  dkrdS dS | r8| dr<| dr<dS |rd|kr|d}|d }t|dd	}|r|rt||}|ft | < dS dS )
a  Seed the cache for filename with module_globals.

    The module loader will be asked for the source only when getlines is
    called, not immediately.

    If there is an entry in the cache already, it is not altered.

    :return: True if a lazy load is registered in the cache,
        otherwise False. To register such a load a module loader with a
        get_source method must be found, the filename must be a cachable
        filename, and the filename must not be already cached.
    r   TFr    r!   
__loader____name__
get_sourceN)r   r   r'   r(   getgetattr	functoolspartial)r	   r   nameloaderr;   	get_linesr   r   r   r)      s    

r)   )N)N)N)N)__doc__r>   r.   r   r2   __all__r   r   r   r   r   r   r)   r   r   r   r   <module>   s   




A