Skip to content

Commit

Permalink
gh-105539: Fix ResourceWarning from unclosed SQLite connections in te…
Browse files Browse the repository at this point in the history
…st_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]>
  • Loading branch information
felixxm and erlend-aasland authored Aug 23, 2023
1 parent 31b61d1 commit 29bc616
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
21 changes: 12 additions & 9 deletions Lib/test/audit-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_sqlite3/test_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_sqlite3/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"):
Expand Down

0 comments on commit 29bc616

Please sign in to comment.