]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Support PEP-263-style coding declarations.
authorGuido van Rossum <guido@python.org>
Wed, 9 May 2007 23:24:46 +0000 (23:24 +0000)
committerGuido van Rossum <guido@python.org>
Wed, 9 May 2007 23:24:46 +0000 (23:24 +0000)
Default to UTF-8 per PEP-3120.

Lib/linecache.py

index 0501a107880d5e00f5227dc4c2215544483c5744..9a16acd2599596af87e4d654c26c41823c91a166 100644 (file)
@@ -7,6 +7,7 @@ that name.
 
 import sys
 import os
+import re
 
 __all__ = ["getline", "clearcache", "checkcache"]
 
@@ -131,6 +132,16 @@ def updatecache(filename, module_globals=None):
     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