|
30 | 30 | import pprint |
31 | 31 | import sys |
32 | 32 | import builtins |
| 33 | +import pkgutil |
33 | 34 | from asyncio import iscoroutinefunction |
34 | 35 | from types import CodeType, ModuleType, MethodType |
35 | 36 | from unittest.util import safe_repr |
@@ -1239,25 +1240,6 @@ class or instance) that acts as the specification for the mock object. If |
1239 | 1240 | """ |
1240 | 1241 |
|
1241 | 1242 |
|
1242 | | -def _dot_lookup(thing, comp, import_path): |
1243 | | - try: |
1244 | | - return getattr(thing, comp) |
1245 | | - except AttributeError: |
1246 | | - __import__(import_path) |
1247 | | - return getattr(thing, comp) |
1248 | | - |
1249 | | - |
1250 | | -def _importer(target): |
1251 | | - components = target.split('.') |
1252 | | - import_path = components.pop(0) |
1253 | | - thing = __import__(import_path) |
1254 | | - |
1255 | | - for comp in components: |
1256 | | - import_path += ".%s" % comp |
1257 | | - thing = _dot_lookup(thing, comp, import_path) |
1258 | | - return thing |
1259 | | - |
1260 | | - |
1261 | 1243 | # _check_spec_arg_typos takes kwargs from commands like patch and checks that |
1262 | 1244 | # they don't contain common misspellings of arguments related to autospeccing. |
1263 | 1245 | def _check_spec_arg_typos(kwargs_to_check): |
@@ -1611,8 +1593,7 @@ def _get_target(target): |
1611 | 1593 | except (TypeError, ValueError): |
1612 | 1594 | raise TypeError("Need a valid target to patch. You supplied: %r" % |
1613 | 1595 | (target,)) |
1614 | | - getter = lambda: _importer(target) |
1615 | | - return getter, attribute |
| 1596 | + return partial(pkgutil.resolve_name, target), attribute |
1616 | 1597 |
|
1617 | 1598 |
|
1618 | 1599 | def _patch_object( |
@@ -1667,7 +1648,7 @@ def _patch_multiple(target, spec=None, create=False, spec_set=None, |
1667 | 1648 | for choosing which methods to wrap. |
1668 | 1649 | """ |
1669 | 1650 | if type(target) is str: |
1670 | | - getter = lambda: _importer(target) |
| 1651 | + getter = partial(pkgutil.resolve_name, target) |
1671 | 1652 | else: |
1672 | 1653 | getter = lambda: target |
1673 | 1654 |
|
@@ -1847,7 +1828,7 @@ def __enter__(self): |
1847 | 1828 | def _patch_dict(self): |
1848 | 1829 | values = self.values |
1849 | 1830 | if isinstance(self.in_dict, str): |
1850 | | - self.in_dict = _importer(self.in_dict) |
| 1831 | + self.in_dict = pkgutil.resolve_name(self.in_dict) |
1851 | 1832 | in_dict = self.in_dict |
1852 | 1833 | clear = self.clear |
1853 | 1834 |
|
|
0 commit comments