]> granicus.if.org Git - python/commitdiff
[3.6] replace dynamic import with 'exec' with importlib.import_module (GH-5433) ...
authorBenjamin Peterson <benjamin@python.org>
Tue, 30 Jan 2018 06:02:09 +0000 (22:02 -0800)
committerGitHub <noreply@github.com>
Tue, 30 Jan 2018 06:02:09 +0000 (22:02 -0800)
(cherry picked from commit 77526f05fa788d6fb12f2121fe6b96c130d9b717)

Lib/test/test_hashlib.py

index 981748892efb3d144087e974d83f756b350207fc..28613496d111b03fdf2c9acfcaf4fe5216adb63a 100644 (file)
@@ -9,6 +9,7 @@
 import array
 from binascii import unhexlify
 import hashlib
+import importlib
 import itertools
 import os
 import sys
@@ -86,11 +87,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()