Author: phd
Date: 2010-02-05 06:43:30 -0700 (Fri, 05 Feb 2010)
New Revision: 4099
Added:
SQLObject/trunk/sqlobject/tests/test_pickle.py
Modified:
SQLObject/trunk/docs/News.txt
SQLObject/trunk/sqlobject/main.py
Log:
SQLObject instances that don't have a per-instance connection can be pickled and unpickled.
Modified: SQLObject/trunk/docs/News.txt
===================================================================
--- SQLObject/trunk/docs/News.txt 2010-02-05 13:31:14 UTC (rev 4098)
+++ SQLObject/trunk/docs/News.txt 2010-02-05 13:43:30 UTC (rev 4099)
@@ -13,6 +13,9 @@
Features & Interface
--------------------
+* SQLObject instances that don't have a per-instance connection can be
+ pickled and unpickled.
+
* Added TimedeltaCol; currently it's only implemented on PostgreSQL as an
INTERVAL type.
Modified: SQLObject/trunk/sqlobject/main.py
===================================================================
--- SQLObject/trunk/sqlobject/main.py 2010-02-05 13:31:14 UTC (rev 4098)
+++ SQLObject/trunk/sqlobject/main.py 2010-02-05 13:43:30 UTC (rev 4099)
@@ -1675,6 +1675,22 @@
return NotImplemented
+ def __getstate__(self):
+ if self.sqlmeta._perConnection:
+ from pickle import PicklingError
+ raise PicklingError('Cannot pickle an SQLObject instance that has a per-instance connection')
+ d = self.__dict__.copy()
+ del d['sqlmeta']
+ del d['_SO_writeLock']
+ return d
+
+ def __setstate__(self, d):
+ self.__init__(_SO_fetch_no_create=1)
+ self._SO_writeLock = threading.Lock()
+ self.__dict__.update(d)
+ self.__class__._connection.cache.put(self.id, self.__class__, self)
+
+
def capitalize(name):
return name[0].capitalize() + name[1:]
Added: SQLObject/trunk/sqlobject/tests/test_pickle.py
===================================================================
--- SQLObject/trunk/sqlobject/tests/test_pickle.py (rev 0)
+++ SQLObject/trunk/sqlobject/tests/test_pickle.py 2010-02-05 13:43:30 UTC (rev 4099)
@@ -0,0 +1,24 @@
+import pickle
+from sqlobject import *
+from sqlobject.tests.dbtest import *
+
+########################################
+## Pickle instances
+########################################
+
+class TestPickle(SQLObject):
+ question = StringCol()
+ answer = IntCol()
+
+test_question = 'The Ulimate Question of Life, the Universe and Everything'
+test_answer = 42
+
+def test_pickleCol():
+ setupClass(TestPickle)
+ test = TestPickle(question=test_question, answer=test_answer)
+
+ pickle_data = pickle.dumps(test, pickle.HIGHEST_PROTOCOL)
+ test = pickle.loads(pickle_data)
+
+ assert test.question == test_question
+ assert test.answer == test_answer
|