Now adapt() errors include the type name

This commit is contained in:
Federico Di Gregorio 2009-11-25 11:51:54 +01:00
parent d40a5321f2
commit bcc836c661
3 changed files with 15 additions and 1 deletions

View File

@ -1,5 +1,8 @@
2009-11-25 Federico Di Gregorio <fog@initd.org>
* psycopg/microprotocols.c: "can't adapt" message now includes full
type information (adapted patch from Eric Chamberlain).
* tests/types_basic.py: fixed test broken by float precision fix.
2009-11-09 Federico Di Gregorio <fog@initd.org>

View File

@ -74,6 +74,7 @@ PyObject *
microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt)
{
PyObject *adapter, *key;
char buffer[256];
/* we don't check for exact type conformance as specified in PEP 246
because the ISQLQuote type is abstract and there is no way to get a
@ -114,7 +115,8 @@ microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt)
}
/* else set the right exception and return NULL */
psyco_set_error(ProgrammingError, NULL, "can't adapt", NULL, NULL);
PyOS_snprintf(buffer, 255, "can't adapt type '%s'", obj->ob_type->tp_name);
psyco_set_error(ProgrammingError, NULL, buffer, NULL, NULL);
return NULL;
}

View File

@ -78,6 +78,15 @@ class TypesExtrasTests(unittest.TestCase):
s = self.execute("SELECT NULL::inet AS foo")
self.failUnless(s is None)
def test_adapt_fail(self):
class Foo(object): pass
self.assertRaises(psycopg2.ProgrammingError,
psycopg2.extensions.adapt, Foo(), psycopg2.extensions.ISQLQuote, None)
try:
psycopg2.extensions.adapt(Foo(), psycopg2.extensions.ISQLQuote, None)
except psycopg2.ProgrammingError, err:
self.failUnless(str(err) == "can't adapt type 'Foo'")
def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__)