[Python-checkins] pydoc.safeimport: Use importlib.import_module instead of __import__ (GH-103118)

brettcannon webhook-mailer at python.org
Wed May 3 19:26:46 EDT 2023


https://github.com/python/cpython/commit/e95dd40aff35775efce4c03bec7d82f03711310b
commit: e95dd40aff35775efce4c03bec7d82f03711310b
branch: main
author: Yuxin Wu <ppwwyyxxc at gmail.com>
committer: brettcannon <brett at python.org>
date: 2023-05-03T16:26:39-07:00
summary:

pydoc.safeimport: Use importlib.import_module instead of __import__ (GH-103118)

files:
M Lib/pydoc.py

diff --git a/Lib/pydoc.py b/Lib/pydoc.py
index b10a5da99402..84e673a7f87f 100755
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -448,7 +448,7 @@ def safeimport(path, forceload=0, cache={}):
                     # Prevent garbage collection.
                     cache[key] = sys.modules[key]
                     del sys.modules[key]
-        module = __import__(path)
+        module = importlib.import_module(path)
     except BaseException as err:
         # Did the error occur before or after the module was found?
         if path in sys.modules:
@@ -463,9 +463,6 @@ def safeimport(path, forceload=0, cache={}):
         else:
             # Some other error occurred during the importing process.
             raise ErrorDuringImport(path, err)
-    for part in path.split('.')[1:]:
-        try: module = getattr(module, part)
-        except AttributeError: return None
     return module
 
 # ---------------------------------------------------- formatter base class



More information about the Python-checkins mailing list