Skip to content

Commit 29bc616

Browse files
gh-105539: Fix ResourceWarning from unclosed SQLite connections in test_sqlite3 (#108360)
Follow up to 1a1bfc2. Explicitly manage connections in: - test_audit.test_sqlite3 - test_sqlite3.test_audit - test_sqlite3.test_backup Co-authored-by: Erlend E. Aasland <[email protected]>
1 parent 31b61d1 commit 29bc616

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

Lib/test/audit-tests.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -398,15 +398,18 @@ def hook(event, *args):
398398
cx2 = sqlite3.Connection(":memory:")
399399

400400
# Configured without --enable-loadable-sqlite-extensions
401-
if hasattr(sqlite3.Connection, "enable_load_extension"):
402-
cx1.enable_load_extension(False)
403-
try:
404-
cx1.load_extension("test")
405-
except sqlite3.OperationalError:
406-
pass
407-
else:
408-
raise RuntimeError("Expected sqlite3.load_extension to fail")
409-
401+
try:
402+
if hasattr(sqlite3.Connection, "enable_load_extension"):
403+
cx1.enable_load_extension(False)
404+
try:
405+
cx1.load_extension("test")
406+
except sqlite3.OperationalError:
407+
pass
408+
else:
409+
raise RuntimeError("Expected sqlite3.load_extension to fail")
410+
finally:
411+
cx1.close()
412+
cx2.close()
410413

411414
def test_sys_getframe():
412415
import sys

Lib/test/test_sqlite3/test_backup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def progress(status, remaining, total):
137137
raise SystemError('nearly out of space')
138138

139139
with self.assertRaises(SystemError) as err:
140-
with sqlite.connect(':memory:') as bck:
140+
with memory_database() as bck:
141141
self.cx.backup(bck, progress=progress)
142142
self.assertEqual(str(err.exception), 'nearly out of space')
143143

Lib/test/test_sqlite3/test_dbapi.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from test.support.os_helper import TESTFN, TESTFN_UNDECODABLE, unlink, temp_dir, FakePath
4141

4242
from .util import memory_database, cx_limit
43+
from .util import MemoryDatabaseMixin
4344

4445

4546
class ModuleTests(unittest.TestCase):
@@ -1740,10 +1741,9 @@ def test_closed_call(self):
17401741
self.check(self.con)
17411742

17421743

1743-
class ClosedCurTests(unittest.TestCase):
1744+
class ClosedCurTests(MemoryDatabaseMixin, unittest.TestCase):
17441745
def test_closed(self):
1745-
con = sqlite.connect(":memory:")
1746-
cur = con.cursor()
1746+
cur = self.cx.cursor()
17471747
cur.close()
17481748

17491749
for method_name in ("execute", "executemany", "executescript", "fetchall", "fetchmany", "fetchone"):

0 commit comments

Comments
 (0)