[Python-3000-checkins] r55209 - python/branches/py3k-struni/Lib/linecache.py

guido.van.rossum python-3000-checkins at python.org
Thu May 10 01:24:50 CEST 2007


Author: guido.van.rossum
Date: Thu May 10 01:24:46 2007
New Revision: 55209

Modified:
   python/branches/py3k-struni/Lib/linecache.py
Log:
Support PEP-263-style coding declarations.
Default to UTF-8 per PEP-3120.


Modified: python/branches/py3k-struni/Lib/linecache.py
==============================================================================
--- python/branches/py3k-struni/Lib/linecache.py	(original)
+++ python/branches/py3k-struni/Lib/linecache.py	Thu May 10 01:24:46 2007
@@ -7,6 +7,7 @@
 
 import sys
 import os
+import re
 
 __all__ = ["getline", "clearcache", "checkcache"]
 
@@ -131,6 +132,16 @@
     except IOError 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 = [unicode(line, coding) for line in lines]
+    except UnicodeError:
+        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-3000-checkins mailing list