Skip to content

Commit 3e3ff09

Browse files
author
Erlend Egeberg Aasland
authored
bpo-44958: Fix ref. leak introduced in GH-27844 (GH-28490)
Modify managed_connect() helper to support in-memory databases. Use it for the regression tests added in GH-27844. Automerge-Triggered-By: GH:pablogsal
1 parent 050d103 commit 3e3ff09

File tree

2 files changed

+36
-30
lines changed

2 files changed

+36
-30
lines changed

Lib/sqlite3/test/test_dbapi.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@
3838

3939
# Helper for tests using TESTFN
4040
@contextlib.contextmanager
41-
def managed_connect(*args, **kwargs):
41+
def managed_connect(*args, in_mem=False, **kwargs):
4242
cx = sqlite.connect(*args, **kwargs)
4343
try:
4444
yield cx
4545
finally:
4646
cx.close()
47-
unlink(TESTFN)
47+
if not in_mem:
48+
unlink(TESTFN)
4849

4950

5051
class ModuleTests(unittest.TestCase):

Lib/sqlite3/test/test_regression.py

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import functools
2929
from test import support
3030

31+
from .test_dbapi import managed_connect
32+
3133
class RegressionTests(unittest.TestCase):
3234
def setUp(self):
3335
self.con = sqlite.connect(":memory:")
@@ -437,38 +439,41 @@ def test_return_empty_bytestring(self):
437439
self.assertEqual(val, b'')
438440

439441
def test_table_lock_cursor_replace_stmt(self):
440-
con = sqlite.connect(":memory:")
441-
cur = con.cursor()
442-
cur.execute("create table t(t)")
443-
cur.executemany("insert into t values(?)", ((v,) for v in range(5)))
444-
con.commit()
445-
cur.execute("select t from t")
446-
cur.execute("drop table t")
447-
con.commit()
442+
with managed_connect(":memory:", in_mem=True) as con:
443+
cur = con.cursor()
444+
cur.execute("create table t(t)")
445+
cur.executemany("insert into t values(?)",
446+
((v,) for v in range(5)))
447+
con.commit()
448+
cur.execute("select t from t")
449+
cur.execute("drop table t")
450+
con.commit()
448451

449452
def test_table_lock_cursor_dealloc(self):
450-
con = sqlite.connect(":memory:")
451-
con.execute("create table t(t)")
452-
con.executemany("insert into t values(?)", ((v,) for v in range(5)))
453-
con.commit()
454-
cur = con.execute("select t from t")
455-
del cur
456-
con.execute("drop table t")
457-
con.commit()
453+
with managed_connect(":memory:", in_mem=True) as con:
454+
con.execute("create table t(t)")
455+
con.executemany("insert into t values(?)",
456+
((v,) for v in range(5)))
457+
con.commit()
458+
cur = con.execute("select t from t")
459+
del cur
460+
con.execute("drop table t")
461+
con.commit()
458462

459463
def test_table_lock_cursor_non_readonly_select(self):
460-
con = sqlite.connect(":memory:")
461-
con.execute("create table t(t)")
462-
con.executemany("insert into t values(?)", ((v,) for v in range(5)))
463-
con.commit()
464-
def dup(v):
465-
con.execute("insert into t values(?)", (v,))
466-
return
467-
con.create_function("dup", 1, dup)
468-
cur = con.execute("select dup(t) from t")
469-
del cur
470-
con.execute("drop table t")
471-
con.commit()
464+
with managed_connect(":memory:", in_mem=True) as con:
465+
con.execute("create table t(t)")
466+
con.executemany("insert into t values(?)",
467+
((v,) for v in range(5)))
468+
con.commit()
469+
def dup(v):
470+
con.execute("insert into t values(?)", (v,))
471+
return
472+
con.create_function("dup", 1, dup)
473+
cur = con.execute("select dup(t) from t")
474+
del cur
475+
con.execute("drop table t")
476+
con.commit()
472477

473478

474479
if __name__ == "__main__":

0 commit comments

Comments
 (0)