From ec88721cb24210c76971fbcfe394035cbbab10ac Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Sat, 5 Apr 2003 14:00:40 +0000 Subject: [PATCH] Made queue pickle-able. --- src/fix.py | 9 +++++++++ test/fix_test.py | 7 ++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/fix.py b/src/fix.py index 72380d3e8..7b34af9b9 100644 --- a/src/fix.py +++ b/src/fix.py @@ -204,6 +204,15 @@ class queue(dict): dict.__setitem__(self, i+self.last, v) except KeyError: 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): diff --git a/test/fix_test.py b/test/fix_test.py index 4c475b330..67c5c9ffa 100644 --- a/test/fix_test.py +++ b/test/fix_test.py @@ -33,6 +33,8 @@ from __future__ import generators from test import * +import pickle + class QueueTest(unittest.TestCase): def testGetitem(self): q = queue() @@ -47,7 +49,6 @@ class QueueTest(unittest.TestCase): self.assertRaises(IndexError, q.__getitem__, n) #self.assertEqual(q[3:7], queue([3, 4, 5, 6])) - def testSetitem(self): q1 = queue() for i in xrange(10): @@ -178,6 +179,10 @@ class QueueTest(unittest.TestCase): for _ in 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): def testCatch(self):