Skip to content

Commit 5d715c5

Browse files
committed
avoid looping over the dict twice
1 parent 09e3ee3 commit 5d715c5

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

Modules/_operator.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,14 +1618,16 @@ methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
16181618
nargs * sizeof(PyObject *));
16191619
}
16201620
if (kwds) {
1621-
mc->vectorcall_kwnames = PySequence_Tuple(kwds);
1621+
const Py_ssize_t nkwds = PyDict_Size(kwds);
1622+
1623+
mc->vectorcall_kwnames = PyTuple_New(nkwds);
16221624
if (!mc->vectorcall_kwnames) {
16231625
return NULL;
16241626
}
1625-
Py_ssize_t i = 0;
1626-
Py_ssize_t ppos = 0;
1627+
Py_ssize_t i = 0, ppos = 0;
16271628
while (PyDict_Next(kwds, &ppos, &key, &value)) {
1628-
mc->vectorcall_args[ nargs + i] = value;
1629+
PyTuple_SET_ITEM(mc->vectorcall_kwnames, i, Py_NewRef(key));
1630+
mc->vectorcall_args[nargs + i] = value; // borrowed reference
16291631
++i;
16301632
}
16311633
}

0 commit comments

Comments
 (0)