This project is archived and is in readonly mode.

#26 ✓resolved
Daniele Varrazzo

Failed adaptation for None in composite types

Reported by Daniele Varrazzo | December 21st, 2010 @ 12:40 PM

microprotocols_adapt has a fast path returning NULL on None. This is wrong: it should return an object conform to ISQLQuote. It goes unnoticed because _mogrify has a fast path not calling microprotocol_getquoted, but the bug emerges trying to adapt a record containing NULL.

In [6]: cur.mogrify("""select %s;""", [ tuple((1,'hi',None)) ])---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)

/home/piro/dev/psycopg2/<ipython console> in <module>()

/home/piro/dev/psycopg2/build/lib.2.6/psycopg2/extensions.pyc in getquoted(self)
    123             if hasattr(obj, 'prepare'):
    124                 obj.prepare(self._conn)
--> 125         qobjs = [str(o.getquoted()) for o in pobjs]
    126         return '(' + ', '.join(qobjs) + ')'

It can be fixed by:

  1. dropping the special case in microprotocol_adapt and adding an adapter such as:
# untested
extensions.register_adapter(type(None), lambda x: exteions.AsIs("NULL"))

it would still be a fast path in mogrify, but would allow uniform treatment of None in composite types;

  1. (I mean 2 but markdown sucks) Put a different fastpath in adapt: always return the same AsIs("NULL").

  2. (three) adding a special case to SQL_IN. No, I don't like it. This is the solution currently adopted by adapter_list.

Note: grep -r '"NULL"' psycopg reveals that AsIs adapts None to "NULL". This is either unnecessary or we can use that... in either case we are a little bit schizophrenic.

Comments and changes to this ticket

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="">the new tracker</a>.
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

People watching this ticket