Skip to content

Commit 2eacf5e

Browse files
committed
push placeholder check to earlier place
1 parent 70e47ed commit 2eacf5e

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

Modules/_functoolsmodule.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ get_functools_state_by_type(PyTypeObject *type)
147147
static PyObject *
148148
partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
149149
{
150-
PyObject *func, *pto_args, *new_args, *pto_kw;
150+
PyObject *func, *pto_args, *new_args, *pto_kw, *phold;
151151
partialobject *pto;
152152
Py_ssize_t pto_phcount = 0;
153153
Py_ssize_t new_nargs = PyTuple_GET_SIZE(args) - 1;
@@ -168,7 +168,16 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
168168
if (state == NULL) {
169169
return NULL;
170170
}
171+
phold = state->placeholder;
171172

173+
/* Placeholder restrictions */
174+
if (new_nargs && PyTuple_GET_ITEM(args, new_nargs) == phold) {
175+
PyErr_SetString(PyExc_TypeError,
176+
"trailing Placeholders are not allowed");
177+
return NULL;
178+
}
179+
180+
/* check wrapped function / object */
172181
pto_args = pto_kw = NULL;
173182
int res = PyObject_TypeCheck(func, state->partial_type);
174183
if (res == -1) {
@@ -193,13 +202,7 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
193202
return NULL;
194203

195204
pto->fn = Py_NewRef(func);
196-
197-
pto->placeholder = state->placeholder;
198-
if (new_nargs && PyTuple_GET_ITEM(args, new_nargs) == pto->placeholder) {
199-
PyErr_SetString(PyExc_TypeError,
200-
"trailing Placeholders are not allowed");
201-
return NULL;
202-
}
205+
pto->placeholder = phold;
203206

204207
new_args = PyTuple_GetSlice(args, 1, new_nargs + 1);
205208
if (new_args == NULL) {
@@ -210,7 +213,7 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
210213
/* Count placeholders */
211214
Py_ssize_t phcount = 0;
212215
for (Py_ssize_t i = 0; i < new_nargs - 1; i++) {
213-
if (PyTuple_GET_ITEM(new_args, i) == pto->placeholder) {
216+
if (PyTuple_GET_ITEM(new_args, i) == phold) {
214217
phcount++;
215218
}
216219
}
@@ -226,7 +229,7 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
226229
for (Py_ssize_t i = 0, j = 0; i < tot_nargs; i++) {
227230
if (i < npargs) {
228231
item = PyTuple_GET_ITEM(pto_args, i);
229-
if (j < new_nargs && item == pto->placeholder) {
232+
if (j < new_nargs && item == phold) {
230233
item = PyTuple_GET_ITEM(new_args, j);
231234
j++;
232235
pto_phcount--;

0 commit comments

Comments
 (0)