diff options
author | Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> | 2022-06-28 16:14:47 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-28 16:14:47 (GMT) |
commit | 079ea445706e2afae8f1bcc53fe967b0839b310c (patch) | |
tree | 1aa5129a6e69b9aea8a456a2b3e1beafa3fbcb12 | |
parent | f6b6b5af784c3b977854e404aaf7c936c517bc4e (diff) | |
download | cpython-079ea445706e2afae8f1bcc53fe967b0839b310c.zip cpython-079ea445706e2afae8f1bcc53fe967b0839b310c.tar.gz cpython-079ea445706e2afae8f1bcc53fe967b0839b310c.tar.bz2 |
[3.10] GH-89988: Fix memory leak in pickle.Pickler dispatch_table lookup (GH-94298) (#94385)
-rw-r--r-- | Lib/test/test_pickle.py | 23 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2022-06-26-10-59-15.gh-issue-89988.K8rnmt.rst | 1 | ||||
-rw-r--r-- | Modules/_pickle.c | 6 |
3 files changed, 29 insertions, 1 deletions
diff --git a/Lib/test/test_pickle.py b/Lib/test/test_pickle.py index 8775ff4..aa18ba2 100644 --- a/Lib/test/test_pickle.py +++ b/Lib/test/test_pickle.py @@ -155,6 +155,29 @@ class PyIdPersPicklerTests(AbstractIdentityPersistentPicklerTests, check(PersPickler) @support.cpython_only + def test_custom_pickler_dispatch_table_memleak(self): + # See https://github.com/python/cpython/issues/89988 + + class Pickler(self.pickler): + def __init__(self, *args, **kwargs): + self.dispatch_table = table + super().__init__(*args, **kwargs) + + class DispatchTable: + pass + + table = DispatchTable() + pickler = Pickler(io.BytesIO()) + self.assertIs(pickler.dispatch_table, table) + table_ref = weakref.ref(table) + self.assertIsNotNone(table_ref()) + del pickler + del table + support.gc_collect() + self.assertIsNone(table_ref()) + + + @support.cpython_only def test_unpickler_reference_cycle(self): def check(Unpickler): for proto in range(pickle.HIGHEST_PROTOCOL + 1): diff --git a/Misc/NEWS.d/next/Library/2022-06-26-10-59-15.gh-issue-89988.K8rnmt.rst b/Misc/NEWS.d/next/Library/2022-06-26-10-59-15.gh-issue-89988.K8rnmt.rst new file mode 100644 index 0000000..811a8d6 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-06-26-10-59-15.gh-issue-89988.K8rnmt.rst @@ -0,0 +1 @@ +Fix memory leak in :class:`pickle.Pickler` when looking up :attr:`dispatch_table`. Patch by Kumar Aditya. diff --git a/Modules/_pickle.c b/Modules/_pickle.c index d05069a..ce54a2d 100644 --- a/Modules/_pickle.c +++ b/Modules/_pickle.c @@ -4790,8 +4790,12 @@ _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, return -1; } + + if (self->dispatch_table != NULL) { + return 0; + } if (_PyObject_LookupAttrId((PyObject *)self, - &PyId_dispatch_table, &self->dispatch_table) < 0) { + &PyId_dispatch_table, &self->dispatch_table) < 0) { return -1; } |