Skip to content

Commit e1eb73e

Browse files
author
hauntsaninja
committed
bpo-42102: make callable runtime subscriptable
1 parent b81c833 commit e1eb73e

File tree

4 files changed

+63
-9
lines changed

4 files changed

+63
-9
lines changed

Include/bltinmodule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
extern "C" {
55
#endif
66

7+
PyAPI_DATA(PyTypeObject) PyCallable_Type;
78
PyAPI_DATA(PyTypeObject) PyFilter_Type;
89
PyAPI_DATA(PyTypeObject) PyMap_Type;
910
PyAPI_DATA(PyTypeObject) PyZip_Type;

Objects/object.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,6 +1774,7 @@ _PyTypes_Init(void)
17741774
INIT_TYPE(&PyMemoryView_Type, "memoryview");
17751775
INIT_TYPE(&PyTuple_Type, "tuple");
17761776
INIT_TYPE(&PyEnum_Type, "enumerate");
1777+
INIT_TYPE(&PyCallable_Type, "callable");
17771778
INIT_TYPE(&PyReversed_Type, "reversed");
17781779
INIT_TYPE(&PyStdPrinter_Type, "StdPrinter");
17791780
INIT_TYPE(&PyCode_Type, "code");

Python/bltinmodule.c

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
2934
static PyObject*
3035
update_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

470480
static 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+
477509
static PyObject *
478510
builtin_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);

Python/clinic/bltinmodule.c.h

Lines changed: 25 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)