FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-159739: Fully clear the unpickler memo by jelle-openai · Pull Request #160173 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
16 changes: 16 additions & 0 deletions Lib/test/pickletester.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -5299,6 +5299,22 @@ def test_clear_pickler_memo(self):
self.assertNotEqual(first_pickled, second_pickled)
self.assertEqual(first_pickled, third_pickled)

def test_clear_unpickler_memo(self):
value = [b'repeated'] * 2
data = pickle.dumps(value, protocol=4)
unpickler = self.unpickler_class(io.BytesIO(data * 2))
self.assertEqual(unpickler.load(), value)
unpickler.memo.clear()
self.assertEqual(unpickler.load(), value)

def test_clear_unpickler_memo_sparse(self):
# Store None at index 1000, then retrieve it in a second record.
unpickler = self.unpickler_class(io.BytesIO(b'Np1000\n.g1000\n.'))
self.assertIsNone(unpickler.load())
unpickler.memo.clear()
with self.assertRaises(pickle.UnpicklingError):
unpickler.load()

def test_priming_pickler_memo(self):
# Verify that we can set the Pickler's memo attribute.
data = ["abcdefg", "abcdefg", 44]
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix clearing a :class:`pickle.Unpickler` memo so that the unpickler can be
reused for independent records.
24 changes: 15 additions & 9 deletions Modules/_pickle.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1762,16 +1762,16 @@ _Unpickler_NewMemo(Py_ssize_t new_size)
return memo;
}

/* Free the unpickler's memo, taking care to decref any items left in it. */
/* Replace the unpickler's memo and release the old entries. */
static void
_Unpickler_MemoCleanup(UnpicklerObject *self)
_Unpickler_MemoCleanup(UnpicklerObject *self, PyObject **replacement)
{
Py_ssize_t i;
PyObject **memo = self->memo;

if (self->memo == NULL)
self->memo = replacement;
if (memo == NULL)
return;
self->memo = NULL;
i = self->memo_size;
while (--i >= 0) {
Py_XDECREF(memo[i]);
Expand Down Expand Up @@ -7750,7 +7750,7 @@ Unpickler_clear(PyObject *op)
}

Py_CLEAR(self->memo_dict);
_Unpickler_MemoCleanup(self);
_Unpickler_MemoCleanup(self, NULL);
PyMem_Free(self->marks);
self->marks = NULL;
PyMem_Free(self->input_line);
Expand Down Expand Up @@ -7897,10 +7897,16 @@ static PyObject *
_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
{
_Unpickler_MemoCleanup(self->unpickler);
self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
if (self->unpickler->memo == NULL)
UnpicklerObject *unpickler = self->unpickler;
PyObject **memo = _Unpickler_NewMemo(unpickler->memo_size);
if (memo == NULL) {
return NULL;
}
PyObject *memo_dict = unpickler->memo_dict;
unpickler->memo_dict = NULL;
unpickler->memo_len = 0;
_Unpickler_MemoCleanup(unpickler, memo);
Py_XDECREF(memo_dict);
Py_RETURN_NONE;
}

Expand Down Expand Up @@ -8105,7 +8111,7 @@ Unpickler_set_memo(PyObject *op, PyObject *obj, void *Py_UNUSED(closure))
return -1;
}

_Unpickler_MemoCleanup(self);
_Unpickler_MemoCleanup(self, NULL);
self->memo_size = new_memo_size;
self->memo = new_memo;

Expand Down
Loading

Back | FazBrowse Home | New Git URL