Commit 7d8c8a09 authored by Victor Stinner's avatar Victor Stinner

(merge 3.2) 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).
parents 3663abab 6c471029
...@@ -226,7 +226,7 @@ def _get_code_from_file(fname): ...@@ -226,7 +226,7 @@ def _get_code_from_file(fname):
code = read_code(f) code = read_code(f)
if code is None: if code is None:
# That didn't work, so try it as normal source code # That didn't work, so try it as normal source code
with open(fname, "r") as f: with open(fname, "rb") as f:
code = compile(f.read(), fname, 'exec') code = compile(f.read(), fname, 'exec')
return code return code
......
...@@ -405,6 +405,16 @@ argv0 = sys.argv[0] ...@@ -405,6 +405,16 @@ argv0 = sys.argv[0]
msg = "recursion depth exceeded" msg = "recursion depth exceeded"
self.assertRaisesRegex(RuntimeError, msg, run_path, zip_name) 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(): def test_main():
......
...@@ -209,6 +209,10 @@ Core and Builtins ...@@ -209,6 +209,10 @@ Core and Builtins
Library 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 - 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. of the text mode (using the locale encoding) to avoid encoding issues.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment