mirror of
https://github.com/python/cpython.git
synced 2025-09-27 18:59:43 +00:00
gh-105539: Emit ResourceWarning if sqlite3 database is not closed explicitly (#108015)
This commit is contained in:
parent
86617518c4
commit
1a1bfc2891
5 changed files with 30 additions and 0 deletions
|
@ -630,6 +630,12 @@ Connection objects
|
||||||
* :ref:`sqlite3-connection-shortcuts`
|
* :ref:`sqlite3-connection-shortcuts`
|
||||||
* :ref:`sqlite3-connection-context-manager`
|
* :ref:`sqlite3-connection-context-manager`
|
||||||
|
|
||||||
|
|
||||||
|
.. versionchanged:: 3.13
|
||||||
|
|
||||||
|
A :exc:`ResourceWarning` is emitted if :meth:`close` is not called before
|
||||||
|
a :class:`!Connection` object is deleted.
|
||||||
|
|
||||||
An SQLite database connection has the following attributes and methods:
|
An SQLite database connection has the following attributes and methods:
|
||||||
|
|
||||||
.. method:: cursor(factory=Cursor)
|
.. method:: cursor(factory=Cursor)
|
||||||
|
|
|
@ -158,6 +158,13 @@ pathlib
|
||||||
:meth:`~pathlib.Path.is_dir`.
|
:meth:`~pathlib.Path.is_dir`.
|
||||||
(Contributed by Barney Gale in :gh:`77609` and :gh:`105793`.)
|
(Contributed by Barney Gale in :gh:`77609` and :gh:`105793`.)
|
||||||
|
|
||||||
|
sqlite3
|
||||||
|
-------
|
||||||
|
|
||||||
|
* A :exc:`ResourceWarning` is now emitted if a :class:`sqlite3.Connection`
|
||||||
|
object is not :meth:`closed <sqlite3.Connection.close>` explicitly.
|
||||||
|
(Contributed by Erlend E. Aasland in :gh:`105539`.)
|
||||||
|
|
||||||
tkinter
|
tkinter
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
|
|
@ -583,6 +583,12 @@ class ConnectionTests(unittest.TestCase):
|
||||||
cx.close()
|
cx.close()
|
||||||
self.assertEqual(cm.filename, __file__)
|
self.assertEqual(cm.filename, __file__)
|
||||||
|
|
||||||
|
def test_connection_resource_warning(self):
|
||||||
|
with self.assertWarns(ResourceWarning):
|
||||||
|
cx = sqlite.connect(":memory:")
|
||||||
|
del cx
|
||||||
|
gc_collect()
|
||||||
|
|
||||||
|
|
||||||
class UninitialisedConnectionTests(unittest.TestCase):
|
class UninitialisedConnectionTests(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
:mod:`sqlite3` now emits an :exc:`ResourceWarning` if a
|
||||||
|
:class:`sqlite3.Connection` object is not :meth:`closed
|
||||||
|
<sqlite3.connection.close>` explicitly. Patch by Erlend E. Aasland.
|
|
@ -493,6 +493,14 @@ connection_finalize(PyObject *self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Clean up if user has not called .close() explicitly. */
|
/* Clean up if user has not called .close() explicitly. */
|
||||||
|
if (con->db) {
|
||||||
|
if (PyErr_ResourceWarning(self, 1, "unclosed database in %R", self)) {
|
||||||
|
/* Spurious errors can appear at shutdown */
|
||||||
|
if (PyErr_ExceptionMatches(PyExc_Warning)) {
|
||||||
|
PyErr_WriteUnraisable(self);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if (connection_close(con) < 0) {
|
if (connection_close(con) < 0) {
|
||||||
if (teardown) {
|
if (teardown) {
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue