[Python-checkins] cpython (3.2): Issue #12451: runpy: run_path() now opens the Python script in binary mode,

victor.stinner python-checkins at python.org
Mon Jul 4 01:47:59 CEST 2011


http://hg.python.org/cpython/rev/cd1759711357
changeset:   71164:cd1759711357
branch:      3.2
parent:      71162:81424281ee59
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Mon Jul 04 01:45:39 2011 +0200
summary:
  Issue #12451: runpy: run_path() now opens the Python script in binary mode,
instead of text mode using the locale encoding, to support other encodings than
UTF-8 (scripts using the coding cookie).

files:
  Lib/runpy.py           |   2 +-
  Lib/test/test_runpy.py |  10 ++++++++++
  Misc/NEWS              |   4 ++++
  3 files changed, 15 insertions(+), 1 deletions(-)


diff --git a/Lib/runpy.py b/Lib/runpy.py
--- a/Lib/runpy.py
+++ b/Lib/runpy.py
@@ -226,7 +226,7 @@
         code = read_code(f)
     if code is None:
         # That didn't work, so try it as normal source code
-        with open(fname, "rU") as f:
+        with open(fname, "rb") as f:
             code = compile(f.read(), fname, 'exec')
     return code
 
diff --git a/Lib/test/test_runpy.py b/Lib/test/test_runpy.py
--- a/Lib/test/test_runpy.py
+++ b/Lib/test/test_runpy.py
@@ -405,6 +405,16 @@
             msg = "recursion depth exceeded"
             self.assertRaisesRegex(RuntimeError, msg, run_path, zip_name)
 
+    def test_encoding(self):
+        with temp_dir() as script_dir:
+            filename = os.path.join(script_dir, 'script.py')
+            with open(filename, 'w', encoding='latin1') as f:
+                f.write("""
+#coding:latin1
+"non-ASCII: h\xe9"
+""")
+            result = run_path(filename)
+            self.assertEqual(result['__doc__'], "non-ASCII: h\xe9")
 
 
 def test_main():
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -19,6 +19,10 @@
 Library
 -------
 
+- Issue #12451: runpy: run_path() now opens the Python script in binary mode,
+  instead of text mode using the locale encoding, to support other encodings
+  than UTF-8 (scripts using the coding cookie).
+
 - Issue #12451: xml.dom.pulldom: parse() now opens files in binary mode instead
   of the text mode (using the locale encoding) to avoid encoding issues.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list