]> granicus.if.org Git - python/commitdiff
Issue #12451: runpy: run_path() now opens the Python script in binary mode,
authorVictor Stinner <victor.stinner@haypocalc.com>
Sun, 3 Jul 2011 23:45:39 +0000 (01:45 +0200)
committerVictor Stinner <victor.stinner@haypocalc.com>
Sun, 3 Jul 2011 23:45:39 +0000 (01:45 +0200)
instead of text mode using the locale encoding, to support other encodings than
UTF-8 (scripts using the coding cookie).

Lib/runpy.py
Lib/test/test_runpy.py
Misc/NEWS

index 4738df307a6b10ea99076de5ec2996450188e96c..31e5e55cfa0efa15962fcf8a3485f6c3c1447e9e 100644 (file)
@@ -226,7 +226,7 @@ def _get_code_from_file(fname):
         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
 
index ad3ab397f49dc106451cd9191fd7344bd5a0958c..7ffb6af634178d88ecb0dbf348d226a2ffee03aa 100644 (file)
@@ -405,6 +405,16 @@ argv0 = sys.argv[0]
             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():
index 1d7669888f451c8d76defaa5a4c14bdf8ba47be6..b233650465705c0c580dceb96dd3ea44f053c4a6 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -19,6 +19,10 @@ Core and Builtins
 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.