[Python-checkins] r67715 - python/branches/py3k/Lib/linecache.py

benjamin.peterson python-checkins at python.org
Fri Dec 12 03:02:24 CET 2008


Author: benjamin.peterson
Date: Fri Dec 12 03:02:24 2008
New Revision: 67715

Log:
revert r67713. it causes build problems

Modified:
   python/branches/py3k/Lib/linecache.py

Modified: python/branches/py3k/Lib/linecache.py
==============================================================================
--- python/branches/py3k/Lib/linecache.py	(original)
+++ python/branches/py3k/Lib/linecache.py	Fri Dec 12 03:02:24 2008
@@ -7,7 +7,7 @@
 
 import sys
 import os
-import tokenize
+import re
 
 __all__ = ["getline", "clearcache", "checkcache"]
 
@@ -121,11 +121,27 @@
                     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


More information about the Python-checkins mailing list