diff --git a/Misc/NEWS b/Misc/NEWS index 4ab4c1cd661..94cb5bbd6cb 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -37,6 +37,8 @@ Core and Builtins Library ------- +- Issue #10987: Fix the recursion limit handling in the _pickle module. + - Issue #10949: Improved robustness of rotating file handlers. - Issue #10955: Fix a potential crash when trying to mmap() a file past its diff --git a/Modules/_pickle.c b/Modules/_pickle.c index c47a8445f34..4eb74ffe04f 100644 --- a/Modules/_pickle.c +++ b/Modules/_pickle.c @@ -1566,7 +1566,12 @@ save_list(PicklerObject *self, PyObject *obj) iter = PyObject_GetIter(obj); if (iter == NULL) goto error; + if (Py_EnterRecursiveCall(" while pickling an object")) { + Py_DECREF(iter); + goto error; + } status = batch_list(self, iter); + Py_LeaveRecursiveCall(); Py_DECREF(iter); } @@ -1814,10 +1819,10 @@ save_dict(PicklerObject *self, PyObject *obj) if (PyDict_CheckExact(obj) && self->proto > 0) { /* We can take certain shortcuts if we know this is a dict and not a dict subclass. */ - if (Py_EnterRecursiveCall(" while pickling an object") == 0) { - status = batch_dict_exact(self, obj); - Py_LeaveRecursiveCall(); - } + if (Py_EnterRecursiveCall(" while pickling an object")) + goto error; + status = batch_dict_exact(self, obj); + Py_LeaveRecursiveCall(); } else { items = PyObject_CallMethod(obj, "items", "()"); if (items == NULL) @@ -1826,7 +1831,12 @@ save_dict(PicklerObject *self, PyObject *obj) Py_DECREF(items); if (iter == NULL) goto error; + if (Py_EnterRecursiveCall(" while pickling an object")) { + Py_DECREF(iter); + goto error; + } status = batch_dict(self, iter); + Py_LeaveRecursiveCall(); Py_DECREF(iter); } } @@ -2353,7 +2363,7 @@ save(PicklerObject *self, PyObject *obj, int pers_save) PyObject *memo_key = NULL; int status = 0; - if (Py_EnterRecursiveCall(" while pickling an object") < 0) + if (Py_EnterRecursiveCall(" while pickling an object")) return -1; /* The extra pers_save argument is necessary to avoid calling save_pers() diff --git a/Tools/scripts/find_recursionlimit.py b/Tools/scripts/find_recursionlimit.py index 2e202bec647..6f75d6da8bb 100644 --- a/Tools/scripts/find_recursionlimit.py +++ b/Tools/scripts/find_recursionlimit.py @@ -77,14 +77,15 @@ def test_cpickle(_cache={}): except ImportError: print("cannot import _pickle, skipped!") return - l = None + k, l = None, None for n in itertools.count(): try: l = _cache[n] continue # Already tried and it works, let's save some time except KeyError: for i in range(100): - l = [l] + l = [k, l] + k = {i: l} _pickle.Pickler(io.BytesIO(), protocol=-1).dump(l) _cache[n] = l