Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a74b3aa

Browse files
committed
Remove more Python 2 compatibility cruft from unittest.mock
1 parent e7c8fde commit a74b3aa

2 files changed

Lines changed: 6 additions & 25 deletions

File tree

Lib/unittest/mock.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,10 @@ def _instance_callable(obj):
143143
# already an instance
144144
return getattr(obj, '__call__', None) is not None
145145

146-
klass = obj
147-
# uses __bases__ instead of __mro__ so that we work with old style classes
148-
if klass.__dict__.get('__call__') is not None:
149-
return True
150-
151-
for base in klass.__bases__:
152-
if _instance_callable(base):
146+
# *could* be broken by a class overriding __mro__ or __dict__ via
147+
# a metaclass
148+
for base in (obj,) + obj.__mro__:
149+
if base.__dict__.get('__call__') is not None:
153150
return True
154151
return False
155152

@@ -2064,11 +2061,7 @@ def _must_skip(spec, entry, is_type):
20642061
if entry in getattr(spec, '__dict__', {}):
20652062
# instance attribute - shouldn't skip
20662063
return False
2067-
# can't use type because of old style classes
20682064
spec = spec.__class__
2069-
if not hasattr(spec, '__mro__'):
2070-
# old style class: can't have descriptors anyway
2071-
return is_type
20722065

20732066
for klass in spec.__mro__:
20742067
result = klass.__dict__.get(entry, DEFAULT)

Lib/unittest/test/testmock/testcallable.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,9 @@ class Sub(CallableX):
107107
class Multi(SomeClass, Sub):
108108
pass
109109

110-
class OldStyle:
111-
def __call__(self):
112-
pass
113-
114-
class OldStyleSub(OldStyle):
115-
pass
116-
117110
for arg in 'spec', 'spec_set':
118-
for Klass in CallableX, Sub, Multi, OldStyle, OldStyleSub:
119-
patcher = patch('%s.X' % __name__, **{arg: Klass})
120-
mock = patcher.start()
121-
122-
try:
111+
for Klass in CallableX, Sub, Multi:
112+
with patch('%s.X' % __name__, **{arg: Klass}) as mock:
123113
instance = mock()
124114
mock.assert_called_once_with()
125115

@@ -136,8 +126,6 @@ class OldStyleSub(OldStyle):
136126
result.assert_called_once_with(3, 2, 1)
137127
result.foo(3, 2, 1)
138128
result.foo.assert_called_once_with(3, 2, 1)
139-
finally:
140-
patcher.stop()
141129

142130

143131
def test_create_autopsec(self):

0 commit comments

Comments
 (0)