summaryrefslogtreecommitdiffstats
path: root/Lib/linecache.py
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2008-12-12 01:33:38 (GMT)
committerBenjamin Peterson <benjamin@python.org>2008-12-12 01:33:38 (GMT)
commitd94726728305dda47726f0b8d9ef9aa4979f6275 (patch)
tree41ec7ce2c25945ebf382476f427aa9255fee90f0 /Lib/linecache.py
parent433f32c3be3b23adc4ec389ff9e78f49c7288f3d (diff)
downloadcpython-d94726728305dda47726f0b8d9ef9aa4979f6275.zip
cpython-d94726728305dda47726f0b8d9ef9aa4979f6275.tar.gz
cpython-d94726728305dda47726f0b8d9ef9aa4979f6275.tar.bz2
reuse tokenize.detect_encoding for linecache #4016
Diffstat (limited to 'Lib/linecache.py')
-rw-r--r--Lib/linecache.py24
1 files changed, 4 insertions, 20 deletions
diff --git a/Lib/linecache.py b/Lib/linecache.py
index 50a0c1b..7f249a0 100644
--- a/Lib/linecache.py
+++ b/Lib/linecache.py
@@ -7,7 +7,7 @@ that name.
import sys
import os
-import re
+import tokenize
__all__ = ["getline", "clearcache", "checkcache"]
@@ -121,27 +121,11 @@ def updatecache(filename, module_globals=None):
pass
else:
# No luck
-## print '*** Cannot stat', filename, ':', msg
return []
-## print("Refreshing cache for %s..." % fullname)
- try:
- fp = open(fullname, 'rU')
+ with open(fullname, 'rb') as fp:
+ coding, line = tokenize.detect_encoding(fp.readline)
+ with open(fullname, 'r', encoding=coding) as fp:
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
cache[filename] = size, mtime, lines, fullname
return lines