diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 72f2e356c34f7c8..13d19ce96f872d9 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1426,104 +1426,61 @@ typedef struct { #define _mapobject_CAST(op) ((mapobject *)(op)) -static PyObject * -map_new(PyTypeObject *type, PyObject *args, PyObject *kwds) -{ - PyObject *it, *iters, *func; - mapobject *lz; - Py_ssize_t numargs, i; - int strict = 0; - - if (kwds) { - PyObject *empty = PyTuple_New(0); - if (empty == NULL) { - return NULL; - } - static char *kwlist[] = {"strict", NULL}; - int parsed = PyArg_ParseTupleAndKeywords( - empty, kwds, "|$p:map", kwlist, &strict); - Py_DECREF(empty); - if (!parsed) { - return NULL; - } - } - - numargs = PyTuple_Size(args); - if (numargs < 2) { - PyErr_SetString(PyExc_TypeError, - "map() must have at least two arguments."); - return NULL; - } +/*[clinic input] +class map "mapobject *" "&PyMap_Type" +@vectorcall +@classmethod +map.__new__ as map_new - iters = PyTuple_New(numargs-1); - if (iters == NULL) - return NULL; + function: object + iterable: object + / + *iterables: array + strict: bool = False - for (i=1 ; itp_alloc(type, 0); - if (lz == NULL) { - Py_DECREF(iters); - return NULL; - } - lz->iters = iters; - func = PyTuple_GET_ITEM(args, 0); - lz->func = Py_NewRef(func); - lz->strict = strict; +The function is called with one argument from each of the iterables. +Stops when the shortest iterable is exhausted. - return (PyObject *)lz; -} +If strict is true and one of the arguments is exhausted before the +others, raise a ValueError. +[clinic start generated code]*/ static PyObject * -map_vectorcall(PyObject *type, PyObject * const*args, - size_t nargsf, PyObject *kwnames) +map_new_impl(PyTypeObject *type, PyObject *function, PyObject *iterable, + PyObject * const *iterables, Py_ssize_t iterables_length, + int strict) +/*[clinic end generated code: output=de1354b02d08604b input=d3bf952f6bb775c8]*/ { - PyTypeObject *tp = _PyType_CAST(type); - - Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); - if (kwnames != NULL && PyTuple_GET_SIZE(kwnames) != 0) { - // Fallback to map_new() - PyThreadState *tstate = _PyThreadState_GET(); - return _PyObject_MakeTpCall(tstate, type, args, nargs, kwnames); - } - - if (nargs < 2) { - PyErr_SetString(PyExc_TypeError, - "map() must have at least two arguments."); + PyObject *iters = PyTuple_New(iterables_length + 1); + if (iters == NULL) { return NULL; } - PyObject *iters = PyTuple_New(nargs-1); - if (iters == NULL) { + PyObject *it = PyObject_GetIter(iterable); + if (it == NULL) { + Py_DECREF(iters); return NULL; } - - for (int i=1; itp_alloc(tp, 0); + mapobject *lz = (mapobject *)type->tp_alloc(type, 0); if (lz == NULL) { Py_DECREF(iters); return NULL; } lz->iters = iters; - lz->func = Py_NewRef(args[0]); - lz->strict = 0; + lz->func = Py_NewRef(function); + lz->strict = strict; return (PyObject *)lz; } @@ -1680,16 +1637,6 @@ static PyMethodDef map_methods[] = { }; -PyDoc_STRVAR(map_doc, -"map(function, iterable, /, *iterables, strict=False)\n\ ---\n\ -\n\ -Make an iterator that computes the function using arguments from\n\ -each of the iterables. Stops when the shortest iterable is exhausted.\n\ -\n\ -If strict is true and one of the arguments is exhausted before the\n\ -others, raise a ValueError."); - PyTypeObject PyMap_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) "map", /* tp_name */ @@ -1713,7 +1660,7 @@ PyTypeObject PyMap_Type = { 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /* tp_flags */ - map_doc, /* tp_doc */ + map_new__doc__, /* tp_doc */ map_traverse, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ diff --git a/Python/clinic/bltinmodule.c.h b/Python/clinic/bltinmodule.c.h index 46d24cbcecdb2c5..2ff13aa61f16563 100644 --- a/Python/clinic/bltinmodule.c.h +++ b/Python/clinic/bltinmodule.c.h @@ -857,6 +857,111 @@ builtin_id(PyObject *self, PyObject *v) return return_value; } +PyDoc_STRVAR(map_new__doc__, +"map(function, iterable, /, *iterables, strict=False)\n" +"--\n" +"\n" +"Make an iterator that applies function to items of the iterables.\n" +"\n" +"The function is called with one argument from each of the iterables.\n" +"Stops when the shortest iterable is exhausted.\n" +"\n" +"If strict is true and one of the arguments is exhausted before the\n" +"others, raise a ValueError."); + +static PyObject * +map_new_impl(PyTypeObject *type, PyObject *function, PyObject *iterable, + PyObject * const *iterables, Py_ssize_t iterables_length, + int strict); + +static PyObject * +map_new_helper(PyTypeObject *type, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) +{ + PyObject *return_value = NULL; + #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) + + #define NUM_KEYWORDS 1 + static struct { + PyGC_Head _this_is_not_used; + PyObject_VAR_HEAD + Py_hash_t ob_hash; + PyObject *ob_item[NUM_KEYWORDS]; + } _kwtuple = { + .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) + .ob_hash = -1, + .ob_item = { &_Py_ID(strict), }, + }; + #undef NUM_KEYWORDS + #define KWTUPLE (&_kwtuple.ob_base.ob_base) + + #else // !Py_BUILD_CORE + # define KWTUPLE NULL + #endif // !Py_BUILD_CORE + + static const char * const _keywords[] = {"", "", "strict", NULL}; + static _PyArg_Parser _parser = { + .keywords = _keywords, + .fname = "map", + .kwtuple = KWTUPLE, + }; + #undef KWTUPLE + PyObject *argsbuf[3]; + PyObject * const *fastargs; + Py_ssize_t noptargs = Py_MIN(nargs, 2) + nkw - 2; + PyObject *function; + PyObject *iterable; + PyObject * const *iterables; + Py_ssize_t iterables_length; + int strict = 0; + + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, + /*minpos*/ 2, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 1, argsbuf); + if (!fastargs) { + goto exit; + } + function = fastargs[0]; + iterable = fastargs[1]; + if (!noptargs) { + goto skip_optional_kwonly; + } + strict = PyObject_IsTrue(fastargs[2]); + if (strict < 0) { + goto exit; + } +skip_optional_kwonly: + iterables = args + 2; + iterables_length = nargs - 2; + return_value = map_new_impl(type, function, iterable, iterables, iterables_length, strict); + +exit: + return return_value; +} + +static PyObject * +map_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return map_new_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +map_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + + assert(_PyType_CAST(type)->tp_new == map_new); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + return map_new_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); +} + PyDoc_STRVAR(builtin_setattr__doc__, "setattr($module, obj, name, value, /)\n" "--\n" @@ -1661,4 +1766,4 @@ zip_vectorcall(PyObject *type, PyObject *const *args, kwnames ? PyTuple_GET_SIZE(kwnames) : 0, NULL, kwnames); } -/*[clinic end generated code: output=a67fa5cf74c85d07 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=7f6bdf6537550b8a input=a9049054013a1b77]*/