From 8ef138824e09264d80ff960cffdd78f3b2c6e82f Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Sun, 22 Nov 2026 20:33:26 +0100 Subject: [PATCH 1/4] Use Argument Clinic vectorcall for map() Co-Authored-By: Claude Fable 5.2 --- Python/bltinmodule.c | 89 +++++++--------------------------- Python/clinic/bltinmodule.c.h | 90 ++++++++++++++++++++++++++++++++++- 2 files changed, 106 insertions(+), 73 deletions(-) diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 72f2e356c34f7c..1f46d470432f2c 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1426,104 +1426,49 @@ 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; - } - - iters = PyTuple_New(numargs-1); - if (iters == NULL) - return NULL; - - 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; +/*[clinic input] +class map "mapobject *" "&PyMap_Type" +@vectorcall +@classmethod +map.__new__ as map_new - return (PyObject *)lz; -} + *args: array + strict: bool = False +[clinic start generated code]*/ static PyObject * -map_vectorcall(PyObject *type, PyObject * const*args, - size_t nargsf, PyObject *kwnames) +map_new_impl(PyTypeObject *type, PyObject * const *args, + Py_ssize_t args_length, int strict) +/*[clinic end generated code: output=a42e882981848ca5 input=f037decab456747b]*/ { - 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) { + if (args_length < 2) { PyErr_SetString(PyExc_TypeError, "map() must have at least two arguments."); return NULL; } - PyObject *iters = PyTuple_New(nargs-1); + PyObject *iters = PyTuple_New(args_length - 1); if (iters == NULL) { 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->strict = strict; return (PyObject *)lz; } diff --git a/Python/clinic/bltinmodule.c.h b/Python/clinic/bltinmodule.c.h index 46d24cbcecdb2c..2440ea867cd642 100644 --- a/Python/clinic/bltinmodule.c.h +++ b/Python/clinic/bltinmodule.c.h @@ -857,6 +857,94 @@ builtin_id(PyObject *self, PyObject *v) return return_value; } +static PyObject * +map_new_impl(PyTypeObject *type, PyObject * const *args, + Py_ssize_t args_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[1]; + PyObject * const *fastargs; + Py_ssize_t noptargs = 0 + nkw - 0; + PyObject * const *__clinic_args; + Py_ssize_t args_length; + int strict = 0; + + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, + /*minpos*/ 0, /*maxpos*/ 0, /*minkw*/ 0, /*varpos*/ 1, argsbuf); + if (!fastargs) { + goto exit; + } + if (!noptargs) { + goto skip_optional_kwonly; + } + strict = PyObject_IsTrue(fastargs[0]); + if (strict < 0) { + goto exit; + } +skip_optional_kwonly: + __clinic_args = args; + args_length = nargs; + return_value = map_new_impl(type, __clinic_args, args_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 +1749,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=d4c5d02ec7922900 input=a9049054013a1b77]*/ From 8fcec2d132d0d1e1a46fce64ab851ae251006087 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Sun, 22 Nov 2026 20:52:16 +0100 Subject: [PATCH 2/4] Use the documented signature in the clinic input of map() Co-Authored-By: Claude Fable 5.2 --- Python/bltinmodule.c | 32 ++++++++++++++++++-------------- Python/clinic/bltinmodule.c.h | 31 ++++++++++++++++++------------- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 1f46d470432f2c..1b941800f2d208 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1432,33 +1432,37 @@ class map "mapobject *" "&PyMap_Type" @classmethod map.__new__ as map_new - *args: array + function: object + iterable: object + / + *iterables: array strict: bool = False [clinic start generated code]*/ static PyObject * -map_new_impl(PyTypeObject *type, PyObject * const *args, - Py_ssize_t args_length, int strict) -/*[clinic end generated code: output=a42e882981848ca5 input=f037decab456747b]*/ +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=5fbe90bd74aaa2e2]*/ { - if (args_length < 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(args_length - 1); - if (iters == NULL) { + PyObject *it = PyObject_GetIter(iterable); + if (it == NULL) { + Py_DECREF(iters); return NULL; } - - for (Py_ssize_t i = 1; i < args_length; i++) { - PyObject *it = PyObject_GetIter(args[i]); + PyTuple_SET_ITEM(iters, 0, it); + for (Py_ssize_t i = 0; i < iterables_length; i++) { + it = PyObject_GetIter(iterables[i]); if (it == NULL) { Py_DECREF(iters); return NULL; } - PyTuple_SET_ITEM(iters, i - 1, it); + PyTuple_SET_ITEM(iters, i + 1, it); } mapobject *lz = (mapobject *)type->tp_alloc(type, 0); @@ -1467,7 +1471,7 @@ map_new_impl(PyTypeObject *type, PyObject * const *args, return NULL; } lz->iters = iters; - lz->func = Py_NewRef(args[0]); + lz->func = Py_NewRef(function); lz->strict = strict; return (PyObject *)lz; diff --git a/Python/clinic/bltinmodule.c.h b/Python/clinic/bltinmodule.c.h index 2440ea867cd642..c2fd423df7776d 100644 --- a/Python/clinic/bltinmodule.c.h +++ b/Python/clinic/bltinmodule.c.h @@ -858,8 +858,9 @@ builtin_id(PyObject *self, PyObject *v) } static PyObject * -map_new_impl(PyTypeObject *type, PyObject * const *args, - Py_ssize_t args_length, int strict); +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, @@ -886,36 +887,40 @@ map_new_helper(PyTypeObject *type, PyObject *const *args, # define KWTUPLE NULL #endif // !Py_BUILD_CORE - static const char * const _keywords[] = {"strict", NULL}; + static const char * const _keywords[] = {"", "", "strict", NULL}; static _PyArg_Parser _parser = { .keywords = _keywords, .fname = "map", .kwtuple = KWTUPLE, }; #undef KWTUPLE - PyObject *argsbuf[1]; + PyObject *argsbuf[3]; PyObject * const *fastargs; - Py_ssize_t noptargs = 0 + nkw - 0; - PyObject * const *__clinic_args; - Py_ssize_t args_length; + 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*/ 0, /*maxpos*/ 0, /*minkw*/ 0, /*varpos*/ 1, argsbuf); + /*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[0]); + strict = PyObject_IsTrue(fastargs[2]); if (strict < 0) { goto exit; } skip_optional_kwonly: - __clinic_args = args; - args_length = nargs; - return_value = map_new_impl(type, __clinic_args, args_length, strict); + iterables = args + 2; + iterables_length = nargs - 2; + return_value = map_new_impl(type, function, iterable, iterables, iterables_length, strict); exit: return return_value; @@ -1749,4 +1754,4 @@ zip_vectorcall(PyObject *type, PyObject *const *args, kwnames ? PyTuple_GET_SIZE(kwnames) : 0, NULL, kwnames); } -/*[clinic end generated code: output=d4c5d02ec7922900 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=5d5ebfb0a3e33097 input=a9049054013a1b77]*/ From 38f90d685aec429dfcabbb4c67def64c286cd4e5 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Sun, 22 Nov 2026 21:23:30 +0100 Subject: [PATCH 3/4] Generate the map() docstring with Argument Clinic Co-Authored-By: Claude Fable 5.2 --- Python/bltinmodule.c | 22 ++++++++++------------ Python/clinic/bltinmodule.c.h | 14 +++++++++++++- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 1b941800f2d208..13d19ce96f872d 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1437,13 +1437,21 @@ map.__new__ as map_new / *iterables: array strict: bool = False + +Make an iterator that applies function to items of the iterables. + +The function is called with one argument from each of the iterables. +Stops when the shortest iterable is exhausted. + +If strict is true and one of the arguments is exhausted before the +others, raise a ValueError. [clinic start generated code]*/ static PyObject * 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=5fbe90bd74aaa2e2]*/ +/*[clinic end generated code: output=de1354b02d08604b input=d3bf952f6bb775c8]*/ { PyObject *iters = PyTuple_New(iterables_length + 1); if (iters == NULL) { @@ -1629,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 */ @@ -1662,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 c2fd423df7776d..2ff13aa61f1656 100644 --- a/Python/clinic/bltinmodule.c.h +++ b/Python/clinic/bltinmodule.c.h @@ -857,6 +857,18 @@ 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, @@ -1754,4 +1766,4 @@ zip_vectorcall(PyObject *type, PyObject *const *args, kwnames ? PyTuple_GET_SIZE(kwnames) : 0, NULL, kwnames); } -/*[clinic end generated code: output=5d5ebfb0a3e33097 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=7f6bdf6537550b8a input=a9049054013a1b77]*/ From 505c2b459ac109c7c0124fb281aff088caf6fa2b Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 23 Nov 2026 11:25:29 +0100 Subject: [PATCH 4/4] Restore the original wording of the map() docstring Co-Authored-By: Claude Fable 5.2 --- Python/bltinmodule.c | 6 +++--- Python/clinic/bltinmodule.c.h | 5 ++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 13d19ce96f872d..78bd26f72470c5 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1428,6 +1428,7 @@ typedef struct { /*[clinic input] class map "mapobject *" "&PyMap_Type" +@permit_long_summary @vectorcall @classmethod map.__new__ as map_new @@ -1438,9 +1439,8 @@ map.__new__ as map_new *iterables: array strict: bool = False -Make an iterator that applies function to items of the iterables. +Make an iterator that computes the function using arguments from each of the iterables. -The function is called with one argument from each of the iterables. Stops when the shortest iterable is exhausted. If strict is true and one of the arguments is exhausted before the @@ -1451,7 +1451,7 @@ static PyObject * 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]*/ +/*[clinic end generated code: output=de1354b02d08604b input=8a35a5f93c39cbcf]*/ { PyObject *iters = PyTuple_New(iterables_length + 1); if (iters == NULL) { diff --git a/Python/clinic/bltinmodule.c.h b/Python/clinic/bltinmodule.c.h index 2ff13aa61f1656..caf485976ca3b9 100644 --- a/Python/clinic/bltinmodule.c.h +++ b/Python/clinic/bltinmodule.c.h @@ -861,9 +861,8 @@ 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" +"Make an iterator that computes the function using arguments from each 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" @@ -1766,4 +1765,4 @@ zip_vectorcall(PyObject *type, PyObject *const *args, kwnames ? PyTuple_GET_SIZE(kwnames) : 0, NULL, kwnames); } -/*[clinic end generated code: output=7f6bdf6537550b8a input=a9049054013a1b77]*/ +/*[clinic end generated code: output=26ee4ad6d2521b8a input=a9049054013a1b77]*/