Fixed RealDictCursor.pop()

Addresses #886, but there might be something else broken there.
This commit is contained in:
Daniele Varrazzo 2019-04-05 19:03:38 +01:00
parent 7b31b39fed
commit 21d16b6f67
3 changed files with 27 additions and 0 deletions

1
NEWS
View File

@ -4,6 +4,7 @@ Current release
What's new in psycopg 2.8.1
---------------------------
- Fixed `RealDictRow.pop()` (:ticket:`#886`).
- Fixed "there's no async cursor" error polling a connection with no cursor
(:ticket:`#887`).

View File

@ -290,6 +290,13 @@ class RealDictRow(dict):
def items(self):
return ((k, self[k]) for k in self._column_mapping)
def pop(self, key, *args):
found = key in self
rv = super(RealDictRow, self).pop(key, *args)
if found:
self._column_mapping.remove(key)
return rv
if PY2:
iterkeys = keys
itervalues = values

View File

@ -221,6 +221,12 @@ class ExtrasDictCursorTests(_DictCursorBase):
class ExtrasDictCursorRealTests(_DictCursorBase):
def testRealMeansReal(self):
curs = self.conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
curs.execute("SELECT * FROM ExtrasDictCursorTests")
row = curs.fetchone()
self.assert_(isinstance(row, dict))
def testDictCursorWithPlainCursorRealFetchOne(self):
self._testWithPlainCursorReal(lambda curs: curs.fetchone())
@ -358,6 +364,19 @@ class ExtrasDictCursorRealTests(_DictCursorBase):
self.assertEqual(list(r1.itervalues()), list(r.itervalues()))
self.assertEqual(list(r1.iteritems()), list(r.iteritems()))
def test_pop(self):
curs = self.conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
curs.execute("select 1 as a, 2 as b, 3 as c")
r = curs.fetchone()
self.assertEqual(r.pop('b'), 2)
self.assertEqual(list(r), ['a', 'c'])
self.assertEqual(list(r.keys()), ['a', 'c'])
self.assertEqual(list(r.values()), [1, 3])
self.assertEqual(list(r.items()), [('a', 1), ('c', 3)])
self.assertEqual(r.pop('b', None), None)
self.assertRaises(KeyError, r.pop, 'b')
class NamedTupleCursorTest(ConnectingTestCase):
def setUp(self):