8000 gh-105539: Emit ResourceWarning if sqlite3 database is not closed explicitly by erlend-aasland · Pull Request #108015 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-105539: Emit ResourceWarning if sqlite3 database is not closed explicitly #108015

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 12 commits into from
Aug 22, 2023
Prev Previous commit
Next Next commit
Address review: move finalizing check from the close helper to the fi…
…nalizer
  • Loading branch information
erlend-aasland committed Aug 17, 2023
commit 8ebc8a73196a0715ed6b629d0e9721384d1e660f
14 changes: 9 additions & 5 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,6 @@ 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);
}
(void)connection_exec_stmt(self, "ROLLBACK");
}

Expand All @@ -468,6 +463,15 @@ connection_close(pysqlite_Connection *self)
static void
connection_finalize(PyObject *self)
{
/*
* Before implicitly closing, make sure we're not accidentally part
* of the interpreter tear-down; in that case, we must not call back
* into Python code.
*/
if (_Py_IsInterpreterFinalizing(PyInterpreterState_Get())) {
remove_callbacks(self->db);
}

pysqlite_Connection *con = (pysqlite_Connection *)self;
PyObject *exc = PyErr_GetRaisedException();
/* Clean up if user has not called .close() explicitly. */
Expand Down
0