10000 gh-108083: Don't ignore exceptions in sqlite3.Connection.__init__() and .close() by erlend-aasland · Pull Request #108084 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
8000

gh-108083: Don't ignore exceptions in sqlite3.Connection.__init__() and .close() #108084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Don't log unraisable exceptions in case of interp shutdown
  • Loading branch information
erlend-aasland committed Aug 17, 2023
commit 379e64e8c162cc22d41d1c0fb143e2fd6b926ed7
46 changes: 34 additions & 12 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,9 @@ free_callback_contexts(pysqlite_Connection *self)
static void
remove_callbacks(sqlite3 *db)
{
if (db == NULL) {
return;
}
sqlite3_trace_v2(db, SQLITE_TRACE_STMT, 0, 0);
sqlite3_progress_handler(db, 0, 0, (void *)0);
(void)sqlite3_set_authorizer(db, NULL, NULL);
Expand All @@ -448,42 +451,60 @@ connection_close(pysqlite_Connection *self)
if (self->autocommit == AUTOCOMMIT_DISABLED &&
!sqlite3_get_autocommit(self->db))
{
/* If close is implicitly called as a result of interpreter
* tear-down, we must not call back into Python. */
if (_Py_IsInterpreterFinalizing(PyInterpreterState_Get())) {
remove_callbacks(self->db);
}
if (connection_exec_stmt(self, "ROLLBACK") < 0) {
return -1;
}
}

free_callback_contexts(self);

sqlite3 *db = self->db;
self->db = NULL;

Py_BEGIN_ALLOW_THREADS
int rc = sqlite3_close_v2(db);
assert(rc == SQLITE_OK), (void)rc;
Py_END_ALLOW_THREADS

free_callback_contexts(self);
}
return 0;
}

static void
connection_dealloc(PyObject *self)
connection_finalize(PyObject *self)
{
pysqlite_Connection *con = (pysqlite_Connection *)self;
PyTypeObject *tp = Py_TYPE(self);
PyObject_GC_UnTrack(self);
tp->tp_clear(self);
PyObject *exc = PyErr_GetRaisedException();

/* If close is implicitly called as a result of interpreter
* tear-down, we must not call back into Python. */
PyInterpreterState *interp = PyInterpreterState_Get();
int teardown = _Py_IsInterpreterFinalizing(interp);
if (teardown) {
remove_callbacks(con->db);
}

/* Clean up if user has not called .close() explicitly. */
if (connection_close(con) < 0) {
PyErr_WriteUnraisable((PyObject *)self);
if (teardown) {
PyErr_Clear();
}
else {
PyErr_WriteUnraisable((PyObject *)self);
}
}

PyErr_SetRaisedException(exc);
}

static void
connection_dealloc(PyObject *self)
{
if (PyObject_CallFinalizerFromDealloc(self) < 0) {
return;
}
PyTypeObject *tp = Py_TYPE(self);
PyObject_GC_UnTrack(self);
tp->tp_clear(self);
tp->tp_free(self);
Py_DECREF(tp);
}
Expand Down Expand Up @@ -2567,6 +2588,7 @@ static struct PyMemberDef connection_members[] =
};

static PyType_Slot connection_slots[] = {
{Py_tp_finalize, connection_finalize},
{Py_tp_dealloc, connection_dealloc},
{Py_tp_doc, (void *)connection_doc},
{Py_tp_methods, connection_methods},
Expand Down
0