diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2011-07-04 00:09:44 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2011-07-04 00:09:44 (GMT) |
commit | 5f9a995ad7cf5d1ec7ceb25c4de66a2f1d491ee4 (patch) | |
tree | 9d01811b78e4aff249057408e249658a43350d06 /Lib | |
parent | 7d8c8a095a19ea63a313c6b937bc2bbcbc7fd6f9 (diff) | |
parent | e975af62f2a5f45307738dbd5220e8590c10f4a4 (diff) | |
download | cpython-5f9a995ad7cf5d1ec7ceb25c4de66a2f1d491ee4.zip cpython-5f9a995ad7cf5d1ec7ceb25c4de66a2f1d491ee4.tar.gz cpython-5f9a995ad7cf5d1ec7ceb25c4de66a2f1d491ee4.tar.bz2 |
(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')
-rwxr-xr-x | Lib/pydoc.py | 26 |
1 files changed, 12 insertions, 14 deletions
diff --git a/Lib/pydoc.py b/Lib/pydoc.py index a39962e..ad6d7c7 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={}): |