mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
[3.10] GH-89988: Fix memory leak in pickle.Pickler dispatch_table lookup (GH-94298) (#94385)
This commit is contained in:
parent
f6b6b5af78
commit
079ea44570
3 changed files with 29 additions and 1 deletions
|
@ -154,6 +154,29 @@ class PyIdPersPicklerTests(AbstractIdentityPersistentPicklerTests,
|
|||
return obj
|
||||
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):
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
Fix memory leak in :class:`pickle.Pickler` when looking up :attr:`dispatch_table`. Patch by Kumar Aditya.
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue