cpython/Doc/library/linecache.rst

50 lines
1.7 KiB
ReStructuredText
Raw Normal View History

2007-08-15 11:28:22 -03:00
:mod:`linecache` --- Random access to text lines
================================================
.. module:: linecache
:synopsis: This module provides random access to individual lines from text files.
.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
The :mod:`linecache` module allows one to get any line from any 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 :mod:`traceback` module
to retrieve source lines for inclusion in the formatted traceback.
The :mod:`linecache` module defines the following functions:
.. function:: getline(filename, lineno, module_globals=None)
2007-08-15 11:28:22 -03:00
Merged revisions 83561,83563,83565-83566,83569,83571,83574-83575,83580,83584,83599,83612,83659,83977,84015-84018,84020,84141 via svnmerge from svn+ssh://svn.python.org/python/branches/py3k ........ r83561 | georg.brandl | 2010-08-02 22:17:50 +0200 (Mo, 02 Aug 2010) | 1 line #4280: remove outdated "versionchecker" tool. ........ r83563 | georg.brandl | 2010-08-02 22:21:21 +0200 (Mo, 02 Aug 2010) | 1 line #9037: add example how to raise custom exceptions from C code. ........ r83565 | georg.brandl | 2010-08-02 22:27:20 +0200 (Mo, 02 Aug 2010) | 1 line #9111: document that do_help() looks at docstrings. ........ r83566 | georg.brandl | 2010-08-02 22:30:57 +0200 (Mo, 02 Aug 2010) | 1 line #9019: remove false (in 3k) claim about Headers updates. ........ r83569 | georg.brandl | 2010-08-02 22:39:35 +0200 (Mo, 02 Aug 2010) | 1 line #7797: be explicit about bytes-oriented interface of base64 functions. ........ r83571 | georg.brandl | 2010-08-02 22:44:34 +0200 (Mo, 02 Aug 2010) | 1 line Clarify that abs() is not a namespace. ........ r83574 | georg.brandl | 2010-08-02 22:47:56 +0200 (Mo, 02 Aug 2010) | 1 line #6867: epoll.register() returns None. ........ r83575 | georg.brandl | 2010-08-02 22:52:10 +0200 (Mo, 02 Aug 2010) | 1 line #9238: zipfile does handle archive comments. ........ r83580 | georg.brandl | 2010-08-02 23:02:36 +0200 (Mo, 02 Aug 2010) | 1 line #8119: fix copy-paste error. ........ r83584 | georg.brandl | 2010-08-02 23:07:14 +0200 (Mo, 02 Aug 2010) | 1 line #9457: fix documentation links for 3.2. ........ r83599 | georg.brandl | 2010-08-02 23:51:18 +0200 (Mo, 02 Aug 2010) | 1 line #9061: warn that single quotes are never escaped. ........ r83612 | georg.brandl | 2010-08-03 00:59:44 +0200 (Di, 03 Aug 2010) | 1 line Fix unicode literal. ........ r83659 | georg.brandl | 2010-08-03 14:06:29 +0200 (Di, 03 Aug 2010) | 1 line Terminology fix: exceptions are raised, except in generator.throw(). ........ r83977 | georg.brandl | 2010-08-13 17:10:49 +0200 (Fr, 13 Aug 2010) | 1 line Fix copy-paste error. ........ r84015 | georg.brandl | 2010-08-14 17:44:34 +0200 (Sa, 14 Aug 2010) | 1 line Add some maintainers. ........ r84016 | georg.brandl | 2010-08-14 17:46:15 +0200 (Sa, 14 Aug 2010) | 1 line Wording fix. ........ r84017 | georg.brandl | 2010-08-14 17:46:59 +0200 (Sa, 14 Aug 2010) | 1 line Typo fix. ........ r84018 | georg.brandl | 2010-08-14 17:48:49 +0200 (Sa, 14 Aug 2010) | 1 line Typo fix. ........ r84020 | georg.brandl | 2010-08-14 17:57:20 +0200 (Sa, 14 Aug 2010) | 1 line Fix format. ........ r84141 | georg.brandl | 2010-08-17 16:11:59 +0200 (Di, 17 Aug 2010) | 1 line Markup nits. ........
2010-10-06 05:35:38 -03:00
Get line *lineno* from file named *filename*. This function will never raise an
2007-08-15 11:28:22 -03:00
exception --- it will return ``''`` on errors (the terminating newline character
will be included for lines that are found).
.. index:: triple: module; search; path
If a file named *filename* is not found, the function will look for it in the
module search path, ``sys.path``, after first checking for a :pep:`302`
``__loader__`` in *module_globals*, in case the module was imported from a
zipfile or other non-filesystem import source.
.. function:: clearcache()
Clear the cache. Use this function if you no longer need lines from files
previously read using :func:`getline`.
.. function:: checkcache(filename=None)
2007-08-15 11:28:22 -03:00
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 *filename* is omitted,
it will check all the entries in the cache.
2007-08-15 11:28:22 -03:00
Example::
>>> import linecache
>>> linecache.getline('/etc/passwd', 4)
'sys:x:3:3:sys:/dev:/bin/sh\n'