@@ -33,10 +33,9 @@ def getlines(filename, module_globals=None):
33
33
"""Get the lines for a Python source file from the cache.
34
34
Update the cache if it doesn't contain an entry for this file already."""
35
35
36
- if filename in cache :
37
- entry = cache [filename ]
38
- if len (entry ) != 1 :
39
- return cache [filename ][2 ]
36
+ entry = cache .get (filename , None )
37
+ if entry is not None and len (entry ) != 1 :
38
+ return entry [2 ]
40
39
41
40
try :
42
41
return updatecache (filename , module_globals )
@@ -56,10 +55,9 @@ def _make_key(code):
56
55
57
56
def _getlines_from_code (code ):
58
57
code_id = _make_key (code )
59
- if code_id in _interactive_cache :
60
- entry = _interactive_cache [code_id ]
61
- if len (entry ) != 1 :
62
- return _interactive_cache [code_id ][2 ]
58
+ entry = _interactive_cache .get (code_id , None )
59
+ if entry is not None and len (entry ) != 1 :
60
+ return entry [2 ]
63
61
return []
64
62
65
63
@@ -84,12 +82,8 @@ def checkcache(filename=None):
84
82
filenames = [filename ]
85
83
86
84
for filename in filenames :
87
- try :
88
- entry = cache [filename ]
89
- except KeyError :
90
- continue
91
-
92
- if len (entry ) == 1 :
85
+ entry = cache .get (filename , None )
86
+ if entry is None or len (entry ) == 1 :
93
87
# lazy cache entry, leave it lazy.
94
88
continue
95
89
size , mtime , lines , fullname = entry
@@ -125,9 +119,7 @@ def updatecache(filename, module_globals=None):
125
119
# These import can fail if the interpreter is shutting down
126
120
return []
127
121
128
- if filename in cache :
129
- if len (cache [filename ]) != 1 :
130
- cache .pop (filename , None )
122
+ entry = cache .pop (filename , None )
131
123
if _source_unavailable (filename ):
132
124
return []
133
125
@@ -146,23 +138,27 @@ def updatecache(filename, module_globals=None):
146
138
147
139
# Realise a lazy loader based lookup if there is one
148
140
# otherwise try to lookup right now.
149
- if lazycache (filename , module_globals ):
141
+ lazy_entry = entry if entry is not None and len (entry ) == 1 else None
142
+ if lazy_entry is None :
143
+ lazy_entry = _make_lazycache_entry (filename , module_globals )
144
+ if lazy_entry is not None :
150
145
try :
151
- data = cache [ filename ] [0 ]()
146
+ data = lazy_entry [0 ]()
152
147
except (ImportError , OSError ):
153
148
pass
154
149
else :
155
150
if data is None :
156
151
# No luck, the PEP302 loader cannot find the source
157
152
# for this module.
158
153
return []
159
- cache [ filename ] = (
154
+ entry = (
160
155
len (data ),
161
156
None ,
162
157
[line + '\n ' for line in data .splitlines ()],
163
158
fullname
164
159
)
165
- return cache [filename ][2 ]
160
+ cache [filename ] = entry
161
+ return entry [2 ]
166
162
167
163
# Try looking through the module search path, which is only useful
168
164
# when handling a relative filename.
@@ -211,13 +207,20 @@ def lazycache(filename, module_globals):
211
207
get_source method must be found, the filename must be a cacheable
212
208
filename, and the filename must not be already cached.
213
209
"""
214
- if filename in cache :
215
- if len (cache [filename ]) == 1 :
216
- return True
217
- else :
218
- return False
210
+ entry = cache .get (filename , None )
211
+ if entry is not None :
212
+ return len (entry ) == 1
213
+
214
+ lazy_entry = _make_lazycache_entry (filename , module_globals )
215
+ if lazy_entry is not None :
216
+ cache [filename ] = lazy_entry
217
+ return True
218
+ return False
219
+
220
+
221
+ def _make_lazycache_entry (filename , module_globals ):
219
222
if not filename or (filename .startswith ('<' ) and filename .endswith ('>' )):
220
- return False
223
+ return None
221
224
# Try for a __loader__, if available
222
225
if module_globals and '__name__' in module_globals :
223
226
spec = module_globals .get ('__spec__' )
@@ -230,9 +233,10 @@ def lazycache(filename, module_globals):
230
233
if name and get_source :
231
234
def get_lines (name = name , * args , ** kwargs ):
232
235
return get_source (name , * args , ** kwargs )
233
- cache [filename ] = (get_lines ,)
234
- return True
235
- return False
236
+ return (get_lines ,)
237
+ return None
238
+
239
+
236
240
237
241
def _register_code (code , string , name ):
238
242
entry = (len (string ),
@@ -245,4 +249,5 @@ def _register_code(code, string, name):
245
249
for const in code .co_consts :
246
250
if isinstance (const , type (code )):
247
251
stack .append (const )
248
- _interactive_cache [_make_key (code )] = entry
252
+ key = _make_key (code )
253
+ _interactive_cache [key ] = entry
0 commit comments