Skip to content

Commit 0260431

Browse files
miss-islingtonsergey-miryanovefimov-mikhail
authored
[3.14] pythonGH-140590: Fix setstate for functools.partial C-module (pythonGH-140671) (python#140698)
pythonGH-140590: Fix setstate for functools.partial C-module (pythonGH-140671) (cherry picked from commit d26686a) Co-authored-by: Sergey Miryanov <[email protected]> Co-authored-by: Mikhail Efimov <[email protected]>
1 parent 84e01df commit 0260431

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

Lib/test/test_functools.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,13 +406,16 @@ def test_setstate(self):
406406

407407
def test_setstate_errors(self):
408408
f = self.partial(signature)
409+
409410
self.assertRaises(TypeError, f.__setstate__, (capture, (), {}))
410411
self.assertRaises(TypeError, f.__setstate__, (capture, (), {}, {}, None))
411412
self.assertRaises(TypeError, f.__setstate__, [capture, (), {}, None])
412413
self.assertRaises(TypeError, f.__setstate__, (None, (), {}, None))
413414
self.assertRaises(TypeError, f.__setstate__, (capture, None, {}, None))
414415
self.assertRaises(TypeError, f.__setstate__, (capture, [], {}, None))
415416
self.assertRaises(TypeError, f.__setstate__, (capture, (), [], None))
417+
self.assertRaises(TypeError, f.__setstate__, (capture, (), {}, ()))
418+
self.assertRaises(TypeError, f.__setstate__, (capture, (), {}, 'test'))
416419

417420
def test_setstate_subclasses(self):
418421
f = self.partial(signature)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix arguments checking for the :meth:`!functools.partial.__setstate__` that
2+
may lead to internal state corruption and crash. Patch by Sergey Miryanov.

Modules/_functoolsmodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,8 @@ partial_setstate(PyObject *self, PyObject *state)
699699
if (!PyArg_ParseTuple(state, "OOOO", &fn, &fnargs, &kw, &dict) ||
700700
!PyCallable_Check(fn) ||
701701
!PyTuple_Check(fnargs) ||
702-
(kw != Py_None && !PyDict_Check(kw)))
702+
(kw != Py_None && !PyDict_Check(kw)) ||
703+
(dict != Py_None && !PyDict_Check(dict)))
703704
{
704705
PyErr_SetString(PyExc_TypeError, "invalid partial state");
705706
return NULL;

0 commit comments

Comments
 (0)