MCPcopy Index your code
hub / github.com/python/cpython / __setattr__

Method __setattr__

Lib/unittest/mock.py:806–845  ·  view source on GitHub ↗
(self, name, value)

Source from the content-addressed store, hash-verified

804
805
806 def __setattr__(self, name, value):
807 if name in _allowed_names:
808 # property setters go through here
809 return object.__setattr__(self, name, value)
810 elif (self._spec_set and self._mock_methods is not None and
811 name not in self._mock_methods and
812 name not in self.__dict__):
813 raise AttributeError("Mock object has no attribute '%s'" % name)
814 elif name in _unsupported_magics:
815 msg = 'Attempting to set unsupported magic method %r.' % name
816 raise AttributeError(msg)
817 elif name in _all_magics:
818 if self._mock_methods is not None and name not in self._mock_methods:
819 raise AttributeError("Mock object has no attribute '%s'" % name)
820
821 if not _is_instance_mock(value):
822 setattr(type(self), name, _get_method(name, value))
823 original = value
824 value = lambda *args, **kw: original(self, *args, **kw)
825 else:
826 # only set _new_name and not name so that mock_calls is tracked
827 # but not method calls
828 _check_and_set_parent(self, value, None, name)
829 setattr(type(self), name, value)
830 self._mock_children[name] = value
831 elif name == '__class__':
832 self._spec_class = value
833 return
834 else:
835 if _check_and_set_parent(self, value, name, name):
836 self._mock_children[name] = value
837
838 if self._mock_sealed and not hasattr(self, name):
839 mock_name = f'{self._extract_mock_name()}.{name}'
840 raise AttributeError(f'Cannot set {mock_name}')
841
842 if isinstance(value, PropertyMock):
843 self.__dict__[name] = value
844 return
845 return object.__setattr__(self, name, value)
846
847
848 def __delattr__(self, name):

Callers

nothing calls this directly

Calls 4

_extract_mock_nameMethod · 0.95
_is_instance_mockFunction · 0.85
_get_methodFunction · 0.85
_check_and_set_parentFunction · 0.85

Tested by

no test coverage detected