https://github.com/python/cpython/commit/17eb0354ff3110b27f811343c2d4b3c85f2685d5
commit: 17eb0354ff3110b27f811343c2d4b3c85f2685d5
branch: main
author: bkap123 <[email protected]>
committer: encukou <[email protected]>
date: 2026-03-12T14:46:37+01:00
summary:

gh-145446: Add critical section in functools module for `PyDict_Next` 
(GH-145487)

files:
A Misc/NEWS.d/next/Library/2026-03-03-23-21-40.gh-issue-145446.0c-TJX.rst
M Modules/_functoolsmodule.c

diff --git 
a/Misc/NEWS.d/next/Library/2026-03-03-23-21-40.gh-issue-145446.0c-TJX.rst 
b/Misc/NEWS.d/next/Library/2026-03-03-23-21-40.gh-issue-145446.0c-TJX.rst
new file mode 100644
index 00000000000000..96eb0d9ddb07ab
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-03-03-23-21-40.gh-issue-145446.0c-TJX.rst
@@ -0,0 +1 @@
+Now :mod:`functools` is safer in free-threaded build when using keywords in 
:func:`functools.partial`
diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c
index 336a2e57ec0179..723080ede1d9ae 100644
--- a/Modules/_functoolsmodule.c
+++ b/Modules/_functoolsmodule.c
@@ -492,12 +492,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);
 
@@ -728,6 +731,8 @@ 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);
@@ -735,9 +740,14 @@ partial_repr(PyObject *self)
                                                 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) {

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to