@@ -26,6 +26,11 @@ _Py_IDENTIFIER(stderr);
2626
2727#include "clinic/bltinmodule.c.h"
2828
29+ /*[clinic input]
30+ class callable "callableobject *" "&PyCallable_Type"
31+ [clinic start generated code]*/
32+ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=186f9359829695da]*/
33+
2934static PyObject *
3035update_bases (PyObject * bases , PyObject * const * args , Py_ssize_t nargs )
3136{
@@ -454,9 +459,14 @@ builtin_bin(PyObject *module, PyObject *number)
454459 return PyNumber_ToBase (number , 2 );
455460}
456461
462+ typedef struct {
463+ PyObject_HEAD
464+ } callableobject ;
465+
457466
458467/*[clinic input]
459- callable as builtin_callable
468+ @classmethod
469+ callable.__new__ as callable_new
460470
461471 obj: object
462472 /
@@ -468,12 +478,34 @@ __call__() method.
468478[clinic start generated code]*/
469479
470480static PyObject *
471- builtin_callable ( PyObject * module , PyObject * obj )
472- /*[clinic end generated code: output=2b095d59d934cb7e input=1423bab99cc41f58 ]*/
481+ callable_new_impl ( PyTypeObject * type , PyObject * obj )
482+ /*[clinic end generated code: output=46784af2d6033e5a input=c0902a59d22a1a02 ]*/
473483{
474484 return PyBool_FromLong ((long )PyCallable_Check (obj ));
475485}
476486
487+ static PyMethodDef callable_methods [] = {
488+ {"__class_getitem__" , (PyCFunction )Py_GenericAlias , METH_O |METH_CLASS , PyDoc_STR ("See PEP 585" )},
489+ {NULL , NULL } /* sentinel */
490+ };
491+
492+ PyDoc_STRVAR (callable_doc ,
493+ "Return whether the object is callable (i.e., some kind of function).\n\n\
494+ Note that classes are callable, as are instances of classes with a\n\
495+ __call__() method." );
496+
497+ PyTypeObject PyCallable_Type = {
498+ PyVarObject_HEAD_INIT (& PyType_Type , 0 )
499+ .tp_name = "callable" ,
500+ .tp_basicsize = sizeof (callableobject ),
501+ .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC ,
502+ .tp_doc = callable_doc ,
503+ .tp_methods = callable_methods ,
504+ .tp_alloc = PyType_GenericAlloc ,
505+ .tp_new = callable_new ,
506+ .tp_free = PyObject_GC_Del ,
507+ };
508+
477509static PyObject *
478510builtin_breakpoint (PyObject * self , PyObject * const * args , Py_ssize_t nargs , PyObject * keywords )
479511{
@@ -2791,7 +2823,6 @@ static PyMethodDef builtin_methods[] = {
27912823 BUILTIN_ASCII_METHODDEF
27922824 BUILTIN_BIN_METHODDEF
27932825 {"breakpoint" , (PyCFunction )(void (* )(void ))builtin_breakpoint , METH_FASTCALL | METH_KEYWORDS , breakpoint_doc },
2794- BUILTIN_CALLABLE_METHODDEF
27952826 BUILTIN_CHR_METHODDEF
27962827 BUILTIN_COMPILE_METHODDEF
27972828 BUILTIN_DELATTR_METHODDEF
@@ -2893,6 +2924,7 @@ _PyBuiltin_Init(PyThreadState *tstate)
28932924 SETBUILTIN ("complex" , & PyComplex_Type );
28942925 SETBUILTIN ("dict" , & PyDict_Type );
28952926 SETBUILTIN ("enumerate" , & PyEnum_Type );
2927+ SETBUILTIN ("callable" , & PyCallable_Type );
28962928 SETBUILTIN ("filter" , & PyFilter_Type );
28972929 SETBUILTIN ("float" , & PyFloat_Type );
28982930 SETBUILTIN ("frozenset" , & PyFrozenSet_Type );
0 commit comments