bpo-44859: Improve error handling in sqlite3 and and raise more accurate exceptions. (GH-27654)

* MemoryError is now raised instead of sqlite3.Warning when
  memory is not enough for encoding a statement to UTF-8
  in Connection.__call__() and Cursor.execute().
* UnicodEncodeError is now raised instead of sqlite3.Warning when
  the statement contains surrogate characters
  in Connection.__call__() and Cursor.execute().
* TypeError is now raised instead of ValueError for non-string
  script argument in Cursor.executescript().
* ValueError is now raised for script containing the null
  character instead of truncating it in Cursor.executescript().
* Correctly handle exceptions raised when getting boolean value
  of the result of the progress handler.
* Add many tests covering different corner cases.

Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@innova.no>
This commit is contained in:
Serhiy Storchaka 2021-08-08 08:49:44 +03:00 committed by GitHub
parent ebecffdb6d
commit 0eec6276fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 226 additions and 52 deletions

View file

@ -997,6 +997,14 @@ static int _progress_handler(void* user_arg)
ret = _PyObject_CallNoArg((PyObject*)user_arg);
if (!ret) {
/* abort query if error occurred */
rc = -1;
}
else {
rc = PyObject_IsTrue(ret);
Py_DECREF(ret);
}
if (rc < 0) {
pysqlite_state *state = pysqlite_get_state(NULL);
if (state->enable_callback_tracebacks) {
PyErr_Print();
@ -1004,12 +1012,6 @@ static int _progress_handler(void* user_arg)
else {
PyErr_Clear();
}
/* abort query if error occurred */
rc = 1;
} else {
rc = (int)PyObject_IsTrue(ret);
Py_DECREF(ret);
}
PyGILState_Release(gilstate);