]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] GH-140590: Fix setstate for functools.partial C-module (GH-140671) (#140699)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 28 Oct 2025 12:44:14 +0000 (13:44 +0100)
committerGitHub <noreply@github.com>
Tue, 28 Oct 2025 12:44:14 +0000 (18:14 +0530)
GH-140590: Fix setstate for functools.partial C-module (GH-140671)

(cherry picked from commit d26686a7f87d63499f7296c0811fa0535637a93b)

Co-authored-by: Sergey Miryanov <sergey.miryanov@gmail.com>
Co-authored-by: Mikhail Efimov <efimov.mikhail@gmail.com>
Lib/test/test_functools.py
Misc/NEWS.d/next/Library/2025-10-27-18-29-42.gh-issue-140590.LT9HHn.rst [new file with mode: 0644]
Modules/_functoolsmodule.c

index 9793df50a3d4a810ae1745b8836a2b1828a6489a..a204fd9aaf97024692177f8078da184ff4d05d8c 100644 (file)
@@ -313,6 +313,7 @@ class TestPartial:
 
     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 @@ class TestPartial:
         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 (file)
index 0000000..8021836
--- /dev/null
@@ -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.
index 7add3987cd02f1ec37bcbf287511d868bc99efc5..cbbba322a1825cd970bf89e3ad9c066777d96b57 100644 (file)
@@ -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;