Skip to content

Commit 8ef1388

Browse files
eendebakptclaude
andcommitted
Use Argument Clinic vectorcall for map()
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 0890d65 commit 8ef1388

2 files changed

Lines changed: 106 additions & 73 deletions

File tree

‎Python/bltinmodule.c‎

Lines changed: 17 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,104 +1426,49 @@ typedef struct {
14261426

14271427
#define _mapobject_CAST(op) ((mapobject *)(op))
14281428

1429-
static PyObject *
1430-
map_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1431-
{
1432-
PyObject *it, *iters, *func;
1433-
mapobject *lz;
1434-
Py_ssize_t numargs, i;
1435-
int strict = 0;
1436-
1437-
if (kwds) {
1438-
PyObject *empty = PyTuple_New(0);
1439-
if (empty == NULL) {
1440-
return NULL;
1441-
}
1442-
static char *kwlist[] = {"strict", NULL};
1443-
int parsed = PyArg_ParseTupleAndKeywords(
1444-
empty, kwds, "|$p:map", kwlist, &strict);
1445-
Py_DECREF(empty);
1446-
if (!parsed) {
1447-
return NULL;
1448-
}
1449-
}
1450-
1451-
numargs = PyTuple_Size(args);
1452-
if (numargs < 2) {
1453-
PyErr_SetString(PyExc_TypeError,
1454-
"map() must have at least two arguments.");
1455-
return NULL;
1456-
}
1457-
1458-
iters = PyTuple_New(numargs-1);
1459-
if (iters == NULL)
1460-
return NULL;
1461-
1462-
for (i=1 ; i<numargs ; i++) {
1463-
/* Get iterator. */
1464-
it = PyObject_GetIter(PyTuple_GET_ITEM(args, i));
1465-
if (it == NULL) {
1466-
Py_DECREF(iters);
1467-
return NULL;
1468-
}
1469-
PyTuple_SET_ITEM(iters, i-1, it);
1470-
}
1471-
1472-
/* create mapobject structure */
1473-
lz = (mapobject *)type->tp_alloc(type, 0);
1474-
if (lz == NULL) {
1475-
Py_DECREF(iters);
1476-
return NULL;
1477-
}
1478-
lz->iters = iters;
1479-
func = PyTuple_GET_ITEM(args, 0);
1480-
lz->func = Py_NewRef(func);
1481-
lz->strict = strict;
1429+
/*[clinic input]
1430+
class map "mapobject *" "&PyMap_Type"
1431+
@vectorcall
1432+
@classmethod
1433+
map.__new__ as map_new
14821434
1483-
return (PyObject *)lz;
1484-
}
1435+
*args: array
1436+
strict: bool = False
1437+
[clinic start generated code]*/
14851438

14861439
static PyObject *
1487-
map_vectorcall(PyObject *type, PyObject * const*args,
1488-
size_t nargsf, PyObject *kwnames)
1440+
map_new_impl(PyTypeObject *type, PyObject * const *args,
1441+
Py_ssize_t args_length, int strict)
1442+
/*[clinic end generated code: output=a42e882981848ca5 input=f037decab456747b]*/
14891443
{
1490-
PyTypeObject *tp = _PyType_CAST(type);
1491-
1492-
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
1493-
if (kwnames != NULL && PyTuple_GET_SIZE(kwnames) != 0) {
1494-
// Fallback to map_new()
1495-
PyThreadState *tstate = _PyThreadState_GET();
1496-
return _PyObject_MakeTpCall(tstate, type, args, nargs, kwnames);
1497-
}
1498-
1499-
if (nargs < 2) {
1444+
if (args_length < 2) {
15001445
PyErr_SetString(PyExc_TypeError,
15011446
"map() must have at least two arguments.");
15021447
return NULL;
15031448
}
15041449

1505-
PyObject *iters = PyTuple_New(nargs-1);
1450+
PyObject *iters = PyTuple_New(args_length - 1);
15061451
if (iters == NULL) {
15071452
return NULL;
15081453
}
15091454

1510-
for (int i=1; i<nargs; i++) {
1455+
for (Py_ssize_t i = 1; i < args_length; i++) {
15111456
PyObject *it = PyObject_GetIter(args[i]);
15121457
if (it == NULL) {
15131458
Py_DECREF(iters);
15141459
return NULL;
15151460
}
1516-
PyTuple_SET_ITEM(iters, i-1, it);
1461+
PyTuple_SET_ITEM(iters, i - 1, it);
15171462
}
15181463

1519-
mapobject *lz = (mapobject *)tp->tp_alloc(tp, 0);
1464+
mapobject *lz = (mapobject *)type->tp_alloc(type, 0);
15201465
if (lz == NULL) {
15211466
Py_DECREF(iters);
15221467
return NULL;
15231468
}
15241469
lz->iters = iters;
15251470
lz->func = Py_NewRef(args[0]);
1526-
lz->strict = 0;
1471+
lz->strict = strict;
15271472

15281473
return (PyObject *)lz;
15291474
}

‎Python/clinic/bltinmodule.c.h‎

Lines changed: 89 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)