summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2011-07-04 00:08:50 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2011-07-04 00:08:50 (GMT)
commite975af62f2a5f45307738dbd5220e8590c10f4a4 (patch)
tree0935172a2f824f61f7966dc9e73fff73673b4649
parent6c471029821677e893a203a0127777735ff526c7 (diff)
downloadcpython-e975af62f2a5f45307738dbd5220e8590c10f4a4.zip
cpython-e975af62f2a5f45307738dbd5220e8590c10f4a4.tar.gz
cpython-e975af62f2a5f45307738dbd5220e8590c10f4a4.tar.bz2
Issue #12451: pydoc: importfile() now opens the Python script in binary mode,
instead of text mode using the locale encoding, to avoid encoding issues.
-rwxr-xr-xLib/pydoc.py26
-rw-r--r--Misc/NEWS3
2 files changed, 15 insertions, 14 deletions
diff --git a/Lib/pydoc.py b/Lib/pydoc.py
index 1619446..2533226 100755
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -256,20 +256,18 @@ class ErrorDuringImport(Exception):
def importfile(path):
"""Import a Python source file or compiled file given its path."""
magic = imp.get_magic()
- file = open(path, 'r')
- if file.read(len(magic)) == magic:
- kind = imp.PY_COMPILED
- else:
- kind = imp.PY_SOURCE
- file.close()
- filename = os.path.basename(path)
- name, ext = os.path.splitext(filename)
- file = open(path, 'r')
- try:
- module = imp.load_module(name, file, path, (ext, 'r', kind))
- except:
- raise ErrorDuringImport(path, sys.exc_info())
- file.close()
+ with open(path, 'rb') as file:
+ if file.read(len(magic)) == magic:
+ kind = imp.PY_COMPILED
+ else:
+ kind = imp.PY_SOURCE
+ file.seek(0)
+ filename = os.path.basename(path)
+ name, ext = os.path.splitext(filename)
+ try:
+ module = imp.load_module(name, file, path, (ext, 'r', kind))
+ except:
+ raise ErrorDuringImport(path, sys.exc_info())
return module
def safeimport(path, forceload=0, cache={}):
diff --git a/Misc/NEWS b/Misc/NEWS
index b233650..5d4ed6f 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -19,6 +19,9 @@ Core and Builtins
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,
instead of text mode using the locale encoding, to support other encodings
than UTF-8 (scripts using the coding cookie).