summaryrefslogtreecommitdiffstats
path: root/Modules/_sqlite
diff options
context:
space:
mode:
authorErlend Egeberg Aasland <erlend.aasland@innova.no>2022-03-09 02:46:40 (GMT)
committerGitHub <noreply@github.com>2022-03-09 02:46:40 (GMT)
commitd1777515f9f53b452a4231d68196a7c0e5deb879 (patch)
tree5e0d097fe8449bacc4c833a31104e13ec6522ae8 /Modules/_sqlite
parentb33a1ae703338e09dc0af5fbfd8ffa01d3ff75da (diff)
downloadcpython-d1777515f9f53b452a4231d68196a7c0e5deb879.zip
cpython-d1777515f9f53b452a4231d68196a7c0e5deb879.tar.gz
cpython-d1777515f9f53b452a4231d68196a7c0e5deb879.tar.bz2
bpo-45138: Expand traced SQL statements in `sqlite3` trace callback (GH-28240)
Diffstat (limited to 'Modules/_sqlite')
-rw-r--r--Modules/_sqlite/connection.c49
1 files changed, 35 insertions, 14 deletions
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
index e4b8ecb..511e8a2 100644
--- a/Modules/_sqlite/connection.c
+++ b/Modules/_sqlite/connection.c
@@ -1079,11 +1079,10 @@ progress_callback(void *ctx)
* to ensure future compatibility.
*/
static int
-trace_callback(unsigned int type, void *ctx, void *prepared_statement,
- void *statement_string)
+trace_callback(unsigned int type, void *ctx, void *stmt, void *sql)
#else
static void
-trace_callback(void *ctx, const char *statement_string)
+trace_callback(void *ctx, const char *sql)
#endif
{
#ifdef HAVE_TRACE_V2
@@ -1094,24 +1093,46 @@ trace_callback(void *ctx, const char *statement_string)
PyGILState_STATE gilstate = PyGILState_Ensure();
- PyObject *py_statement = NULL;
- PyObject *ret = NULL;
- py_statement = PyUnicode_DecodeUTF8(statement_string,
- strlen(statement_string), "replace");
assert(ctx != NULL);
+ PyObject *py_statement = NULL;
+#ifdef HAVE_TRACE_V2
+ assert(stmt != NULL);
+ const char *expanded_sql = sqlite3_expanded_sql((sqlite3_stmt *)stmt);
+ if (expanded_sql == NULL) {
+ sqlite3 *db = sqlite3_db_handle((sqlite3_stmt *)stmt);
+ if (sqlite3_errcode(db) == SQLITE_NOMEM) {
+ (void)PyErr_NoMemory();
+ goto exit;
+ }
+
+ pysqlite_state *state = ((callback_context *)ctx)->state;
+ assert(state != NULL);
+ PyErr_SetString(state->DataError,
+ "Expanded SQL string exceeds the maximum string "
+ "length");
+ print_or_clear_traceback((callback_context *)ctx);
+
+ // Fall back to unexpanded sql
+ py_statement = PyUnicode_FromString((const char *)sql);
+ }
+ else {
+ py_statement = PyUnicode_FromString(expanded_sql);
+ sqlite3_free((void *)expanded_sql);
+ }
+#else
+ py_statement = PyUnicode_FromString(sql);
+#endif
if (py_statement) {
PyObject *callable = ((callback_context *)ctx)->callable;
- ret = PyObject_CallOneArg(callable, py_statement);
+ PyObject *ret = PyObject_CallOneArg(callable, py_statement);
Py_DECREF(py_statement);
+ Py_XDECREF(ret);
}
- if (ret) {
- Py_DECREF(ret);
- }
- else {
- print_or_clear_traceback(ctx);
+exit:
+ if (PyErr_Occurred()) {
+ print_or_clear_traceback((callback_context *)ctx);
}
-
PyGILState_Release(gilstate);
#ifdef HAVE_TRACE_V2
return 0;