From 8bd14fb398b1b89c82defdac6c5755c9ca86859b Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Thu, 8 Nov 2007 16:34:32 +0000 Subject: [PATCH] Fixed #1403 where compileall and py_compile choked on an encoding header in a py file. Both modules need more unit tests. --- Lib/io.py | 3 +++ Lib/py_compile.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Lib/io.py b/Lib/io.py index b305b53bb0b..c2f5d3ebfaa 100644 --- a/Lib/io.py +++ b/Lib/io.py @@ -1063,6 +1063,9 @@ class TextIOWrapper(TextIOBase): else: encoding = locale.getpreferredencoding() + if not isinstance(encoding, str): + raise ValueError("invalid encoding: %r" % encoding) + self.buffer = buffer self._encoding = encoding self._readuniversal = not newline diff --git a/Lib/py_compile.py b/Lib/py_compile.py index f7a200204ca..912972bcac2 100644 --- a/Lib/py_compile.py +++ b/Lib/py_compile.py @@ -88,7 +88,7 @@ def read_encoding(file, default): break m = re.match(r".*\bcoding:\s*(\S+)\b", line) if m: - return str(m.group(1)) + return m.group(1).decode("ascii") return default finally: f.close()