Made queue pickle-able.

This commit is contained in:
Jeremy Fincher 2003-04-05 14:00:40 +00:00
parent 6d3c060208
commit ec88721cb2
2 changed files with 15 additions and 1 deletions

View File

@ -205,6 +205,15 @@ class queue(dict):
except KeyError: except KeyError:
raise IndexError, i raise IndexError, i
def __getstate__(self):
return (self.first, self.last, dict(self))
def __setstate__(self, (first, last, d)):
self.first = first
self.last = last
for (k, v) in d.iteritems():
dict.__setitem__(self, k, v)
class IterableMap(object): class IterableMap(object):
"""Define .iteritems() in a class and subclass this to get the other iters. """Define .iteritems() in a class and subclass this to get the other iters.

View File

@ -33,6 +33,8 @@ from __future__ import generators
from test import * from test import *
import pickle
class QueueTest(unittest.TestCase): class QueueTest(unittest.TestCase):
def testGetitem(self): def testGetitem(self):
q = queue() q = queue()
@ -47,7 +49,6 @@ class QueueTest(unittest.TestCase):
self.assertRaises(IndexError, q.__getitem__, n) self.assertRaises(IndexError, q.__getitem__, n)
#self.assertEqual(q[3:7], queue([3, 4, 5, 6])) #self.assertEqual(q[3:7], queue([3, 4, 5, 6]))
def testSetitem(self): def testSetitem(self):
q1 = queue() q1 = queue()
for i in xrange(10): for i in xrange(10):
@ -178,6 +179,10 @@ class QueueTest(unittest.TestCase):
for _ in queue(): for _ in queue():
self.fail('no elements should be in empty queue') self.fail('no elements should be in empty queue')
def testPickleCopy(self):
q = queue(range(10))
self.assertEqual(q, pickle.loads(pickle.dumps(q)))
class FunctionsTest(unittest.TestCase): class FunctionsTest(unittest.TestCase):
def testCatch(self): def testCatch(self):