]> granicus.if.org Git - python/commitdiff
replace dynamic import with 'exec' with importlib.import_module (#5433)
authorBenjamin Peterson <benjamin@python.org>
Tue, 30 Jan 2018 02:03:01 +0000 (18:03 -0800)
committerGregory P. Smith <greg@krypto.org>
Tue, 30 Jan 2018 02:03:01 +0000 (18:03 -0800)
Lib/test/test_hashlib.py

index e4e5280dc8ea334d4f4c48e2e7d645d69f3f4b0e..15fc22b02d7779321588c2a3884e627bd3a7044c 100644 (file)
@@ -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()