summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorPierre Glaser <pierreglaser@msn.com>2019-05-08 19:40:25 (GMT)
committerAntoine Pitrou <antoine@python.org>2019-05-08 19:40:25 (GMT)
commit65d98d0f53f558d7c799098da0abf376068c15fd (patch)
tree4354710a0984cd5afca6e5745309b988d1054213 /Lib
parent39889864c09741909da4ec489459d0197ea8f1fc (diff)
downloadcpython-65d98d0f53f558d7c799098da0abf376068c15fd.zip
cpython-65d98d0f53f558d7c799098da0abf376068c15fd.tar.gz
cpython-65d98d0f53f558d7c799098da0abf376068c15fd.tar.bz2
bpo-35900: Add a state_setter arg to save_reduce (GH-12588)
Allow reduction methods to return a 6-item tuple where the 6th item specifies a custom state-setting method that's called instead of the regular ``__setstate__`` method.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/pickle.py27
-rw-r--r--Lib/test/pickletester.py40
2 files changed, 61 insertions, 6 deletions
diff --git a/Lib/pickle.py b/Lib/pickle.py
index d533e66..47f0d28 100644
--- a/Lib/pickle.py
+++ b/Lib/pickle.py
@@ -537,9 +537,9 @@ class _Pickler:
# Assert that it returned an appropriately sized tuple
l = len(rv)
- if not (2 <= l <= 5):
+ if not (2 <= l <= 6):
raise PicklingError("Tuple returned by %s must have "
- "two to five elements" % reduce)
+ "two to six elements" % reduce)
# Save the reduce() output and finally memoize the object
self.save_reduce(obj=obj, *rv)
@@ -561,7 +561,7 @@ class _Pickler:
"persistent IDs in protocol 0 must be ASCII strings")
def save_reduce(self, func, args, state=None, listitems=None,
- dictitems=None, obj=None):
+ dictitems=None, state_setter=None, obj=None):
# This API is called by some subclasses
if not isinstance(args, tuple):
@@ -655,8 +655,25 @@ class _Pickler:
self._batch_setitems(dictitems)
if state is not None:
- save(state)
- write(BUILD)
+ if state_setter is None:
+ save(state)
+ write(BUILD)
+ else:
+ # If a state_setter is specified, call it instead of load_build
+ # to update obj's with its previous state.
+ # First, push state_setter and its tuple of expected arguments
+ # (obj, state) onto the stack.
+ save(state_setter)
+ save(obj) # simple BINGET opcode as obj is already memoized.
+ save(state)
+ write(TUPLE2)
+ # Trigger a state_setter(obj, state) function call.
+ write(REDUCE)
+ # The purpose of state_setter is to carry-out an
+ # inplace modification of obj. We do not care about what the
+ # method might return, so its output is eventually removed from
+ # the stack.
+ write(POP)
# Methods below this point are dispatched through the dispatch table
diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py
index bb8e6ce..19e8823 100644
--- a/Lib/test/pickletester.py
+++ b/Lib/test/pickletester.py
@@ -2992,7 +2992,26 @@ class AAA(object):
return str, (REDUCE_A,)
class BBB(object):
- pass
+ def __init__(self):
+ # Add an instance attribute to enable state-saving routines at pickling
+ # time.
+ self.a = "some attribute"
+
+ def __setstate__(self, state):
+ self.a = "BBB.__setstate__"
+
+
+def setstate_bbb(obj, state):
+ """Custom state setter for BBB objects
+
+ Such callable may be created by other persons than the ones who created the
+ BBB class. If passed as the state_setter item of a custom reducer, this
+ allows for custom state setting behavior of BBB objects. One can think of
+ it as the analogous of list_setitems or dict_setitems but for foreign
+ classes/functions.
+ """
+ obj.a = "custom state_setter"
+
class AbstractDispatchTableTests(unittest.TestCase):
@@ -3081,6 +3100,25 @@ class AbstractDispatchTableTests(unittest.TestCase):
self.assertEqual(default_load_dump(a), REDUCE_A)
self.assertIsInstance(default_load_dump(b), BBB)
+ # End-to-end testing of save_reduce with the state_setter keyword
+ # argument. This is a dispatch_table test as the primary goal of
+ # state_setter is to tweak objects reduction behavior.
+ # In particular, state_setter is useful when the default __setstate__
+ # behavior is not flexible enough.
+
+ # No custom reducer for b has been registered for now, so
+ # BBB.__setstate__ should be used at unpickling time
+ self.assertEqual(default_load_dump(b).a, "BBB.__setstate__")
+
+ def reduce_bbb(obj):
+ return BBB, (), obj.__dict__, None, None, setstate_bbb
+
+ dispatch_table[BBB] = reduce_bbb
+
+ # The custom reducer reduce_bbb includes a state setter, that should
+ # have priority over BBB.__setstate__
+ self.assertEqual(custom_load_dump(b).a, "custom state_setter")
+
if __name__ == "__main__":
# Print some stuff that can be used to rewrite DATA{0,1,2}