Fixed pickling of DictRow objects too

This commit is contained in:
Daniele Varrazzo 2012-12-11 01:10:01 +00:00
parent 99bedd1bb2
commit 6df6e6adfe
3 changed files with 23 additions and 2 deletions

2
NEWS
View File

@ -37,7 +37,7 @@ What's new in psycopg 2.4.6
- Fixed pickling of FixedOffsetTimezone objects (ticket #135). - Fixed pickling of FixedOffsetTimezone objects (ticket #135).
- Release the GIL around PQgetResult calls after COPY (ticket #140). - Release the GIL around PQgetResult calls after COPY (ticket #140).
- Fixed empty strings handling in composite caster (ticket #141). - Fixed empty strings handling in composite caster (ticket #141).
- Fixed pickling of RealDictRow objects. - Fixed pickling of DictRow and RealDictRow objects.
What's new in psycopg 2.4.5 What's new in psycopg 2.4.5

View File

@ -184,7 +184,14 @@ class DictRow(list):
def __contains__(self, x): def __contains__(self, x):
return x in self._index return x in self._index
# grop the crusty Py2 methods def __getstate__(self):
return self[:], self._index.copy()
def __setstate__(self, data):
self[:] = data[0]
self._index = data[1]
# drop the crusty Py2 methods
if sys.version_info[0] > 2: if sys.version_info[0] > 2:
items = iteritems; del iteritems items = iteritems; del iteritems
keys = iterkeys; del iterkeys keys = iterkeys; del iterkeys

View File

@ -205,6 +205,20 @@ class ExtrasDictCursorTests(unittest.TestCase):
for i, r in enumerate(curs): for i, r in enumerate(curs):
self.assertEqual(i + 1, curs.rownumber) self.assertEqual(i + 1, curs.rownumber)
def testPickleDictRow(self):
import pickle
curs = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
curs.execute("select 10 as a, 20 as b")
r = curs.fetchone()
d = pickle.dumps(r)
r1 = pickle.loads(d)
self.assertEqual(r, r1)
self.assertEqual(r[0], r1[0])
self.assertEqual(r[1], r1[1])
self.assertEqual(r['a'], r1['a'])
self.assertEqual(r['b'], r1['b'])
self.assertEqual(r._index, r1._index)
def testPickleRealDictRow(self): def testPickleRealDictRow(self):
import pickle import pickle
curs = self.conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) curs = self.conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)