Revert "bpo-44686 replace unittest.mock._importer with pkgutil.resolv… · testing-cabal/mock@bc04ea7 (original) (raw)

`@@ -30,7 +30,6 @@

`

30

30

`import pprint

`

31

31

`import sys

`

32

32

`import builtins

`

33

``

`-

import pkgutil

`

34

33

`from asyncio import iscoroutinefunction

`

35

34

`from types import CodeType, ModuleType, MethodType

`

36

35

`from unittest.util import safe_repr

`

`@@ -1250,6 +1249,25 @@ class or instance) that acts as the specification for the mock object. If

`

1250

1249

` """

`

1251

1250

``

1252

1251

``

``

1252

`+

def _dot_lookup(thing, comp, import_path):

`

``

1253

`+

try:

`

``

1254

`+

return getattr(thing, comp)

`

``

1255

`+

except AttributeError:

`

``

1256

`+

import(import_path)

`

``

1257

`+

return getattr(thing, comp)

`

``

1258

+

``

1259

+

``

1260

`+

def _importer(target):

`

``

1261

`+

components = target.split('.')

`

``

1262

`+

import_path = components.pop(0)

`

``

1263

`+

thing = import(import_path)

`

``

1264

+

``

1265

`+

for comp in components:

`

``

1266

`+

import_path += ".%s" % comp

`

``

1267

`+

thing = _dot_lookup(thing, comp, import_path)

`

``

1268

`+

return thing

`

``

1269

+

``

1270

+

1253

1271

`# _check_spec_arg_typos takes kwargs from commands like patch and checks that

`

1254

1272

`# they don't contain common misspellings of arguments related to autospeccing.

`

1255

1273

`def _check_spec_arg_typos(kwargs_to_check):

`

`@@ -1603,7 +1621,8 @@ def _get_target(target):

`

1603

1621

`except (TypeError, ValueError):

`

1604

1622

`raise TypeError("Need a valid target to patch. You supplied: %r" %

`

1605

1623

` (target,))

`

1606

``

`-

return partial(pkgutil.resolve_name, target), attribute

`

``

1624

`+

getter = lambda: _importer(target)

`

``

1625

`+

return getter, attribute

`

1607

1626

``

1608

1627

``

1609

1628

`def _patch_object(

`

`@@ -1658,7 +1677,7 @@ def _patch_multiple(target, spec=None, create=False, spec_set=None,

`

1658

1677

` for choosing which methods to wrap.

`

1659

1678

` """

`

1660

1679

`if type(target) is str:

`

1661

``

`-

getter = partial(pkgutil.resolve_name, target)

`

``

1680

`+

getter = lambda: _importer(target)

`

1662

1681

`else:

`

1663

1682

`getter = lambda: target

`

1664

1683

``

`@@ -1838,7 +1857,7 @@ def enter(self):

`

1838

1857

`def _patch_dict(self):

`

1839

1858

`values = self.values

`

1840

1859

`if isinstance(self.in_dict, str):

`

1841

``

`-

self.in_dict = pkgutil.resolve_name(self.in_dict)

`

``

1860

`+

self.in_dict = _importer(self.in_dict)

`

1842

1861

`in_dict = self.in_dict

`

1843

1862

`clear = self.clear

`

1844

1863

``