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
Only remove callbacks if there is a database to close
  • Loading branch information
erlend-aasland committed Aug 17, 2023
commit 03a034d3a8807edae8df4474fbd3e3e2481c823a
17 changes: 8 additions & 9 deletions Modules/_sqlite/connection.c
AF4A
Original file line number Diff line number Diff line change
Expand Up @@ -466,17 +466,16 @@ connection_finalize(PyObject *self)
pysqlite_Connection *con = (pysqlite_Connection *)self;
PyObject *exc = PyErr_GetRaisedException();

/*
* 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(con->db);
}

/* Clean up if user has not called .close() explicitly. */
if (con->db) {
/*
* 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(con->db);
}
if (PyErr_ResourceWarning(self, 1, "unclosed database in %R", self)) {
/* Spurious errors can appear at shutdown */
if (PyErr_ExceptionMatches(PyExc_Warning)) {
Expand Down
0