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

Co-authored-by: Mikhail Efimov <efimov.mikhail@gmail.com>
This commit is contained in:
Sergey Miryanov 2025-10-28 14:28:32 +05:00 committed by GitHub
parent f4e6370582
commit d26686a7f8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 7 additions and 1 deletions

View file

@ -406,6 +406,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])
@ -413,6 +414,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)

View file

@ -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.

View file

@ -778,7 +778,8 @@ partial_setstate(PyObject *self, PyObject *state)
if (!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;