From a5bbf200172d52714a64f9294d86c14bd8eda501 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Fri, 12 Dec 2008 02:02:24 +0000 Subject: [PATCH] revert r67713. it causes build problems --- Lib/linecache.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/Lib/linecache.py b/Lib/linecache.py index 7f249a045cf..50a0c1b480c 100644 --- a/Lib/linecache.py +++ b/Lib/linecache.py @@ -7,7 +7,7 @@ that name. import sys import os -import tokenize +import re __all__ = ["getline", "clearcache", "checkcache"] @@ -121,11 +121,27 @@ def updatecache(filename, module_globals=None): pass else: # No luck +## print '*** Cannot stat', filename, ':', msg return [] - with open(fullname, 'rb') as fp: - coding, line = tokenize.detect_encoding(fp.readline) - with open(fullname, 'r', encoding=coding) as fp: +## print("Refreshing cache for %s..." % fullname) + try: + fp = open(fullname, 'rU') 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