Closes Issue 21238: New keyword argument unsafe to Mock.

It raises `AttributeError` incase of an attribute startswith assert
or assret.
This commit is contained in:
Kushal Das 2014-04-16 23:32:21 +05:30
parent c3ac9af6d0
commit 8c14534df6
4 changed files with 27 additions and 3 deletions

View file

@ -379,7 +379,7 @@ class NonCallableMock(Base):
def __init__(
self, spec=None, wraps=None, name=None, spec_set=None,
parent=None, _spec_state=None, _new_name='', _new_parent=None,
_spec_as_instance=False, _eat_self=None, **kwargs
_spec_as_instance=False, _eat_self=None, unsafe=False, **kwargs
):
if _new_parent is None:
_new_parent = parent
@ -409,6 +409,7 @@ class NonCallableMock(Base):
__dict__['_mock_mock_calls'] = _CallList()
__dict__['method_calls'] = _CallList()
__dict__['_mock_unsafe'] = unsafe
if kwargs:
self.configure_mock(**kwargs)
@ -565,13 +566,16 @@ class NonCallableMock(Base):
def __getattr__(self, name):
if name == '_mock_methods':
if name in {'_mock_methods', '_mock_unsafe'}:
raise AttributeError(name)
elif self._mock_methods is not None:
if name not in self._mock_methods or name in _all_magics:
raise AttributeError("Mock object has no attribute %r" % name)
elif _is_magic(name):
raise AttributeError(name)
if not self._mock_unsafe:
if name.startswith(('assert', 'assret')):
raise AttributeError(name)
result = self._mock_children.get(name)
if result is _deleted: