
    Rh                     `    d Z ddlZddlZddlZddlZg dZi Zd Zd
dZd
dZ	d
dZ
d
dZd	 Zy)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
checkcache	lazycachec                  ,    t         j                          y)zClear the cache entirely.N)cacheclear      /usr/lib/python3.12/linecache.pyr   r      s    	KKMr   c                 Z    t        | |      }d|cxk  rt        |      k  rn y||dz
     S y)z~Get a line for a Python source file from the cache.
    Update the cache if it doesn't contain an entry for this file already.    )getlineslen)filenamelinenomodule_globalsliness       r   r   r      s8     X~.EF c%j  VaZ  r   c                     | t         v r#t         |    }t        |      dk7  rt         |    d   S 	 t        | |      S # t        $ r t	                g cY S w xY w)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      )r   r   updatecacheMemoryErrorr   )r   r   entrys      r   r   r   $   sZ     5hu:??1%%8^44 	s   9 AAc                    | t        t        j                               }n| t        v r| g}ny|D ]o  } t        |    }t        |      dk(  r|\  }}}}|%	 t	        j
                  |      }||j                  k7  s||j                  k7  sZt        j                  | d       q y# t        $ r t        j                  | d       Y w xY w)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   s           r   r   r   4   s     &		U	J	 &hu:?',$eUH=	778$D 4<<5DMM#9IIh%&  	IIh%	s   B##CCc                    | t         v r+t        t         |          dk7  rt         j                  | d       | r"| j                  d      r| j	                  d      rg S | }	 t        j                  |      }	 t#        j$                  |      5 }|j'                         }	ddd       	r!|	d   j	                  d      s|	dxx   dz  cc<   |j,                  |j.                  }}
|
||	|ft         | <   |	S # t        $ r  | }t        | |      ru	 t        |    d          }|g cY S t        |      d|j                         D cg c]  }|dz   	 nc c}w c}|ft         | <   t         |    d   cY S # t        t        f$ r Y nw xY wt
        j                  j                  |       rg cY S t        j                  D ]_  }	 t
        j                  j                  ||      }n# t        t         f$ r Y 6w xY w	 t        j                  |      } n# t        $ r Y ]w xY w g cY S Y w xY w# 1 sw Y   xY w# t        t(        t*        f$ r g cY S w xY w)	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   
r   )r   r   r!   
startswithendswithr   r   r    r   
splitlinesImportErrorpathisabssysjoin	TypeErrorAttributeErrortokenizeopen	readlinesUnicodeDecodeErrorSyntaxErrorr"   r#   )r   r   r'   r   basenamedatalinedirnamefpr   r%   r&   s               r   r   r   P   s5   
 5uX1$IIh%++C0X5F5Fs5K	H*wwx T]]8$ 	#LLNE	# U2Y''-b	T	,,%DE5(2E(OLe  ( X~.*Xq)+ < II-1__->?TTD[??	#h Xq))  ) " 77=="Ixx 	G77<<:~. wwx( 	 I	 I(T	# 	#'5 	s   C+ 3H% HH% +HE#HH7E
H#E52H4E55$HH/ GHG"H!G""H&G=;H=	H	HH		HHH"H% %H>=H>c                    | t         v rt        t         |          dk(  ryy| r"| j                  d      r| j                  d      ry|r{d|v rw|j	                  d      }t        |dd	      xs |d   }t        |d
d	      }||j	                  d      }t        |dd	      }|r#|r!t        j                  ||      }|ft         | <   yy)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 cacheable
        filename, and the filename must not be already cached.
    r   TFr)   r*   __name____spec__nameNloader
__loader__
get_source)r   r   r-   r.   getgetattr	functoolspartial)r   r   specrD   rE   rG   	get_liness          r   r   r      s     5uX1$++C0X5F5Fs5K*6!!*-tVT*HnZ.Hx.>#''5FV\48
J!))*d;I(lE(Or   )N)__doc__rJ   r3   r   r7   __all__r   r   r   r   r   r   r   r
   r   r   <module>rP      sD     
 	 
>
 	
 &8@F!r   