[Python-checkins] bpo-44686 replace unittest.mock._importer with pkgutil.resolve_name (GH-18544)

miss-islington webhook-mailer at python.org
Wed Jul 21 07:48:14 EDT 2021


https://github.com/python/cpython/commit/ab7fcc8fbdc11091370deeb000a787fb02f9b13d
commit: ab7fcc8fbdc11091370deeb000a787fb02f9b13d
branch: main
author: Thomas Grainger <tagrain at gmail.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-07-21T04:47:44-07:00
summary:

bpo-44686 replace unittest.mock._importer with pkgutil.resolve_name (GH-18544)



Automerge-Triggered-By: GH:cjw296

files:
A Misc/NEWS.d/next/Library/2021-07-20-19-35-49.bpo-44686.ucCGhu.rst
M Lib/unittest/mock.py

diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 5be91203ec7d7c..ecf84d224fce37 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -30,6 +30,7 @@
 import pprint
 import sys
 import builtins
+import pkgutil
 from asyncio import iscoroutinefunction
 from types import CodeType, ModuleType, MethodType
 from unittest.util import safe_repr
@@ -1239,25 +1240,6 @@ class or instance) that acts as the specification for the mock object. If
     """
 
 
-def _dot_lookup(thing, comp, import_path):
-    try:
-        return getattr(thing, comp)
-    except AttributeError:
-        __import__(import_path)
-        return getattr(thing, comp)
-
-
-def _importer(target):
-    components = target.split('.')
-    import_path = components.pop(0)
-    thing = __import__(import_path)
-
-    for comp in components:
-        import_path += ".%s" % comp
-        thing = _dot_lookup(thing, comp, import_path)
-    return thing
-
-
 # _check_spec_arg_typos takes kwargs from commands like patch and checks that
 # they don't contain common misspellings of arguments related to autospeccing.
 def _check_spec_arg_typos(kwargs_to_check):
@@ -1611,8 +1593,7 @@ def _get_target(target):
     except (TypeError, ValueError):
         raise TypeError("Need a valid target to patch. You supplied: %r" %
                         (target,))
-    getter = lambda: _importer(target)
-    return getter, attribute
+    return partial(pkgutil.resolve_name, target), attribute
 
 
 def _patch_object(
@@ -1667,7 +1648,7 @@ def _patch_multiple(target, spec=None, create=False, spec_set=None,
     for choosing which methods to wrap.
     """
     if type(target) is str:
-        getter = lambda: _importer(target)
+        getter = partial(pkgutil.resolve_name, target)
     else:
         getter = lambda: target
 
@@ -1847,7 +1828,7 @@ def __enter__(self):
     def _patch_dict(self):
         values = self.values
         if isinstance(self.in_dict, str):
-            self.in_dict = _importer(self.in_dict)
+            self.in_dict = pkgutil.resolve_name(self.in_dict)
         in_dict = self.in_dict
         clear = self.clear
 
diff --git a/Misc/NEWS.d/next/Library/2021-07-20-19-35-49.bpo-44686.ucCGhu.rst b/Misc/NEWS.d/next/Library/2021-07-20-19-35-49.bpo-44686.ucCGhu.rst
new file mode 100644
index 00000000000000..d9c78020e4a9ba
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-07-20-19-35-49.bpo-44686.ucCGhu.rst
@@ -0,0 +1 @@
+Replace ``unittest.mock._importer`` with ``pkgutil.resolve_name``.



More information about the Python-checkins mailing list