summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-07-15 14:02:52 (GMT)
committerGuido van Rossum <guido@python.org>2007-07-15 14:02:52 (GMT)
commit88079f472ec290975f10f9bc02b24b67e4df60fa (patch)
tree34ef0a80592f3c29a35522f2e382e6ede124016f
parent827bfd07420ae3f503ade1d60324a82a2e24023f (diff)
downloadcpython-88079f472ec290975f10f9bc02b24b67e4df60fa.zip
cpython-88079f472ec290975f10f9bc02b24b67e4df60fa.tar.gz
cpython-88079f472ec290975f10f9bc02b24b67e4df60fa.tar.bz2
Use the encoding specification when reading the source file.
-rw-r--r--Lib/py_compile.py19
1 files changed, 18 insertions, 1 deletions
diff --git a/Lib/py_compile.py b/Lib/py_compile.py
index bf01296..1d884d0 100644
--- a/Lib/py_compile.py
+++ b/Lib/py_compile.py
@@ -7,6 +7,7 @@ import __builtin__
import imp
import marshal
import os
+import re
import sys
import traceback
@@ -77,6 +78,21 @@ def wr_long(f, x):
(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 @@ def compile(file, cfile=None, dfile=None, doraise=False):
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: