diff options
author | Antony Lee <anntzer.lee@gmail.com> | 2021-09-24 15:22:49 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-24 15:22:49 (GMT) |
commit | 6587fc60d447603fb8c631d81d9bb379f53c39ab (patch) | |
tree | c52c3172f8845e2cb51eed4338436c9e6f58965a /Modules | |
parent | 8d8729146f21f61af66e70d3ae9501ea6bdccd09 (diff) | |
download | cpython-6587fc60d447603fb8c631d81d9bb379f53c39ab.zip cpython-6587fc60d447603fb8c631d81d9bb379f53c39ab.tar.gz cpython-6587fc60d447603fb8c631d81d9bb379f53c39ab.tar.bz2 |
bpo-44019: Implement operator.call(). (GH-27888)
Having `operator.call(obj, arg)` mean `type(obj).__call__(obj, arg)` is
consistent with the other dunder operators. The semantics with `*args,
**kwargs` then follow naturally from the single-arg semantics.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_operator.c | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/Modules/_operator.c b/Modules/_operator.c index f051513..12a5bf6 100644 --- a/Modules/_operator.c +++ b/Modules/_operator.c @@ -886,6 +886,27 @@ _operator__compare_digest_impl(PyObject *module, PyObject *a, PyObject *b) return PyBool_FromLong(rc); } +PyDoc_STRVAR(_operator_call__doc__, +"call($module, obj, /, *args, **kwargs)\n" +"--\n" +"\n" +"Same as obj(*args, **kwargs)."); + +#define _OPERATOR_CALL_METHODDEF \ + {"call", (PyCFunction)(void(*)(void))_operator_call, METH_FASTCALL | METH_KEYWORDS, _operator_call__doc__}, + +static PyObject * +_operator_call(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) +{ + if (!_PyArg_CheckPositional("call", nargs, 1, PY_SSIZE_T_MAX)) { + return NULL; + } + return PyObject_Vectorcall( + args[0], + &args[1], (PyVectorcall_NARGS(nargs) - 1) | PY_VECTORCALL_ARGUMENTS_OFFSET, + kwnames); +} + /* operator methods **********************************************************/ static struct PyMethodDef operator_methods[] = { @@ -942,6 +963,7 @@ static struct PyMethodDef operator_methods[] = { _OPERATOR_GE_METHODDEF _OPERATOR__COMPARE_DIGEST_METHODDEF _OPERATOR_LENGTH_HINT_METHODDEF + _OPERATOR_CALL_METHODDEF {NULL, NULL} /* sentinel */ }; |