diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index e6b103d5d6f..6371fc8bd6e 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -167,16 +167,15 @@ def _set_signature(mock, original, instance=False): signature, func = result src = "lambda %s: None" % signature - context = {'_mock_': mock} - checksig = eval(src, context) + checksig = eval(src, {}) _copy_func_details(func, checksig) name = original.__name__ if not name.isidentifier(): name = 'funcopy' - context = {'checksig': checksig, 'mock': mock} + context = {'_checksig_': checksig, 'mock': mock} src = """def %s(*args, **kwargs): - checksig(*args, **kwargs) + _checksig_(*args, **kwargs) return mock(*args, **kwargs)""" % name exec (src, context) funcopy = context[name] @@ -620,14 +619,16 @@ class NonCallableMock(Base): def __dir__(self): """Filter the output of `dir(mock)` to only useful members.""" + if not FILTER_DIR: + return object.__dir__(self) + extras = self._mock_methods or [] from_type = dir(type(self)) from_dict = list(self.__dict__) - if FILTER_DIR: - from_type = [e for e in from_type if not e.startswith('_')] - from_dict = [e for e in from_dict if not e.startswith('_') or - _is_magic(e)] + from_type = [e for e in from_type if not e.startswith('_')] + from_dict = [e for e in from_dict if not e.startswith('_') or + _is_magic(e)] return sorted(set(extras + from_type + from_dict + list(self._mock_children))) @@ -1582,6 +1583,10 @@ _calculate_return_value = { } _return_values = { + '__lt__': NotImplemented, + '__gt__': NotImplemented, + '__le__': NotImplemented, + '__ge__': NotImplemented, '__int__': 1, '__contains__': False, '__len__': 0, @@ -2000,7 +2005,6 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None, _parent._mock_children[_name] = mock if is_type and not instance and 'return_value' not in kwargs: - # XXXX could give a name to the return_value mock? mock.return_value = create_autospec(spec, spec_set, instance=True, _name='()', _parent=mock) diff --git a/Lib/unittest/test/testmock/testmagicmethods.py b/Lib/unittest/test/testmock/testmagicmethods.py index 65d2711149e..bd52e2589b9 100644 --- a/Lib/unittest/test/testmock/testmagicmethods.py +++ b/Lib/unittest/test/testmock/testmagicmethods.py @@ -156,6 +156,19 @@ class TestMockingMagicMethods(unittest.TestCase): self. assertTrue(mock <= 3) self. assertTrue(mock >= 3) + self.assertRaises(TypeError, lambda: MagicMock() < object()) + self.assertRaises(TypeError, lambda: object() < MagicMock()) + self.assertRaises(TypeError, lambda: MagicMock() < MagicMock()) + self.assertRaises(TypeError, lambda: MagicMock() > object()) + self.assertRaises(TypeError, lambda: object() > MagicMock()) + self.assertRaises(TypeError, lambda: MagicMock() > MagicMock()) + self.assertRaises(TypeError, lambda: MagicMock() <= object()) + self.assertRaises(TypeError, lambda: object() <= MagicMock()) + self.assertRaises(TypeError, lambda: MagicMock() <= MagicMock()) + self.assertRaises(TypeError, lambda: MagicMock() >= object()) + self.assertRaises(TypeError, lambda: object() >= MagicMock()) + self.assertRaises(TypeError, lambda: MagicMock() >= MagicMock()) + def test_equality(self): for mock in Mock(), MagicMock(): diff --git a/Misc/NEWS b/Misc/NEWS index 582d1dfb140..df13d40e63d 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -34,6 +34,8 @@ Core and Builtins Library ------- +- Issue #14295: Add unittest.mock + - Issue #7652: Add --with-system-libmpdec option to configure for linking the _decimal module against an installed libmpdec.