Commit d9472672 authored by Benjamin Peterson's avatar Benjamin Peterson

reuse tokenize.detect_encoding for linecache #4016

parent 433f32c3
...@@ -7,7 +7,7 @@ that name. ...@@ -7,7 +7,7 @@ that name.
import sys import sys
import os import os
import re import tokenize
__all__ = ["getline", "clearcache", "checkcache"] __all__ = ["getline", "clearcache", "checkcache"]
...@@ -121,27 +121,11 @@ def updatecache(filename, module_globals=None): ...@@ -121,27 +121,11 @@ def updatecache(filename, module_globals=None):
pass pass
else: else:
# No luck # No luck
## print '*** Cannot stat', filename, ':', msg
return [] return []
## print("Refreshing cache for %s..." % fullname) with open(fullname, 'rb') as fp:
try: coding, line = tokenize.detect_encoding(fp.readline)
fp = open(fullname, 'rU') with open(fullname, 'r', encoding=coding) as fp:
lines = fp.readlines() lines = fp.readlines()
fp.close()
except Exception as msg:
## print '*** Cannot open', fullname, ':', msg
return []
coding = "utf-8"
for line in lines[:2]:
m = re.search(r"coding[:=]\s*([-\w.]+)", line)
if m:
coding = m.group(1)
break
try:
lines = [line if isinstance(line, str) else str(line, coding)
for line in lines]
except:
pass # Hope for the best
size, mtime = stat.st_size, stat.st_mtime size, mtime = stat.st_size, stat.st_mtime
cache[filename] = size, mtime, lines, fullname cache[filename] = size, mtime, lines, fullname
return lines return lines
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment