From 5c30ad3e2467eea58f1f33b8ef1a2354c3e88146 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Wed, 23 Aug 2023 09:25:00 +0200 Subject: [PATCH] gh-105539: Fix ResourceWarning from unclosed SQLite connection in tests. Follow up to 1a1bfc28912a39b500c578e9f10a8a222638d411. Co-authored-by: Erlend E. Aasland --- Lib/test/audit-tests.py | 21 ++++++++++++--------- Lib/test/test_sqlite3/test_backup.py | 2 +- Lib/test/test_sqlite3/test_dbapi.py | 6 +++--- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/Lib/test/audit-tests.py b/Lib/test/audit-tests.py index 9504829e96f00e..341997f190f89c 100644 --- a/Lib/test/audit-tests.py +++ b/Lib/test/audit-tests.py @@ -398,15 +398,18 @@ def hook(event, *args): cx2 = sqlite3.Connection(":memory:") # Configured without --enable-loadable-sqlite-extensions - if hasattr(sqlite3.Connection, "enable_load_extension"): - cx1.enable_load_extension(False) - try: - cx1.load_extension("test") - except sqlite3.OperationalError: - pass - else: - raise RuntimeError("Expected sqlite3.load_extension to fail") - + try: + if hasattr(sqlite3.Connection, "enable_load_extension"): + cx1.enable_load_extension(False) + try: + cx1.load_extension("test") + except sqlite3.OperationalError: + pass + else: + raise RuntimeError("Expected sqlite3.load_extension to fail") + finally: + cx1.close() + cx2.close() def test_sys_getframe(): import sys diff --git a/Lib/test/test_sqlite3/test_backup.py b/Lib/test/test_sqlite3/test_backup.py index 4584d976bce0c6..c7400d8b2165e6 100644 --- a/Lib/test/test_sqlite3/test_backup.py +++ b/Lib/test/test_sqlite3/test_backup.py @@ -137,7 +137,7 @@ def progress(status, remaining, total): raise SystemError('nearly out of space') with self.assertRaises(SystemError) as err: - with sqlite.connect(':memory:') as bck: + with memory_database() as bck: self.cx.backup(bck, progress=progress) self.assertEqual(str(err.exception), 'nearly out of space') diff --git a/Lib/test/test_sqlite3/test_dbapi.py b/Lib/test/test_sqlite3/test_dbapi.py index d80ad7af3200f0..9dedbdbc4bb6d2 100644 --- a/Lib/test/test_sqlite3/test_dbapi.py +++ b/Lib/test/test_sqlite3/test_dbapi.py @@ -40,6 +40,7 @@ from test.support.os_helper import TESTFN, TESTFN_UNDECODABLE, unlink, temp_dir, FakePath from .util import memory_database, cx_limit +from .util import MemoryDatabaseMixin class ModuleTests(unittest.TestCase): @@ -1740,10 +1741,9 @@ def test_closed_call(self): self.check(self.con) -class ClosedCurTests(unittest.TestCase): +class ClosedCurTests(MemoryDatabaseMixin, unittest.TestCase): def test_closed(self): - con = sqlite.connect(":memory:") - cur = con.cursor() + cur = self.cx.cursor() cur.close() for method_name in ("execute", "executemany", "executescript", "fetchall", "fetchmany", "fetchone"):