[Python-checkins] replace dynamic import with 'exec' with importlib.import_module (#5433)

Gregory P. Smith webhook-mailer at python.org
Mon Jan 29 21:03:04 EST 2018


https://github.com/python/cpython/commit/77526f05fa788d6fb12f2121fe6b96c130d9b717
commit: 77526f05fa788d6fb12f2121fe6b96c130d9b717
branch: master
author: Benjamin Peterson <benjamin at python.org>
committer: Gregory P. Smith <greg at krypto.org>
date: 2018-01-29T18:03:01-08:00
summary:

replace dynamic import with 'exec' with importlib.import_module (#5433)

files:
M Lib/test/test_hashlib.py

diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py
index e4e5280dc8ea..15fc22b02d77 100644
--- a/Lib/test/test_hashlib.py
+++ b/Lib/test/test_hashlib.py
@@ -9,6 +9,7 @@
 import array
 from binascii import unhexlify
 import hashlib
+import importlib
 import itertools
 import os
 import sys
@@ -83,11 +84,11 @@ class HashLibTestCase(unittest.TestCase):
     def _conditional_import_module(self, module_name):
         """Import a module and return a reference to it or None on failure."""
         try:
-            exec('import '+module_name)
-        except ImportError as error:
+            return importlib.import_module(module_name)
+        except ModuleNotFoundError as error:
             if self._warn_on_extension_import:
                 warnings.warn('Did a C extension fail to compile? %s' % error)
-        return locals().get(module_name)
+        return None
 
     def __init__(self, *args, **kwargs):
         algorithms = set()



More information about the Python-checkins mailing list