mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	Issue #10518: Bring back the callable() builtin.
Approved by Guido (BDFL) and Georg (RM).
This commit is contained in:
		
							parent
							
								
									dc9b17d922
								
							
						
					
					
						commit
						e71362d3de
					
				
					 5 changed files with 61 additions and 14 deletions
				
			
		|  | @ -121,6 +121,19 @@ are always available.  They are listed here in alphabetical order. | ||||||
|    Bytes objects can also be created with literals, see :ref:`strings`. |    Bytes objects can also be created with literals, see :ref:`strings`. | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | .. function:: callable(object) | ||||||
|  | 
 | ||||||
|  |    Return :const:`True` if the *object* argument appears callable, | ||||||
|  |    :const:`False` if not.  If this returns true, it is still possible that a | ||||||
|  |    call fails, but if it is false, calling *object* will never succeed. | ||||||
|  |    Note that classes are callable (calling a class returns a new instance); | ||||||
|  |    instances are callable if their class has a :meth:`__call__` method. | ||||||
|  | 
 | ||||||
|  |    .. versionadded:: 3.2 | ||||||
|  |       This function was first removed in Python 3.0 and then brought back | ||||||
|  |       in Python 3.2. | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| .. function:: chr(i) | .. function:: chr(i) | ||||||
| 
 | 
 | ||||||
|    Return the string representing a character whose Unicode codepoint is the integer |    Return the string representing a character whose Unicode codepoint is the integer | ||||||
|  |  | ||||||
|  | @ -174,8 +174,8 @@ def test_hasattr(self): | ||||||
|         self.assertIs(hasattr([], "wobble"), False) |         self.assertIs(hasattr([], "wobble"), False) | ||||||
| 
 | 
 | ||||||
|     def test_callable(self): |     def test_callable(self): | ||||||
|         self.assertIs(hasattr(len, '__call__'), True) |         self.assertIs(callable(len), True) | ||||||
|         self.assertIs(hasattr(1, '__call__'), False) |         self.assertIs(callable(1), False) | ||||||
| 
 | 
 | ||||||
|     def test_isinstance(self): |     def test_isinstance(self): | ||||||
|         self.assertIs(isinstance(True, bool), True) |         self.assertIs(isinstance(True, bool), True) | ||||||
|  |  | ||||||
|  | @ -207,22 +207,39 @@ def test_neg(self): | ||||||
|         self.assertTrue(isinstance(x, int)) |         self.assertTrue(isinstance(x, int)) | ||||||
|         self.assertEqual(-x, sys.maxsize+1) |         self.assertEqual(-x, sys.maxsize+1) | ||||||
| 
 | 
 | ||||||
|     # XXX(nnorwitz): This test case for callable should probably be removed. |  | ||||||
|     def test_callable(self): |     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 |         def f(): pass | ||||||
|         self.assertTrue(hasattr(f, '__call__')) |         self.assertTrue(callable(f)) | ||||||
|         class C: | 
 | ||||||
|  |         class C1: | ||||||
|             def meth(self): pass |             def meth(self): pass | ||||||
|         self.assertTrue(hasattr(C, '__call__')) |         self.assertTrue(callable(C1)) | ||||||
|         x = C() |         c = C1() | ||||||
|         self.assertTrue(hasattr(x.meth, '__call__')) |         self.assertTrue(callable(c.meth)) | ||||||
|         self.assertTrue(not hasattr(x, '__call__')) |         self.assertFalse(callable(c)) | ||||||
|         class D(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 |             def __call__(self): pass | ||||||
|         y = D() |         c2 = C2() | ||||||
|         self.assertTrue(hasattr(y, '__call__')) |         self.assertTrue(callable(c2)) | ||||||
|         y() |         c2.__call__ = None | ||||||
|  |         self.assertTrue(callable(c2)) | ||||||
|  |         class C3(C2): pass | ||||||
|  |         c3 = C3() | ||||||
|  |         self.assertTrue(callable(c3)) | ||||||
| 
 | 
 | ||||||
|     def test_chr(self): |     def test_chr(self): | ||||||
|         self.assertEqual(chr(32), ' ') |         self.assertEqual(chr(32), ' ') | ||||||
|  |  | ||||||
|  | @ -10,6 +10,8 @@ What's New in Python 3.2 Beta 1? | ||||||
| Core and Builtins | Core and Builtins | ||||||
| ----------------- | ----------------- | ||||||
| 
 | 
 | ||||||
|  | - Issue #10518: Bring back the callable() builtin. | ||||||
|  | 
 | ||||||
| - Issue #8879. Add os.link support for Windows. | - Issue #8879. Add os.link support for Windows. | ||||||
| 
 | 
 | ||||||
| - Issue #10027. st_nlink was not being set on Windows calls to os.stat or | - Issue #10027. st_nlink was not being set on Windows calls to os.stat or | ||||||
|  |  | ||||||
|  | @ -311,6 +311,20 @@ PyDoc_STRVAR(bin_doc, | ||||||
| Return the binary representation of an integer or long integer."); | Return the binary representation of an integer or long integer."); | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | static PyObject * | ||||||
|  | builtin_callable(PyObject *self, PyObject *v) | ||||||
|  | { | ||||||
|  |     return PyBool_FromLong((long)PyCallable_Check(v)); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | PyDoc_STRVAR(callable_doc, | ||||||
|  | "callable(object) -> bool\n\
 | ||||||
|  | \n\ | ||||||
|  | Return whether the object is callable (i.e., some kind of function).\n\ | ||||||
|  | Note that classes are callable, as are instances of classes with a\n\ | ||||||
|  | __call__() method."); | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| typedef struct { | typedef struct { | ||||||
|     PyObject_HEAD |     PyObject_HEAD | ||||||
|     PyObject *func; |     PyObject *func; | ||||||
|  | @ -2242,6 +2256,7 @@ static PyMethodDef builtin_methods[] = { | ||||||
|     {"any",             builtin_any,        METH_O, any_doc}, |     {"any",             builtin_any,        METH_O, any_doc}, | ||||||
|     {"ascii",           builtin_ascii,      METH_O, ascii_doc}, |     {"ascii",           builtin_ascii,      METH_O, ascii_doc}, | ||||||
|     {"bin",             builtin_bin,        METH_O, bin_doc}, |     {"bin",             builtin_bin,        METH_O, bin_doc}, | ||||||
|  |     {"callable",        builtin_callable,   METH_O, callable_doc}, | ||||||
|     {"chr",             builtin_chr,        METH_VARARGS, chr_doc}, |     {"chr",             builtin_chr,        METH_VARARGS, chr_doc}, | ||||||
|     {"compile",         (PyCFunction)builtin_compile,    METH_VARARGS | METH_KEYWORDS, compile_doc}, |     {"compile",         (PyCFunction)builtin_compile,    METH_VARARGS | METH_KEYWORDS, compile_doc}, | ||||||
|     {"delattr",         builtin_delattr,    METH_VARARGS, delattr_doc}, |     {"delattr",         builtin_delattr,    METH_VARARGS, delattr_doc}, | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Antoine Pitrou
						Antoine Pitrou