]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.10/Lib/linecache.py
1 """Cache lines from files.
3 This is intended to read lines from modules imported -- hence if a filename
4 is not found, it will look down the module search path for a file by
11 __all__
= ["getline", "clearcache", "checkcache"]
13 def getline(filename
, lineno
, module_globals
=None):
14 lines
= getlines(filename
, module_globals
)
15 if 1 <= lineno
<= len(lines
):
16 return lines
[lineno
-1]
23 cache
= {} # The cache
27 """Clear the cache entirely."""
33 def getlines(filename
, module_globals
=None):
34 """Get the lines for a file from the cache.
35 Update the cache if it doesn't contain an entry for this file already."""
38 return cache
[filename
][2]
41 return updatecache(filename
, module_globals
)
47 def checkcache(filename
=None):
48 """Discard cache entries that are out of date.
49 (This is not checked upon each call!)"""
52 filenames
= cache
.keys()
55 filenames
= [filename
]
59 for filename
in filenames
:
60 size
, mtime
, lines
, fullname
= cache
[filename
]
62 continue # no-op for files loaded via a __loader__
64 stat
= os
.stat(fullname
)
68 if size
!= stat
.st_size
or mtime
!= stat
.st_mtime
:
72 def updatecache(filename
, module_globals
=None):
73 """Update a cache entry and return its list of lines.
74 If something's wrong, print a message, discard the cache entry,
75 and return an empty list."""
79 if not filename
or (filename
.startswith('<') and filename
.endswith('>')):
84 stat
= os
.stat(fullname
)
88 # Try for a __loader__, if available
89 if module_globals
and '__loader__' in module_globals
:
90 name
= module_globals
.get('__name__')
91 loader
= module_globals
['__loader__']
92 get_source
= getattr(loader
, 'get_source', None)
94 if name
and get_source
:
96 data
= get_source(name
)
97 except (ImportError, IOError):
101 # No luck, the PEP302 loader cannot find the source
106 [line
+'\n' for line
in data
.splitlines()], fullname
108 return cache
[filename
][2]
110 # Try looking through the module search path, which is only useful
111 # when handling a relative filename.
112 if os
.path
.isabs(filename
):
115 for dirname
in sys
.path
:
116 # When using imputil, sys.path may contain things other than
117 # strings; ignore them when it happens.
119 fullname
= os
.path
.join(dirname
, basename
)
120 except (TypeError, AttributeError):
121 # Not sufficiently string-like to do anything useful with.
124 stat
= os
.stat(fullname
)
131 with
open(fullname
, 'rU') as fp
:
132 lines
= fp
.readlines()
135 if lines
and not lines
[-1].endswith('\n'):
137 size
, mtime
= stat
.st_size
, stat
.st_mtime
138 cache
[filename
] = size
, mtime
, lines
, fullname