MCPcopy Index your code
hub / github.com/python/cpython / updatecache

Function updatecache

Lib/linecache.py:106–197  ·  view source on GitHub ↗

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.

(filename, module_globals=None)

Source from the content-addressed store, hash-verified

104
105
106def updatecache(filename, module_globals=None):
107 """Update a cache entry and return its list of lines.
108 If something's wrong, print a message, discard the cache entry,
109 and return an empty list."""
110
111 # These imports are not at top level because linecache is in the critical
112 # path of the interpreter startup and importing os and sys take a lot of time
113 # and slows down the startup sequence.
114 try:
115 import os
116 import sys
117 import tokenize
118 except ImportError:
119 # These import can fail if the interpreter is shutting down
120 return []
121
122 entry = cache.pop(filename, None)
123 if _source_unavailable(filename):
124 return []
125
126 if filename.startswith('<frozen '):
127 # This is a frozen module, so we need to use the filename
128 # from the module globals.
129 if module_globals is None:
130 return []
131
132 fullname = module_globals.get('__file__')
133 if fullname is None:
134 return []
135 else:
136 fullname = filename
137 try:
138 stat = os.stat(fullname)
139 except OSError:
140 basename = filename
141
142 # Realise a lazy loader based lookup if there is one
143 # otherwise try to lookup right now.
144 lazy_entry = entry if entry is not None and len(entry) == 1 else None
145 if lazy_entry is None:
146 lazy_entry = _make_lazycache_entry(filename, module_globals)
147 if lazy_entry is not None:
148 try:
149 data = lazy_entry[0]()
150 except (ImportError, OSError):
151 pass
152 else:
153 if data is None:
154 # No luck, the PEP302 loader cannot find the source
155 # for this module.
156 return []
157 entry = (
158 len(data),
159 None,
160 [line + '\n' for line in data.splitlines()],
161 fullname
162 )
163 cache[filename] = entry

Callers 1

getlinesFunction · 0.85

Calls 12

_source_unavailableFunction · 0.85
_make_lazycache_entryFunction · 0.85
popMethod · 0.45
startswithMethod · 0.45
getMethod · 0.45
statMethod · 0.45
splitlinesMethod · 0.45
isabsMethod · 0.45
joinMethod · 0.45
openMethod · 0.45
readlinesMethod · 0.45
endswithMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…