summaryrefslogtreecommitdiffstats
path: root/Lib/unittest
diff options
context:
space:
mode:
authorKushal Das <kushaldas@gmail.com>2014-04-16 18:02:21 (GMT)
committerKushal Das <kushaldas@gmail.com>2014-04-16 18:02:21 (GMT)
commit8c14534df6c7bd561fac31985fba60306e181265 (patch)
tree6a5b70b694cef522be062194b0d6953fb412ab3a /Lib/unittest
parentc3ac9af6d00ec9b5939a0ce31984113ddb9b0a9c (diff)
downloadcpython-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')
-rw-r--r--Lib/unittest/mock.py8
-rw-r--r--Lib/unittest/test/testmock/testmock.py11
2 files changed, 17 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:
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
index 23675b9..59353a0 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