Commit e975af62 authored by Victor Stinner's avatar Victor Stinner

Issue #12451: pydoc: importfile() now opens the Python script in binary mode,

instead of text mode using the locale encoding, to avoid encoding issues.
parent 6c471029
...@@ -256,20 +256,18 @@ class ErrorDuringImport(Exception): ...@@ -256,20 +256,18 @@ class ErrorDuringImport(Exception):
def importfile(path): def importfile(path):
"""Import a Python source file or compiled file given its path.""" """Import a Python source file or compiled file given its path."""
magic = imp.get_magic() magic = imp.get_magic()
file = open(path, 'r') with open(path, 'rb') as file:
if file.read(len(magic)) == magic: if file.read(len(magic)) == magic:
kind = imp.PY_COMPILED kind = imp.PY_COMPILED
else: else:
kind = imp.PY_SOURCE kind = imp.PY_SOURCE
file.close() file.seek(0)
filename = os.path.basename(path) filename = os.path.basename(path)
name, ext = os.path.splitext(filename) name, ext = os.path.splitext(filename)
file = open(path, 'r') try:
try: module = imp.load_module(name, file, path, (ext, 'r', kind))
module = imp.load_module(name, file, path, (ext, 'r', kind)) except:
except: raise ErrorDuringImport(path, sys.exc_info())
raise ErrorDuringImport(path, sys.exc_info())
file.close()
return module return module
def safeimport(path, forceload=0, cache={}): def safeimport(path, forceload=0, cache={}):
......
...@@ -19,6 +19,9 @@ Core and Builtins ...@@ -19,6 +19,9 @@ Core and Builtins
Library Library
------- -------
- Issue #12451: pydoc: importfile() now opens the Python script in binary mode,
instead of text mode using the locale encoding, to avoid encoding issues.
- Issue #12451: runpy: run_path() now opens the Python script in binary mode, - 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 instead of text mode using the locale encoding, to support other encodings
than UTF-8 (scripts using the coding cookie). than UTF-8 (scripts using the coding cookie).
......
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