Skip to content

Commit a6c6ef2

Browse files
committed
CR Part 2
1 parent fd16189 commit a6c6ef2

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

Lib/functools.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,14 +305,13 @@ def _partial_prepare_merger(args):
305305
if not nargs:
306306
return 0, None
307307
order = []
308-
i, j = 0, nargs
309-
for a in args:
308+
j = nargs
309+
for i, a in enumerate(args):
310310
if a is Placeholder:
311311
order.append(j)
312312
j += 1
313313
else:
314314
order.append(i)
315-
i += 1
316315
phcount = j - nargs
317316
merger = itemgetter(*order) if phcount else None
318317
return phcount, merger

Modules/_functoolsmodule.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ get_functools_state(PyObject *module)
4747
// The 'Placeholder' singleton indicates which formal positional
4848
// parameters are to be bound first when using a 'partial' object.
4949

50-
static PyObject* placeholder_instance;
51-
5250
typedef struct {
5351
PyObject_HEAD
5452
} placeholderobject;
5553

54+
static inline _functools_state *
55+
get_functools_state_by_type(PyTypeObject *type);
56+
5657
PyDoc_STRVAR(placeholder_doc,
5758
"The type of the Placeholder singleton.\n\n"
5859
"Used as a placeholder for partial arguments.");
@@ -91,10 +92,11 @@ placeholder_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
9192
PyErr_SetString(PyExc_TypeError, "PlaceholderType takes no arguments");
9293
return NULL;
9394
}
94-
if (placeholder_instance == NULL) {
95-
placeholder_instance = PyType_GenericNew(type, NULL, NULL);
95+
_functools_state *state = get_functools_state_by_type(type);
96+
if (state->placeholder == NULL) {
97+
state->placeholder = PyType_GenericNew(type, NULL, NULL);
9698
}
97-
return placeholder_instance;
99+
return state->placeholder;
98100
}
99101

100102
static PyType_Slot placeholder_type_slots[] = {
@@ -209,7 +211,7 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
209211
/* Count placeholders */
210212
Py_ssize_t phcount = 0;
211213
for (Py_ssize_t i = 0; i < new_nargs - 1; i++) {
212-
if (Py_Is(PyTuple_GET_ITEM(new_args, i), pto->placeholder)) {
214+
if (PyTuple_GET_ITEM(new_args, i) == pto->placeholder) {
213215
phcount++;
214216
}
215217
}
@@ -225,7 +227,7 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
225227
for (Py_ssize_t i = 0, j = 0; i < tot_nargs; i++) {
226228
if (i < npargs) {
227229
item = PyTuple_GET_ITEM(pto_args, i);
228-
if ((j < new_nargs) && Py_Is(item, pto->placeholder)) {
230+
if (j < new_nargs && item == pto->placeholder) {
229231
item = PyTuple_GET_ITEM(new_args, j);
230232
j++;
231233
pto_phcount--;
@@ -678,7 +680,7 @@ partial_setstate(partialobject *pto, PyObject *state)
678680
/* Count placeholders */
679681
Py_ssize_t phcount = 0;
680682
for (Py_ssize_t i = 0; i < nargs - 1; i++) {
681-
if (PyTuple_GET_ITEM(fnargs, i), pto->placeholder) {
683+
if (PyTuple_GET_ITEM(fnargs, i) == pto->placeholder) {
682684
phcount++;
683685
}
684686
}

0 commit comments

Comments
 (0)