]> granicus.if.org Git - python/commitdiff
Issue #12451: pydoc: importfile() now opens the Python script in binary mode,
authorVictor Stinner <victor.stinner@haypocalc.com>
Mon, 4 Jul 2011 00:08:50 +0000 (02:08 +0200)
committerVictor Stinner <victor.stinner@haypocalc.com>
Mon, 4 Jul 2011 00:08:50 +0000 (02:08 +0200)
instead of text mode using the locale encoding, to avoid encoding issues.

Lib/pydoc.py
Misc/NEWS

index 1619446c2d66be090987d80617d0ac8445cfd7b7..2533226025ec6a391512ad78d894a89e032a647c 100755 (executable)
@@ -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={}):
index b233650465705c0c580dceb96dd3ea44f053c4a6..5d4ed6fe4444b7edc4d3a082719db7bb58cd7e3e 100644 (file)
--- 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).