From f5d5d71a090a251157c7b9e567d79ce06d0a4120 Mon Sep 17 00:00:00 2001 From: Hamdy Abou El Anein Date: Sun, 22 Nov 2026 10:29:31 +0100 Subject: [PATCH 1/3] gh-162334: Restore memo capacity when inserting at index zero Based on the fix proposed by @hamarshehmhmd in gh-162334. --- Modules/_pickle.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Modules/_pickle.c b/Modules/_pickle.c index b8f9b1c92f20723..8666358d8ec82e0 100644 --- a/Modules/_pickle.c +++ b/Modules/_pickle.c @@ -1735,7 +1735,9 @@ _Unpickler_MemoPut(UnpicklerObject *self, size_t idx, PyObject *value) Py_DECREF(key); return 0; } - if (_Unpickler_ResizeMemoList(self, idx * 2) < 0) + /* idx is 0 if the memo is empty, e.g. after "memo = {}" (gh-162334); + start again from the initial memo size then. */ + if (_Unpickler_ResizeMemoList(self, idx ? idx * 2 : 32) < 0) return -1; assert(idx < self->memo_size); } From 23504bcd5de86d33d23f59daadedcc5bf62d0b4b Mon Sep 17 00:00:00 2001 From: Hamdy Abou El Anein Date: Sun, 22 Nov 2026 10:29:40 +0100 Subject: [PATCH 2/3] gh-162334: Add regression tests for empty unpickler memo Tests proposed by @hamarshehmhmd in gh-162334. --- Lib/test/pickletester.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py index e9d367f9ea95aa8..270bdc62fc5ba04 100644 --- a/Lib/test/pickletester.py +++ b/Lib/test/pickletester.py @@ -5299,6 +5299,22 @@ def test_clear_pickler_memo(self): self.assertNotEqual(first_pickled, second_pickled) self.assertEqual(first_pickled, third_pickled) + def test_empty_unpickler_memo(self): + # gh-162334: assigning an empty memo used to crash the C unpickler + # on the next memoized object. + for proto in protocols: + with self.subTest(proto=proto): + value = [b'repeated'] * 2 + data = pickle.dumps(value, protocol=proto) + unpickler = self.unpickler_class(io.BytesIO(data)) + unpickler.memo = {} + result = unpickler.load() + self.assertEqual(result, value) + self.assertIs(result[0], result[1]) + unpickler = self.unpickler_class(io.BytesIO(b'\x80\x04N\x94.')) + unpickler.memo = {} + self.assertIsNone(unpickler.load()) + def test_clear_unpickler_memo(self): value = [b'repeated'] * 2 data = pickle.dumps(value, protocol=4) From a91a2aa57482a3b3e770779101d3e9eb395224fb Mon Sep 17 00:00:00 2001 From: Hamdy Abou El Anein Date: Sun, 22 Nov 2026 10:29:47 +0100 Subject: [PATCH 3/3] gh-162334: Document fix for empty Unpickler memo --- .../Library/2026-11-22-00-00-00.gh-issue-162334.empty-memo.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-22-00-00-00.gh-issue-162334.empty-memo.rst diff --git a/Misc/NEWS.d/next/Library/2026-11-22-00-00-00.gh-issue-162334.empty-memo.rst b/Misc/NEWS.d/next/Library/2026-11-22-00-00-00.gh-issue-162334.empty-memo.rst new file mode 100644 index 000000000000000..2af673ba1c55198 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-22-00-00-00.gh-issue-162334.empty-memo.rst @@ -0,0 +1,2 @@ +Fix a crash in the C-accelerated :class:`pickle.Unpickler` when assigning +an empty memo dictionary before processing a memoized object.