Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Now :mod:`functools` is safer in free-threaded build when using keywords in :func:`functools.partial`
31 changes: 26 additions & 5 deletions Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,20 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
if (kw != NULL) {
PyObject *key, *val;
Py_ssize_t pos = 0;
int error = 0;
Py_BEGIN_CRITICAL_SECTION(kw);
while (PyDict_Next(kw, &pos, &key, &val)) {
if (val == phold) {
PyErr_SetString(PyExc_TypeError,
"Placeholder cannot be passed as a keyword argument");
return NULL;
error = 1;
break;
}
}
Py_END_CRITICAL_SECTION();
if (error) {
PyErr_SetString(PyExc_TypeError,
"Placeholder cannot be passed as a keyword argument");
return NULL;
}
}

/* check wrapped function / object */
Expand Down Expand Up @@ -487,12 +494,15 @@ partial_vectorcall(PyObject *self, PyObject *const *args,
/* Copy pto_keywords with overlapping call keywords merged
* Note, tail is already coppied. */
Py_ssize_t pos = 0, i = 0;
while (PyDict_Next(n_merges ? pto_kw_merged : pto->kw, &pos, &key, &val)) {
PyObject *keyword_dict = n_merges ? pto_kw_merged : pto->kw;
Py_BEGIN_CRITICAL_SECTION(keyword_dict);
while (PyDict_Next(keyword_dict, &pos, &key, &val)) {
assert(i < pto_nkwds);
PyTuple_SET_ITEM(tot_kwnames, i, Py_NewRef(key));
stack[tot_nargs + i] = val;
i++;
}
Py_END_CRITICAL_SECTION();
assert(i == pto_nkwds);
Py_XDECREF(pto_kw_merged);

Expand Down Expand Up @@ -723,16 +733,23 @@ partial_repr(PyObject *self)
}
}
/* Pack keyword arguments */
int error = 0;
Py_BEGIN_CRITICAL_SECTION(kw);
for (i = 0; PyDict_Next(kw, &i, &key, &value);) {
/* Prevent key.__str__ from deleting the value. */
Py_INCREF(value);
Py_SETREF(arglist, PyUnicode_FromFormat("%U, %S=%R", arglist,
key, value));
Py_DECREF(value);
if (arglist == NULL) {
goto done;
error = 1;
break;
}
}
Py_END_CRITICAL_SECTION();
if (error) {
goto done;
}

mod = PyType_GetModuleName(Py_TYPE(pto));
if (mod == NULL) {
Expand Down Expand Up @@ -1247,10 +1264,12 @@ lru_cache_make_key(PyObject *kwd_mark, PyObject *args,
}
if (kwds_size) {
PyTuple_SET_ITEM(key, key_pos++, Py_NewRef(kwd_mark));
Py_BEGIN_CRITICAL_SECTION(kwds);
for (pos = 0; PyDict_Next(kwds, &pos, &keyword, &value);) {
PyTuple_SET_ITEM(key, key_pos++, Py_NewRef(keyword));
PyTuple_SET_ITEM(key, key_pos++, Py_NewRef(value));
}
Py_END_CRITICAL_SECTION();
assert(key_pos == PyTuple_GET_SIZE(args) + kwds_size * 2 + 1);
}
if (typed) {
Expand All @@ -1259,10 +1278,12 @@ lru_cache_make_key(PyObject *kwd_mark, PyObject *args,
PyTuple_SET_ITEM(key, key_pos++, Py_NewRef(item));
}
if (kwds_size) {
Py_BEGIN_CRITICAL_SECTION(kwds);
for (pos = 0; PyDict_Next(kwds, &pos, &keyword, &value);) {
PyObject *item = (PyObject *)Py_TYPE(value);
PyTuple_SET_ITEM(key, key_pos++, Py_NewRef(item));
}
Py_END_CRITICAL_SECTION();
}
}
assert(key_pos == key_size);
Expand Down
Loading