| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,7 +7,7 @@ | |||
| 7 | 7 | ||
| 8 | 8 | import sys | |
| 9 | 9 | import os | |
| 10 | - import tokenize | ||
| 10 | + import re | ||
| 11 | 11 | ||
| 12 | 12 | __all__ = ["getline", "clearcache", "checkcache"] | |
| 13 | 13 | ||
@@ -121,11 +121,27 @@ def updatecache(filename, module_globals=None): | |||
| 121 | 121 | pass | |
| 122 | 122 | else: | |
| 123 | 123 | # No luck | |
| 124 | + ## print '*** Cannot stat', filename, ':', msg | ||
| 124 | 125 | return [] | |
| 125 | - with open(fullname, 'rb') as fp: | ||
| 126 | - coding, line = tokenize.detect_encoding(fp.readline) | ||
| 127 | - with open(fullname, 'r', encoding=coding) as fp: | ||
| 126 | + ## print("Refreshing cache for %s..." % fullname) | ||
| 127 | + try: | ||
| 128 | + fp = open(fullname, 'rU') | ||
| 128 | 129 | lines = fp.readlines() | |
| 130 | + fp.close() | ||
| 131 | + except Exception as msg: | ||
| 132 | + ## print '*** Cannot open', fullname, ':', msg | ||
| 133 | + return [] | ||
| 134 | + coding = "utf-8" | ||
| 135 | + for line in lines[:2]: | ||
| 136 | + m = re.search(r"coding[:=]\s*([-\w.]+)", line) | ||
| 137 | + if m: | ||
| 138 | + coding = m.group(1) | ||
| 139 | + break | ||
| 140 | + try: | ||
| 141 | + lines = [line if isinstance(line, str) else str(line, coding) | ||
| 142 | + for line in lines] | ||
| 143 | + except: | ||
| 144 | + pass # Hope for the best | ||
| 129 | 145 | size, mtime = stat.st_size, stat.st_mtime | |
| 130 | 146 | cache[filename] = size, mtime, lines, fullname | |
| 131 | 147 | return lines | |
| Back | FazBrowse Home | New Git URL |
0 commit comments