https://github.com/python/cpython/commit/bec053c1846f0395e92381583bf5a9b855caf99a
commit: bec053c1846f0395e92381583bf5a9b855caf99a
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2025-10-28T18:14:14+05:30
summary:

[3.13] GH-140590: Fix setstate for functools.partial C-module (GH-140671) 
(#140699)

GH-140590: Fix setstate for functools.partial C-module (GH-140671)

(cherry picked from commit d26686a7f87d63499f7296c0811fa0535637a93b)

Co-authored-by: Sergey Miryanov <[email protected]>
Co-authored-by: Mikhail Efimov <[email protected]>

files:
A Misc/NEWS.d/next/Library/2025-10-27-18-29-42.gh-issue-140590.LT9HHn.rst
M Lib/test/test_functools.py
M Modules/_functoolsmodule.c

diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
index 9793df50a3d4a8..a204fd9aaf9702 100644
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -313,6 +313,7 @@ def test_setstate(self):
 
     def test_setstate_errors(self):
         f = self.partial(signature)
+
         self.assertRaises(TypeError, f.__setstate__, (capture, (), {}))
         self.assertRaises(TypeError, f.__setstate__, (capture, (), {}, {}, 
None))
         self.assertRaises(TypeError, f.__setstate__, [capture, (), {}, None])
@@ -320,6 +321,8 @@ def test_setstate_errors(self):
         self.assertRaises(TypeError, f.__setstate__, (capture, None, {}, None))
         self.assertRaises(TypeError, f.__setstate__, (capture, [], {}, None))
         self.assertRaises(TypeError, f.__setstate__, (capture, (), [], None))
+        self.assertRaises(TypeError, f.__setstate__, (capture, (), {}, ()))
+        self.assertRaises(TypeError, f.__setstate__, (capture, (), {}, 'test'))
 
     def test_setstate_subclasses(self):
         f = self.partial(signature)
diff --git 
a/Misc/NEWS.d/next/Library/2025-10-27-18-29-42.gh-issue-140590.LT9HHn.rst 
b/Misc/NEWS.d/next/Library/2025-10-27-18-29-42.gh-issue-140590.LT9HHn.rst
new file mode 100644
index 00000000000000..802183673cfacc
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-10-27-18-29-42.gh-issue-140590.LT9HHn.rst
@@ -0,0 +1,2 @@
+Fix arguments checking for the :meth:`!functools.partial.__setstate__` that
+may lead to internal state corruption and crash. Patch by Sergey Miryanov.
diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c
index 7add3987cd02f1..cbbba322a1825c 100644
--- a/Modules/_functoolsmodule.c
+++ b/Modules/_functoolsmodule.c
@@ -471,7 +471,8 @@ partial_setstate(partialobject *pto, PyObject *state)
         !PyArg_ParseTuple(state, "OOOO", &fn, &fnargs, &kw, &dict) ||
         !PyCallable_Check(fn) ||
         !PyTuple_Check(fnargs) ||
-        (kw != Py_None && !PyDict_Check(kw)))
+        (kw != Py_None && !PyDict_Check(kw)) ||
+        (dict != Py_None && !PyDict_Check(dict)))
     {
         PyErr_SetString(PyExc_TypeError, "invalid partial state");
         return 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