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) + ')'
127
It can be fixed by:
- 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;
-
(I mean 2 but markdown sucks) Put a different
fastpath in adapt: always return the same AsIs("NULL").
-
(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.