8000 gh-89850: Add default C implementations of persistent_id() and persistent_load() by serhiy-storchaka · Pull Request #113579 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-89850: Add default C implementations of persistent_id() and persistent_load() #113579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8000
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.
Loading
0