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