Fixed adaptation of None in composite types (ticket #26).

Added an adapter for None: it is usually not invoked as adaptation to
NULL is a fast path in mogrify, but can be invoked by composite types.
Notice that composite types still have the option to fast-path None
(e.g. list adapter does).
This commit is contained in:
Daniele Varrazzo 2010-12-31 19:21:44 +01:00
parent 30921b58a8
commit fdfa2de1a1
4 changed files with 41 additions and 0 deletions

View File

@ -1,3 +1,10 @@
What's new in psycopg 2.3.3
---------------------------
- Fixed adaptation of None in composite types (ticket #26). Bug report by
Karsten Hilbert.
What's new in psycopg 2.3.2
---------------------------

View File

@ -82,6 +82,7 @@ from _psycopg import __version__
import psycopg2.extensions as _ext
_ext.register_adapter(tuple, _ext.SQL_IN)
_ext.register_adapter(type(None), _ext.NoneAdapter)
__all__ = filter(lambda k: not k.startswith('_'), locals().keys())

View File

@ -128,4 +128,17 @@ class SQL_IN(object):
__str__ = getquoted
class NoneAdapter(object):
"""Adapt None to NULL.
This adapter is not used normally as a fast path in mogrify uses NULL,
but it makes easier to adapt composite types.
"""
def __init__(self, obj):
pass
def getquoted(self):
return "NULL"
__all__ = filter(lambda k: not k.startswith('_'), locals().keys())

View File

@ -364,6 +364,26 @@ class AdaptTypeTestCase(unittest.TestCase):
d = curs.fetchone()[0]
self.assertEqual("(42,)", d)
def test_none_fast_path(self):
# the None adapter is not actually invoked in regular adaptation
ext = psycopg2.extensions
class WonkyAdapter(object):
def __init__(self, obj): pass
def getquoted(self): return "NOPE!"
curs = self.conn.cursor()
orig_adapter = ext.adapters[type(None), ext.ISQLQuote]
try:
ext.register_adapter(type(None), WonkyAdapter)
self.assertEqual(ext.adapt(None).getquoted(), "NOPE!")
s = curs.mogrify("SELECT %s;", (None,))
self.assertEqual("SELECT NULL;", s)
finally:
ext.register_adapter(type(None), orig_adapter)
def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__)