Back to index

python3.2  3.2.2
Functions | Variables
linecache Namespace Reference

Functions

def getline
def clearcache
def getlines
def checkcache
def updatecache

Variables

list __all__ = ["getline", "clearcache", "checkcache"]
dictionary cache = {}

Detailed Description

Cache lines from 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.

Function Documentation

def linecache.checkcache (   filename = None)
Discard cache entries that are out of date.
(This is not checked upon each call!)

Definition at line 44 of file linecache.py.

00044 
00045 def checkcache(filename=None):
00046     """Discard cache entries that are out of date.
00047     (This is not checked upon each call!)"""
00048 
00049     if filename is None:
00050         filenames = list(cache.keys())
00051     else:
00052         if filename in cache:
00053             filenames = [filename]
00054         else:
00055             return
00056 
00057     for filename in filenames:
00058         size, mtime, lines, fullname = cache[filename]
00059         if mtime is None:
00060             continue   # no-op for files loaded via a __loader__
00061         try:
00062             stat = os.stat(fullname)
00063         except os.error:
00064             del cache[filename]
00065             continue
00066         if size != stat.st_size or mtime != stat.st_mtime:
00067             del cache[filename]
00068 

Here is the caller graph for this function:

Clear the cache entirely.

Definition at line 27 of file linecache.py.

00027 
00028 def clearcache():
00029     """Clear the cache entirely."""
00030 
00031     global cache
00032     cache = {}
00033 

Here is the caller graph for this function:

def linecache.getline (   filename,
  lineno,
  module_globals = None 
)

Definition at line 14 of file linecache.py.

00014 
00015 def getline(filename, lineno, module_globals=None):
00016     lines = getlines(filename, module_globals)
00017     if 1 <= lineno <= len(lines):
00018         return lines[lineno-1]
00019     else:
00020         return ''
00021 
00022 
00023 # The cache

Here is the call graph for this function:

Here is the caller graph for this function:

def linecache.getlines (   filename,
  module_globals = None 
)
Get the lines for a file from the cache.
Update the cache if it doesn't contain an entry for this file already.

Definition at line 34 of file linecache.py.

00034 
00035 def getlines(filename, module_globals=None):
00036     """Get the lines for a file from the cache.
00037     Update the cache if it doesn't contain an entry for this file already."""
00038 
00039     if filename in cache:
00040         return cache[filename][2]
00041     else:
00042         return updatecache(filename, module_globals)
00043 

Here is the call graph for this function:

Here is the caller graph for this function:

def linecache.updatecache (   filename,
  module_globals = None 
)
Update 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.

Definition at line 69 of file linecache.py.

00069 
00070 def updatecache(filename, module_globals=None):
00071     """Update a cache entry and return its list of lines.
00072     If something's wrong, print a message, discard the cache entry,
00073     and return an empty list."""
00074 
00075     if filename in cache:
00076         del cache[filename]
00077     if not filename or (filename.startswith('<') and filename.endswith('>')):
00078         return []
00079 
00080     fullname = filename
00081     try:
00082         stat = os.stat(fullname)
00083     except OSError:
00084         basename = filename
00085 
00086         # Try for a __loader__, if available
00087         if module_globals and '__loader__' in module_globals:
00088             name = module_globals.get('__name__')
00089             loader = module_globals['__loader__']
00090             get_source = getattr(loader, 'get_source', None)
00091 
00092             if name and get_source:
00093                 try:
00094                     data = get_source(name)
00095                 except (ImportError, IOError):
00096                     pass
00097                 else:
00098                     if data is None:
00099                         # No luck, the PEP302 loader cannot find the source
00100                         # for this module.
00101                         return []
00102                     cache[filename] = (
00103                         len(data), None,
00104                         [line+'\n' for line in data.splitlines()], fullname
00105                     )
00106                     return cache[filename][2]
00107 
00108         # Try looking through the module search path, which is only useful
00109         # when handling a relative filename.
00110         if os.path.isabs(filename):
00111             return []
00112 
00113         for dirname in sys.path:
00114             try:
00115                 fullname = os.path.join(dirname, basename)
00116             except (TypeError, AttributeError):
00117                 # Not sufficiently string-like to do anything useful with.
00118                 continue
00119             try:
00120                 stat = os.stat(fullname)
00121                 break
00122             except os.error:
00123                 pass
00124         else:
00125             return []
00126     try:
00127         with tokenize.open(fullname) as fp:
00128             lines = fp.readlines()
00129     except IOError:
00130         return []
00131     if lines and not lines[-1].endswith('\n'):
00132         lines[-1] += '\n'
00133     size, mtime = stat.st_size, stat.st_mtime
00134     cache[filename] = size, mtime, lines, fullname
00135     return lines

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

list linecache.__all__ = ["getline", "clearcache", "checkcache"]

Definition at line 12 of file linecache.py.

Definition at line 24 of file linecache.py.