@@ -207,22 +207,39 @@ def test_neg(self):
207207 self .assertTrue (isinstance (x , int ))
208208 self .assertEqual (- x , sys .maxsize + 1 )
209209
210- # XXX(nnorwitz): This test case for callable should probably be removed.
211210 def test_callable (self ):
212- self .assertTrue (hasattr (len , '__call__' ))
211+ self .assertTrue (callable (len ))
212+ self .assertFalse (callable ("a" ))
213+ self .assertTrue (callable (callable ))
214+ self .assertTrue (callable (lambda x , y : x + y ))
215+ self .assertFalse (callable (__builtins__ ))
213216 def f (): pass
214- self .assertTrue (hasattr (f , '__call__' ))
215- class C :
217+ self .assertTrue (callable (f ))
218+
219+ class C1 :
216220 def meth (self ): pass
217- self .assertTrue (hasattr (C , '__call__' ))
218- x = C ()
219- self .assertTrue (hasattr (x .meth , '__call__' ))
220- self .assertTrue (not hasattr (x , '__call__' ))
221- class D (C ):
221+ self .assertTrue (callable (C1 ))
222+ c = C1 ()
223+ self .assertTrue (callable (c .meth ))
224+ self .assertFalse (callable (c ))
225+
226+ # __call__ is looked up on the class, not the instance
227+ c .__call__ = None
228+ self .assertFalse (callable (c ))
229+ c .__call__ = lambda self : 0
230+ self .assertFalse (callable (c ))
231+ del c .__call__
232+ self .assertFalse (callable (c ))
233+
234+ class C2 (object ):
222235 def __call__ (self ): pass
223- y = D ()
224- self .assertTrue (hasattr (y , '__call__' ))
225- y ()
236+ c2 = C2 ()
237+ self .assertTrue (callable (c2 ))
238+ c2 .__call__ = None
239+ self .assertTrue (callable (c2 ))
240+ class C3 (C2 ): pass
241+ c3 = C3 ()
242+ self .assertTrue (callable (c3 ))
226243
227244 def test_chr (self ):
228245 self .assertEqual (chr (32 ), ' ' )
0 commit comments