Extend linecache.checkcache to preserve the entries Rather than repeating the linecache code, patch it to save the entries, call the original linecache.checkcache() (skipping them), and then restore the saved entries. orig_checkcache is bound at definiti
(filename=None,
orig_checkcache=linecache.checkcache)
| 98 | capture_warnings(True) |
| 99 | |
| 100 | def extended_linecache_checkcache(filename=None, |
| 101 | orig_checkcache=linecache.checkcache): |
| 102 | """Extend linecache.checkcache to preserve the <pyshell#...> entries |
| 103 | |
| 104 | Rather than repeating the linecache code, patch it to save the |
| 105 | <pyshell#...> entries, call the original linecache.checkcache() |
| 106 | (skipping them), and then restore the saved entries. |
| 107 | |
| 108 | orig_checkcache is bound at definition time to the original |
| 109 | method, allowing it to be patched. |
| 110 | """ |
| 111 | cache = linecache.cache |
| 112 | save = {} |
| 113 | for key in list(cache): |
| 114 | if key[:1] + key[-1:] == '<>': |
| 115 | save[key] = cache.pop(key) |
| 116 | orig_checkcache(filename) |
| 117 | cache.update(save) |
| 118 | |
| 119 | # Patch linecache.checkcache(): |
| 120 | linecache.checkcache = extended_linecache_checkcache |