From 8c14534df6c7bd561fac31985fba60306e181265 Mon Sep 17 00:00:00 2001 From: Kushal Das Date: Wed, 16 Apr 2014 23:32:21 +0530 Subject: Closes Issue 21238: New keyword argument `unsafe` to Mock. It raises `AttributeError` incase of an attribute startswith assert or assret. --- Lib/unittest/mock.py | 8 ++++++-- Lib/unittest/test/testmock/testmock.py | 11 +++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) (limited to 'Lib/unittest') diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 42cf23abb7..48e7dd078f 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: diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index 23675b9af6..59353a095b 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -1187,6 +1187,17 @@ class MockTest(unittest.TestCase): m = mock.create_autospec(object(), name='sweet_func') self.assertIn('sweet_func', repr(m)) + #Issue21238 + def test_mock_unsafe(self): + m = Mock() + with self.assertRaises(AttributeError): + m.assert_foo_call() + with self.assertRaises(AttributeError): + m.assret_foo_call() + m = Mock(unsafe=True) + m.assert_foo_call() + m.assret_foo_call() + def test_mock_add_spec(self): class _One(object): one = 1 -- cgit v1.2.1