| 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 re | ||
| 10 | + import tokenize | ||
| 11 | 11 | ||
| 12 | 12 | __all__ = ["getline", "clearcache", "checkcache"] | |
| 13 | 13 | ||
@@ -120,27 +120,11 @@ def updatecache(filename, module_globals=None): | |||
| 120 | 120 | pass | |
| 121 | 121 | else: | |
| 122 | 122 | # No luck | |
| 123 | - ## print '*** Cannot stat', filename, ':', msg | ||
| 124 | 123 | return [] | |
| 125 | - ## print("Refreshing cache for %s..." % fullname) | ||
| 126 | - try: | ||
| 127 | - fp = open(fullname, 'rU') | ||
| 124 | + with open(fullname, 'rb') as fp: | ||
| 125 | + coding, line = tokenize.detect_encoding(fp.readline) | ||
| 126 | + with open(fullname, 'r', encoding=coding) as fp: | ||
| 128 | 127 | lines = fp.readlines() | |
| 129 | - fp.close() | ||
| 130 | - except Exception as msg: | ||
| 131 | - ## print '*** Cannot open', fullname, ':', msg | ||
| 132 | - return [] | ||
| 133 | - coding = "utf-8" | ||
| 134 | - for line in lines[:2]: | ||
| 135 | - m = re.search(r"coding[:=]\s*([-\w.]+)", line) | ||
| 136 | - if m: | ||
| 137 | - coding = m.group(1) | ||
| 138 | - break | ||
| 139 | - try: | ||
| 140 | - lines = [line if isinstance(line, str) else str(line, coding) | ||
| 141 | - for line in lines] | ||
| 142 | - except: | ||
| 143 | - pass # Hope for the best | ||
| 144 | 128 | size, mtime = stat.st_size, stat.st_mtime | |
| 145 | 129 | cache[filename] = size, mtime, lines, fullname | |
| 146 | 130 | return lines | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,7 +27,6 @@ | |||
| 27 | 27 | import re, string, sys | |
| 28 | 28 | from token import * | |
| 29 | 29 | from codecs import lookup, BOM_UTF8 | |
| 30 | - from itertools import chain, repeat | ||
| 31 | 30 | cookie_re = re.compile("coding[:=]\s*([-\w.]+)") | |
| 32 | 31 | ||
| 33 | 32 | import token | |
@@ -327,13 +326,15 @@ def tokenize(readline): | |||
| 327 | 326 | which tells you which encoding was used to decode the bytes stream. | |
| 328 | 327 | """ | |
| 329 | 328 | encoding, consumed = detect_encoding(readline) | |
| 330 | - def readline_generator(): | ||
| 329 | + def readline_generator(consumed): | ||
| 330 | + for line in consumed: | ||
| 331 | + yield line | ||
| 331 | 332 | while True: | |
| 332 | 333 | try: | |
| 333 | 334 | yield readline() | |
| 334 | 335 | except StopIteration: | |
| 335 | 336 | return | |
| 336 | - chained = chain(consumed, readline_generator()) | ||
| 337 | + chained = readline_generator(consumed) | ||
| 337 | 338 | return _tokenize(chained.__next__, encoding) | |
| 338 | 339 | ||
| 339 | 340 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments