diff Lib/linecache.py @ 41665:a3248fefc0dc

Support PEP-263-style coding declarations. Default to UTF-8 per PEP-3120.
author Guido van Rossum <guido@python.org>
date Wed, 09 May 2007 23:24:46 +0000
parents 3093ea9b4024
children d07d34b53db5
line wrap: on
line diff
--- a/Lib/linecache.py	Wed May 09 19:52:16 2007 +0000
+++ b/Lib/linecache.py	Wed May 09 23:24:46 2007 +0000
@@ -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