diff -r 72cca30f4707 Lib/test/test_functools.py --- a/Lib/test/test_functools.py Mon Nov 02 14:44:29 2015 +0200 +++ b/Lib/test/test_functools.py Tue Nov 03 10:36:58 2015 +0200 @@ -207,6 +207,24 @@ class TestPartialC(TestPartial, unittest ['{}({!r}, {}, {})'.format(name, capture, args_repr, kwargs_repr) for kwargs_repr in kwargs_reprs]) + def test_recursive_repr(self): + if self.partial is c_functools.partial: + name = 'functools.partial' + else: + name = self.partial.__name__ + + f = self.partial(capture) + f.__setstate__((f, (), {}, {})) + self.assertEqual(repr(f), '%s(%s(...))' % (name, name)) + + f = self.partial(capture) + f.__setstate__((capture, (f,), {}, {})) + self.assertEqual(repr(f), '%s(%r, %s(...))' % (name, capture, name)) + + f = self.partial(capture) + f.__setstate__((capture, (), {'a': f}, {})) + self.assertEqual(repr(f), '%s(%r, a=%s(...))' % (name, capture, name)) + def test_pickle(self): f = self.partial(signature, 'asdf', bar=True) f.add_something_to__dict__ = True @@ -214,6 +232,25 @@ class TestPartialC(TestPartial, unittest f_copy = pickle.loads(pickle.dumps(f, proto)) self.assertEqual(signature(f), signature(f_copy)) + def test_recursive_pickle(self): + f = self.partial(capture) + f.__setstate__((f, (), {}, {})) + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + with self.assertRaises(RecursionError): + pickle.dumps(f, proto) + + f = self.partial(capture) + f.__setstate__((capture, (f,), {}, {})) + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + f_copy = pickle.loads(pickle.dumps(f, proto)) + self.assertIs(f_copy.args[0], f_copy) + + f = self.partial(capture) + f.__setstate__((capture, (), {'a': f}, {})) + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + f_copy = pickle.loads(pickle.dumps(f, proto)) + self.assertIs(f_copy.keywords['a'], f_copy) + # Issue 6083: Reference counting bug def test_setstate_refcount(self): class BadSequence: diff -r 72cca30f4707 Modules/_functoolsmodule.c --- a/Modules/_functoolsmodule.c Mon Nov 02 14:44:29 2015 +0200 +++ b/Modules/_functoolsmodule.c Tue Nov 03 10:36:58 2015 +0200 @@ -217,9 +217,18 @@ partial_repr(partialobject *pto) PyObject *arglist; PyObject *tmp; Py_ssize_t i, n; + int status; + + status = Py_ReprEnter((PyObject *)pto); + if (status != 0) { + if (status < 0) + return NULL; + return PyUnicode_FromFormat("%s(...)", Py_TYPE(pto)->tp_name); + } arglist = PyUnicode_FromString(""); if (arglist == NULL) { + Py_ReprLeave((PyObject *)pto); return NULL; } /* Pack positional arguments */ @@ -229,8 +238,10 @@ partial_repr(partialobject *pto) tmp = PyUnicode_FromFormat("%U, %R", arglist, PyTuple_GET_ITEM(pto->args, i)); Py_DECREF(arglist); - if (tmp == NULL) + if (tmp == NULL) { + Py_ReprLeave((PyObject *)pto); return NULL; + } arglist = tmp; } /* Pack keyword arguments */ @@ -241,14 +252,17 @@ partial_repr(partialobject *pto) tmp = PyUnicode_FromFormat("%U, %U=%R", arglist, key, value); Py_DECREF(arglist); - if (tmp == NULL) + if (tmp == NULL) { + Py_ReprLeave((PyObject *)pto); return NULL; + } arglist = tmp; } } result = PyUnicode_FromFormat("%s(%R%U)", Py_TYPE(pto)->tp_name, pto->fn, arglist); Py_DECREF(arglist); + Py_ReprLeave((PyObject *)pto); return result; }