[Python-3000-checkins] r61142 - in python/branches/py3k: Lib/sqlite3/test/regression.py Modules/_sqlite/cursor.c

gerhard.haering python-3000-checkins at python.org
Fri Feb 29 23:22:10 CET 2008


Author: gerhard.haering
Date: Fri Feb 29 23:22:09 2008
New Revision: 61142

Modified:
   python/branches/py3k/Lib/sqlite3/test/regression.py
   python/branches/py3k/Modules/_sqlite/cursor.c
Log:
Make sure we get usable error messages when text could not be decoded when fetched from the database.


Modified: python/branches/py3k/Lib/sqlite3/test/regression.py
==============================================================================
--- python/branches/py3k/Lib/sqlite3/test/regression.py	(original)
+++ python/branches/py3k/Lib/sqlite3/test/regression.py	Fri Feb 29 23:22:09 2008
@@ -79,6 +79,20 @@
         cur.fetchone()
         cur.fetchone()
 
+    def CheckErrorMsgDecodeError(self):
+        # When porting the module to Python 3.0, the error message about
+        # decoding errors disappeared. This verifies they're back again.
+        failure = None
+        try:
+            self.con.execute("select 'xxx' || ? || 'yyy' colname", (bytes(bytearray([250])),)).fetchone()
+            failure = "should have raised an OperationalError with detailed description"
+        except sqlite.OperationalError as e:
+            msg = e.args[0]
+            if not msg.startswith("Could not decode to UTF-8 column 'colname' with text 'xxx"):
+                failure = "OperationalError did not have expected description text"
+        if failure:
+            self.fail(failure)
+
 def suite():
     regression_suite = unittest.makeSuite(RegressionTests, "Check")
     return unittest.TestSuite((regression_suite,))

Modified: python/branches/py3k/Modules/_sqlite/cursor.c
==============================================================================
--- python/branches/py3k/Modules/_sqlite/cursor.c	(original)
+++ python/branches/py3k/Modules/_sqlite/cursor.c	Fri Feb 29 23:22:09 2008
@@ -295,6 +295,8 @@
     const char* val_str;
     char buf[200];
     const char* colname;
+    PyObject* buf_bytes;
+    PyObject* error_obj;
 
     Py_BEGIN_ALLOW_THREADS
     numcols = sqlite3_data_count(self->statement->st);
@@ -363,7 +365,19 @@
                         }
                         PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
                                      colname , val_str);
-                        PyErr_SetString(pysqlite_OperationalError, buf);
+                        buf_bytes = PyBytes_FromStringAndSize(buf, strlen(buf)); 
+                        if (!buf_bytes) {
+                            PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
+                        } else {
+                            error_obj = PyUnicode_FromEncodedObject(buf_bytes, "ascii", "replace");
+                            if (!error_obj) {
+                                PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
+                                Py_DECREF(error_obj);
+                            } else {
+                                PyErr_SetObject(pysqlite_OperationalError, error_obj);
+                            }
+                            Py_DECREF(buf_bytes);
+                        }
                     }
                 } else if (self->connection->text_factory == (PyObject*)&PyString_Type) {
                     converted = PyString_FromString(val_str);


More information about the Python-3000-checkins mailing list