diff options
author | Kushal Das <kushaldas@gmail.com> | 2014-04-16 18:02:21 (GMT) |
---|---|---|
committer | Kushal Das <kushaldas@gmail.com> | 2014-04-16 18:02:21 (GMT) |
commit | 8c14534df6c7bd561fac31985fba60306e181265 (patch) | |
tree | 6a5b70b694cef522be062194b0d6953fb412ab3a /Lib/unittest/mock.py | |
parent | c3ac9af6d00ec9b5939a0ce31984113ddb9b0a9c (diff) | |
download | cpython-8c14534df6c7bd561fac31985fba60306e181265.zip cpython-8c14534df6c7bd561fac31985fba60306e181265.tar.gz cpython-8c14534df6c7bd561fac31985fba60306e181265.tar.bz2 |
Closes Issue 21238: New keyword argument `unsafe` to Mock.
It raises `AttributeError` incase of an attribute startswith assert
or assret.
Diffstat (limited to 'Lib/unittest/mock.py')
-rw-r--r-- | Lib/unittest/mock.py | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 42cf23a..48e7dd0 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -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: |