summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-11-27 18:46:34 (GMT)
committerGitHub <noreply@github.com>2023-11-27 18:46:34 (GMT)
commit054d18e883c23d7bca71714c0573c7f32f3a760b (patch)
treea9f2bb325ea68aa6354eef95e363e53f41569195
parent581b24415525f10ca178d5537e9cbd7558ce938a (diff)
downloadcpython-054d18e883c23d7bca71714c0573c7f32f3a760b.zip
cpython-054d18e883c23d7bca71714c0573c7f32f3a760b.tar.gz
cpython-054d18e883c23d7bca71714c0573c7f32f3a760b.tar.bz2
[3.11] bpo-41422: Visit the Pickler's and Unpickler's memo in tp_traverse (GH-21664) (GH-112465)
(cherry picked from commit 967f2a3052c2d22e31564b428a9aa8cc63dc2a9f) Co-authored-by: kale-smoothie <34165060+kale-smoothie@users.noreply.github.com>
-rw-r--r--Misc/NEWS.d/next/Library/2020-07-28-20-48-05.bpo-41422.iMwnMu.rst2
-rw-r--r--Modules/_pickle.c14
2 files changed, 16 insertions, 0 deletions
diff --git a/Misc/NEWS.d/next/Library/2020-07-28-20-48-05.bpo-41422.iMwnMu.rst b/Misc/NEWS.d/next/Library/2020-07-28-20-48-05.bpo-41422.iMwnMu.rst
new file mode 100644
index 0000000..8bde68f
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-07-28-20-48-05.bpo-41422.iMwnMu.rst
@@ -0,0 +1,2 @@
+Fixed memory leaks of :class:`pickle.Pickler` and :class:`pickle.Unpickler` involving cyclic references via the
+internal memo mapping.
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
index 840877e..f0cb302 100644
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -4691,6 +4691,13 @@ Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
Py_VISIT(self->fast_memo);
Py_VISIT(self->reducer_override);
Py_VISIT(self->buffer_callback);
+ PyMemoTable *memo = self->memo;
+ if (memo && memo->mt_table) {
+ Py_ssize_t i = memo->mt_allocated;
+ while (--i >= 0) {
+ Py_VISIT(memo->mt_table[i].me_key);
+ }
+ }
return 0;
}
@@ -7215,6 +7222,13 @@ Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
Py_VISIT(self->stack);
Py_VISIT(self->pers_func);
Py_VISIT(self->buffers);
+ PyObject **memo = self->memo;
+ if (memo) {
+ Py_ssize_t i = self->memo_size;
+ while (--i >= 0) {
+ Py_VISIT(memo[i]);
+ }
+ }
return 0;
}