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)
| 78 | |
| 79 | |
| 80 | def updatecache(filename, module_globals=None): |
| 81 | """Update a cache entry and return its list of lines. |
| 82 | If something's wrong, print a message, discard the cache entry, |
| 83 | and return an empty list.""" |
| 84 | |
| 85 | if filename in cache: |
| 86 | if len(cache[filename]) != 1: |
| 87 | cache.pop(filename, None) |
| 88 | if not filename or (filename.startswith('<') and filename.endswith('>')): |
| 89 | return [] |
| 90 | |
| 91 | fullname = filename |
| 92 | try: |
| 93 | stat = os.stat(fullname) |
| 94 | except OSError: |
| 95 | basename = filename |
| 96 | |
| 97 | # Realise a lazy loader based lookup if there is one |
| 98 | # otherwise try to lookup right now. |
| 99 | if lazycache(filename, module_globals): |
| 100 | try: |
| 101 | data = cache[filename][0]() |
| 102 | except (ImportError, OSError): |
| 103 | pass |
| 104 | else: |
| 105 | if data is None: |
| 106 | # No luck, the PEP302 loader cannot find the source |
| 107 | # for this module. |
| 108 | return [] |
| 109 | cache[filename] = ( |
| 110 | len(data), |
| 111 | None, |
| 112 | [line + '\n' for line in data.splitlines()], |
| 113 | fullname |
| 114 | ) |
| 115 | return cache[filename][2] |
| 116 | |
| 117 | # Try looking through the module search path, which is only useful |
| 118 | # when handling a relative filename. |
| 119 | if os.path.isabs(filename): |
| 120 | return [] |
| 121 | |
| 122 | for dirname in sys.path: |
| 123 | try: |
| 124 | fullname = os.path.join(dirname, basename) |
| 125 | except (TypeError, AttributeError): |
| 126 | # Not sufficiently string-like to do anything useful with. |
| 127 | continue |
| 128 | try: |
| 129 | stat = os.stat(fullname) |
| 130 | break |
| 131 | except OSError: |
| 132 | pass |
| 133 | else: |
| 134 | return [] |
| 135 | try: |
| 136 | with tokenize.open(fullname) as fp: |
| 137 | lines = fp.readlines() |
no test coverage detected