Skip to content

gh-119109: functools.partial.fallforward fix #119125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ typedef struct {
PyObject *dict; /* __dict__ */
PyObject *weakreflist; /* List of weak references */
vectorcallfunc vectorcall;
int can_vcall; /* Cache whether function allows vector call */
} partialobject;

static void partial_setvectorcall(partialobject *pto);
Expand Down Expand Up @@ -198,32 +199,21 @@ partial_dealloc(partialobject *pto)
}


/* Merging keyword arguments using the vectorcall convention is messy, so
* if we would need to do that, we stop using vectorcall and fall back
* to using partial_call() instead. */
Py_NO_INLINE static PyObject *
partial_vectorcall_fallback(PyThreadState *tstate, partialobject *pto,
PyObject *const *args, size_t nargsf,
PyObject *kwnames)
{
pto->vectorcall = NULL;
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
return _PyObject_MakeTpCall(tstate, (PyObject *)pto,
args, nargs, kwnames);
}

static PyObject *
partial_vectorcall(partialobject *pto, PyObject *const *args,
size_t nargsf, PyObject *kwnames)
{
PyThreadState *tstate = _PyThreadState_GET();

Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
/* pto->kw is mutable, so need to check every time */
if (PyDict_GET_SIZE(pto->kw)) {
return partial_vectorcall_fallback(tstate, pto, args, nargsf, kwnames);
/* Merging keyword arguments using the vectorcall convention is messy, so
* if we would need to do that, we stop using vectorcall and fall back
* to using partial_call() instead. */
pto->vectorcall = NULL;
return _PyObject_MakeTpCall(tstate, (PyObject *)pto, args, nargs, kwnames);
}

Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
Py_ssize_t nargs_total = nargs;
if (kwnames != NULL) {
nargs_total += PyTuple_GET_SIZE(kwnames);
Expand Down Expand Up @@ -286,12 +276,14 @@ partial_setvectorcall(partialobject *pto)
if (PyVectorcall_Function(pto->fn) == NULL) {
/* Don't use vectorcall if the underlying function doesn't support it */
pto->vectorcall = NULL;
pto->can_vcall = 0;
}
/* We could have a special case if there are no arguments,
* but that is unlikely (why use partial without arguments?),
* so we don't optimize that */
else {
pto->vectorcall = (vectorcallfunc)partial_vectorcall;
pto->can_vcall = 1;
}
}

Expand All @@ -304,9 +296,17 @@ partial_call(partialobject *pto, PyObject *args, PyObject *kwargs)
assert(PyTuple_Check(pto->args));
assert(PyDict_Check(pto->kw));

/* pto->kw is mutable, so need to check every time
* if kwds are empty to switch to more efficient vectorcall */
Py_ssize_t pto_nkwargs = PyDict_GET_SIZE(pto->kw);
if ((pto_nkwargs == 0) & pto->can_vcall){
pto->vectorcall = (vectorcallfunc)partial_vectorcall;
return PyObject_Call((PyObject *)pto, args, kwargs);
}

/* Merge keywords */
PyObject *kwargs2;
if (PyDict_GET_SIZE(pto->kw) == 0) {
if (pto_nkwargs == 0) {
/* kwargs can be NULL */
kwargs2 = Py_XNewRef(kwargs);
}
Expand Down