Skip to content

Commit d840ad7

Browse files
committed
V3
1 parent f23021c commit d840ad7

File tree

1 file changed

+31
-43
lines changed

1 file changed

+31
-43
lines changed

Modules/_functoolsmodule.c

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -429,15 +429,31 @@ partial_vectorcall(partialobject *pto, PyObject *const *args,
429429
return ret;
430430
}
431431
else {
432+
PyObject *tot_kw = NULL;
433+
PyObject *tot_kwnames = NULL;
432434
PyObject *key, *val;
433-
tot_nkwds = PyDict_GET_SIZE(pto->kw) + nkwds;
434-
for (Py_ssize_t i = 0; i < nkwds; i++) {
435-
key = PyTuple_GET_ITEM(kwnames, i);
436-
if (PyDict_Contains(pto->kw, key)) {
437-
tot_nkwds--;
435+
int has_nkwds = 0;
436+
437+
if (!nkwds) {
438+
tot_kw = pto->kw;
439+
tot_nkwds = pto_nkwds;
440+
tot_nargskw = tot_nargs + tot_nkwds;
441+
}
442+
else {
443+
has_nkwds = 1;
444+
tot_kw = PyDict_Copy(pto->kw);
445+
for (Py_ssize_t i=0; i < nkwds; i++) {
446+
key = PyTuple_GET_ITEM(kwnames, i);
447+
val = args[nargs + i];
448+
if (PyDict_SetItem(tot_kw, key, val)) {
449+
Py_DECREF(tot_kw);
450+
return NULL;
451+
}
438452
}
453+
454+
tot_nkwds = PyDict_GET_SIZE(tot_kw);
455+
tot_nargskw = tot_nargs + tot_nkwds;
439456
}
440-
tot_nargskw = tot_nargs + tot_nkwds;
441457

442458
/* Allocate Stack */
443459
PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
@@ -477,46 +493,18 @@ partial_vectorcall(partialobject *pto, PyObject *const *args,
477493
}
478494

479495
/* Copy Keywords to new stack */
480-
PyObject *tot_kwnames = PyTuple_New(tot_nkwds);
481-
if (tot_kwnames == NULL) {
482-
return NULL;
496+
tot_kwnames = PyTuple_New(tot_nkwds);
497+
Py_ssize_t pos = 0, i = 0;
498+
while (PyDict_Next(tot_kw, &pos, &key, &val)) {
499+
PyTuple_SET_ITEM(tot_kwnames, i, key);
500+
Py_INCREF(key);
501+
stack[tot_nargs + i] = val;
502+
i += 1;
483503
}
484-
if (nkwds) {
485-
/* Merge keywords with pto->kw */
486-
PyObject *tot_kw = PyDict_Copy(pto->kw);
487-
if (tot_kw == NULL) {
488-
Py_DECREF(tot_kwnames);
489-
return NULL;
490-
}
491-
for (Py_ssize_t i=0; i < nkwds; i++) {
492-
key = PyTuple_GET_ITEM(kwnames, i);
493-
val = args[nargs + i];
494-
if (PyDict_SetItem(tot_kw, key, val)) {
495-
Py_DECREF(tot_kwnames);
496-
Py_DECREF(tot_kw);
497-
return NULL;
498-
}
499-
}
500-
501-
Py_ssize_t pos = 0, i = 0;
502-
while (PyDict_Next(tot_kw, &pos, &key, &val)) {
503-
PyTuple_SET_ITEM(tot_kwnames, i, key);
504-
Py_INCREF(key);
505-
stack[tot_nargs + i] = val;
506-
i += 1;
507-
}
504+
if (has_nkwds) {
508505
Py_DECREF(tot_kw);
509506
}
510-
else {
511-
/* Call with pto->kw only */
512-
Py_ssize_t pos = 0, i = 0;
513-
while (PyDict_Next(pto->kw, &pos, &key, &val)) {
514-
PyTuple_SET_ITEM(tot_kwnames, i, key);
515-
Py_INCREF(key);
516-
stack[tot_nargs + i] = val;
517-
i += 1;
518-
}
519-
}
507+
520508
ret = _PyObject_VectorcallTstate(tstate, pto->fn, stack, tot_nargs, tot_kwnames);
521509
Py_DECREF(tot_kwnames);
522510

0 commit comments

Comments
 (0)