Skip to content

Commit

Permalink
pythongh-89850: Add default C implementations of persistent_id() and …
Browse files Browse the repository at this point in the history
…persistent_load()

Previously the C implementation of pickle.Pickler and pickle.Unpickler
classes did not have such methods and they could only be used if
they were overloaded in subclasses or set as instance attributes.

Fixed calling super().persistent_id() and super().persistent_load() in
subclasses of the C implementation of pickle.Pickler and pickle.Unpickler
classes. It no longer causes an infinite recursion.
  • Loading branch information
serhiy-storchaka committed Dec 30, 2023
1 parent cf34b77 commit d793b10
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 214 deletions.
8 changes: 8 additions & 0 deletions Doc/library/pickle.rst
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ The :mod:`pickle` module exports three classes, :class:`Pickler`,

See :ref:`pickle-persistent` for details and examples of uses.

.. versionchanged:: 3.13
Add the default implementation of this method in the C implementation
of :class:`!Pickler`.

.. attribute:: dispatch_table

A pickler object's dispatch table is a registry of *reduction
Expand Down Expand Up @@ -446,6 +450,10 @@ The :mod:`pickle` module exports three classes, :class:`Pickler`,

See :ref:`pickle-persistent` for details and examples of uses.

.. versionchanged:: 3.13
Add the default implementation of this method in the C implementation
of :class:`!Unpickler`.

.. method:: find_class(module, name)

Import *module* if necessary and return the object called *name* from it,
Expand Down
29 changes: 26 additions & 3 deletions Lib/test/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class PyIdPersPicklerTests(AbstractIdentityPersistentPicklerTests,

pickler = pickle._Pickler
unpickler = pickle._Unpickler
persistent_load_error = pickle.UnpicklingError

@support.cpython_only
def test_pickler_reference_cycle(self):
Expand Down Expand Up @@ -176,7 +177,6 @@ class DispatchTable:
support.gc_collect()
self.assertIsNone(table_ref())


@support.cpython_only
def test_unpickler_reference_cycle(self):
def check(Unpickler):
Expand Down Expand Up @@ -206,6 +206,28 @@ def persistent_load(pid):
return pid
check(PersUnpickler)

def test_pickler_super(self):
class PersPickler(self.pickler):
def persistent_id(subself, obj):
self.assertIsNone(super().persistent_id(obj))
return obj

for proto in range(pickle.HIGHEST_PROTOCOL + 1):
f = io.BytesIO()
pickler = PersPickler(f, proto)
pickler.dump('abc')
self.assertEqual(self.loads(f.getvalue()), 'abc')

def test_unpickler_super(self):
class PersUnpickler(self.unpickler):
def persistent_load(subself, pid):
with self.assertRaises(self.persistent_load_error):
super().persistent_load(pid)
return pid

for proto in range(pickle.HIGHEST_PROTOCOL + 1):
unpickler = PersUnpickler(io.BytesIO(self.dumps('abc', proto)))
self.assertEqual(unpickler.load(), 'abc')

class PyPicklerUnpicklerObjectTests(AbstractPicklerUnpicklerObjectTests, unittest.TestCase):

Expand Down Expand Up @@ -256,6 +278,7 @@ class CPersPicklerTests(PyPersPicklerTests):
class CIdPersPicklerTests(PyIdPersPicklerTests):
pickler = _pickle.Pickler
unpickler = _pickle.Unpickler
persistent_load_error = _pickle.UnpicklingError

class CDumpPickle_LoadPickle(PyPicklerTests):
pickler = _pickle.Pickler
Expand Down Expand Up @@ -326,7 +349,7 @@ class SizeofTests(unittest.TestCase):
check_sizeof = support.check_sizeof

def test_pickler(self):
basesize = support.calcobjsize('7P2n3i2n3i2P')
basesize = support.calcobjsize('6P2n3i2n3i2P')
p = _pickle.Pickler(io.BytesIO())
self.assertEqual(object.__sizeof__(p), basesize)
MT_size = struct.calcsize('3nP0n')
Expand All @@ -343,7 +366,7 @@ def test_pickler(self):
0) # Write buffer is cleared after every dump().

def test_unpickler(self):
basesize = support.calcobjsize('2P2n2P 2P2n2i5P 2P3n8P2n2i')
basesize = support.calcobjsize('2P2nP 2P2n2i5P 2P3n8P2n2i')
unpickler = _pickle.Unpickler
P = struct.calcsize('P') # Size of memo table entry.
n = struct.calcsize('n') # Size of mark table entry.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Add default implementations of :meth:`pickle.Pickler.persistent_id` and
:meth:`pickle.Unpickler.persistent_load` methods in the C implementation.
Calling ``super().persistent_id()`` and ``super().persistent_load()`` in
subclasses of the C implementation of :class:`pickle.Pickler` and
:class:`pickle.Unpickler` classes no longer causes infinite recursion.

0 comments on commit d793b10

Please sign in to comment.