Skip to content

Commit d1b94db

Browse files
BrijBrij
authored andcommitted
Add critical section in functools module for
1 parent dc12d19 commit d1b94db

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

Modules/_functoolsmodule.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,19 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
194194
if (kw != NULL) {
195195
PyObject *key, *val;
196196
Py_ssize_t pos = 0;
197+
Py_BEGIN_CRITICAL_SECTION(kw);
197198
while (PyDict_Next(kw, &pos, &key, &val)) {
198199
if (val == phold) {
199200
PyErr_SetString(PyExc_TypeError,
200201
"Placeholder cannot be passed as a keyword argument");
202+
#ifdef Py_GIL_DISABLED
203+
/* Need to release lock in case of error */
204+
PyCriticalSection_End(&_py_cs);
205+
#endif
201206
return NULL;
202207
}
203208
}
209+
Py_END_CRITICAL_SECTION();
204210
}
205211

206212
/* check wrapped function / object */
@@ -487,12 +493,15 @@ partial_vectorcall(PyObject *self, PyObject *const *args,
487493
/* Copy pto_keywords with overlapping call keywords merged
488494
* Note, tail is already coppied. */
489495
Py_ssize_t pos = 0, i = 0;
490-
while (PyDict_Next(n_merges ? pto_kw_merged : pto->kw, &pos, &key, &val)) {
496+
PyObject *keyword_dict = n_merges ? pto_kw_merged : pto->kw;
497+
Py_BEGIN_CRITICAL_SECTION(keyword_dict);
498+
while (PyDict_Next(keyword_dict, &pos, &key, &val)) {
491499
assert(i < pto_nkwds);
492500
PyTuple_SET_ITEM(tot_kwnames, i, Py_NewRef(key));
493501
stack[tot_nargs + i] = val;
494502
i++;
495503
}
504+
Py_END_CRITICAL_SECTION();
496505
assert(i == pto_nkwds);
497506
Py_XDECREF(pto_kw_merged);
498507

@@ -723,16 +732,22 @@ partial_repr(PyObject *self)
723732
}
724733
}
725734
/* Pack keyword arguments */
735+
Py_BEGIN_CRITICAL_SECTION(kw);
726736
for (i = 0; PyDict_Next(kw, &i, &key, &value);) {
727737
/* Prevent key.__str__ from deleting the value. */
728738
Py_INCREF(value);
729739
Py_SETREF(arglist, PyUnicode_FromFormat("%U, %S=%R", arglist,
730740
key, value));
731741
Py_DECREF(value);
732742
if (arglist == NULL) {
743+
#ifdef Py_GIL_DISABLED
744+
/* Need to release lock in case of error */
745+
PyCriticalSection_End(&_py_cs);
746+
#endif
733747
goto done;
734748
}
735749
}
750+
Py_END_CRITICAL_SECTION();
736751

737752
mod = PyType_GetModuleName(Py_TYPE(pto));
738753
if (mod == NULL) {
@@ -1247,10 +1262,12 @@ lru_cache_make_key(PyObject *kwd_mark, PyObject *args,
12471262
}
12481263
if (kwds_size) {
12491264
PyTuple_SET_ITEM(key, key_pos++, Py_NewRef(kwd_mark));
1265+
Py_BEGIN_CRITICAL_SECTION(kwds);
12501266
for (pos = 0; PyDict_Next(kwds, &pos, &keyword, &value);) {
12511267
PyTuple_SET_ITEM(key, key_pos++, Py_NewRef(keyword));
12521268
PyTuple_SET_ITEM(key, key_pos++, Py_NewRef(value));
12531269
}
1270+
Py_END_CRITICAL_SECTION();
12541271
assert(key_pos == PyTuple_GET_SIZE(args) + kwds_size * 2 + 1);
12551272
}
12561273
if (typed) {
@@ -1259,10 +1276,12 @@ lru_cache_make_key(PyObject *kwd_mark, PyObject *args,
12591276
PyTuple_SET_ITEM(key, key_pos++, Py_NewRef(item));
12601277
}
12611278
if (kwds_size) {
1279+
Py_BEGIN_CRITICAL_SECTION(kwds);
12621280
for (pos = 0; PyDict_Next(kwds, &pos, &keyword, &value);) {
12631281
PyObject *item = (PyObject *)Py_TYPE(value);
12641282
PyTuple_SET_ITEM(key, key_pos++, Py_NewRef(item));
12651283
}
1284+
Py_END_CRITICAL_SECTION();
12661285
}
12671286
}
12681287
assert(key_pos == key_size);

0 commit comments

Comments
 (0)