diff options
author | Andrew Dunai <andunai@gmail.com> | 2018-12-04 11:08:45 +0200 |
---|---|---|
committer | Chris Withers <chris@withers.org> | 2019-04-30 08:39:55 +0100 |
commit | 962077b08d62752a3655b69f46c72872aefc1833 (patch) | |
tree | 746a57081a159fe06845cceb3d56de2fa06ea999 /mock | |
parent | c052ed132803a7ba4aa0067f9be3228a68ae95a7 (diff) | |
download | mock-962077b08d62752a3655b69f46c72872aefc1833.tar.gz |
bpo-35357: Add _mock_ prefix to name/parent/from_kall attributes of _Call/_MagicProxy. (#10873)
Fix minor typo in test function name.
Backports: e63e617ebbe481c498bdf037a62e09f4f9f3963f
Signed-off-by: Chris Withers <chris@withers.org>
Diffstat (limited to 'mock')
-rw-r--r-- | mock/mock.py | 28 | ||||
-rw-r--r-- | mock/tests/testmock.py | 18 |
2 files changed, 29 insertions, 17 deletions
diff --git a/mock/mock.py b/mock/mock.py index 88f30b5..9160268 100644 --- a/mock/mock.py +++ b/mock/mock.py @@ -2203,9 +2203,9 @@ class _Call(tuple): def __init__(self, value=(), name=None, parent=None, two=False, from_kall=True): - self.name = name - self.parent = parent - self.from_kall = from_kall + self._mock_name = name + self._mock_parent = parent + self._mock_from_kall = from_kall def __eq__(self, other): @@ -2222,8 +2222,8 @@ class _Call(tuple): else: self_name, self_args, self_kwargs = self - if (getattr(self, 'parent', None) and getattr(other, 'parent', None) - and self.parent != other.parent): + if (getattr(self, '_mock_parent', None) and getattr(other, '_mock_parent', None) + and self._mock_parent != other._mock_parent): return False other_name = '' @@ -2269,17 +2269,17 @@ class _Call(tuple): __hash__ = None def __call__(self, *args, **kwargs): - if self.name is None: + if self._mock_name is None: return _Call(('', args, kwargs), name='()') - name = self.name + '()' - return _Call((self.name, args, kwargs), name=name, parent=self) + name = self._mock_name + '()' + return _Call((self._mock_name, args, kwargs), name=name, parent=self) def __getattr__(self, attr): - if self.name is None: + if self._mock_name is None: return _Call(name=attr, from_kall=False) - name = '{}.{}'.format(self.name, attr) + name = '{}.{}'.format(self._mock_name, attr) return _Call(name=name, parent=self, from_kall=False) @@ -2290,8 +2290,8 @@ class _Call(tuple): return self.__getattr__('index')(*args, **kwargs) def __repr__(self): - if not self.from_kall: - name = self.name or 'call' + if not self._mock_from_kall: + name = self._mock_name or 'call' if name.startswith('()'): name = 'call%s' % name return name @@ -2317,9 +2317,9 @@ class _Call(tuple): vals = [] thing = self while thing is not None: - if thing.from_kall: + if thing._mock_from_kall: vals.append(thing) - thing = thing.parent + thing = thing._mock_parent return _CallList(reversed(vals)) diff --git a/mock/tests/testmock.py b/mock/tests/testmock.py index 53dda6f..43e23b2 100644 --- a/mock/tests/testmock.py +++ b/mock/tests/testmock.py @@ -12,13 +12,12 @@ import six import unittest import mock -from mock import ( +from mock.mock import ( call, DEFAULT, patch, sentinel, MagicMock, Mock, NonCallableMock, - NonCallableMagicMock, + NonCallableMagicMock, _Call, _CallList, create_autospec ) -from mock.mock import _CallList from mock.tests.support import is_instance @@ -1731,6 +1730,19 @@ class MockTest(unittest.TestCase): self.assertIsInstance(mock, int) mock.foo + def test_name_attribute_of_call(self): + # bpo-35357: _Call should not disclose any attributes whose names + # may clash with popular ones (such as ".name") + self.assertIsNotNone(call.name) + self.assertEqual(type(call.name), _Call) + self.assertEqual(type(call.name().name), _Call) + + def test_parent_attribute_of_call(self): + # bpo-35357: _Call should not disclose any attributes whose names + # may clash with popular ones (such as ".parent") + self.assertIsNotNone(call.parent) + self.assertEqual(type(call.parent), _Call) + self.assertEqual(type(call.parent().parent), _Call) @unittest.expectedFailure def test_pickle(self): |