summaryrefslogtreecommitdiffstats
path: root/Lib/py_compile.py
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2010-03-18 22:37:38 (GMT)
committerBenjamin Peterson <benjamin@python.org>2010-03-18 22:37:38 (GMT)
commit00888934476bf21fc7b0b76b96adf08cee7db57e (patch)
tree832e8d091b83e5d054b8ad157334a66625f6a003 /Lib/py_compile.py
parent1613ed810801df8327ae6f55b7785fec3a9dc6bb (diff)
downloadcpython-00888934476bf21fc7b0b76b96adf08cee7db57e.zip
cpython-00888934476bf21fc7b0b76b96adf08cee7db57e.tar.gz
cpython-00888934476bf21fc7b0b76b96adf08cee7db57e.tar.bz2
kill py_compile's homemade encoding detection in favor of tokenize.detect_encoding() (see #8168)
Diffstat (limited to 'Lib/py_compile.py')
-rw-r--r--Lib/py_compile.py20
1 files changed, 3 insertions, 17 deletions
diff --git a/Lib/py_compile.py b/Lib/py_compile.py
index 10af1bb..8591365 100644
--- a/Lib/py_compile.py
+++ b/Lib/py_compile.py
@@ -7,8 +7,8 @@ import builtins
import imp
import marshal
import os
-import re
import sys
+import tokenize
import traceback
MAGIC = imp.get_magic()
@@ -69,21 +69,6 @@ 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(br".*\bcoding:\s*(\S+)\b", line)
- if m:
- return m.group(1).decode("ascii")
- return default
- finally:
- f.close()
-
def compile(file, cfile=None, dfile=None, doraise=False):
"""Byte-compile one Python source file to Python bytecode.
@@ -119,7 +104,8 @@ def compile(file, cfile=None, dfile=None, doraise=False):
directories).
"""
- encoding = read_encoding(file, "utf-8")
+ with open(file, "rb") as f:
+ encoding = tokenize.detect_encoding(f.readline)[0]
with open(file, encoding=encoding) as f:
try:
timestamp = int(os.fstat(f.fileno()).st_mtime)