Skip to content

bpo-42102: [draft] make callable runtime subscriptable #22848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Include/bltinmodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
extern "C" {
#endif

PyAPI_DATA(PyTypeObject) PyCallable_Type;
PyAPI_DATA(PyTypeObject) PyFilter_Type;
PyAPI_DATA(PyTypeObject) PyMap_Type;
PyAPI_DATA(PyTypeObject) PyZip_Type;
Expand Down
1 change: 1 addition & 0 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -1774,6 +1774,7 @@ _PyTypes_Init(void)
INIT_TYPE(&PyMemoryView_Type, "memoryview");
INIT_TYPE(&PyTuple_Type, "tuple");
INIT_TYPE(&PyEnum_Type, "enumerate");
INIT_TYPE(&PyCallable_Type, "callable");
INIT_TYPE(&PyReversed_Type, "reversed");
INIT_TYPE(&PyStdPrinter_Type, "StdPrinter");
INIT_TYPE(&PyCode_Type, "code");
Expand Down
40 changes: 36 additions & 4 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ _Py_IDENTIFIER(stderr);

#include "clinic/bltinmodule.c.h"

/*[clinic input]
class callable "callableobject *" "&PyCallable_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=186f9359829695da]*/

static PyObject*
update_bases(PyObject *bases, PyObject *const *args, Py_ssize_t nargs)
{
Expand Down Expand Up @@ -454,9 +459,14 @@ builtin_bin(PyObject *module, PyObject *number)
return PyNumber_ToBase(number, 2);
}

typedef struct {
PyObject_HEAD
} callableobject;


/*[clinic input]
callable as builtin_callable
@classmethod
callable.__new__ as callable_new

obj: object
/
Expand All @@ -468,12 +478,34 @@ __call__() method.
[clinic start generated code]*/

static PyObject *
builtin_callable(PyObject *module, PyObject *obj)
/*[clinic end generated code: output=2b095d59d934cb7e input=1423bab99cc41f58]*/
callable_new_impl(PyTypeObject *type, PyObject *obj)
/*[clinic end generated code: output=46784af2d6033e5a input=c0902a59d22a1a02]*/
{
return PyBool_FromLong((long)PyCallable_Check(obj));
}

static PyMethodDef callable_methods[] = {
{"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
{NULL, NULL} /* sentinel */
};

PyDoc_STRVAR(callable_doc,
"Return whether the object is callable (i.e., some kind of function).\n\n\
Note that classes are callable, as are instances of classes with a\n\
__call__() method.");

PyTypeObject PyCallable_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
.tp_name = "callable",
.tp_basicsize = sizeof(callableobject),
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
.tp_doc = callable_doc,
.tp_methods = callable_methods,
.tp_alloc = PyType_GenericAlloc,
.tp_new = callable_new,
.tp_free = PyObject_GC_Del,
};

static PyObject *
builtin_breakpoint(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *keywords)
{
Expand Down Expand Up @@ -2791,7 +2823,6 @@ static PyMethodDef builtin_methods[] = {
BUILTIN_ASCII_METHODDEF
BUILTIN_BIN_METHODDEF
{"breakpoint", (PyCFunction)(void(*)(void))builtin_breakpoint, METH_FASTCALL | METH_KEYWORDS, breakpoint_doc},
BUILTIN_CALLABLE_METHODDEF
BUILTIN_CHR_METHODDEF
BUILTIN_COMPILE_METHODDEF
BUILTIN_DELATTR_METHODDEF
Expand Down Expand Up @@ -2893,6 +2924,7 @@ _PyBuiltin_Init(PyThreadState *tstate)
SETBUILTIN("complex", &PyComplex_Type);
SETBUILTIN("dict", &PyDict_Type);
SETBUILTIN("enumerate", &PyEnum_Type);
SETBUILTIN("callable", &PyCallable_Type);
SETBUILTIN("filter", &PyFilter_Type);
SETBUILTIN("float", &PyFloat_Type);
SETBUILTIN("frozenset", &PyFrozenSet_Type);
Expand Down
30 changes: 25 additions & 5 deletions Python/clinic/bltinmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.