"exactly two or three elements" % reduce)
# Save the reduce() output and finally memoize the object
- self.save_reduce(func, args, state)
- self.memoize(obj)
+ self.save_reduce(func, args, state, obj)
def persistent_id(self, obj):
# This exists so a subclass can override it
else:
self.write(PERSID + str(pid) + '\n')
- def save_reduce(self, func, args, state=None):
+ def save_reduce(self, func, args, state=None, obj=None):
# This API is be called by some subclasses
# Assert that args is a tuple or None
if not hasattr(cls, "__new__"):
raise PicklingError(
"args[0] from __newobj__ args has no __new__")
+ if obj is not None and cls is not obj.__class__:
+ raise PicklingError(
+ "args[0] from __newobj__ args has the wrong class")
args = args[1:]
save(cls)
save(args)
save(args)
write(REDUCE)
+ if obj is not None:
+ self.memoize(obj)
+
if state is not None:
save(state)
write(BUILD)