Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Failed adaptation for None in composite types

#26

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:

  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.

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

State: resolved
Milestone: none
Assigned to: nobody

Activity

  1. Daniele Varrazzo
    Daniele Varrazzo
    • Tag changed from adapt none null to adapt, none, null

    December 21st, 2010 @ 12:40 PM

  2. Daniele Varrazzo
    Daniele Varrazzo
    • State changed from open to resolved

    Issue resolved in my repos. To be released.

    January 2nd, 2011 @ 11:45 AM