As with deque (SF #1062279), two problems with set.__reduce__:
1. Recursive sets (which can be constructed with the aid
of a hashable mutable object) aren't pickled correctly
because __reduce__ wants a reference to itself in the
call to its constructor. Fix by moving the keys to the
state argument and resurrecting them in __setstate__
(test_pickling_recursive).
2. Without the standard reduce, we have to take care of
the instance dictionary ourselves. The test for this is
in a new TestSubclassOps class that is mixed in to
TestSetSubclass and TestFrozenSetSubclass. I'm not sure
if such a mixin is the best way to distribute that test.
The biggest drawback to this patch is that __setstate__
makes it possible to mutate a frozenset. This implementation
clears the cached hash after such a mutation, but even then
it can be used to cause havoc in dicts. Such havoc isn't
fatal (this doesn't do anything that a regular class can't
do), but it can be confusing. Not being able to do this was
a desirable property of frozenset, but it's unlikely to
happen on accident, and sets.ImmutableSet has surived
without it. Unless one of the pickle gurus provides a better
alternative to SF #1062277, this might be the best option.
|