summaryrefslogtreecommitdiff
path: root/Lib/pydoc.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2011-07-04 02:09:44 +0200
committerVictor Stinner <victor.stinner@haypocalc.com>2011-07-04 02:09:44 +0200
commita5f928c2185ea2c578453497a6e5bf92b25b88d5 (patch)
tree492c4cb407c250a46ea20128db970d921d6c447a /Lib/pydoc.py
parenta7e4c66d6b3ef00cef13b7f9523b7006521668dd (diff)
parent19546c54e19e3d57d3273d9705cee16898a54afd (diff)
downloadcpython-a5f928c2185ea2c578453497a6e5bf92b25b88d5.tar.gz
(merge 3.2) Issue #12451: pydoc: importfile() now opens the Python script in
binary mode, instead of text mode using the locale encoding, to avoid encoding issues.
Diffstat (limited to 'Lib/pydoc.py')
-rwxr-xr-xLib/pydoc.py26
1 files changed, 12 insertions, 14 deletions
diff --git a/Lib/pydoc.py b/Lib/pydoc.py
index a39962e695..ad6d7c7019 100755
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -250,20 +250,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={}):