17db96d56Sopenharmony_ci:mod:`linecache` --- Random access to text lines
27db96d56Sopenharmony_ci================================================
37db96d56Sopenharmony_ci
47db96d56Sopenharmony_ci.. module:: linecache
57db96d56Sopenharmony_ci   :synopsis: Provides random access to individual lines from text files.
67db96d56Sopenharmony_ci
77db96d56Sopenharmony_ci.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
87db96d56Sopenharmony_ci
97db96d56Sopenharmony_ci**Source code:** :source:`Lib/linecache.py`
107db96d56Sopenharmony_ci
117db96d56Sopenharmony_ci--------------
127db96d56Sopenharmony_ci
137db96d56Sopenharmony_ciThe :mod:`linecache` module allows one to get any line from a Python source file, while
147db96d56Sopenharmony_ciattempting to optimize internally, using a cache, the common case where many
157db96d56Sopenharmony_cilines are read from a single file.  This is used by the :mod:`traceback` module
167db96d56Sopenharmony_cito retrieve source lines for inclusion in  the formatted traceback.
177db96d56Sopenharmony_ci
187db96d56Sopenharmony_ciThe :func:`tokenize.open` function is used to open files. This
197db96d56Sopenharmony_cifunction uses :func:`tokenize.detect_encoding` to get the encoding of the
207db96d56Sopenharmony_cifile; in the absence of an encoding token, the file encoding defaults to UTF-8.
217db96d56Sopenharmony_ci
227db96d56Sopenharmony_ciThe :mod:`linecache` module defines the following functions:
237db96d56Sopenharmony_ci
247db96d56Sopenharmony_ci
257db96d56Sopenharmony_ci.. function:: getline(filename, lineno, module_globals=None)
267db96d56Sopenharmony_ci
277db96d56Sopenharmony_ci   Get line *lineno* from file named *filename*. This function will never raise an
287db96d56Sopenharmony_ci   exception --- it will return ``''`` on errors (the terminating newline character
297db96d56Sopenharmony_ci   will be included for lines that are found).
307db96d56Sopenharmony_ci
317db96d56Sopenharmony_ci   .. index:: triple: module; search; path
327db96d56Sopenharmony_ci
337db96d56Sopenharmony_ci   If a file named *filename* is not found, the function first checks
347db96d56Sopenharmony_ci   for a :pep:`302` ``__loader__`` in *module_globals*.
357db96d56Sopenharmony_ci   If there is such a loader and it defines a ``get_source`` method,
367db96d56Sopenharmony_ci   then that determines the source lines
377db96d56Sopenharmony_ci   (if ``get_source()`` returns ``None``, then ``''`` is returned).
387db96d56Sopenharmony_ci   Finally, if *filename* is a relative filename,
397db96d56Sopenharmony_ci   it is looked up relative to the entries in the module search path, ``sys.path``.
407db96d56Sopenharmony_ci
417db96d56Sopenharmony_ci
427db96d56Sopenharmony_ci.. function:: clearcache()
437db96d56Sopenharmony_ci
447db96d56Sopenharmony_ci   Clear the cache.  Use this function if you no longer need lines from files
457db96d56Sopenharmony_ci   previously read using :func:`getline`.
467db96d56Sopenharmony_ci
477db96d56Sopenharmony_ci
487db96d56Sopenharmony_ci.. function:: checkcache(filename=None)
497db96d56Sopenharmony_ci
507db96d56Sopenharmony_ci   Check the cache for validity.  Use this function if files in the cache  may have
517db96d56Sopenharmony_ci   changed on disk, and you require the updated version.  If *filename* is omitted,
527db96d56Sopenharmony_ci   it will check all the entries in the cache.
537db96d56Sopenharmony_ci
547db96d56Sopenharmony_ci.. function:: lazycache(filename, module_globals)
557db96d56Sopenharmony_ci
567db96d56Sopenharmony_ci   Capture enough detail about a non-file-based module to permit getting its
577db96d56Sopenharmony_ci   lines later via :func:`getline` even if *module_globals* is ``None`` in the later
587db96d56Sopenharmony_ci   call. This avoids doing I/O until a line is actually needed, without having
597db96d56Sopenharmony_ci   to carry the module globals around indefinitely.
607db96d56Sopenharmony_ci
617db96d56Sopenharmony_ci   .. versionadded:: 3.5
627db96d56Sopenharmony_ci
637db96d56Sopenharmony_ciExample::
647db96d56Sopenharmony_ci
657db96d56Sopenharmony_ci   >>> import linecache
667db96d56Sopenharmony_ci   >>> linecache.getline(linecache.__file__, 8)
677db96d56Sopenharmony_ci   'import sys\n'
68