Remove more Python 2 compatibility cruft from unittest.mock

This commit is contained in:
Michael Foord 2012-03-14 14:40:22 -07:00
parent e7c8fdee18
commit a74b3aa0cc
2 changed files with 6 additions and 25 deletions

View File

@ -143,13 +143,10 @@ def _instance_callable(obj):
# already an instance # already an instance
return getattr(obj, '__call__', None) is not None return getattr(obj, '__call__', None) is not None
klass = obj # *could* be broken by a class overriding __mro__ or __dict__ via
# uses __bases__ instead of __mro__ so that we work with old style classes # a metaclass
if klass.__dict__.get('__call__') is not None: for base in (obj,) + obj.__mro__:
return True if base.__dict__.get('__call__') is not None:
for base in klass.__bases__:
if _instance_callable(base):
return True return True
return False return False
@ -2064,11 +2061,7 @@ def _must_skip(spec, entry, is_type):
if entry in getattr(spec, '__dict__', {}): if entry in getattr(spec, '__dict__', {}):
# instance attribute - shouldn't skip # instance attribute - shouldn't skip
return False return False
# can't use type because of old style classes
spec = spec.__class__ spec = spec.__class__
if not hasattr(spec, '__mro__'):
# old style class: can't have descriptors anyway
return is_type
for klass in spec.__mro__: for klass in spec.__mro__:
result = klass.__dict__.get(entry, DEFAULT) result = klass.__dict__.get(entry, DEFAULT)

View File

@ -107,19 +107,9 @@ class TestCallable(unittest.TestCase):
class Multi(SomeClass, Sub): class Multi(SomeClass, Sub):
pass pass
class OldStyle:
def __call__(self):
pass
class OldStyleSub(OldStyle):
pass
for arg in 'spec', 'spec_set': for arg in 'spec', 'spec_set':
for Klass in CallableX, Sub, Multi, OldStyle, OldStyleSub: for Klass in CallableX, Sub, Multi:
patcher = patch('%s.X' % __name__, **{arg: Klass}) with patch('%s.X' % __name__, **{arg: Klass}) as mock:
mock = patcher.start()
try:
instance = mock() instance = mock()
mock.assert_called_once_with() mock.assert_called_once_with()
@ -136,8 +126,6 @@ class TestCallable(unittest.TestCase):
result.assert_called_once_with(3, 2, 1) result.assert_called_once_with(3, 2, 1)
result.foo(3, 2, 1) result.foo(3, 2, 1)
result.foo.assert_called_once_with(3, 2, 1) result.foo.assert_called_once_with(3, 2, 1)
finally:
patcher.stop()
def test_create_autopsec(self): def test_create_autopsec(self):