summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2010-11-27 22:00:11 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2010-11-27 22:00:11 (GMT)
commite71362d3dee9de626abb7d2d564550b5e59a1151 (patch)
tree1703c0a9f8d209b9dbff568a20f49a3541185dde /Lib
parentdc9b17d922ba30cbfbcc767dfb0738b6bf66015d (diff)
downloadcpython-e71362d3dee9de626abb7d2d564550b5e59a1151.zip
cpython-e71362d3dee9de626abb7d2d564550b5e59a1151.tar.gz
cpython-e71362d3dee9de626abb7d2d564550b5e59a1151.tar.bz2
Issue #10518: Bring back the callable() builtin.
Approved by Guido (BDFL) and Georg (RM).
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_bool.py4
-rw-r--r--Lib/test/test_builtin.py41
2 files changed, 31 insertions, 14 deletions
diff --git a/Lib/test/test_bool.py b/Lib/test/test_bool.py
index 7282c0a..b296870 100644
--- a/Lib/test/test_bool.py
+++ b/Lib/test/test_bool.py
@@ -174,8 +174,8 @@ class BoolTest(unittest.TestCase):
self.assertIs(hasattr([], "wobble"), False)
def test_callable(self):
- self.assertIs(hasattr(len, '__call__'), True)
- self.assertIs(hasattr(1, '__call__'), False)
+ self.assertIs(callable(len), True)
+ self.assertIs(callable(1), False)
def test_isinstance(self):
self.assertIs(isinstance(True, bool), True)
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 26c7034..7b73949 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -207,22 +207,39 @@ class BuiltinTest(unittest.TestCase):
self.assertTrue(isinstance(x, int))
self.assertEqual(-x, sys.maxsize+1)
- # XXX(nnorwitz): This test case for callable should probably be removed.
def test_callable(self):
- self.assertTrue(hasattr(len, '__call__'))
+ self.assertTrue(callable(len))
+ self.assertFalse(callable("a"))
+ self.assertTrue(callable(callable))
+ self.assertTrue(callable(lambda x, y: x + y))
+ self.assertFalse(callable(__builtins__))
def f(): pass
- self.assertTrue(hasattr(f, '__call__'))
- class C:
+ self.assertTrue(callable(f))
+
+ class C1:
def meth(self): pass
- self.assertTrue(hasattr(C, '__call__'))
- x = C()
- self.assertTrue(hasattr(x.meth, '__call__'))
- self.assertTrue(not hasattr(x, '__call__'))
- class D(C):
+ self.assertTrue(callable(C1))
+ c = C1()
+ self.assertTrue(callable(c.meth))
+ self.assertFalse(callable(c))
+
+ # __call__ is looked up on the class, not the instance
+ c.__call__ = None
+ self.assertFalse(callable(c))
+ c.__call__ = lambda self: 0
+ self.assertFalse(callable(c))
+ del c.__call__
+ self.assertFalse(callable(c))
+
+ class C2(object):
def __call__(self): pass
- y = D()
- self.assertTrue(hasattr(y, '__call__'))
- y()
+ c2 = C2()
+ self.assertTrue(callable(c2))
+ c2.__call__ = None
+ self.assertTrue(callable(c2))
+ class C3(C2): pass
+ c3 = C3()
+ self.assertTrue(callable(c3))
def test_chr(self):
self.assertEqual(chr(32), ' ')