Skip to content

Commit 404bf8c

Browse files
author
gerhard.haering
committed
Make sure we get usable error messages when text could not be decoded when fetched from the database.
git-svn-id: http://svn.python.org/projects/python/branches/py3k@61142 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent d05087d commit 404bf8c

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

Lib/sqlite3/test/regression.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@ def CheckStatementAvailable(self):
7979
cur.fetchone()
8080
cur.fetchone()
8181

82+
def CheckErrorMsgDecodeError(self):
83+
# When porting the module to Python 3.0, the error message about
84+
# decoding errors disappeared. This verifies they're back again.
85+
failure = None
86+
try:
87+
self.con.execute("select 'xxx' || ? || 'yyy' colname", (bytes(bytearray([250])),)).fetchone()
88+
failure = "should have raised an OperationalError with detailed description"
89+
except sqlite.OperationalError as e:
90+
msg = e.args[0]
91+
if not msg.startswith("Could not decode to UTF-8 column 'colname' with text 'xxx"):
92+
failure = "OperationalError did not have expected description text"
93+
if failure:
94+
self.fail(failure)
95+
8296
def suite():
8397
regression_suite = unittest.makeSuite(RegressionTests, "Check")
8498
return unittest.TestSuite((regression_suite,))

Modules/_sqlite/cursor.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
295295
const char* val_str;
296296
char buf[200];
297297
const char* colname;
298+
PyObject* buf_bytes;
299+
PyObject* error_obj;
298300

299301
Py_BEGIN_ALLOW_THREADS
300302
numcols = sqlite3_data_count(self->statement->st);
@@ -363,7 +365,19 @@ PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
363365
}
364366
PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
365367
colname , val_str);
366-
PyErr_SetString(pysqlite_OperationalError, buf);
368+
buf_bytes = PyBytes_FromStringAndSize(buf, strlen(buf));
369+
if (!buf_bytes) {
370+
PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
371+
} else {
372+
error_obj = PyUnicode_FromEncodedObject(buf_bytes, "ascii", "replace");
373+
if (!error_obj) {
374+
PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
375+
Py_DECREF(error_obj);
376+
} else {
377+
PyErr_SetObject(pysqlite_OperationalError, error_obj);
378+
}
379+
Py_DECREF(buf_bytes);
380+
}
367381
}
368382
} else if (self->connection->text_factory == (PyObject*)&PyString_Type) {
369383
converted = PyString_FromString(val_str);

0 commit comments

Comments
 (0)