[Python-3000-checkins] r56398 - python/branches/py3k-struni/Lib/py_compile.py

guido.van.rossum python-3000-checkins at python.org
Sun Jul 15 16:02:52 CEST 2007


Author: guido.van.rossum
Date: Sun Jul 15 16:02:52 2007
New Revision: 56398

Modified:
   python/branches/py3k-struni/Lib/py_compile.py
Log:
Use the encoding specification when reading the source file.


Modified: python/branches/py3k-struni/Lib/py_compile.py
==============================================================================
--- python/branches/py3k-struni/Lib/py_compile.py	(original)
+++ python/branches/py3k-struni/Lib/py_compile.py	Sun Jul 15 16:02:52 2007
@@ -7,6 +7,7 @@
 import imp
 import marshal
 import os
+import re
 import sys
 import traceback
 
@@ -77,6 +78,21 @@
                    (x >> 16) & 0xff,
                    (x >> 24) & 0xff]))
 
+def read_encoding(file, default):
+    """Read the first two lines of the file looking for coding: xyzzy."""
+    f = open(file, "rb")
+    try:
+        for i in range(2):
+            line = f.readline()
+            if not line:
+                break
+            m = re.match(r".*\bcoding:\s*(\S+)\b", line)
+            if m:
+                return str(m.group(1))
+        return default
+    finally:
+        f.close()
+
 def compile(file, cfile=None, dfile=None, doraise=False):
     """Byte-compile one Python source file to Python bytecode.
 
@@ -112,7 +128,8 @@
     directories).
 
     """
-    f = open(file, 'U')
+    encoding = read_encoding(file, "utf-8")
+    f = open(file, 'U', encoding=encoding)
     try:
         timestamp = int(os.fstat(f.fileno()).st_mtime)
     except AttributeError:


More information about the Python-3000-checkins mailing list