Perform the patch.
(self)
| 1477 | |
| 1478 | |
| 1479 | def __enter__(self): |
| 1480 | """Perform the patch.""" |
| 1481 | if self.is_started: |
| 1482 | raise RuntimeError("Patch is already started") |
| 1483 | |
| 1484 | new, spec, spec_set = self.new, self.spec, self.spec_set |
| 1485 | autospec, kwargs = self.autospec, self.kwargs |
| 1486 | new_callable = self.new_callable |
| 1487 | self.target = self.getter() |
| 1488 | |
| 1489 | # normalise False to None |
| 1490 | if spec is False: |
| 1491 | spec = None |
| 1492 | if spec_set is False: |
| 1493 | spec_set = None |
| 1494 | if autospec is False: |
| 1495 | autospec = None |
| 1496 | |
| 1497 | if spec is not None and autospec is not None: |
| 1498 | raise TypeError("Can't specify spec and autospec") |
| 1499 | if ((spec is not None or autospec is not None) and |
| 1500 | spec_set not in (True, None)): |
| 1501 | raise TypeError("Can't provide explicit spec_set *and* spec or autospec") |
| 1502 | |
| 1503 | original, local = self.get_original() |
| 1504 | |
| 1505 | if new is DEFAULT and autospec is None: |
| 1506 | inherit = False |
| 1507 | if spec is True: |
| 1508 | # set spec to the object we are replacing |
| 1509 | spec = original |
| 1510 | if spec_set is True: |
| 1511 | spec_set = original |
| 1512 | spec = None |
| 1513 | elif spec is not None: |
| 1514 | if spec_set is True: |
| 1515 | spec_set = spec |
| 1516 | spec = None |
| 1517 | elif spec_set is True: |
| 1518 | spec_set = original |
| 1519 | |
| 1520 | if spec is not None or spec_set is not None: |
| 1521 | if original is DEFAULT: |
| 1522 | raise TypeError("Can't use 'spec' with create=True") |
| 1523 | if isinstance(original, type): |
| 1524 | # If we're patching out a class and there is a spec |
| 1525 | inherit = True |
| 1526 | |
| 1527 | # Determine the Klass to use |
| 1528 | if new_callable is not None: |
| 1529 | Klass = new_callable |
| 1530 | elif spec is None and _is_async_obj(original): |
| 1531 | Klass = AsyncMock |
| 1532 | elif spec is not None or spec_set is not None: |
| 1533 | this_spec = spec |
| 1534 | if spec_set is not None: |
| 1535 | this_spec = spec_set |
| 1536 | if _is_list(this_spec): |
no test coverage detected