Perform the patch.
(self)
| 1423 | |
| 1424 | |
| 1425 | def __enter__(self): |
| 1426 | """Perform the patch.""" |
| 1427 | new, spec, spec_set = self.new, self.spec, self.spec_set |
| 1428 | autospec, kwargs = self.autospec, self.kwargs |
| 1429 | new_callable = self.new_callable |
| 1430 | self.target = self.getter() |
| 1431 | |
| 1432 | # normalise False to None |
| 1433 | if spec is False: |
| 1434 | spec = None |
| 1435 | if spec_set is False: |
| 1436 | spec_set = None |
| 1437 | if autospec is False: |
| 1438 | autospec = None |
| 1439 | |
| 1440 | if spec is not None and autospec is not None: |
| 1441 | raise TypeError("Can't specify spec and autospec") |
| 1442 | if ((spec is not None or autospec is not None) and |
| 1443 | spec_set not in (True, None)): |
| 1444 | raise TypeError("Can't provide explicit spec_set *and* spec or autospec") |
| 1445 | |
| 1446 | original, local = self.get_original() |
| 1447 | |
| 1448 | if new is DEFAULT and autospec is None: |
| 1449 | inherit = False |
| 1450 | if spec is True: |
| 1451 | # set spec to the object we are replacing |
| 1452 | spec = original |
| 1453 | if spec_set is True: |
| 1454 | spec_set = original |
| 1455 | spec = None |
| 1456 | elif spec is not None: |
| 1457 | if spec_set is True: |
| 1458 | spec_set = spec |
| 1459 | spec = None |
| 1460 | elif spec_set is True: |
| 1461 | spec_set = original |
| 1462 | |
| 1463 | if spec is not None or spec_set is not None: |
| 1464 | if original is DEFAULT: |
| 1465 | raise TypeError("Can't use 'spec' with create=True") |
| 1466 | if isinstance(original, type): |
| 1467 | # If we're patching out a class and there is a spec |
| 1468 | inherit = True |
| 1469 | if spec is None and _is_async_obj(original): |
| 1470 | Klass = AsyncMock |
| 1471 | else: |
| 1472 | Klass = MagicMock |
| 1473 | _kwargs = {} |
| 1474 | if new_callable is not None: |
| 1475 | Klass = new_callable |
| 1476 | elif spec is not None or spec_set is not None: |
| 1477 | this_spec = spec |
| 1478 | if spec_set is not None: |
| 1479 | this_spec = spec_set |
| 1480 | if _is_list(this_spec): |
| 1481 | not_callable = '__call__' not in this_spec |
| 1482 | else: |
no test coverage detected