blob: 72c7743413453799c614ca3948381b0677a27cf1 [file] [log] [blame]
Fred Drake21572fd1999-06-14 19:47:47 +00001\section{\module{linecache} ---
Fred Drake97c2fa01999-06-17 16:38:18 +00002 Random access to text lines}
Fred Drake21572fd1999-06-14 19:47:47 +00003
4\declaremodule{standard}{linecache}
Fred Drake57657bc2000-12-01 15:25:23 +00005\sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il}
Fred Drake97c2fa01999-06-17 16:38:18 +00006\modulesynopsis{This module provides random access to individual lines
7 from text files.}
Fred Drake21572fd1999-06-14 19:47:47 +00008
9
10The \module{linecache} module allows one to get any line from any file,
11while attempting to optimize internally, using a cache, the common case
Fred Drake97c2fa01999-06-17 16:38:18 +000012where many lines are read from a single file. This is used by the
13\refmodule{traceback} module to retrieve source lines for inclusion in
14the formatted traceback.
Fred Drake21572fd1999-06-14 19:47:47 +000015
16The \module{linecache} module defines the following functions:
17
Phillip J. Eby47032112006-04-11 01:07:43 +000018\begin{funcdesc}{getline}{filename, lineno\optional{, module_globals}}
Fred Drake21572fd1999-06-14 19:47:47 +000019Get line \var{lineno} from file named \var{filename}. This function
Fred Drake38e5d272000-04-03 20:13:55 +000020will never throw an exception --- it will return \code{''} on errors
21(the terminating newline character will be included for lines that are
22found).
Fred Drake21572fd1999-06-14 19:47:47 +000023
24If a file named \var{filename} is not found, the function will look
Fred Drake97c2fa01999-06-17 16:38:18 +000025for it in the module\indexiii{module}{search}{path} search path,
Phillip J. Eby678b8ec2006-04-11 01:15:28 +000026\code{sys.path}, after first checking for a \pep{302} \code{__loader__}
Phillip J. Eby47032112006-04-11 01:07:43 +000027in \var{module_globals}, in case the module was imported from a zipfile
28or other non-filesystem import source.
29
30\versionadded[The \var{module_globals} parameter was added]{2.5}
Fred Drake21572fd1999-06-14 19:47:47 +000031\end{funcdesc}
32
33\begin{funcdesc}{clearcache}{}
Fred Drake38e5d272000-04-03 20:13:55 +000034Clear the cache. Use this function if you no longer need lines from
35files previously read using \function{getline()}.
Fred Drake21572fd1999-06-14 19:47:47 +000036\end{funcdesc}
37
Hye-Shik Chang182ac852004-10-26 09:16:42 +000038\begin{funcdesc}{checkcache}{\optional{filename}}
Fred Drake97c2fa01999-06-17 16:38:18 +000039Check the cache for validity. Use this function if files in the cache
Hye-Shik Chang182ac852004-10-26 09:16:42 +000040may have changed on disk, and you require the updated version. If
Andrew M. Kuchlingf4152c32006-08-04 21:10:03 +000041\var{filename} is omitted, it will check all the entries in the cache.
Fred Drake21572fd1999-06-14 19:47:47 +000042\end{funcdesc}
43
44Example:
45
46\begin{verbatim}
47>>> import linecache
48>>> linecache.getline('/etc/passwd', 4)
Ka-Ping Yeefa004ad2001-01-24 17:19:08 +000049'sys:x:3:3:sys:/dev:/bin/sh\n'
Fred Drake21572fd1999-06-14 19:47:47 +000050\end{verbatim}