diff -r 89e2201142f9 Lib/sqlite3/test/types.py --- a/Lib/sqlite3/test/types.py Fri Nov 18 10:42:10 2016 -0800 +++ b/Lib/sqlite3/test/types.py Fri Nov 18 23:39:21 2016 +0100 @@ -74,6 +74,24 @@ row = self.cur.fetchone() self.assertEqual(row[0], sample) + def CheckErrorInAdapt(self): + class Point: + def __init__(self, x, y): + self.x, self.y = x, y + + def adapt_point(point): + assert False, 'Problem in adapter' + + sqlite.register_adapter(Point, adapt_point) + + p = Point(4.0, -3.2) + try: + self.cur.execute("select ?", (p,)) + except AssertionError as ex: + self.assertEqual(str(ex), 'Problem in adapter') + else: + assert False, 'Should have raised' + def CheckUnicodeExecute(self): self.cur.execute("select 'Österreich'") row = self.cur.fetchone() diff -r 89e2201142f9 Modules/_sqlite/microprotocols.c --- a/Modules/_sqlite/microprotocols.c Fri Nov 18 10:42:10 2016 -0800 +++ b/Modules/_sqlite/microprotocols.c Fri Nov 18 23:39:21 2016 +0100 @@ -49,7 +49,9 @@ } -/* pysqlite_microprotocols_add - add a reverse type-caster to the dictionary */ +/* pysqlite_microprotocols_add - add a reverse type-caster to the dictionary +Returns NULL without an exception in the case there's legitimately +nothing to do. */ int pysqlite_microprotocols_add(PyTypeObject *type, PyObject *proto, PyObject *cast) @@ -127,9 +129,6 @@ return NULL; } } - - /* else set the right exception and return NULL */ - PyErr_SetString(pysqlite_ProgrammingError, "can't adapt"); return NULL; } @@ -140,7 +139,17 @@ { PyObject *obj, *alt = NULL; PyObject *proto = (PyObject*)&pysqlite_PrepareProtocolType; + PyObject *adapted; - if (!PyArg_ParseTuple(args, "O|OO", &obj, &proto, &alt)) return NULL; - return pysqlite_microprotocols_adapt(obj, proto, alt); + if (!PyArg_ParseTuple(args, "O|OO", &obj, &proto, &alt)) + return NULL; + adapted = pysqlite_microprotocols_adapt(obj, proto, alt); + if (adapted != NULL) { + return adapted; + } else { + Py_DECREF(adapted); + if (!PyErr_Occurred()) + PyErr_SetString(pysqlite_ProgrammingError, "can't adapt"); + return NULL; + } } diff -r 89e2201142f9 Modules/_sqlite/statement.c --- a/Modules/_sqlite/statement.c Fri Nov 18 10:42:10 2016 -0800 +++ b/Modules/_sqlite/statement.c Fri Nov 18 23:39:21 2016 +0100 @@ -252,7 +252,8 @@ if (adapted) { Py_DECREF(current_param); } else { - PyErr_Clear(); + if (PyErr_Occurred()) + return; adapted = current_param; } } @@ -297,7 +298,8 @@ if (adapted) { Py_DECREF(current_param); } else { - PyErr_Clear(); + if (PyErr_Occurred()) + return; adapted = current_param; } }