[Python-checkins] cpython: Issue #18157: stop using imp.load_module() in imp.

brett.cannon python-checkins at python.org
Tue Jun 11 23:09:53 CEST 2013


http://hg.python.org/cpython/rev/2c747be20d05
changeset:   84088:2c747be20d05
user:        Brett Cannon <brett at python.org>
date:        Tue Jun 11 17:09:36 2013 -0400
summary:
  Issue #18157: stop using imp.load_module() in imp.

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


diff --git a/Lib/pydoc.py b/Lib/pydoc.py
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -53,6 +53,7 @@
 
 import builtins
 import imp
+import importlib._bootstrap
 import importlib.machinery
 import inspect
 import io
@@ -269,18 +270,17 @@
     """Import a Python source file or compiled file given its path."""
     magic = imp.get_magic()
     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
+        is_bytecode = magic == file.read(len(magic))
+    filename = os.path.basename(path)
+    name, ext = os.path.splitext(filename)
+    if is_bytecode:
+        loader = importlib._bootstrap.SourcelessFileLoader(name, path)
+    else:
+        loader = importlib._bootstrap.SourceFileLoader(name, path)
+    try:
+        return loader.load_module(name)
+    except:
+        raise ErrorDuringImport(path, sys.exc_info())
 
 def safeimport(path, forceload=0, cache={}):
     """Import a module; handle errors; return None if the module isn't found.
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -120,6 +120,8 @@
 Library
 -------
 
+- Issue #18157: Stop using imp.load_module() in pydoc.
+
 - Issue #16102: Make uuid._netbios_getnode() work again on Python 3.
 
 - Issue #17134: Add ssl.enum_cert_store() as interface to Windows' cert store.

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


More information about the Python-checkins mailing list