[Python-checkins] cpython (merge 3.2 -> default): (merge 3.2) Issue #12451: pydoc: importfile() now opens the Python script in

victor.stinner python-checkins at python.org
Mon Jul 4 02:14:56 CEST 2011


http://hg.python.org/cpython/rev/5ca136dccbf7
changeset:   71167:5ca136dccbf7
parent:      71165:e240af1f0ae1
parent:      71166:a1b4f1716b73
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Mon Jul 04 02:09:44 2011 +0200
summary:
  (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.

files:
  Lib/pydoc.py |  26 ++++++++++++--------------
  Misc/NEWS    |   3 +++
  2 files changed, 15 insertions(+), 14 deletions(-)


diff --git a/Lib/pydoc.py b/Lib/pydoc.py
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -250,20 +250,18 @@
 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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -209,6 +209,9 @@
 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).

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list