mirror of
https://github.com/python/cpython.git
synced 2025-08-01 23:53:15 +00:00
Add getstate and setstate implementation to concrete set classes.
This commit is contained in:
parent
66abcee948
commit
cd58b8f532
2 changed files with 21 additions and 1 deletions
12
Lib/sets.py
12
Lib/sets.py
|
@ -366,6 +366,11 @@ class ImmutableSet(BaseSet):
|
||||||
self._hashcode = self._compute_hash()
|
self._hashcode = self._compute_hash()
|
||||||
return self._hashcode
|
return self._hashcode
|
||||||
|
|
||||||
|
def __getstate__(self):
|
||||||
|
return self._data, self._hashcode
|
||||||
|
|
||||||
|
def __setstate__(self, state):
|
||||||
|
self._data, self._hashcode = state
|
||||||
|
|
||||||
class Set(BaseSet):
|
class Set(BaseSet):
|
||||||
""" Mutable set class."""
|
""" Mutable set class."""
|
||||||
|
@ -380,6 +385,13 @@ class Set(BaseSet):
|
||||||
if iterable is not None:
|
if iterable is not None:
|
||||||
self._update(iterable)
|
self._update(iterable)
|
||||||
|
|
||||||
|
def __getstate__(self):
|
||||||
|
# getstate's results are ignored if it is not
|
||||||
|
return self._data,
|
||||||
|
|
||||||
|
def __setstate__(self, data):
|
||||||
|
self._data, = data
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
"""A Set cannot be hashed."""
|
"""A Set cannot be hashed."""
|
||||||
# We inherit object.__hash__, so we must deny this explicitly
|
# We inherit object.__hash__, so we must deny this explicitly
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import unittest, operator, copy
|
import unittest, operator, copy, pickle
|
||||||
from sets import Set, ImmutableSet
|
from sets import Set, ImmutableSet
|
||||||
from test import test_support
|
from test import test_support
|
||||||
|
|
||||||
|
@ -74,6 +74,14 @@ class TestBasicOps(unittest.TestCase):
|
||||||
for v in self.set:
|
for v in self.set:
|
||||||
self.assert_(v in self.values)
|
self.assert_(v in self.values)
|
||||||
|
|
||||||
|
def test_pickling(self):
|
||||||
|
p = pickle.dumps(self.set)
|
||||||
|
print repr(p)
|
||||||
|
copy = pickle.loads(p)
|
||||||
|
repr(copy)
|
||||||
|
self.assertEqual(self.set, copy,
|
||||||
|
"%s != %s" % (self.set, copy))
|
||||||
|
|
||||||
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
class TestBasicOpsEmpty(TestBasicOps):
|
class TestBasicOpsEmpty(TestBasicOps):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue