This project is archived and is in readonly mode.

#85 ✓invalid
Samuel PHAN

Row type is not the expected one, depending on the used factory connection

Reported by Samuel PHAN | December 21st, 2011 @ 10:12 AM

Depending on the connection factory, the row type, when returned in an iterator way (curs.next()) is not the right one, or is bugged.

  • psycopg2.extras.RealDictConnection

    • cursor.next() : OK
    • cursor.fetch*() : OK
  • psycopg2.extras.DictConnection

    • cursor.next() : KO - the returned row can't be accessed in a dict-fashion.
    • cursor.fetch*() : OK
>>> row = curs.next()
>>> type(row)
<class 'psycopg2.extras.DictRow'>
>>> row['id']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/dist-packages/psycopg2/extras.py", line 155, in __getitem__
    x = self._index[x]
KeyError: 'id'
>>> row.keys()
[]
  • psycopg2.extras.NamedTupleConnection
    • cursor.next() : KO - the returned row type is a tuple, not a named tuple.
    • cursor.fetch*() : OK
>>> row = curs3.next()
>>> type(row)
<type 'tuple'>
>>> row.id
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'id'
  • psycopg2._psycopg.connection
    • cursor.next() : OK
    • cursor.fetch() : OK

rel-2.4.4
os-linux (Ubuntu 10.10)

Comments and changes to this ticket

  • Daniele Varrazzo

    Daniele Varrazzo January 10th, 2012 @ 02:10 AM

    • State changed from “new” to “invalid”

    cursor.next() is not part of the public interface, but an implementation detail: the cursor is iterable (you can call iter() on it), the fact it is also an iterator (exposing next()) is an implementation detail which may also change in the future.

    The artefacts you are seeing are due to __iter__ not being called before next(): the latter should only be called as part of the iteration protocol; the only well defined behaviour is that:

    cur = ...
    cur.execute(SQL)
    i = iter(cur)
    i.next()
    

    works as expected for any cursor type.

    The fact next() is not called __next__(), thus resembling a public method, has been acknowledged as a python design mistake: see http://www.python.org/dev/peps/pep-3114/.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

<b>WARNING:</b> the informations in this tracker are archived. Please submit new tickets or comments to <a href="https://github.com/psycopg/psycopg2/issues">the new tracker</a>.
<br/>
Psycopg is the most used PostgreSQL adapter for the Python programming language. At the core it fully implements the Python DB API 2.0 specifications. Several extensions allow access to many of the features offered by PostgreSQL.

Shared Ticket Bins

Pages