def __enter__(self):
new, spec, spec_set = self.new, self.spec, self.spec_set<EOL>autospec, kwargs = self.autospec, self.kwargs<EOL>new_callable = self.new_callable<EOL>self.target = self.getter()<EOL>if spec is False:<EOL><INDENT>spec = None<EOL><DEDENT>if spec_set is False:<EOL><INDENT>spec_set = None<EOL><DEDENT>if autospec is False:<EOL><INDENT>autospec = None<EOL><DEDENT>if spec is not None and autospec is not None:<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>if ((spec is not None or autospec is not None) and<EOL>spec_set not in (True, None)):<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>original, local = self.get_original()<EOL>if new is DEFAULT and autospec is None:<EOL><INDENT>inherit = False<EOL>if spec is True:<EOL><INDENT>spec = original<EOL>if spec_set is True:<EOL><INDENT>spec_set = original<EOL>spec = None<EOL><DEDENT><DEDENT>elif spec is not None:<EOL><INDENT>if spec_set is True:<EOL><INDENT>spec_set = spec<EOL>spec = None<EOL><DEDENT><DEDENT>elif spec_set is True:<EOL><INDENT>spec_set = original<EOL><DEDENT>if spec is not None or spec_set is not None:<EOL><INDENT>if original is DEFAULT:<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>if isinstance(original, ClassTypes):<EOL><INDENT>inherit = True<EOL><DEDENT><DEDENT>Klass = MagicMock<EOL>_kwargs = {}<EOL>if new_callable is not None:<EOL><INDENT>Klass = new_callable<EOL><DEDENT>elif spec is not None or spec_set is not None:<EOL><INDENT>this_spec = spec<EOL>if spec_set is not None:<EOL><INDENT>this_spec = spec_set<EOL><DEDENT>if _is_list(this_spec):<EOL><INDENT>not_callable = '<STR_LIT>' not in this_spec<EOL><DEDENT>else:<EOL><INDENT>not_callable = not _callable(this_spec)<EOL><DEDENT>if not_callable:<EOL><INDENT>Klass = NonCallableMagicMock<EOL><DEDENT><DEDENT>if spec is not None:<EOL><INDENT>_kwargs['<STR_LIT>'] = spec<EOL><DEDENT>if spec_set is not None:<EOL><INDENT>_kwargs['<STR_LIT>'] = spec_set<EOL><DEDENT>if (isinstance(Klass, type) and<EOL>issubclass(Klass, NonCallableMock) and self.attribute):<EOL><INDENT>_kwargs['<STR_LIT:name>'] = self.attribute<EOL><DEDENT>_kwargs.update(kwargs)<EOL>new = Klass(**_kwargs)<EOL>if inherit and _is_instance_mock(new):<EOL><INDENT>this_spec = spec<EOL>if spec_set is not None:<EOL><INDENT>this_spec = spec_set<EOL><DEDENT>if (not _is_list(this_spec) and not<EOL>_instance_callable(this_spec)):<EOL><INDENT>Klass = NonCallableMagicMock<EOL><DEDENT>_kwargs.pop('<STR_LIT:name>')<EOL>new.return_value = Klass(_new_parent=new, _new_name='<STR_LIT>',<EOL>**_kwargs)<EOL><DEDENT><DEDENT>elif autospec is not None:<EOL><INDENT>if new is not DEFAULT:<EOL><INDENT>raise TypeError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if original is DEFAULT:<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>spec_set = bool(spec_set)<EOL>if autospec is True:<EOL><INDENT>autospec = original<EOL><DEDENT>new = create_autospec(autospec, spec_set=spec_set,<EOL>_name=self.attribute, **kwargs)<EOL><DEDENT>elif kwargs:<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>new_attr = new<EOL>self.temp_original = original<EOL>self.is_local = local<EOL>setattr(self.target, self.attribute, new_attr)<EOL>if self.attribute_name is not None:<EOL><INDENT>extra_args = {}<EOL>if self.new is DEFAULT:<EOL><INDENT>extra_args[self.attribute_name] =  new<EOL><DEDENT>for patching in self.additional_patchers:<EOL><INDENT>arg = patching.__enter__()<EOL>if patching.new is DEFAULT:<EOL><INDENT>extra_args.update(arg)<EOL><DEDENT><DEDENT>return extra_args<EOL><DEDENT>return new<EOL>
Perform the patch.