From f202c535d552d5da99b7cfb5e49ba3bd081da5f2 Mon Sep 17 00:00:00 2001 From: Richard Shea Date: Sat, 2 Apr 2011 13:57:45 -0400 Subject: [PATCH] change ObjectId __getstate__ and __setstate__ to just use __id also added unit test for pickling/unpickling ObjectId --- bson/objectid.py | 6 +++--- test/test_objectid.py | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/bson/objectid.py b/bson/objectid.py index 7af4e5de0..0dfe8186f 100644 --- a/bson/objectid.py +++ b/bson/objectid.py @@ -191,12 +191,12 @@ class ObjectId(object): """return value of object for pickling. needed explicitly because __slots__() defined. """ - return self.__str__() + return self.__id - def __setstate__(self, oid): + def __setstate__(self, value): """explicit state set from pickling """ - self.__validate(oid) + self.__id = value def __str__(self): return self.__id.encode("hex") diff --git a/test/test_objectid.py b/test/test_objectid.py index ffca43f53..1e16f6438 100644 --- a/test/test_objectid.py +++ b/test/test_objectid.py @@ -15,6 +15,7 @@ """Tests for the objectid module.""" import datetime +import pickle import warnings import unittest import sys @@ -124,5 +125,9 @@ class TestObjectId(unittest.TestCase): oid = ObjectId.from_datetime(aware) self.assertEqual(as_utc, oid.generation_time) + def test_pickling(self): + orig = ObjectId() + self.assertEqual(orig, pickle.loads(pickle.dumps(orig))) + if __name__ == "__main__": unittest.main()