diff options
Diffstat (limited to 'devdocs/python~3.12/library%2Flinecache.html')
| -rw-r--r-- | devdocs/python~3.12/library%2Flinecache.html | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Flinecache.html b/devdocs/python~3.12/library%2Flinecache.html new file mode 100644 index 00000000..ce9cfb6e --- /dev/null +++ b/devdocs/python~3.12/library%2Flinecache.html @@ -0,0 +1,21 @@ + <span id="linecache-random-access-to-text-lines"></span><h1>linecache — Random access to text lines</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/linecache.py">Lib/linecache.py</a></p> <p>The <a class="reference internal" href="#module-linecache" title="linecache: Provides random access to individual lines from text files."><code>linecache</code></a> module allows one to get any line from a Python source file, while attempting to optimize internally, using a cache, the common case where many lines are read from a single file. This is used by the <a class="reference internal" href="traceback#module-traceback" title="traceback: Print or retrieve a stack traceback."><code>traceback</code></a> module to retrieve source lines for inclusion in the formatted traceback.</p> <p>The <a class="reference internal" href="tokenize#tokenize.open" title="tokenize.open"><code>tokenize.open()</code></a> function is used to open files. This function uses <a class="reference internal" href="tokenize#tokenize.detect_encoding" title="tokenize.detect_encoding"><code>tokenize.detect_encoding()</code></a> to get the encoding of the file; in the absence of an encoding token, the file encoding defaults to UTF-8.</p> <p>The <a class="reference internal" href="#module-linecache" title="linecache: Provides random access to individual lines from text files."><code>linecache</code></a> module defines the following functions:</p> <dl class="py function"> <dt class="sig sig-object py" id="linecache.getline"> +<code>linecache.getline(filename, lineno, module_globals=None)</code> </dt> <dd> +<p>Get line <em>lineno</em> from file named <em>filename</em>. This function will never raise an exception — it will return <code>''</code> on errors (the terminating newline character will be included for lines that are found).</p> <p id="index-0">If a file named <em>filename</em> is not found, the function first checks for a <span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0302/"><strong>PEP 302</strong></a> <code>__loader__</code> in <em>module_globals</em>. If there is such a loader and it defines a <code>get_source</code> method, then that determines the source lines (if <code>get_source()</code> returns <code>None</code>, then <code>''</code> is returned). Finally, if <em>filename</em> is a relative filename, it is looked up relative to the entries in the module search path, <code>sys.path</code>.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="linecache.clearcache"> +<code>linecache.clearcache()</code> </dt> <dd> +<p>Clear the cache. Use this function if you no longer need lines from files previously read using <a class="reference internal" href="#linecache.getline" title="linecache.getline"><code>getline()</code></a>.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="linecache.checkcache"> +<code>linecache.checkcache(filename=None)</code> </dt> <dd> +<p>Check the cache for validity. Use this function if files in the cache may have changed on disk, and you require the updated version. If <em>filename</em> is omitted, it will check all the entries in the cache.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="linecache.lazycache"> +<code>linecache.lazycache(filename, module_globals)</code> </dt> <dd> +<p>Capture enough detail about a non-file-based module to permit getting its lines later via <a class="reference internal" href="#linecache.getline" title="linecache.getline"><code>getline()</code></a> even if <em>module_globals</em> is <code>None</code> in the later call. This avoids doing I/O until a line is actually needed, without having to carry the module globals around indefinitely.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> </dd> +</dl> <p>Example:</p> <pre data-language="python">>>> import linecache +>>> linecache.getline(linecache.__file__, 8) +'import sys\n' +</pre> <div class="_attribution"> + <p class="_attribution-p"> + © 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br> + <a href="https://docs.python.org/3.12/library/linecache.html" class="_attribution-link">https://docs.python.org/3.12/library/linecache.html</a> + </p> +</div> |
