Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python2....
File: linecache.py
"""Cache lines from files.
[0] Fix | Delete
[1] Fix | Delete
This is intended to read lines from modules imported -- hence if a filename
[2] Fix | Delete
is not found, it will look down the module search path for a file by
[3] Fix | Delete
that name.
[4] Fix | Delete
"""
[5] Fix | Delete
[6] Fix | Delete
import sys
[7] Fix | Delete
import os
[8] Fix | Delete
[9] Fix | Delete
__all__ = ["getline", "clearcache", "checkcache"]
[10] Fix | Delete
[11] Fix | Delete
def getline(filename, lineno, module_globals=None):
[12] Fix | Delete
lines = getlines(filename, module_globals)
[13] Fix | Delete
if 1 <= lineno <= len(lines):
[14] Fix | Delete
return lines[lineno-1]
[15] Fix | Delete
else:
[16] Fix | Delete
return ''
[17] Fix | Delete
[18] Fix | Delete
[19] Fix | Delete
# The cache
[20] Fix | Delete
[21] Fix | Delete
cache = {} # The cache
[22] Fix | Delete
[23] Fix | Delete
[24] Fix | Delete
def clearcache():
[25] Fix | Delete
"""Clear the cache entirely."""
[26] Fix | Delete
[27] Fix | Delete
global cache
[28] Fix | Delete
cache = {}
[29] Fix | Delete
[30] Fix | Delete
[31] Fix | Delete
def getlines(filename, module_globals=None):
[32] Fix | Delete
"""Get the lines for a file from the cache.
[33] Fix | Delete
Update the cache if it doesn't contain an entry for this file already."""
[34] Fix | Delete
[35] Fix | Delete
if filename in cache:
[36] Fix | Delete
return cache[filename][2]
[37] Fix | Delete
[38] Fix | Delete
try:
[39] Fix | Delete
return updatecache(filename, module_globals)
[40] Fix | Delete
except MemoryError:
[41] Fix | Delete
clearcache()
[42] Fix | Delete
return []
[43] Fix | Delete
[44] Fix | Delete
[45] Fix | Delete
def checkcache(filename=None):
[46] Fix | Delete
"""Discard cache entries that are out of date.
[47] Fix | Delete
(This is not checked upon each call!)"""
[48] Fix | Delete
[49] Fix | Delete
if filename is None:
[50] Fix | Delete
filenames = cache.keys()
[51] Fix | Delete
else:
[52] Fix | Delete
if filename in cache:
[53] Fix | Delete
filenames = [filename]
[54] Fix | Delete
else:
[55] Fix | Delete
return
[56] Fix | Delete
[57] Fix | Delete
for filename in filenames:
[58] Fix | Delete
size, mtime, lines, fullname = cache[filename]
[59] Fix | Delete
if mtime is None:
[60] Fix | Delete
continue # no-op for files loaded via a __loader__
[61] Fix | Delete
try:
[62] Fix | Delete
stat = os.stat(fullname)
[63] Fix | Delete
except os.error:
[64] Fix | Delete
del cache[filename]
[65] Fix | Delete
continue
[66] Fix | Delete
if size != stat.st_size or mtime != stat.st_mtime:
[67] Fix | Delete
del cache[filename]
[68] Fix | Delete
[69] Fix | Delete
[70] Fix | Delete
def updatecache(filename, module_globals=None):
[71] Fix | Delete
"""Update a cache entry and return its list of lines.
[72] Fix | Delete
If something's wrong, print a message, discard the cache entry,
[73] Fix | Delete
and return an empty list."""
[74] Fix | Delete
[75] Fix | Delete
if filename in cache:
[76] Fix | Delete
del cache[filename]
[77] Fix | Delete
if not filename or (filename.startswith('<') and filename.endswith('>')):
[78] Fix | Delete
return []
[79] Fix | Delete
[80] Fix | Delete
fullname = filename
[81] Fix | Delete
try:
[82] Fix | Delete
stat = os.stat(fullname)
[83] Fix | Delete
except OSError:
[84] Fix | Delete
basename = filename
[85] Fix | Delete
[86] Fix | Delete
# Try for a __loader__, if available
[87] Fix | Delete
if module_globals and '__loader__' in module_globals:
[88] Fix | Delete
name = module_globals.get('__name__')
[89] Fix | Delete
loader = module_globals['__loader__']
[90] Fix | Delete
get_source = getattr(loader, 'get_source', None)
[91] Fix | Delete
[92] Fix | Delete
if name and get_source:
[93] Fix | Delete
try:
[94] Fix | Delete
data = get_source(name)
[95] Fix | Delete
except (ImportError, IOError):
[96] Fix | Delete
pass
[97] Fix | Delete
else:
[98] Fix | Delete
if data is None:
[99] Fix | Delete
# No luck, the PEP302 loader cannot find the source
[100] Fix | Delete
# for this module.
[101] Fix | Delete
return []
[102] Fix | Delete
cache[filename] = (
[103] Fix | Delete
len(data), None,
[104] Fix | Delete
[line+'\n' for line in data.splitlines()], fullname
[105] Fix | Delete
)
[106] Fix | Delete
return cache[filename][2]
[107] Fix | Delete
[108] Fix | Delete
# Try looking through the module search path, which is only useful
[109] Fix | Delete
# when handling a relative filename.
[110] Fix | Delete
if os.path.isabs(filename):
[111] Fix | Delete
return []
[112] Fix | Delete
[113] Fix | Delete
for dirname in sys.path:
[114] Fix | Delete
# When using imputil, sys.path may contain things other than
[115] Fix | Delete
# strings; ignore them when it happens.
[116] Fix | Delete
try:
[117] Fix | Delete
fullname = os.path.join(dirname, basename)
[118] Fix | Delete
except (TypeError, AttributeError):
[119] Fix | Delete
# Not sufficiently string-like to do anything useful with.
[120] Fix | Delete
continue
[121] Fix | Delete
try:
[122] Fix | Delete
stat = os.stat(fullname)
[123] Fix | Delete
break
[124] Fix | Delete
except os.error:
[125] Fix | Delete
pass
[126] Fix | Delete
else:
[127] Fix | Delete
return []
[128] Fix | Delete
try:
[129] Fix | Delete
with open(fullname, 'rU') as fp:
[130] Fix | Delete
lines = fp.readlines()
[131] Fix | Delete
except IOError:
[132] Fix | Delete
return []
[133] Fix | Delete
if lines and not lines[-1].endswith('\n'):
[134] Fix | Delete
lines[-1] += '\n'
[135] Fix | Delete
size, mtime = stat.st_size, stat.st_mtime
[136] Fix | Delete
cache[filename] = size, mtime, lines, fullname
[137] Fix | Delete
return lines
[138] Fix | Delete
[139] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function