diff options
author | Erlend Egeberg Aasland <erlend.aasland@innova.no> | 2022-03-17 06:58:25 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-16 22:58:25 -0700 |
commit | 4674fd4e938eb4a29ccd5b12c15455bd2a41c335 (patch) | |
tree | 9b6d452caca59b4fa2e3eee150de2d3e098d73fa /Modules/_sqlite | |
parent | bpo-46480: add typing.assert_type (GH-30843) (diff) | |
download | cpython-4674fd4e938eb4a29ccd5b12c15455bd2a41c335.tar.gz cpython-4674fd4e938eb4a29ccd5b12c15455bd2a41c335.tar.bz2 cpython-4674fd4e938eb4a29ccd5b12c15455bd2a41c335.zip |
bpo-44859: Raise more accurate exceptions in `sqlite3` (GH-27695)
* Improve exception compliance with PEP 249
* Raise InterfaceError instead of ProgrammingError for SQLITE_MISUSE.
If SQLITE_MISUSE is raised, it is a sqlite3 module bug. Users of the
sqlite3 module are not responsible for using the SQLite C API correctly.
* Don't overwrite BufferError with ValueError when conversion to BLOB fails.
* Raise ProgrammingError instead of Warning if user tries to execute() more
than one SQL statement.
* Raise ProgrammingError instead of ValueError if an SQL query contains null characters.
* Make sure `_pysqlite_set_result` raises an exception if it returns -1.
Diffstat (limited to 'Modules/_sqlite')
-rw-r--r-- | Modules/_sqlite/connection.c | 7 | ||||
-rw-r--r-- | Modules/_sqlite/statement.c | 5 |
2 files changed, 7 insertions, 5 deletions
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index e4b8ecb5e2d..37f6d0fa5a5 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -578,8 +578,6 @@ _pysqlite_set_result(sqlite3_context* context, PyObject* py_val) } else if (PyObject_CheckBuffer(py_val)) { Py_buffer view; if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) { - PyErr_SetString(PyExc_ValueError, - "could not convert BLOB to buffer"); return -1; } if (view.len > INT_MAX) { @@ -591,6 +589,11 @@ _pysqlite_set_result(sqlite3_context* context, PyObject* py_val) sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT); PyBuffer_Release(&view); } else { + callback_context *ctx = (callback_context *)sqlite3_user_data(context); + PyErr_Format(ctx->state->ProgrammingError, + "User-defined functions cannot return '%s' values to " + "SQLite", + Py_TYPE(py_val)->tp_name); return -1; } return 0; diff --git a/Modules/_sqlite/statement.c b/Modules/_sqlite/statement.c index 6885b50f616..baa1b71a8da 100644 --- a/Modules/_sqlite/statement.c +++ b/Modules/_sqlite/statement.c @@ -67,7 +67,7 @@ pysqlite_statement_create(pysqlite_Connection *connection, PyObject *sql) return NULL; } if (strlen(sql_cstr) != (size_t)size) { - PyErr_SetString(PyExc_ValueError, + PyErr_SetString(connection->ProgrammingError, "the query contains a null character"); return NULL; } @@ -85,7 +85,7 @@ pysqlite_statement_create(pysqlite_Connection *connection, PyObject *sql) } if (pysqlite_check_remaining_sql(tail)) { - PyErr_SetString(connection->Warning, + PyErr_SetString(connection->ProgrammingError, "You can only execute one statement at a time."); goto error; } @@ -190,7 +190,6 @@ int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObjec case TYPE_BUFFER: { Py_buffer view; if (PyObject_GetBuffer(parameter, &view, PyBUF_SIMPLE) != 0) { - PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer"); return -1; } if (view.len > INT_MAX) { |