]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-105539: Fix ResourceWarning from unclosed SQLite connections in test_sqlite3 ...
authorMariusz Felisiak <felisiak.mariusz@gmail.com>
Wed, 23 Aug 2023 10:10:08 +0000 (12:10 +0200)
committerGitHub <noreply@github.com>
Wed, 23 Aug 2023 10:10:08 +0000 (10:10 +0000)
Follow up to 1a1bfc28912a39b500c578e9f10a8a222638d411.

Explicitly manage connections in:

- test_audit.test_sqlite3
- test_sqlite3.test_audit
- test_sqlite3.test_backup

Co-authored-by: Erlend E. Aasland <erlend@python.org>
Lib/test/audit-tests.py
Lib/test/test_sqlite3/test_backup.py
Lib/test/test_sqlite3/test_dbapi.py

index cc614eab908500ac5ca7aeec7fbef31513c5e172..ad8f72f556331da105264fe3cf7e80e148dedc98 100644 (file)
@@ -398,15 +398,18 @@ def test_sqlite3():
     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
index 4584d976bce0c6fa48a51ffccb2f8b5603fcd0e2..c7400d8b2165e63e4be71c09601da0c83d8a11fb 100644 (file)
@@ -137,7 +137,7 @@ class BackupTests(unittest.TestCase):
             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')
 
index d80ad7af3200f0fe896abb69eb20e34eb69ee3b2..9dedbdbc4bb6d2b6eb89e2cf2d903f2d80744dbb 100644 (file)
@@ -40,6 +40,7 @@ from os import SEEK_SET, SEEK_CUR, SEEK_END
 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 @@ class ClosedConTests(unittest.TestCase):
         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"):