diff options
author | Erlend E. Aasland <erlend@python.org> | 2025-02-11 08:49:25 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-02-11 07:49:25 +0000 |
commit | 3b366a4a4b0cea483824adbc2fa5a19d82d3dc6a (patch) | |
tree | 6db10c7a3e90a315c9bdba32e7f2c7811e19bc49 /Modules/_sqlite/util.c | |
parent | 3a2e7aacf6f414bbbedaf072e7cb1b48e3d402fa (diff) | |
download | cpython-3b366a4a4b0cea483824adbc2fa5a19d82d3dc6a.tar.gz cpython-3b366a4a4b0cea483824adbc2fa5a19d82d3dc6a.zip |
gh-129928: Rework sqlite3 error helpers (#129929)
Add a helper for raising DB-API compatible exceptions based on the
result code of SQLite C APIs. Some APIs do not store the error indicator
on the database pointer, so we need to be able to deduce the DB-API
compatible exception directly from the error code.
- rename _pysqlite_seterror() as set_error_from_db()
- introduce set_error_from_code()
Diffstat (limited to 'Modules/_sqlite/util.c')
-rw-r--r-- | Modules/_sqlite/util.c | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/Modules/_sqlite/util.c b/Modules/_sqlite/util.c index b0622e66928..103248ff55a 100644 --- a/Modules/_sqlite/util.c +++ b/Modules/_sqlite/util.c @@ -118,18 +118,31 @@ exit: Py_XDECREF(exc); } +void +set_error_from_code(pysqlite_state *state, int code) +{ + PyObject *exc_class = get_exception_class(state, code); + if (exc_class == NULL) { + // No new exception need be raised. + return; + } + + const char *errmsg = sqlite3_errstr(code); + assert(errmsg != NULL); + raise_exception(exc_class, code, errmsg); +} + /** * Checks the SQLite error code and sets the appropriate DB-API exception. - * Returns the error code (0 means no error occurred). */ -int -_pysqlite_seterror(pysqlite_state *state, sqlite3 *db) +void +set_error_from_db(pysqlite_state *state, sqlite3 *db) { int errorcode = sqlite3_errcode(db); PyObject *exc_class = get_exception_class(state, errorcode); if (exc_class == NULL) { - // No new exception need be raised; just pass the error code - return errorcode; + // No new exception need be raised. + return; } /* Create and set the exception. */ @@ -137,7 +150,6 @@ _pysqlite_seterror(pysqlite_state *state, sqlite3 *db) // sqlite3_errmsg() always returns an UTF-8 encoded message const char *errmsg = sqlite3_errmsg(db); raise_exception(exc_class, extended_errcode, errmsg); - return extended_errcode; } #ifdef WORDS_BIGENDIAN |